tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sd / source / ui / remotecontrol / BufferedStreamSocket.cxx
blob2834bb74a9f9c8f400760fd96282a6fb64f23dbc
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 osl;
29 BufferedStreamSocket::BufferedStreamSocket( const osl::StreamSocket &aSocket ):
30 StreamSocket( aSocket ),
31 aRead( 0 ),
32 mSocket( 0 ),
33 usingCSocket( false )
37 BufferedStreamSocket::BufferedStreamSocket( int aSocket ):
38 aRead( 0 ),
39 mSocket( aSocket ),
40 usingCSocket( true )
44 BufferedStreamSocket::~BufferedStreamSocket() {
45 close();
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(
60 mSocket,
61 #if defined(_WIN32)
62 static_cast<char const *>(pBuffer),
63 #else
64 pBuffer,
65 #endif
66 static_cast<size_t>(n), 0 );
69 void BufferedStreamSocket::close()
71 if( usingCSocket && mSocket != -1 )
73 #ifdef _WIN32
74 ::closesocket( mSocket );
75 #else
76 ::close( mSocket );
77 #endif
78 mSocket = -1;
80 else
81 ::osl::StreamSocket::close();
84 sal_Int32 BufferedStreamSocket::readLine( OString& aLine )
86 while ( true )
88 // Process buffer first in case data already present.
89 std::vector<char>::iterator aIt;
90 if ( (aIt = find( aBuffer.begin(), aBuffer.end(), '\n' ))
91 != aBuffer.end() )
93 sal_uInt64 aLocation = aIt - aBuffer.begin();
95 aLine = OString( &(*aBuffer.begin()), aLocation );
97 aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the empty line
98 aRead -= (aLocation + 1);
100 SAL_INFO( "sdremote.bluetooth", "recv line '" << aLine << "'" );
102 return aLine.getLength() + 1;
105 // Then try and receive if nothing present
106 aBuffer.resize( aRead + 100 );
108 sal_Int32 nRet;
109 if (!usingCSocket)
111 // coverity[ tainted_data_return : FALSE ] version 2023.12.2
112 nRet = StreamSocket::recv( &aBuffer[aRead], 100 );
114 else
115 nRet = ::recv( mSocket, &aBuffer[aRead], 100, 0 );
117 SAL_INFO( "sdremote.bluetooth", "recv " << nRet << " aBuffer len " << aBuffer.size() );
118 if (nRet <= 0)
119 return 0;
121 // Prevent buffer from growing massively large.
122 if ( aRead > MAX_LINE_LENGTH )
124 aBuffer.clear();
125 return 0;
127 aRead += nRet;
131 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */