From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- .../ui/remotecontrol/BufferedStreamSocket.cxx | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 sd/source/ui/remotecontrol/BufferedStreamSocket.cxx (limited to 'sd/source/ui/remotecontrol/BufferedStreamSocket.cxx') diff --git a/sd/source/ui/remotecontrol/BufferedStreamSocket.cxx b/sd/source/ui/remotecontrol/BufferedStreamSocket.cxx new file mode 100644 index 000000000..64ad5eb8d --- /dev/null +++ b/sd/source/ui/remotecontrol/BufferedStreamSocket.cxx @@ -0,0 +1,130 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include "BufferedStreamSocket.hxx" + +#include +#include +#include + +#ifdef _WIN32 + // LO vs WinAPI conflict + #undef WB_LEFT + #undef WB_RIGHT + + #include +#else + #include + #include +#endif +using namespace sd; +using namespace osl; + +BufferedStreamSocket::BufferedStreamSocket( const osl::StreamSocket &aSocket ): + StreamSocket( aSocket ), + aRet( 0 ), + aRead( 0 ), + mSocket( 0 ), + usingCSocket( false ) +{ +} + +BufferedStreamSocket::BufferedStreamSocket( int aSocket ): + aRet( 0 ), + aRead( 0 ), + mSocket( aSocket ), + usingCSocket( true ) +{ +} + +BufferedStreamSocket::~BufferedStreamSocket() { + close(); +} + +void BufferedStreamSocket::getPeerAddr(osl::SocketAddr& rAddr) +{ + assert ( !usingCSocket ); + StreamSocket::getPeerAddr( rAddr ); +} + +sal_Int32 BufferedStreamSocket::write( const void* pBuffer, sal_uInt32 n ) +{ + if ( !usingCSocket ) + return StreamSocket::write( pBuffer, n ); + else + return ::send( + mSocket, +#if defined(_WIN32) + static_cast(pBuffer), +#else + pBuffer, +#endif + static_cast(n), 0 ); +} + +void BufferedStreamSocket::close() +{ + if( usingCSocket && mSocket != -1 ) + { +#ifdef _WIN32 + ::closesocket( mSocket ); +#else + ::close( mSocket ); +#endif + mSocket = -1; + } + else + ::osl::StreamSocket::close(); +} + +sal_Int32 BufferedStreamSocket::readLine( OString& aLine ) +{ + while ( true ) + { + // Process buffer first in case data already present. + std::vector::iterator aIt; + if ( (aIt = find( aBuffer.begin(), aBuffer.end(), '\n' )) + != aBuffer.end() ) + { + sal_uInt64 aLocation = aIt - aBuffer.begin(); + + aLine = OString( &(*aBuffer.begin()), aLocation ); + + aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the empty line + aRead -= (aLocation + 1); + + SAL_INFO( "sdremote.bluetooth", "recv line '" << aLine << "'" ); + + return aLine.getLength() + 1; + } + + // Then try and receive if nothing present + aBuffer.resize( aRead + 100 ); + if ( !usingCSocket) + aRet = StreamSocket::recv( &aBuffer[aRead], 100 ); + else + aRet = ::recv( mSocket, &aBuffer[aRead], 100, 0 ); + + SAL_INFO( "sdremote.bluetooth", "recv " << aRet << " aBuffer len " << aBuffer.size() ); + if ( aRet <= 0 ) + { + return 0; + } + // Prevent buffer from growing massively large. + if ( aRead > MAX_LINE_LENGTH ) + { + aBuffer.clear(); + return 0; + } + aRead += aRet; + } + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3