bump product version to 6.3.0.0.beta1
[LibreOffice.git] / sd / source / ui / remotecontrol / BufferedStreamSocket.cxx
blobb3a9cc154e37e5184fbed2fcecd3a4756cfc8cf6
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 <osl/socket.hxx>
13 #include <sal/log.hxx>
14 #include <algorithm>
16 #ifdef _WIN32
17 // LO vs WinAPI conflict
18 #undef WB_LEFT
19 #undef WB_RIGHT
21 #include <winsock2.h>
22 #else
23 #include <sys/socket.h>
24 #include <unistd.h>
25 #endif
26 using namespace sd;
27 using namespace std;
28 using namespace osl;
30 BufferedStreamSocket::BufferedStreamSocket( const osl::StreamSocket &aSocket ):
31 StreamSocket( aSocket ),
32 aRet( 0 ),
33 aRead( 0 ),
34 aBuffer(),
35 mSocket( 0 ),
36 usingCSocket( false )
40 BufferedStreamSocket::BufferedStreamSocket( int aSocket ):
41 StreamSocket(),
42 aRet( 0 ),
43 aRead( 0 ),
44 aBuffer(),
45 mSocket( aSocket ),
46 usingCSocket( true )
50 void BufferedStreamSocket::getPeerAddr(osl::SocketAddr& rAddr)
52 assert ( !usingCSocket );
53 StreamSocket::getPeerAddr( rAddr );
56 sal_Int32 BufferedStreamSocket::write( const void* pBuffer, sal_uInt32 n )
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 );
71 void BufferedStreamSocket::close()
73 if( usingCSocket && mSocket != -1 )
75 #ifdef _WIN32
76 ::closesocket( mSocket );
77 #else
78 ::close( mSocket );
79 #endif
80 mSocket = -1;
82 else
83 ::osl::StreamSocket::close();
86 sal_Int32 BufferedStreamSocket::readLine( OString& aLine )
88 while ( true )
90 // Process buffer first in case data already present.
91 vector<char>::iterator aIt;
92 if ( (aIt = find( aBuffer.begin(), aBuffer.end(), '\n' ))
93 != aBuffer.end() )
95 sal_uInt64 aLocation = aIt - aBuffer.begin();
97 aLine = OString( &(*aBuffer.begin()), aLocation );
99 aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the empty line
100 aRead -= (aLocation + 1);
102 SAL_INFO( "sdremote.bluetooth", "recv line '" << aLine << "'" );
104 return aLine.getLength() + 1;
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 );
114 SAL_INFO( "sdremote.bluetooth", "recv " << aRet << " aBuffer len " << aBuffer.size() );
115 if ( aRet <= 0 )
117 return 0;
119 // Prevent buffer from growing massively large.
120 if ( aRead > MAX_LINE_LENGTH )
122 aBuffer.clear();
123 return 0;
125 aRead += aRet;
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */