LibreOffice Module sd (master) 1
BufferedStreamSocket.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
11
12#include <osl/socket.hxx>
13#include <sal/log.hxx>
14#include <algorithm>
15
16#ifdef _WIN32
17 // LO vs WinAPI conflict
18 #undef WB_LEFT
19 #undef WB_RIGHT
20
21 #include <winsock2.h>
22#else
23 #include <sys/socket.h>
24 #include <unistd.h>
25#endif
26using namespace sd;
27using namespace osl;
28
29BufferedStreamSocket::BufferedStreamSocket( const osl::StreamSocket &aSocket ):
30 StreamSocket( aSocket ),
31 aRet( 0 ),
32 aRead( 0 ),
33 mSocket( 0 ),
34 usingCSocket( false )
35{
36}
37
39 aRet( 0 ),
40 aRead( 0 ),
41 mSocket( aSocket ),
42 usingCSocket( true )
43{
44}
45
47 close();
48}
49
50void BufferedStreamSocket::getPeerAddr(osl::SocketAddr& rAddr)
51{
52 assert ( !usingCSocket );
53 StreamSocket::getPeerAddr( rAddr );
54}
55
56sal_Int32 BufferedStreamSocket::write( const void* pBuffer, sal_uInt32 n )
57{
58 if ( !usingCSocket )
59 return StreamSocket::write( pBuffer, n );
60 else
61 return ::send(
62 mSocket,
63#if defined(_WIN32)
64 static_cast<char const *>(pBuffer),
65#else
66 pBuffer,
67#endif
68 static_cast<size_t>(n), 0 );
69}
70
72{
73 if( usingCSocket && mSocket != -1 )
74 {
75#ifdef _WIN32
76 ::closesocket( mSocket );
77#else
79#endif
80 mSocket = -1;
81 }
82 else
83 ::osl::StreamSocket::close();
84}
85
86sal_Int32 BufferedStreamSocket::readLine( OString& aLine )
87{
88 while ( true )
89 {
90 // Process buffer first in case data already present.
91 std::vector<char>::iterator aIt;
92 if ( (aIt = find( aBuffer.begin(), aBuffer.end(), '\n' ))
93 != aBuffer.end() )
94 {
95 sal_uInt64 aLocation = aIt - aBuffer.begin();
96
97 aLine = OString( &(*aBuffer.begin()), aLocation );
98
99 aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the empty line
100 aRead -= (aLocation + 1);
101
102 SAL_INFO( "sdremote.bluetooth", "recv line '" << aLine << "'" );
103
104 return aLine.getLength() + 1;
105 }
106
107 // Then try and receive if nothing present
108 aBuffer.resize( aRead + 100 );
109 if ( !usingCSocket)
110 aRet = StreamSocket::recv( &aBuffer[aRead], 100 );
111 else
112 aRet = ::recv( mSocket, &aBuffer[aRead], 100, 0 );
113
114 SAL_INFO( "sdremote.bluetooth", "recv " << aRet << " aBuffer len " << aBuffer.size() );
115 if ( aRet <= 0 )
116 {
117 return 0;
118 }
119 // Prevent buffer from growing massively large.
120 if ( aRead > MAX_LINE_LENGTH )
121 {
122 aBuffer.clear();
123 return 0;
124 }
125 aRead += aRet;
126 }
127
128}
129
130/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define MAX_LINE_LENGTH
virtual void close() override
virtual sal_Int32 write(const void *pBuffer, sal_uInt32 n) override
Write a number of bytes.
void getPeerAddr(osl::SocketAddr &)
BufferedStreamSocket(const osl::StreamSocket &aSocket)
Create a BufferedStreamSocket on top of an osl::StreamSocket.
virtual sal_Int32 readLine(OString &aLine) override
Blocks until a line is read.
sal_Int64 n
#define SAL_INFO(area, stream)
OSQLColumns::const_iterator find(const OSQLColumns::const_iterator &first, const OSQLColumns::const_iterator &last, std::u16string_view _rVal, const ::comphelper::UStringMixEqual &_rCase)