fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / sd / source / ui / remotecontrol / BufferedStreamSocket.cxx
blob4417e09130f74bbfaa922b6a139b12a37e71a0b2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <BufferedStreamSocket.hxx>
12 #include <algorithm>
14 #ifdef WIN32
15 // LO vs WinAPI conflict
16 #undef WB_LEFT
17 #undef WB_RIGHT
19 #include <winsock2.h>
20 #else
21 #include <sys/socket.h>
22 #include <unistd.h>
23 #endif
24 using namespace sd;
25 using namespace std;
26 using namespace osl;
28 BufferedStreamSocket::BufferedStreamSocket( const osl::StreamSocket &aSocket ):
29 StreamSocket( aSocket ),
30 aRet( 0 ),
31 aRead( 0 ),
32 aBuffer(),
33 mSocket( 0 ),
34 usingCSocket( false )
38 BufferedStreamSocket::BufferedStreamSocket( int aSocket ):
39 StreamSocket(),
40 aRet( 0 ),
41 aRead( 0 ),
42 aBuffer(),
43 mSocket( aSocket ),
44 usingCSocket( true )
48 void BufferedStreamSocket::getPeerAddr(osl::SocketAddr& rAddr)
50 assert ( !usingCSocket );
51 StreamSocket::getPeerAddr( rAddr );
54 sal_Int32 BufferedStreamSocket::write( const void* pBuffer, sal_uInt32 n )
56 if ( !usingCSocket )
57 return StreamSocket::write( pBuffer, n );
58 else
59 return ::send( mSocket, (const char *) pBuffer, (size_t) n, 0 );
62 void BufferedStreamSocket::close()
64 if( usingCSocket && mSocket != -1 )
66 #ifdef WIN32
67 ::closesocket( mSocket );
68 #else
69 ::close( mSocket );
70 #endif
71 mSocket = -1;
73 else
74 ::osl::StreamSocket::close();
77 sal_Int32 BufferedStreamSocket::readLine( OString& aLine )
79 while ( true )
81 // Process buffer first incase data already present.
82 vector<char>::iterator aIt;
83 if ( (aIt = find( aBuffer.begin(), aBuffer.end(), '\n' ))
84 != aBuffer.end() )
86 sal_uInt64 aLocation = aIt - aBuffer.begin();
88 aLine = OString( &(*aBuffer.begin()), aLocation );
90 aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the empty line
91 aRead -= (aLocation + 1);
93 SAL_INFO( "sdremote.bluetooth", "recv line '" << aLine << "'" );
95 return aLine.getLength() + 1;
98 // Then try and receive if nothing present
99 aBuffer.resize( aRead + 100 );
100 if ( !usingCSocket)
101 aRet = StreamSocket::recv( &aBuffer[aRead], 100 );
102 else
103 aRet = ::recv( mSocket, &aBuffer[aRead], 100, 0 );
105 SAL_INFO( "sdremote.bluetooth", "recv " << aRet << " aBuffer len " << aBuffer.size() );
106 if ( aRet <= 0 )
108 return 0;
110 // Prevent buffer from growing massively large.
111 if ( aRead > MAX_LINE_LENGTH )
113 aBuffer.erase( aBuffer.begin(), aBuffer.end() );
114 return 0;
116 aRead += aRet;
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */