1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include "BufferedStreamSocket.hxx"
12 #include <osl/socket.hxx>
13 #include <sal/log.hxx>
17 // LO vs WinAPI conflict
23 #include <sys/socket.h>
30 BufferedStreamSocket::BufferedStreamSocket( const osl::StreamSocket
&aSocket
):
31 StreamSocket( aSocket
),
40 BufferedStreamSocket::BufferedStreamSocket( int aSocket
):
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
)
59 return StreamSocket::write( pBuffer
, n
);
64 static_cast<char const *>(pBuffer
),
68 static_cast<size_t>(n
), 0 );
71 void BufferedStreamSocket::close()
73 if( usingCSocket
&& mSocket
!= -1 )
76 ::closesocket( mSocket
);
83 ::osl::StreamSocket::close();
86 sal_Int32
BufferedStreamSocket::readLine( OString
& aLine
)
90 // Process buffer first in case data already present.
91 vector
<char>::iterator aIt
;
92 if ( (aIt
= find( aBuffer
.begin(), aBuffer
.end(), '\n' ))
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 );
110 aRet
= StreamSocket::recv( &aBuffer
[aRead
], 100 );
112 aRet
= ::recv( mSocket
, &aBuffer
[aRead
], 100, 0 );
114 SAL_INFO( "sdremote.bluetooth", "recv " << aRet
<< " aBuffer len " << aBuffer
.size() );
119 // Prevent buffer from growing massively large.
120 if ( aRead
> MAX_LINE_LENGTH
)
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */