Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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 : : */
9 : :
10 : : #include <BufferedStreamSocket.hxx>
11 : :
12 : : #include <algorithm>
13 : :
14 : : #ifdef WIN32
15 : : #include <winsock2.h>
16 : : #else
17 : : #include <sys/socket.h>
18 : : #include <unistd.h>
19 : : #endif
20 : : using namespace sd;
21 : : using namespace std;
22 : : using namespace osl;
23 : :
24 : 0 : BufferedStreamSocket::BufferedStreamSocket( const osl::StreamSocket &aSocket ):
25 : : StreamSocket( aSocket ),
26 : : aRet( 0 ),
27 : : aRead( 0 ),
28 : : aBuffer(),
29 : : mSocket( 0 ),
30 [ # # ]: 0 : usingCSocket( false)
31 : : {
32 : 0 : }
33 : :
34 : 0 : BufferedStreamSocket::BufferedStreamSocket( int aSocket ):
35 : : StreamSocket(),
36 : : aRet( 0 ),
37 : : aRead( 0 ),
38 : : aBuffer(),
39 : : mSocket( aSocket ),
40 [ # # ]: 0 : usingCSocket( true )
41 : : {
42 : 0 : }
43 : :
44 : 0 : void BufferedStreamSocket::getPeerAddr(osl::SocketAddr& rAddr)
45 : : {
46 : : assert ( !usingCSocket );
47 : 0 : StreamSocket::getPeerAddr( rAddr );
48 : 0 : }
49 : :
50 : 0 : sal_Int32 BufferedStreamSocket::write( const void* pBuffer, sal_uInt32 n )
51 : : {
52 [ # # ]: 0 : if ( !usingCSocket )
53 : 0 : return StreamSocket::write( pBuffer, n );
54 : : else
55 : 0 : return ::send( mSocket, (const char *) pBuffer, (size_t) n, 0 );
56 : : }
57 : :
58 : 0 : sal_Int32 BufferedStreamSocket::readLine( OString& aLine )
59 : : {
60 : 0 : while ( true )
61 : : {
62 : : // Process buffer first incase data already present.
63 : 0 : vector<char>::iterator aIt;
64 [ # # ][ # # ]: 0 : if ( (aIt = find( aBuffer.begin(), aBuffer.end(), '\n' ))
65 [ # # ]: 0 : != aBuffer.end() )
66 : : {
67 [ # # ]: 0 : sal_uInt64 aLocation = aIt - aBuffer.begin();
68 : :
69 [ # # ]: 0 : aLine = OString( &(*aBuffer.begin()), aLocation );
70 : :
71 [ # # ][ # # ]: 0 : aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the empty line
72 : 0 : aRead -= (aLocation + 1);
73 : :
74 : 0 : return aLine.getLength() + 1;
75 : : }
76 : :
77 : : // Then try and receive if nothing present
78 [ # # ]: 0 : aBuffer.resize( aRead + 100 );
79 [ # # ]: 0 : if ( !usingCSocket)
80 [ # # ][ # # ]: 0 : aRet = StreamSocket::recv( &aBuffer[aRead], 100 );
81 : : else
82 [ # # ][ # # ]: 0 : aRet = ::recv( mSocket, &aBuffer[aRead], 100, 0 );
83 : :
84 [ # # ]: 0 : if ( aRet == 0 )
85 : : {
86 : 0 : return aRet;
87 : : }
88 : : // Prevent buffer from growing massively large.
89 [ # # ]: 0 : if ( aRead > MAX_LINE_LENGTH )
90 : : {
91 [ # # ]: 0 : aBuffer.erase( aBuffer.begin(), aBuffer.end() );
92 : 0 : return 0;
93 : : }
94 : 0 : aRead += aRet;
95 : : }
96 : :
97 : :
98 : : }
99 : :
100 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|