osl_StreamSocket.cxx (86e1cf34) osl_StreamSocket.cxx (63d99982)
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 40 unchanged lines hidden (view full) ---

49 4. following functions are declared but not implemented:
50 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
51*/
52
53//------------------------------------------------------------------------
54// include files
55//------------------------------------------------------------------------
56
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 40 unchanged lines hidden (view full) ---

49 4. following functions are declared but not implemented:
50 inline sal_Bool SAL_CALL operator== (const SocketAddr & Addr) const;
51*/
52
53//------------------------------------------------------------------------
54// include files
55//------------------------------------------------------------------------
56
57#include <testshl/simpleheader.hxx>
58
59//#include "osl_Socket_Const.h"
60#include "sockethelper.hxx"
61#include <osl/conditn.hxx>
57
58//#include "osl_Socket_Const.h"
59#include "sockethelper.hxx"
60#include <osl/conditn.hxx>
61#include <osl/signal.h>
62#include "gtest/gtest.h"
62
63using namespace osl;
64using namespace rtl;
65
66#define IP_PORT_MYPORT9 8897
67#define IP_PORT_MYPORT10 18900
68
69const char * pTestString1 = "test socket";

--- 14 unchanged lines hidden (view full) ---

84 {
85 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
86 ::osl::StreamSocket ssStreamConnection;
87
88 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
89 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
90 if ( sal_True != bOK1 )
91 {
63
64using namespace osl;
65using namespace rtl;
66
67#define IP_PORT_MYPORT9 8897
68#define IP_PORT_MYPORT10 18900
69
70const char * pTestString1 = "test socket";

--- 14 unchanged lines hidden (view full) ---

85 {
86 ::osl::SocketAddr saLocalSocketAddr( aHostIP, IP_PORT_MYPORT9 );
87 ::osl::StreamSocket ssStreamConnection;
88
89 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
90 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
91 if ( sal_True != bOK1 )
92 {
92 t_print("# AcceptorSocket bind address failed.\n" ) ;
93 printf("# AcceptorSocket bind address failed.\n" ) ;
93 return;
94 }
95 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
96 if ( sal_True != bOK2 )
97 {
94 return;
95 }
96 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
97 if ( sal_True != bOK2 )
98 {
98 t_print("# AcceptorSocket listen address failed.\n" ) ;
99 printf("# AcceptorSocket listen address failed.\n" ) ;
99 return;
100 }
101
102 asAcceptorSocket.enableNonBlockingMode( sal_False );
103
104 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
105 if (eResult != osl_Socket_Ok )
106 {
107 bOK = sal_True;
100 return;
101 }
102
103 asAcceptorSocket.enableNonBlockingMode( sal_False );
104
105 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
106 if (eResult != osl_Socket_Ok )
107 {
108 bOK = sal_True;
108 t_print("AcceptorThread: acceptConnection failed! \n");
109 printf("AcceptorThread: acceptConnection failed! \n");
109 }
110 }
111public:
112 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
113 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
114 {
115 bOK = sal_False;
116 }
117
118 sal_Bool isOK() { return bOK; }
119
120 ~AcceptorThread( )
121 {
122 if ( isRunning( ) )
123 {
124 asAcceptorSocket.shutdown();
110 }
111 }
112public:
113 AcceptorThread(::osl::AcceptorSocket & asSocket, ::rtl::OUString const& aBindIP )
114 : asAcceptorSocket( asSocket ), aHostIP( aBindIP )
115 {
116 bOK = sal_False;
117 }
118
119 sal_Bool isOK() { return bOK; }
120
121 ~AcceptorThread( )
122 {
123 if ( isRunning( ) )
124 {
125 asAcceptorSocket.shutdown();
125 t_print("# error: Acceptor thread not terminated.\n" );
126 printf("# error: Acceptor thread not terminated.\n" );
126 }
127 }
128};
129
130/** Server Socket Thread, served as a temp little server to communicate with client.
131 */
132class ServerSocketThread : public Thread
133{

--- 9 unchanged lines hidden (view full) ---

143
144 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
145 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
146 while ( schedule( ) == sal_True )
147 {
148 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
149 if ( sal_True != bOK1 )
150 {
127 }
128 }
129};
130
131/** Server Socket Thread, served as a temp little server to communicate with client.
132 */
133class ServerSocketThread : public Thread
134{

--- 9 unchanged lines hidden (view full) ---

144
145 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
146 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //integer not sal_Bool : sal_True);
147 while ( schedule( ) == sal_True )
148 {
149 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
150 if ( sal_True != bOK1 )
151 {
151 t_print("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ;
152 printf("# ServerSocketThread: AcceptorSocket bind address failed.\n" ) ;
152 break;
153 }
154 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
155 if ( sal_True != bOK2 )
156 {
153 break;
154 }
155 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
156 if ( sal_True != bOK2 )
157 {
157 t_print("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ;
158 printf("# ServerSocketThread: AcceptorSocket listen address failed.\n" ) ;
158 break;
159 }
160
161 asAcceptorSocket.enableNonBlockingMode( sal_False );
162 m_aCondition.set();
163
164 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
165 if (eResult != osl_Socket_Ok )
166 {
159 break;
160 }
161
162 asAcceptorSocket.enableNonBlockingMode( sal_False );
163 m_aCondition.set();
164
165 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
166 if (eResult != osl_Socket_Ok )
167 {
167 t_print("ServerSocketThread: acceptConnection failed! \n");
168 printf("ServerSocketThread: acceptConnection failed! \n");
168 break;
169 }
170 sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 );
171 sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 );
172 pReadBuffer[nReadNumber1 + nReadNumber2] = '\0';
169 break;
170 }
171 sal_Int32 nReadNumber1 = ssStreamConnection.recv( pReadBuffer, 11 );
172 sal_Int32 nReadNumber2 = ssStreamConnection.recv( pReadBuffer + nReadNumber1, 11 );
173 pReadBuffer[nReadNumber1 + nReadNumber2] = '\0';
173 //t_print("# read buffer content: %s\n", pReadBuffer );
174 //printf("# read buffer content: %s\n", pReadBuffer );
174 break;
175 }
176 ssStreamConnection.close();
177 asAcceptorSocket.close();
178
179 }
180
181 void SAL_CALL onTerminated( )
182 {
175 break;
176 }
177 ssStreamConnection.close();
178 asAcceptorSocket.close();
179
180 }
181
182 void SAL_CALL onTerminated( )
183 {
183 //t_print("# normally terminate this server thread %d!\n", m_id );
184 //printf("# normally terminate this server thread %d!\n", m_id );
184 }
185
186public:
187 // public to check if data transmition is OK
188 sal_Char pReadBuffer[30];
189 ServerSocketThread( osl::Condition &_aCond ):m_aCondition(_aCond)
190 {
191 m_aCondition.reset();
185 }
186
187public:
188 // public to check if data transmition is OK
189 sal_Char pReadBuffer[30];
190 ServerSocketThread( osl::Condition &_aCond ):m_aCondition(_aCond)
191 {
192 m_aCondition.reset();
192 t_print("#init ServerSocketThread\n");
193 printf("#init ServerSocketThread\n");
193 m_id = getIdentifier( );
194 m_id = getIdentifier( );
194 //t_print("# successfully creat this ServerSocketThread %d!\n", m_id );
195 //printf("# successfully creat this ServerSocketThread %d!\n", m_id );
195 }
196
197 ~ServerSocketThread( )
198 {
199 if ( isRunning( ) )
196 }
197
198 ~ServerSocketThread( )
199 {
200 if ( isRunning( ) )
200 t_print("# error: ServerSocketThread has not terminated.\n" );
201 printf("# error: ServerSocketThread has not terminated.\n" );
201 }
202};
203
204/** Client Socket Thread, served as a temp little client to communicate with server.
205 */
206class ClientSocketThread : public Thread
207{
208protected:

--- 19 unchanged lines hidden (view full) ---

228 }
229
230 if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
231 {
232 m_csConnectorSocket.send( pTestString1, 11 ); // "test socket"
233 m_csConnectorSocket.send( pTestString2, 10);
234 }
235 else
202 }
203};
204
205/** Client Socket Thread, served as a temp little client to communicate with server.
206 */
207class ClientSocketThread : public Thread
208{
209protected:

--- 19 unchanged lines hidden (view full) ---

229 }
230
231 if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
232 {
233 m_csConnectorSocket.send( pTestString1, 11 ); // "test socket"
234 m_csConnectorSocket.send( pTestString2, 10);
235 }
236 else
236 t_print("# ClientSocketThread: connect failed! \n");
237 printf("# ClientSocketThread: connect failed! \n");
237 // terminate();
238 //}
239 m_csConnectorSocket.close();
240 free( pTimeout );
241 }
242
243 void SAL_CALL onTerminated( )
244 {
238 // terminate();
239 //}
240 m_csConnectorSocket.close();
241 free( pTimeout );
242 }
243
244 void SAL_CALL onTerminated( )
245 {
245 //t_print("# normally terminate this thread %d!\n", m_id );
246 //printf("# normally terminate this thread %d!\n", m_id );
246 }
247
248public:
249 ClientSocketThread( osl::Condition &_aCond ):
250 m_aCondition(_aCond),
251 m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ),
252 m_csConnectorSocket( )
253 {
254 m_id = getIdentifier( );
247 }
248
249public:
250 ClientSocketThread( osl::Condition &_aCond ):
251 m_aCondition(_aCond),
252 m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ),
253 m_csConnectorSocket( )
254 {
255 m_id = getIdentifier( );
255 //t_print("# successfully creat this client thread %d!\n", m_id );
256 //printf("# successfully creat this client thread %d!\n", m_id );
256 }
257
258 ~ClientSocketThread( )
259 {
260 if ( isRunning( ) )
257 }
258
259 ~ClientSocketThread( )
260 {
261 if ( isRunning( ) )
261 t_print("# error: client thread not terminated.\n" );
262 printf("# error: client thread not terminated.\n" );
262 }
263
264};
265
266// -----------------------------------------------------------------------------
267// Helper functions, to create buffers, check buffers
268class ValueCheckProvider
269{

--- 61 unchanged lines hidden (view full) ---

331
332 void SAL_CALL run( )
333 {
334 ::osl::SocketAddr m_aTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 );
335 ::osl::ConnectorSocket m_aConnectorSocket;
336
337 if (! m_aTargetSocketAddr.is())
338 {
263 }
264
265};
266
267// -----------------------------------------------------------------------------
268// Helper functions, to create buffers, check buffers
269class ValueCheckProvider
270{

--- 61 unchanged lines hidden (view full) ---

332
333 void SAL_CALL run( )
334 {
335 ::osl::SocketAddr m_aTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 );
336 ::osl::ConnectorSocket m_aConnectorSocket;
337
338 if (! m_aTargetSocketAddr.is())
339 {
339 t_print("# SocketAddr was NOT created successfully!\n");
340 printf("# SocketAddr was NOT created successfully!\n");
340 }
341 else
342 {
341 }
342 else
343 {
343 t_print("start ReadSocketThread\n");
344 printf("start ReadSocketThread\n");
344
345 TimeValue *pTimeout;
346 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
347 pTimeout->Seconds = 5;
348 pTimeout->Nanosec = 0;
349
350 m_aCondition.wait();
351
345
346 TimeValue *pTimeout;
347 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
348 pTimeout->Seconds = 5;
349 pTimeout->Nanosec = 0;
350
351 m_aCondition.wait();
352
352 t_print("connect()\n");
353 printf("connect()\n");
353
354 oslSocketResult eResult = m_aConnectorSocket.connect( m_aTargetSocketAddr, pTimeout );
355 if ( osl_Socket_Ok == eResult )
356 {
357 sal_Int32 nReadCount = m_aConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() );
358 m_aValues.checkValues(nReadCount, m_nValue);
359 }
360 else
361 {
354
355 oslSocketResult eResult = m_aConnectorSocket.connect( m_aTargetSocketAddr, pTimeout );
356 if ( osl_Socket_Ok == eResult )
357 {
358 sal_Int32 nReadCount = m_aConnectorSocket.read( m_aValues.getWriteBuffer(), m_aValues.getBufferSize() );
359 m_aValues.checkValues(nReadCount, m_nValue);
360 }
361 else
362 {
362 t_print("# ReadSocketThread: connect failed! \n");
363 printf("# ReadSocketThread: connect failed! \n");
363 printSocketResult(eResult);
364 }
365
366 //remove this line for deadlock on solaris( margritte.germany )
367 m_aConnectorSocket.close();
368 free( pTimeout );
369 }
370 }
371
372 void SAL_CALL onTerminated( )
373 {
364 printSocketResult(eResult);
365 }
366
367 //remove this line for deadlock on solaris( margritte.germany )
368 m_aConnectorSocket.close();
369 free( pTimeout );
370 }
371 }
372
373 void SAL_CALL onTerminated( )
374 {
374 //t_print("# normally terminate this thread %d!\n", m_id );
375 //printf("# normally terminate this thread %d!\n", m_id );
375 }
376
377public:
378 sal_Int32 getCount() {return m_aValues.getBufferSize();}
379 bool isOk() {return m_aValues.isFailure() == true ? false : true;}
380
381 ReadSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
382 : m_nValue( _nValue ),
383 m_aCondition(_aCond)
384 {
376 }
377
378public:
379 sal_Int32 getCount() {return m_aValues.getBufferSize();}
380 bool isOk() {return m_aValues.isFailure() == true ? false : true;}
381
382 ReadSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
383 : m_nValue( _nValue ),
384 m_aCondition(_aCond)
385 {
385 t_print("#init ReadSocketThread\n");
386 printf("#init ReadSocketThread\n");
386 m_id = getIdentifier( );
387
387 m_id = getIdentifier( );
388
388 //t_print("# successfully creat this client thread %d!\n", m_id );
389 //printf("# successfully creat this client thread %d!\n", m_id );
389 m_aValues.createBuffer(_nBufferSize, 0);
390 }
391
392 ~ReadSocketThread( )
393 {
394 if ( isRunning( ) )
390 m_aValues.createBuffer(_nBufferSize, 0);
391 }
392
393 ~ReadSocketThread( )
394 {
395 if ( isRunning( ) )
395 t_print("# error: client thread not terminated.\n" );
396 printf("# error: client thread not terminated.\n" );
396 m_aValues.freeBuffer();
397 }
398
399};
400
401/** Server Socket Thread, write a file which is large
402 */
403class WriteSocketThread : public Thread
404{
405 ValueCheckProvider m_aValues;
406 osl::Condition &m_aCondition;
407
408protected:
409 oslThreadIdentifier m_id;
410
411 void SAL_CALL run( )
412 {
397 m_aValues.freeBuffer();
398 }
399
400};
401
402/** Server Socket Thread, write a file which is large
403 */
404class WriteSocketThread : public Thread
405{
406 ValueCheckProvider m_aValues;
407 osl::Condition &m_aCondition;
408
409protected:
410 oslThreadIdentifier m_id;
411
412 void SAL_CALL run( )
413 {
413 t_print("start WriteSocketThread\n");
414 printf("start WriteSocketThread\n");
414 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
415 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 );
416 if (! saLocalSocketAddr.is())
417 {
415 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
416 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT10 );
417 if (! saLocalSocketAddr.is())
418 {
418 t_print("LocalSocketAddr was NOT created successfully!\n");
419 printf("LocalSocketAddr was NOT created successfully!\n");
419 }
420
421 ::osl::StreamSocket ssStreamConnection;
422
423 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
424 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
425
426 /// if the thread should terminate, schedule return false
427 // while ( schedule( ) == sal_True )
428 // {
420 }
421
422 ::osl::StreamSocket ssStreamConnection;
423
424 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
425 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
426
427 /// if the thread should terminate, schedule return false
428 // while ( schedule( ) == sal_True )
429 // {
429 t_print("bind()\n");
430 printf("bind()\n");
430 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
431 if ( sal_True != bOK1 )
432 {
431 sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
432 if ( sal_True != bOK1 )
433 {
433 t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
434 printf("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
434 }
435 else
436 {
435 }
436 else
437 {
437 t_print("listen()\n");
438 printf("listen()\n");
438 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
439 if ( sal_True != bOK2 )
440 {
439 sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
440 if ( sal_True != bOK2 )
441 {
441 t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
442 printf("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
442 }
443 else
444 {
445
446 // blocking mode, if read/recv failed, block until success
447 asAcceptorSocket.enableNonBlockingMode( sal_False);
443 }
444 else
445 {
446
447 // blocking mode, if read/recv failed, block until success
448 asAcceptorSocket.enableNonBlockingMode( sal_False);
448 t_print("acceptConnection()\n");
449 printf("acceptConnection()\n");
449 m_aCondition.set();
450
451 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
452 if (eResult != osl_Socket_Ok )
453 {
450 m_aCondition.set();
451
452 oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
453 if (eResult != osl_Socket_Ok )
454 {
454 t_print("WriteSocketThread: acceptConnection failed! \n");
455 printf("WriteSocketThread: acceptConnection failed! \n");
455 }
456 else
457 {
458
459// LLA: removed, due to the fact, this is to error prone
460// LLA: char * pSrcRoot = getenv("SOURCE_ROOT");
461// LLA: // LLA: This is absolute wrong!
462// LLA: // strcat( pSrcRoot, "/sal/inc/osl/file.hxx");

--- 11 unchanged lines hidden (view full) ---

474// LLA:
475// LLA: if( (stream = fopen( oustring2char( sFilePath ), "r+t" )) != NULL )
476// LLA: {
477// LLA: /* Attempt to read in 25 characters */
478// LLA: nCount_read = fread( buffer_read, sizeof( char ), FILE_READ, stream );
479// LLA: fclose( stream );
480// LLA: }
481// LLA: else
456 }
457 else
458 {
459
460// LLA: removed, due to the fact, this is to error prone
461// LLA: char * pSrcRoot = getenv("SOURCE_ROOT");
462// LLA: // LLA: This is absolute wrong!
463// LLA: // strcat( pSrcRoot, "/sal/inc/osl/file.hxx");

--- 11 unchanged lines hidden (view full) ---

475// LLA:
476// LLA: if( (stream = fopen( oustring2char( sFilePath ), "r+t" )) != NULL )
477// LLA: {
478// LLA: /* Attempt to read in 25 characters */
479// LLA: nCount_read = fread( buffer_read, sizeof( char ), FILE_READ, stream );
480// LLA: fclose( stream );
481// LLA: }
482// LLA: else
482// LLA: t_print("# File $SRC_ROOT/sal/inc/osl/file.hxx could not be opened\n" );
483// LLA: printf("# File $SRC_ROOT/sal/inc/osl/file.hxx could not be opened\n" );
483
484
484 t_print("write()\n");
485 printf("write()\n");
485
486 ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
486
487 ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
487 t_print("done written.\n");
488 printf("done written.\n");
488 }
489 }
490 }
491 ssStreamConnection.close();
492 asAcceptorSocket.close();
493 }
494
495 void SAL_CALL onTerminated( )
496 {
489 }
490 }
491 }
492 ssStreamConnection.close();
493 asAcceptorSocket.close();
494 }
495
496 void SAL_CALL onTerminated( )
497 {
497 //t_print("# normally terminate this server thread %d!\n", m_id );
498 //printf("# normally terminate this server thread %d!\n", m_id );
498 }
499
500public:
501 // public to check if data transmition is OK
502 WriteSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
503 : m_aCondition(_aCond)
504 {
505 m_aCondition.reset();
506
499 }
500
501public:
502 // public to check if data transmition is OK
503 WriteSocketThread(sal_Int32 _nBufferSize, int _nValue, osl::Condition &_aCond )
504 : m_aCondition(_aCond)
505 {
506 m_aCondition.reset();
507
507 t_print("#init WriteSocketThread\n");
508 printf("#init WriteSocketThread\n");
508 m_id = getIdentifier( );
509 m_id = getIdentifier( );
509 //t_print("# successfully creat this server thread %d!\n", m_id );
510 //printf("# successfully creat this server thread %d!\n", m_id );
510
511 m_aValues.createBuffer(_nBufferSize, _nValue);
512 }
513
514 ~WriteSocketThread( )
515 {
516 if ( isRunning( ) )
511
512 m_aValues.createBuffer(_nBufferSize, _nValue);
513 }
514
515 ~WriteSocketThread( )
516 {
517 if ( isRunning( ) )
517 t_print("# error: server thread not terminated.\n" );
518 printf("# error: server thread not terminated.\n" );
518 m_aValues.freeBuffer();
519 }
520};
521
522// -----------------------------------------------------------------------------
523
524namespace osl_StreamSocket
525{

--- 5 unchanged lines hidden (view full) ---

531
532 inline StreamSocket( const StreamSocket & );
533
534 inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
535
536 inline StreamSocket( oslSocket Socket );
537 */
538
519 m_aValues.freeBuffer();
520 }
521};
522
523// -----------------------------------------------------------------------------
524
525namespace osl_StreamSocket
526{

--- 5 unchanged lines hidden (view full) ---

532
533 inline StreamSocket( const StreamSocket & );
534
535 inline StreamSocket( oslSocket Socket , __sal_NoAcquire noacquire );
536
537 inline StreamSocket( oslSocket Socket );
538 */
539
539 class ctors : public CppUnit::TestFixture
540 class ctors : public ::testing::Test
540 {
541 public:
542 oslSocket sHandle;
543 // initialization
541 {
542 public:
543 oslSocket sHandle;
544 // initialization
544 void setUp( )
545 void SetUp( )
545 {
546 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
547 }
548
546 {
547 sHandle = osl_createSocket( osl_Socket_FamilyInet, osl_Socket_TypeStream, osl_Socket_ProtocolIp );
548 }
549
549 void tearDown( )
550 void TearDown( )
550 {
551 sHandle = NULL;
552 }
551 {
552 sHandle = NULL;
553 }
554 }; // class ctors
553
555
556 TEST_F(ctors, ctors_none)
557 {
558 /// Socket constructor.
559 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
554
560
555 void ctors_none()
556 {
557 /// Socket constructor.
558 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
561 ASSERT_TRUE(osl_Socket_TypeStream == ssSocket.getType( )) << "test for ctors_none constructor function: check if the stream socket was created successfully.";
562 }
559
563
560 CPPUNIT_ASSERT_MESSAGE( "test for ctors_none constructor function: check if the stream socket was created successfully.",
561 osl_Socket_TypeStream == ssSocket.getType( ) );
562 }
564 TEST_F(ctors, ctors_acquire)
565 {
566 /// Socket constructor.
567 ::osl::StreamSocket ssSocket( sHandle );
563
568
564 void ctors_acquire()
565 {
566 /// Socket constructor.
567 ::osl::StreamSocket ssSocket( sHandle );
569 ASSERT_TRUE(osl_Socket_TypeStream == ssSocket.getType( )) << "test for ctors_acquire constructor function: check if the socket was created successfully";
570 }
568
571
569 CPPUNIT_ASSERT_MESSAGE( "test for ctors_acquire constructor function: check if the socket was created successfully",
570 osl_Socket_TypeStream == ssSocket.getType( ) );
571 }
572 TEST_F(ctors, ctors_no_acquire)
573 {
574 /// Socket constructor.
575 ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE );
572
576
573 void ctors_no_acquire()
574 {
575 /// Socket constructor.
576 ::osl::StreamSocket ssSocket( sHandle, SAL_NO_ACQUIRE );
577 ASSERT_TRUE(osl_Socket_TypeStream == ssSocket.getType( )) << " test for ctors_no_acquire constructor function: check if the socket was created successfully";
578 }
577
579
578 CPPUNIT_ASSERT_MESSAGE(" test for ctors_no_acquire constructor function: check if the socket was created successfully",
579 osl_Socket_TypeStream == ssSocket.getType( ) );
580 }
580 TEST_F(ctors, ctors_copy_ctor)
581 {
582 /// Socket constructor.
583 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
584 /// Socket copy constructor.
585 ::osl::StreamSocket copySocket( ssSocket );
581
586
582 void ctors_copy_ctor()
583 {
584 /// Socket constructor.
585 ::osl::StreamSocket ssSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
586 /// Socket copy constructor.
587 ::osl::StreamSocket copySocket( ssSocket );
587 ASSERT_TRUE(osl_Socket_TypeStream == copySocket.getType( )) << " test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor";
588 }
588
589
589 CPPUNIT_ASSERT_MESSAGE(" test for ctors_copy_ctor constructor function: create new Socket instance using copy constructor",
590 osl_Socket_TypeStream == copySocket.getType( ) );
591 }
592
593 CPPUNIT_TEST_SUITE( ctors );
594 CPPUNIT_TEST( ctors_none );
595 CPPUNIT_TEST( ctors_acquire );
596 CPPUNIT_TEST( ctors_no_acquire );
597 CPPUNIT_TEST( ctors_copy_ctor );
598 CPPUNIT_TEST_SUITE_END();
599
600 }; // class ctors
601
602 class send_recv: public CppUnit::TestFixture
590 class send_recv: public ::testing::Test
603 {
604 public:
605 // initialization
591 {
592 public:
593 // initialization
606 void setUp( )
594 void SetUp( )
607 {
608 }
609
595 {
596 }
597
610 void tearDown( )
598 void TearDown( )
611 {
612
613 }
614
599 {
600
601 }
602
615 void send_recv1()
616 {
617 osl::Condition aCondition;
618 //client sent two strings, and server received, check the order and value
619 ServerSocketThread myServerThread( aCondition );
620 ClientSocketThread myClientThread( aCondition );
621 myServerThread.create( );
622 myClientThread.create( );
623
624 //wait until the thread terminate
625 myClientThread.join( );
626 myServerThread.join( );
627 sal_Char myStr[30] = "";
628 strcat( myStr, pTestString1 );
629 strcat( myStr, pTestString2 );
630 sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr );
631 CPPUNIT_ASSERT_MESSAGE(" test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.",
632 nRes == 0 );
633 }
634
635 // error when recv
636 void send_recv2()
637 {
638 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
639 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 );
640 ::osl::StreamSocket ssStreamConnection;
641 sal_Char pReadBuffer[30] = "";
642
643 osl::Condition aCondition;
644 aCondition.reset();
645 ClientSocketThread myClientThread( aCondition );
646 myClientThread.create( );
647
648 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
649
650 asAcceptorSocket.bind( saLocalSocketAddr );
651 asAcceptorSocket.listen( 1 );
652 asAcceptorSocket.enableNonBlockingMode( sal_True );
653 aCondition.set();
654
655 asAcceptorSocket.acceptConnection( ssStreamConnection );
656 sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 );
657
658 myClientThread.join( ) ;
659 ssStreamConnection.close();
660 asAcceptorSocket.close();
661 CPPUNIT_ASSERT_MESSAGE(" test for send/recv, recv error!", nReadNumber == -1 );
662 }
663
664 // LLA: This is a helper function, which create 2 threads, a server and a client.
665 // the server writes the buffersize to the client.
666
667 void write_read(sal_Int32 _nBufferSize, int _nValue)
668 {
669 //client sent two strings, and server received, check the order and value
670 osl::Condition aCondition;
671 WriteSocketThread myServerThread(_nBufferSize, _nValue, aCondition);

--- 6 unchanged lines hidden (view full) ---

678 myClientThread.join( );
679 myServerThread.join( );
680
681 //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500
682 // Proteon PRONET = 2046), so here test read 4000 bytes
683 sal_Int32 nLength = myClientThread.getCount();
684 bool bIsOk = myClientThread.isOk(); // check if the values are right.
685
603 // LLA: This is a helper function, which create 2 threads, a server and a client.
604 // the server writes the buffersize to the client.
605
606 void write_read(sal_Int32 _nBufferSize, int _nValue)
607 {
608 //client sent two strings, and server received, check the order and value
609 osl::Condition aCondition;
610 WriteSocketThread myServerThread(_nBufferSize, _nValue, aCondition);

--- 6 unchanged lines hidden (view full) ---

617 myClientThread.join( );
618 myServerThread.join( );
619
620 //Maximum Packet Size is ( ARPANET, MILNET = 1007 Ethernet (10Mb) = 1500
621 // Proteon PRONET = 2046), so here test read 4000 bytes
622 sal_Int32 nLength = myClientThread.getCount();
623 bool bIsOk = myClientThread.isOk(); // check if the values are right.
624
686 t_print("Length:=%d\n", nLength);
687 t_print(" bIsOk:=%d\n", bIsOk);
625 printf("Length:=%d\n", nLength);
626 printf(" bIsOk:=%d\n", bIsOk);
688
627
689 CPPUNIT_ASSERT_MESSAGE(" test for write/read values with two threads: send data from server, check readed data in client.",
690 nLength == _nBufferSize && bIsOk == true);
628 ASSERT_TRUE(nLength == _nBufferSize && bIsOk == true) << " test for write/read values with two threads: send data from server, check readed data in client.";
691 }
629 }
630 }; // class send_recv
692
631
693 // Tests with different values and sizes
694 void write_read_001()
695 {
696 write_read(50, 10);
697 }
698 void write_read_002()
699 {
700 write_read(1024, 20);
701 }
702 void write_read_003()
703 {
704 write_read(4000, 1);
705 }
706 void write_read_004()
707 {
708 write_read(8192, 3);
709 }
710 void write_read_005()
711 {
712 write_read(32768, 3);
713 }
632 TEST_F(send_recv, send_recv1)
633 {
634 osl::Condition aCondition;
635 //client sent two strings, and server received, check the order and value
636 ServerSocketThread myServerThread( aCondition );
637 ClientSocketThread myClientThread( aCondition );
638 myServerThread.create( );
639 myClientThread.create( );
714
640
715 CPPUNIT_TEST_SUITE( send_recv );
716 CPPUNIT_TEST( write_read_001 );
717 CPPUNIT_TEST( write_read_002 );
718 CPPUNIT_TEST( write_read_003 );
719 CPPUNIT_TEST( write_read_004 );
720 CPPUNIT_TEST( write_read_005 );
721 CPPUNIT_TEST( send_recv1 );
722 CPPUNIT_TEST( send_recv2 );
723// CPPUNIT_TEST( write_read );
724 CPPUNIT_TEST_SUITE_END();
725 }; // class send_recv
641 //wait until the thread terminate
642 myClientThread.join( );
643 myServerThread.join( );
644 sal_Char myStr[30] = "";
645 strcat( myStr, pTestString1 );
646 strcat( myStr, pTestString2 );
647 sal_Int32 nRes = strcmp( myServerThread.pReadBuffer, myStr );
648 ASSERT_TRUE(nRes == 0) << " test for send/recv with two threads: launch Server/Client threads, send data from client, check received data in Server thread.";
649 }
726
650
651 // error when recv
652 TEST_F(send_recv, send_recv2)
653 {
654 ::osl::AcceptorSocket asAcceptorSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
655 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 );
656 ::osl::StreamSocket ssStreamConnection;
657 sal_Char pReadBuffer[30] = "";
658
659 osl::Condition aCondition;
660 aCondition.reset();
661 ClientSocketThread myClientThread( aCondition );
662 myClientThread.create( );
663
664 asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
665
666 asAcceptorSocket.bind( saLocalSocketAddr );
667 asAcceptorSocket.listen( 1 );
668 asAcceptorSocket.enableNonBlockingMode( sal_True );
669 aCondition.set();
670
671 asAcceptorSocket.acceptConnection( ssStreamConnection );
672 sal_Int32 nReadNumber = ssStreamConnection.recv( pReadBuffer, 11 );
673
674 myClientThread.join( ) ;
675 ssStreamConnection.close();
676 asAcceptorSocket.close();
677 ASSERT_TRUE(nReadNumber == -1) << " test for send/recv, recv error!";
678 }
679
680 // Tests with different values and sizes
681 TEST_F(send_recv, write_read_001)
682 {
683 write_read(50, 10);
684 }
685 TEST_F(send_recv, write_read_002)
686 {
687 write_read(1024, 20);
688 }
689 TEST_F(send_recv, write_read_003)
690 {
691 write_read(4000, 1);
692 }
693 TEST_F(send_recv, write_read_004)
694 {
695 write_read(8192, 3);
696 }
697 TEST_F(send_recv, write_read_005)
698 {
699 write_read(32768, 3);
700 }
701
727// -----------------------------------------------------------------------------
728
729 class SendClientThread : public Thread
730 {
731 protected:
732 ::osl::SocketAddr m_saTargetSocketAddr;
733 ::osl::ConnectorSocket m_csConnectorSocket;
734 void SAL_CALL run( )

--- 5 unchanged lines hidden (view full) ---

740
741 if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
742 {
743 sal_Int32 nWrite1 = m_csConnectorSocket.write( pTestString1, 11 ); // "test socket"
744
745 sal_Int32 nWrite2 = m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
746 thread_sleep( 2 );
747 m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
702// -----------------------------------------------------------------------------
703
704 class SendClientThread : public Thread
705 {
706 protected:
707 ::osl::SocketAddr m_saTargetSocketAddr;
708 ::osl::ConnectorSocket m_csConnectorSocket;
709 void SAL_CALL run( )

--- 5 unchanged lines hidden (view full) ---

715
716 if ( osl_Socket_Ok == m_csConnectorSocket.connect( m_saTargetSocketAddr, pTimeout ))
717 {
718 sal_Int32 nWrite1 = m_csConnectorSocket.write( pTestString1, 11 ); // "test socket"
719
720 sal_Int32 nWrite2 = m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
721 thread_sleep( 2 );
722 m_csConnectorSocket.write( pTestString2, strlen( pTestString2 ) + 1 );
748 t_print("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 );
723 printf("nWrite1 is %d, nWrite2 is %d\n", nWrite1, nWrite2 );
749 //thread_sleep( 1 );
750 }
751 else
724 //thread_sleep( 1 );
725 }
726 else
752 t_print("# SendClientThread: connect failed! \n");
727 printf("# SendClientThread: connect failed! \n");
753
754 m_csConnectorSocket.close();
755 free( pTimeout );
756 }
757 public:
758 SendClientThread( ):
759 m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ),
760 m_csConnectorSocket( )
761 {
728
729 m_csConnectorSocket.close();
730 free( pTimeout );
731 }
732 public:
733 SendClientThread( ):
734 m_saTargetSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9 ),
735 m_csConnectorSocket( )
736 {
762 //t_print("# successfully creat this SendClientThread %d!\n", m_id );
737 //printf("# successfully creat this SendClientThread %d!\n", m_id );
763 }
764
765 ~SendClientThread( )
766 {
767 if ( isRunning( ) )
738 }
739
740 ~SendClientThread( )
741 {
742 if ( isRunning( ) )
768 t_print("# error: SendClientThread has not terminated.\n" );
743 printf("# error: SendClientThread has not terminated.\n" );
769 }
770
771 };
772
744 }
745
746 };
747
773 class shutdown: public CppUnit::TestFixture
748 class shutdown: public ::testing::Test
774 {
775 public:
776 // initialization
749 {
750 public:
751 // initialization
777 void setUp( )
752 void SetUp( )
778 {
779 }
780
753 {
754 }
755
781 void tearDown( )
756 void TearDown( )
782 {
783
784 }
757 {
758
759 }
760 }; // class shutdown
785
761
786 // similar to close_002
787 void shutdown_001()
788 {
762 // similar to close_002
763 TEST_F(shutdown, shutdown_001)
764 {
789#if defined(LINUX)
765#if defined(LINUX)
790 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
791 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") );
792 myAcceptorThread.create();
766 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
767 AcceptorThread myAcceptorThread( asSocket, rtl::OUString::createFromAscii("127.0.0.1") );
768 myAcceptorThread.create();
793
769
794 thread_sleep( 1 );
770 thread_sleep( 1 );
795
771
796 //when accepting, shutdown the socket, the thread will not block for accepting
797 asSocket.shutdown();
798 myAcceptorThread.join();
772 //when accepting, shutdown the socket, the thread will not block for accepting
773 asSocket.shutdown();
774 myAcceptorThread.join();
799
775
800 CPPUNIT_ASSERT_MESSAGE( "test for close when is accepting: the socket will quit accepting status.",
801 myAcceptorThread.isOK( ) == sal_True );
776 ASSERT_TRUE(myAcceptorThread.isOK( ) == sal_True) << "test for close when is accepting: the socket will quit accepting status.";
802#endif
777#endif
803 }
778 }
804
779
805 void shutdown_002()
806 {
807 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
808 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9);
809 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
810 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
811 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
812 sal_Char pReadBuffer[40];
780 TEST_F(shutdown, shutdown_002)
781 {
782 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
783 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9);
784 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
785 ASSERT_TRUE(asSocket.bind( saLocalSocketAddr ) == sal_True) << "shutdown_002: bind fail";
786 ASSERT_TRUE(asSocket.listen( 1 ) == sal_True) << "shutdown_002: listen fail";
787 sal_Char pReadBuffer[40];
813// osl::Condition aCondition;
788// osl::Condition aCondition;
814 SendClientThread mySendThread;
815 mySendThread.create();
789 SendClientThread mySendThread;
790 mySendThread.create();
816
791
817 asSocket.enableNonBlockingMode( sal_False );
818 ::osl::StreamSocket ssConnectionSocket;
819 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
820 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
792 asSocket.enableNonBlockingMode( sal_False );
793 ::osl::StreamSocket ssConnectionSocket;
794 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
795 ASSERT_TRUE(eResult == osl_Socket_Ok) << "shutdown_002: acceptConnection fail";
821
796
822 /* set socket option SO_LINGER 0, so close immediately */
823 linger aLingerSet;
824 sal_Int32 nBufferLen = sizeof( struct linger );
825 aLingerSet.l_onoff = 0;
826 aLingerSet.l_linger = 0;
797 /* set socket option SO_LINGER 0, so close immediately */
798 linger aLingerSet;
799 sal_Int32 nBufferLen = sizeof( struct linger );
800 aLingerSet.l_onoff = 0;
801 aLingerSet.l_linger = 0;
827
802
828 ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
829 thread_sleep( 1 );
830 //sal_uInt32 nRecv1 = 0;
831 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
803 ssConnectionSocket.setOption( osl_Socket_OptionLinger, &aLingerSet, nBufferLen );
804 thread_sleep( 1 );
805 //sal_uInt32 nRecv1 = 0;
806 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
832
807
833 //shutdown read after client the first send complete
834 ssConnectionSocket.shutdown( osl_Socket_DirRead );
808 //shutdown read after client the first send complete
809 ssConnectionSocket.shutdown( osl_Socket_DirRead );
835
810
836 sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 );
837 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 );
838 t_print("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 );
839 mySendThread.join();
811 sal_Int32 nRead2 = ssConnectionSocket.read( pReadBuffer + nRead1, 12 );
812 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 + nRead2, 12 );
813 printf("after read 2, nRead1 is %d, nRead2 is %d, nRead3 is %d \n", nRead1, nRead2, nRead3 );
814 mySendThread.join();
840
815
841 ssConnectionSocket.close();
842 asSocket.close();
816 ssConnectionSocket.close();
817 asSocket.close();
843
818
844 /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0,
845 http://dbforums.com/arch/186/2002/12/586417
846 While on Solaris, after shutdown(DirRead), all read will return 0
847 */
819 /* on Linux, if send is before shutdown(DirRead), can read, nRecv2 still > 0,
820 http://dbforums.com/arch/186/2002/12/586417
821 While on Solaris, after shutdown(DirRead), all read will return 0
822 */
848#ifdef LINUX
823#ifdef LINUX
849 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
850 nRead1 > 0 && nRead3 == 0 );
824 ASSERT_TRUE(nRead1 > 0 && nRead3 == 0) << "test for shutdown read direction: the socket can not read(recv).";
851#else
825#else
852 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not read(recv).",
853 nRead1 > 0 && nRead2 == 0 && nRead3 == 0 );
826 ASSERT_TRUE(nRead1 > 0 && nRead2 == 0 && nRead3 == 0) << "test for shutdown read direction: the socket can not read(recv).";
854#endif
855
827#endif
828
856 }
829 }
857
830
858 void shutdown_003()
859 {
860 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
861 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9);
862 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
863 CPPUNIT_ASSERT_MESSAGE("shutdown_002: bind fail", asSocket.bind( saLocalSocketAddr ) == sal_True);
864 CPPUNIT_ASSERT_MESSAGE("shutdown_002: listen fail", asSocket.listen( 1 ) == sal_True );
865 sal_Char pReadBuffer[40];
866 osl::Condition aCondition;
867 SendClientThread mySendThread;
868 mySendThread.create();
831 TEST_F(shutdown, shutdown_003)
832 {
833 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
834 ::osl::SocketAddr saLocalSocketAddr( rtl::OUString::createFromAscii("127.0.0.1"), IP_PORT_MYPORT9);
835 asSocket.setOption( osl_Socket_OptionReuseAddr, 1 );
836 ASSERT_TRUE(asSocket.bind( saLocalSocketAddr ) == sal_True) << "shutdown_002: bind fail";
837 ASSERT_TRUE(asSocket.listen( 1 ) == sal_True) << "shutdown_002: listen fail";
838 sal_Char pReadBuffer[40];
839 osl::Condition aCondition;
840 SendClientThread mySendThread;
841 mySendThread.create();
869
842
870 asSocket.enableNonBlockingMode( sal_False );
871 ::osl::StreamSocket ssConnectionSocket;
872 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
873 CPPUNIT_ASSERT_MESSAGE("shutdown_002: acceptConnection fail", eResult == osl_Socket_Ok );
843 asSocket.enableNonBlockingMode( sal_False );
844 ::osl::StreamSocket ssConnectionSocket;
845 oslSocketResult eResult = asSocket.acceptConnection( ssConnectionSocket );
846 ASSERT_TRUE(eResult == osl_Socket_Ok) << "shutdown_002: acceptConnection fail";
874
847
875 thread_sleep( 1 );
876 //shutdown write after client the first send complete
877 ssConnectionSocket.shutdown( osl_Socket_DirWrite );
848 thread_sleep( 1 );
849 //shutdown write after client the first send complete
850 ssConnectionSocket.shutdown( osl_Socket_DirWrite );
878
851
879 // recv should not shutdown
880 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
852 // recv should not shutdown
853 sal_Int32 nRead1 = ssConnectionSocket.read( pReadBuffer, 11 );
881
854
882 sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 );
883 // still can read
884 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 );
885 t_print("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 );
886 mySendThread.join();
887 ssConnectionSocket.close();
888 asSocket.close();
855 sal_Int32 nWrite = ssConnectionSocket.write( pReadBuffer, 11 );
856 // still can read
857 sal_Int32 nRead3 = ssConnectionSocket.read( pReadBuffer + nRead1 , 12 );
858 printf("after read 2, nRead1 is %d, nWrite is %d, nRead3 is %d\n", nRead1, nWrite, nRead3 );
859 mySendThread.join();
860 ssConnectionSocket.close();
861 asSocket.close();
889
862
890 CPPUNIT_ASSERT_MESSAGE( "test for shutdown read direction: the socket can not send(write).",
891 nRead1 > 0 && nWrite == 0 && nRead3 > 0);
863 ASSERT_TRUE(nRead1 > 0 && nWrite == 0 && nRead3 > 0) << "test for shutdown read direction: the socket can not send(write).";
892
864
893 }
865 }
894
866
895 CPPUNIT_TEST_SUITE( shutdown );
896 CPPUNIT_TEST( shutdown_001 );
897 CPPUNIT_TEST( shutdown_002 );
898 CPPUNIT_TEST( shutdown_003 );
899 CPPUNIT_TEST_SUITE_END();
900 }; // class shutdown
901
902 class isExceptionPending: public CppUnit::TestFixture
867 class isExceptionPending: public ::testing::Test
903 {
904 public:
868 {
869 public:
905 void isExPending_001()
906 {
907 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
908 TimeValue *pTimeout;
909 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
910 pTimeout->Seconds = 3;
911 pTimeout->Nanosec = 0;
912 sal_Bool bOk = asSocket.isExceptionPending( pTimeout );
913 free( pTimeout );
870 }; // class isExceptionPending
914
871
915 CPPUNIT_ASSERT_MESSAGE( "test for isExceptionPending.",
916 bOk == sal_False );
917 }
872 /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/
873 TEST_F(isExceptionPending, isExPending_001)
874 {
875 ::osl::AcceptorSocket asSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
876 TimeValue *pTimeout;
877 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
878 pTimeout->Seconds = 3;
879 pTimeout->Nanosec = 0;
880 sal_Bool bOk = asSocket.isExceptionPending( pTimeout );
881 free( pTimeout );
918
882
919 /**tester's comments: lack of a case that return sal_True, do not know when it will return sal_True*/
883 ASSERT_TRUE(bOk == sal_False) << "test for isExceptionPending.";
884 }
920
885
921
922 CPPUNIT_TEST_SUITE( isExceptionPending );
923 CPPUNIT_TEST( isExPending_001 );
924 CPPUNIT_TEST_SUITE_END();
925 }; // class isExceptionPending
926
927// -----------------------------------------------------------------------------
928/** Server Socket Thread, write a file which is large
929 */
930// LLA: class WriteSocketThread : public Thread
931// LLA: {
932// LLA: ValueCheckProvider m_aValues;
933// LLA:
934// LLA: protected:

--- 9 unchanged lines hidden (view full) ---

944// LLA: asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
945// LLA:
946// LLA: /// if the thread should terminate, schedule return false
947// LLA: while ( schedule( ) == sal_True )
948// LLA: {
949// LLA: sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
950// LLA: if ( sal_True != bOK1 )
951// LLA: {
886// -----------------------------------------------------------------------------
887/** Server Socket Thread, write a file which is large
888 */
889// LLA: class WriteSocketThread : public Thread
890// LLA: {
891// LLA: ValueCheckProvider m_aValues;
892// LLA:
893// LLA: protected:

--- 9 unchanged lines hidden (view full) ---

903// LLA: asAcceptorSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
904// LLA:
905// LLA: /// if the thread should terminate, schedule return false
906// LLA: while ( schedule( ) == sal_True )
907// LLA: {
908// LLA: sal_Bool bOK1 = asAcceptorSocket.bind( saLocalSocketAddr );
909// LLA: if ( sal_True != bOK1 )
910// LLA: {
952// LLA: t_print("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
911// LLA: printf("# WriteSocketThread: AcceptorSocket bind address failed. \n" ) ;
953// LLA: break;
954// LLA: }
955// LLA: sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
956// LLA: if ( sal_True != bOK2 )
957// LLA: {
912// LLA: break;
913// LLA: }
914// LLA: sal_Bool bOK2 = asAcceptorSocket.listen( 1 );
915// LLA: if ( sal_True != bOK2 )
916// LLA: {
958// LLA: t_print("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
917// LLA: printf("# WriteSocketThread: AcceptorSocket listen address failed. \n" ) ;
959// LLA: break;
960// LLA: }
961// LLA: // blocking mode, if read/recv failed, block until success
962// LLA: asAcceptorSocket.enableNonBlockingMode( sal_False);
963// LLA:
964// LLA: oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
965// LLA: if (eResult != osl_Socket_Ok )
966// LLA: {
918// LLA: break;
919// LLA: }
920// LLA: // blocking mode, if read/recv failed, block until success
921// LLA: asAcceptorSocket.enableNonBlockingMode( sal_False);
922// LLA:
923// LLA: oslSocketResult eResult = asAcceptorSocket.acceptConnection( ssStreamConnection );
924// LLA: if (eResult != osl_Socket_Ok )
925// LLA: {
967// LLA: t_print("WriteSocketThread: acceptConnection failed! \n");
926// LLA: printf("WriteSocketThread: acceptConnection failed! \n");
968// LLA: break;
969// LLA: }
970// LLA:
971// LLA:
972// LLA: sal_Int32 nReadNumber1 = ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
973// LLA: break;
974// LLA: }
975// LLA: ssStreamConnection.close();

--- 35 unchanged lines hidden (view full) ---

1011
1012 // 10.16.66.252
1013 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(m_sAddr.getStr()), IP_PORT_TEST );
1014 ::osl::ConnectorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1015
1016 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1017
1018 m_aCondition.wait();
927// LLA: break;
928// LLA: }
929// LLA:
930// LLA:
931// LLA: sal_Int32 nReadNumber1 = ssStreamConnection.write( m_aValues.getBuffer(), m_aValues.getBufferSize() );
932// LLA: break;
933// LLA: }
934// LLA: ssStreamConnection.close();

--- 35 unchanged lines hidden (view full) ---

970
971 // 10.16.66.252
972 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(m_sAddr.getStr()), IP_PORT_TEST );
973 ::osl::ConnectorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
974
975 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
976
977 m_aCondition.wait();
1019 t_print("wait done\n");
978 printf("wait done\n");
1020
1021 TimeValue *pTimeout;
1022 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
1023 pTimeout->Seconds = 20;
1024 pTimeout->Nanosec = 0;
1025
1026
1027 // blocking mode, if read/recv failed, block until success
979
980 TimeValue *pTimeout;
981 pTimeout = ( TimeValue* )malloc( sizeof( TimeValue ) );
982 pTimeout->Seconds = 20;
983 pTimeout->Nanosec = 0;
984
985
986 // blocking mode, if read/recv failed, block until success
1028 t_print("enableNonBlockingMode(false)\n");
987 printf("enableNonBlockingMode(false)\n");
1029 aSocket.enableNonBlockingMode( sal_False );
1030
1031
988 aSocket.enableNonBlockingMode( sal_False );
989
990
1032 t_print("connect()\n");
991 printf("connect()\n");
1033 oslSocketResult eResult = aSocket.connect( aSocketAddr, pTimeout );
1034 if ( osl_Socket_Ok == eResult)
1035 {
1036 if (m_pBuffer)
1037 {
992 oslSocketResult eResult = aSocket.connect( aSocketAddr, pTimeout );
993 if ( osl_Socket_Ok == eResult)
994 {
995 if (m_pBuffer)
996 {
1038 t_print("read()\n");
997 printf("read()\n");
1039 m_nReadCount = aSocket.read( m_pBuffer, m_nBufferSize );
998 m_nReadCount = aSocket.read( m_pBuffer, m_nBufferSize );
1040 t_print("%d bytes received.\n", m_nReadCount);
999 printf("%d bytes received.\n", m_nReadCount);
1041 }
1042 }
1043 else
1044 {
1000 }
1001 }
1002 else
1003 {
1045 t_print("# ReadSocket2Thread: connect failed! \n");
1004 printf("# ReadSocket2Thread: connect failed! \n");
1046 printSocketResult(eResult);
1047 setFailed();
1048 }
1049
1050 //remove this line for deadlock on solaris( margritte.germany )
1051 aSocket.close();
1052 free( pTimeout );
1053 }
1054
1055 void SAL_CALL run( )
1056 {
1057 read();
1058 }
1059
1060 void SAL_CALL onTerminated( )
1061 {
1005 printSocketResult(eResult);
1006 setFailed();
1007 }
1008
1009 //remove this line for deadlock on solaris( margritte.germany )
1010 aSocket.close();
1011 free( pTimeout );
1012 }
1013
1014 void SAL_CALL run( )
1015 {
1016 read();
1017 }
1018
1019 void SAL_CALL onTerminated( )
1020 {
1062 //t_print("# normally terminate this thread %d!\n", m_id );
1021 //printf("# normally terminate this thread %d!\n", m_id );
1063 }
1064
1065 public:
1066 sal_Int32 getCount() {return m_nReadCount;}
1067 bool isOk() {return m_nReadCount == 0 ? false : true;}
1068 bool getFailed() {return m_bOk == false ? true : false;}
1069
1070 ReadSocket2Thread(osl::Condition &_aCondition)

--- 4 unchanged lines hidden (view full) ---

1075 m_aCondition.reset();
1076 m_pBuffer = (char*) malloc(1024);
1077 if (m_pBuffer)
1078 {
1079 m_nBufferSize = 1024;
1080 }
1081
1082 m_id = getIdentifier( );
1022 }
1023
1024 public:
1025 sal_Int32 getCount() {return m_nReadCount;}
1026 bool isOk() {return m_nReadCount == 0 ? false : true;}
1027 bool getFailed() {return m_bOk == false ? true : false;}
1028
1029 ReadSocket2Thread(osl::Condition &_aCondition)

--- 4 unchanged lines hidden (view full) ---

1034 m_aCondition.reset();
1035 m_pBuffer = (char*) malloc(1024);
1036 if (m_pBuffer)
1037 {
1038 m_nBufferSize = 1024;
1039 }
1040
1041 m_id = getIdentifier( );
1083 //t_print("# successfully creat this client thread %d!\n", m_id );
1042 //printf("# successfully creat this client thread %d!\n", m_id );
1084 }
1085
1086 void setAddr(rtl::OString const& _sAddr)
1087 {
1088 m_sAddr = _sAddr;
1089 }
1090
1091 ~ReadSocket2Thread( )
1092 {
1093 if ( isRunning( ) )
1043 }
1044
1045 void setAddr(rtl::OString const& _sAddr)
1046 {
1047 m_sAddr = _sAddr;
1048 }
1049
1050 ~ReadSocket2Thread( )
1051 {
1052 if ( isRunning( ) )
1094 t_print("# error: client thread not terminated.\n" );
1053 printf("# error: client thread not terminated.\n" );
1095 free(m_pBuffer);
1096 }
1097
1098 };
1099
1100 // -----------------------------------------------------------------------------
1101
1054 free(m_pBuffer);
1055 }
1056
1057 };
1058
1059 // -----------------------------------------------------------------------------
1060
1102 class justtest : public CppUnit::TestFixture
1061 class justtest : public ::testing::Test
1103 {
1062 {
1063 protected:
1104 void send_Acceptor(rtl::OString const& _sAddr, osl::Condition &)
1105 {
1106 ::osl::AcceptorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1107 ::osl::SocketAddr aSocketAddr;
1108
1109 if (! aSocketAddr.setPort(IP_PORT_TEST))
1110 {
1064 void send_Acceptor(rtl::OString const& _sAddr, osl::Condition &)
1065 {
1066 ::osl::AcceptorSocket aSocket; // ( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1067 ::osl::SocketAddr aSocketAddr;
1068
1069 if (! aSocketAddr.setPort(IP_PORT_TEST))
1070 {
1111 t_print("# can't set port\n" );
1071 printf("# can't set port\n" );
1112 }
1113
1114 if (! aSocketAddr.setHostname(rtl::OUString::createFromAscii(_sAddr.getStr())))
1115 {
1072 }
1073
1074 if (! aSocketAddr.setHostname(rtl::OUString::createFromAscii(_sAddr.getStr())))
1075 {
1116 t_print("# can't set hostname/ip\n" );
1076 printf("# can't set hostname/ip\n" );
1117 }
1118
1119 rtl::OUString aHostname = aSocketAddr.getHostname();
1120 aSocketAddr.getPort();
1121
1122
1123 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1124 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1125
1126 /// if the thread should terminate, schedule return false
1127 // while ( schedule( ) == sal_True )
1128 // {
1129 if (! aSocket.bind( aSocketAddr ))
1130 {
1077 }
1078
1079 rtl::OUString aHostname = aSocketAddr.getHostname();
1080 aSocketAddr.getPort();
1081
1082
1083 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1084 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True);
1085
1086 /// if the thread should terminate, schedule return false
1087 // while ( schedule( ) == sal_True )
1088 // {
1089 if (! aSocket.bind( aSocketAddr ))
1090 {
1131 t_print("# can't bind.\n" );
1091 printf("# can't bind.\n" );
1132 }
1133 if (! aSocket.listen( ))
1134 {
1092 }
1093 if (! aSocket.listen( ))
1094 {
1135 t_print("# can't listen. \n" );
1095 printf("# can't listen. \n" );
1136 }
1137
1138 // blocking mode, if read/recv failed, block until success
1139 aSocket.enableNonBlockingMode( sal_False);
1140 ::osl::StreamSocket ssStreamConnection;
1141
1142 oslSocketResult eResult = aSocket.acceptConnection( ssStreamConnection );
1143 if (eResult != osl_Socket_Ok )
1144 {
1096 }
1097
1098 // blocking mode, if read/recv failed, block until success
1099 aSocket.enableNonBlockingMode( sal_False);
1100 ::osl::StreamSocket ssStreamConnection;
1101
1102 oslSocketResult eResult = aSocket.acceptConnection( ssStreamConnection );
1103 if (eResult != osl_Socket_Ok )
1104 {
1145 t_print("WriteSocketThread: acceptConnection failed! \n");
1105 printf("WriteSocketThread: acceptConnection failed! \n");
1146 // break;
1147 }
1148 char const * pBuffer = "Test String\n";
1149 sal_Int32 nBufferSize = strlen(pBuffer);
1150 ssStreamConnection.write( pBuffer, nBufferSize );
1151 // break;
1152 // }
1153

--- 5 unchanged lines hidden (view full) ---

1159
1160 void send_Connector(rtl::OString const& _sAddr, osl::Condition &/*_aCondition*/ )
1161 {
1162 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1163 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
1164
1165 if (! aSocketAddr.is())
1166 {
1106 // break;
1107 }
1108 char const * pBuffer = "Test String\n";
1109 sal_Int32 nBufferSize = strlen(pBuffer);
1110 ssStreamConnection.write( pBuffer, nBufferSize );
1111 // break;
1112 // }
1113

--- 5 unchanged lines hidden (view full) ---

1119
1120 void send_Connector(rtl::OString const& _sAddr, osl::Condition &/*_aCondition*/ )
1121 {
1122 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1123 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
1124
1125 if (! aSocketAddr.is())
1126 {
1167 t_print("is failed.\n");
1127 printf("is failed.\n");
1168 return;
1169 }
1170
1171 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1172 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1173
1174 oslSocketResult aResult = aSocket.connect( aSocketAddr );
1175 if ( aResult != osl_Socket_Ok )
1176 {
1128 return;
1129 }
1130
1131 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1132 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1133
1134 oslSocketResult aResult = aSocket.connect( aSocketAddr );
1135 if ( aResult != osl_Socket_Ok )
1136 {
1177 t_print("# send_Connector: connect failed. \n" );
1137 printf("# send_Connector: connect failed. \n" );
1178 }
1179 else
1180 {
1181 // blocking mode, if read/recv failed, block until success
1182// aSocket.enableNonBlockingMode( sal_False );
1183
1184// _aCondition.set();
1185

--- 6 unchanged lines hidden (view full) ---

1192 char *pBufferPeek = (char*) malloc(1024);
1193 sal_Int32 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
1194 free(pBufferPeek);
1195
1196 char *pBuffer2 = (char*) malloc(nReadNumber + 1);
1197 sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
1198 pBuffer2[nReadNumberReal] = '\0';
1199
1138 }
1139 else
1140 {
1141 // blocking mode, if read/recv failed, block until success
1142// aSocket.enableNonBlockingMode( sal_False );
1143
1144// _aCondition.set();
1145

--- 6 unchanged lines hidden (view full) ---

1152 char *pBufferPeek = (char*) malloc(1024);
1153 sal_Int32 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
1154 free(pBufferPeek);
1155
1156 char *pBuffer2 = (char*) malloc(nReadNumber + 1);
1157 sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
1158 pBuffer2[nReadNumberReal] = '\0';
1159
1200 t_print("received: %s\n", pBuffer2);
1160 printf("received: %s\n", pBuffer2);
1201
1202 // char * pBuffer3 = "quit\n";
1203 // nBufferSize = strlen(pBuffer3);
1204 // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1205
1206 rtl::OUString suError = ssStreamConnection.getErrorAsString();
1207 free(pBuffer2);
1208 // ssStreamConnection.close();

--- 4 unchanged lines hidden (view full) ---

1213 aSocket.close();
1214 }
1215
1216
1217 public:
1218// LLA: orig void send_recv()
1219// LLA: orig {
1220// LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True )
1161
1162 // char * pBuffer3 = "quit\n";
1163 // nBufferSize = strlen(pBuffer3);
1164 // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1165
1166 rtl::OUString suError = ssStreamConnection.getErrorAsString();
1167 free(pBuffer2);
1168 // ssStreamConnection.close();

--- 4 unchanged lines hidden (view full) ---

1173 aSocket.close();
1174 }
1175
1176
1177 public:
1178// LLA: orig void send_recv()
1179// LLA: orig {
1180// LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True )
1221// LLA: orig t_print("margritte is alive ! \n");
1181// LLA: orig printf("margritte is alive ! \n");
1222// LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("10.16.66.252")) == sal_False )
1223// LLA: orig {
1182// LLA: orig if ( ifAvailable(rtl::OUString::createFromAscii("10.16.66.252")) == sal_False )
1183// LLA: orig {
1224// LLA: orig t_print("ip 10.16.66.252 is not alive! \n");
1184// LLA: orig printf("ip 10.16.66.252 is not alive! \n");
1225// LLA: orig return;
1226// LLA: orig }
1227// LLA: orig ReadSocket2Thread myReadThread;
1228// LLA: orig myReadThread.create();
1229// LLA: orig
1230// LLA: orig thread_sleep( 2 );
1231// LLA: orig // send_Acceptor();
1232// LLA: orig send_Connector();
1233// LLA: orig
1234// LLA: orig myReadThread.join();
1235// LLA: orig
1236// LLA: orig // statistics
1237// LLA: orig sal_uInt32 nLength = myReadThread.getCount();
1238// LLA: orig bool bIsOk = myReadThread.isOk(); // check if the values are right.
1239// LLA: orig
1185// LLA: orig return;
1186// LLA: orig }
1187// LLA: orig ReadSocket2Thread myReadThread;
1188// LLA: orig myReadThread.create();
1189// LLA: orig
1190// LLA: orig thread_sleep( 2 );
1191// LLA: orig // send_Acceptor();
1192// LLA: orig send_Connector();
1193// LLA: orig
1194// LLA: orig myReadThread.join();
1195// LLA: orig
1196// LLA: orig // statistics
1197// LLA: orig sal_uInt32 nLength = myReadThread.getCount();
1198// LLA: orig bool bIsOk = myReadThread.isOk(); // check if the values are right.
1199// LLA: orig
1240// LLA: orig t_print("Length:=%d\n", nLength);
1241// LLA: orig t_print(" bIsOk:=%d\n", bIsOk);
1200// LLA: orig printf("Length:=%d\n", nLength);
1201// LLA: orig printf(" bIsOk:=%d\n", bIsOk);
1242// LLA: orig }
1243
1244 // -----------------------------------------------------------------------------
1245
1246 // LLA: send_Connector_2_margritte works, it send strings to echo server on margritte
1247 // but can not receive anything
1248
1249 void send_Connector_2_margritte(rtl::OString const& _sAddr)
1250 {
1251 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1252 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
1253
1254 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1255 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1256
1257 oslSocketResult aResult = aSocket.connect( aSocketAddr );
1258 if ( aResult != osl_Socket_Ok )
1259 {
1202// LLA: orig }
1203
1204 // -----------------------------------------------------------------------------
1205
1206 // LLA: send_Connector_2_margritte works, it send strings to echo server on margritte
1207 // but can not receive anything
1208
1209 void send_Connector_2_margritte(rtl::OString const& _sAddr)
1210 {
1211 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1212 ::osl::SocketAddr aSocketAddr( rtl::OUString::createFromAscii(_sAddr.getStr()), IP_PORT_TEST );
1213
1214 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1215 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1216
1217 oslSocketResult aResult = aSocket.connect( aSocketAddr );
1218 if ( aResult != osl_Socket_Ok )
1219 {
1260 t_print("# connect failed. \n" );
1220 printf("# connect failed. \n" );
1261 }
1262 else
1263 {
1264 // blocking mode, if read/recv failed, block until success
1265 aSocket.enableNonBlockingMode( sal_False );
1266
1267 ::osl::StreamSocket ssStreamConnection(aSocket);
1268

--- 14 unchanged lines hidden (view full) ---

1283 }
1284
1285 void send_recv_2_margritte()
1286 {
1287 rtl::OString sAddr;
1288 sAddr = "margritte.germany.sun.com";
1289 if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
1290 {
1221 }
1222 else
1223 {
1224 // blocking mode, if read/recv failed, block until success
1225 aSocket.enableNonBlockingMode( sal_False );
1226
1227 ::osl::StreamSocket ssStreamConnection(aSocket);
1228

--- 14 unchanged lines hidden (view full) ---

1243 }
1244
1245 void send_recv_2_margritte()
1246 {
1247 rtl::OString sAddr;
1248 sAddr = "margritte.germany.sun.com";
1249 if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
1250 {
1291 t_print("found %s!\n", sAddr.getStr());
1251 printf("found %s!\n", sAddr.getStr());
1292 }
1293 send_Connector_2_margritte(sAddr);
1294 }
1295
1296 // -----------------------------------------------------------------------------
1297
1252 }
1253 send_Connector_2_margritte(sAddr);
1254 }
1255
1256 // -----------------------------------------------------------------------------
1257
1298 void send_recv()
1299 {
1300 rtl::OString sAddr;
1301 // if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True )
1302 // {
1303 // t_print("margritte is alive ! \n");
1304 // sAddr = "margritte.germany";
1305 // }
1306
1258
1307 sAddr = "margritte.germany.sun.com";
1308 if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
1309 {
1310 t_print("found %s!\n", sAddr.getStr());
1311 }
1259
1260 void getPage(rtl::OString const& _sAddr);
1261 }; // class isExceptionPending
1262
1263 TEST_F(justtest, send_recv)
1264 {
1265 rtl::OString sAddr;
1266 // if ( ifAvailable(rtl::OUString::createFromAscii("margritte.germany")) == sal_True )
1267 // {
1268 // printf("margritte is alive ! \n");
1269 // sAddr = "margritte.germany";
1270 // }
1271
1272 sAddr = "margritte.germany.sun.com";
1273 if ( ifAvailable(rtl::OUString::createFromAscii(sAddr.getStr())) == sal_True )
1274 {
1275 printf("found %s!\n", sAddr.getStr());
1276 }
1312// else
1313// {
1314// if ( ifAvailable(rtl::OUString::createFromAscii("192.168.7.2")) == sal_True )
1315// {
1316// sAddr = "192.168.7.2";
1277// else
1278// {
1279// if ( ifAvailable(rtl::OUString::createFromAscii("192.168.7.2")) == sal_True )
1280// {
1281// sAddr = "192.168.7.2";
1317// t_print("moon found ! \n");
1282// printf("moon found ! \n");
1318// }
1319// else
1320// {
1321// if ( ifAvailable(rtl::OUString::createFromAscii("moon.linux.bogus")) == sal_True )
1322// {
1323// sAddr = "moon.linux.bogus";
1283// }
1284// else
1285// {
1286// if ( ifAvailable(rtl::OUString::createFromAscii("moon.linux.bogus")) == sal_True )
1287// {
1288// sAddr = "moon.linux.bogus";
1324// t_print("moon found ! \n");
1289// printf("moon found ! \n");
1325// }
1326// else
1327// {
1328// if ( ifAvailable(rtl::OUString::createFromAscii("moon")) == sal_True )
1329// {
1330// sAddr = "moon";
1290// }
1291// else
1292// {
1293// if ( ifAvailable(rtl::OUString::createFromAscii("moon")) == sal_True )
1294// {
1295// sAddr = "moon";
1331// t_print("moon found ! \n");
1296// printf("moon found ! \n");
1332// }
1333// }
1334// }
1335// }
1336
1297// }
1298// }
1299// }
1300// }
1301
1337 // if ( ifAvailable(rtl::OUString::createFromAscii("10.16.64.196")) == sal_False )
1338 // {
1339 // t_print("ip 10.16.64.196 is not alive! \n");
1340 // return;
1341 // }
1302 // if ( ifAvailable(rtl::OUString::createFromAscii("10.16.64.196")) == sal_False )
1303 // {
1304 // printf("ip 10.16.64.196 is not alive! \n");
1305 // return;
1306 // }
1342
1307
1343 osl::Condition aCondition;
1344 ReadSocket2Thread myReadThread(aCondition);
1345 myReadThread.setAddr(sAddr);
1308 osl::Condition aCondition;
1309 ReadSocket2Thread myReadThread(aCondition);
1310 myReadThread.setAddr(sAddr);
1346// myReadThread.create();
1347
1311// myReadThread.create();
1312
1313 thread_sleep( 2 );
1314 if (! myReadThread.getFailed())
1315 {
1316 // send_Acceptor(sAddr, aCondition);
1317 send_Connector(sAddr, aCondition);
1318
1348 thread_sleep( 2 );
1319 thread_sleep( 2 );
1349 if (! myReadThread.getFailed())
1320 if (myReadThread.isRunning())
1350 {
1321 {
1351 // send_Acceptor(sAddr, aCondition);
1352 send_Connector(sAddr, aCondition);
1353
1354 thread_sleep( 2 );
1355 if (myReadThread.isRunning())
1356 {
1357 myReadThread.join();
1358 }
1359 // termAndJoinThread(&myReadThread);
1360
1361 // statistics
1362 sal_uInt32 nLength = myReadThread.getCount();
1363 bool bIsOk = myReadThread.isOk(); // check if the values are right.
1364
1365 t_print("Length:=%d\n", nLength);
1366 t_print(" bIsOk:=%d\n", bIsOk);
1322 myReadThread.join();
1367 }
1323 }
1368 else
1369 {
1370 t_print("ERROR: No echo Server on %s found.\n", sAddr.getStr());
1371 }
1372 }
1324 // termAndJoinThread(&myReadThread);
1373
1325
1326 // statistics
1327 sal_uInt32 nLength = myReadThread.getCount();
1328 bool bIsOk = myReadThread.isOk(); // check if the values are right.
1374
1329
1375 void getPage(rtl::OString const& _sAddr);
1376 void test_getPage()
1330 printf("Length:=%d\n", nLength);
1331 printf(" bIsOk:=%d\n", bIsOk);
1332 }
1333 else
1377 {
1334 {
1378 // rtl::OString sPage("lla-1.germany.sun.com");
1379 // getPage(sPage);
1380
1381 rtl::OString sPage("lla-1");
1382 getPage(sPage);
1335 printf("ERROR: No echo Server on %s found.\n", sAddr.getStr());
1383 }
1336 }
1337 }
1384
1338
1385 CPPUNIT_TEST_SUITE( justtest );
1386 CPPUNIT_TEST( send_recv );
1387 CPPUNIT_TEST( test_getPage );
1388 CPPUNIT_TEST_SUITE_END();
1389 }; // class isExceptionPending
1339 TEST_F(justtest, test_getPage)
1340 {
1341 // rtl::OString sPage("lla-1.germany.sun.com");
1342 // getPage(sPage);
1390
1343
1344 rtl::OString sPage("lla-1");
1345 getPage(sPage);
1346 }
1391
1347
1348
1392 void justtest::getPage(rtl::OString const& _sAddr)
1393 {
1394 rtl::OUString suAddr = rtl::OUString::createFromAscii(_sAddr.getStr());
1395 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1396 ::osl::SocketAddr aSocketAddr( suAddr, 80 );
1397
1398 {
1399 // some checks
1400 aSocketAddr.getPort();
1401 oslSocketResult aResult;
1402 rtl::ByteSequence aSeq = aSocketAddr.getAddr(&aResult);
1403 if (aResult != osl_Socket_Ok)
1404 {
1349 void justtest::getPage(rtl::OString const& _sAddr)
1350 {
1351 rtl::OUString suAddr = rtl::OUString::createFromAscii(_sAddr.getStr());
1352 ::osl::ConnectorSocket aSocket( osl_Socket_FamilyInet, osl_Socket_ProtocolIp, osl_Socket_TypeStream );
1353 ::osl::SocketAddr aSocketAddr( suAddr, 80 );
1354
1355 {
1356 // some checks
1357 aSocketAddr.getPort();
1358 oslSocketResult aResult;
1359 rtl::ByteSequence aSeq = aSocketAddr.getAddr(&aResult);
1360 if (aResult != osl_Socket_Ok)
1361 {
1405 t_print("problem with getAddr: ");
1362 printf("problem with getAddr: ");
1406 printSocketResult(aResult);
1407 }
1408
1409 rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
1410 if (aResult != osl_Socket_Ok)
1411 {
1363 printSocketResult(aResult);
1364 }
1365
1366 rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
1367 if (aResult != osl_Socket_Ok)
1368 {
1412 t_print("problem with hostname: ");
1369 printf("problem with hostname: ");
1413 printSocketResult(aResult);
1414 }
1415 }
1416
1417 oslSocketResult aResult;
1418
1419 // SocketAddr::resolveHostname(suAddr, aSocketAddr);
1420 // if (! aSocketAddr.is())
1421 // {
1370 printSocketResult(aResult);
1371 }
1372 }
1373
1374 oslSocketResult aResult;
1375
1376 // SocketAddr::resolveHostname(suAddr, aSocketAddr);
1377 // if (! aSocketAddr.is())
1378 // {
1422 // t_print("Can't resolve Hostname.\n");
1379 // printf("Can't resolve Hostname.\n");
1423 // return;
1424 // }
1425 // rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
1426 // if (aResult != osl_Socket_Ok)
1427 // {
1380 // return;
1381 // }
1382 // rtl::OUString sStr = aSocketAddr.getHostname(&aResult);
1383 // if (aResult != osl_Socket_Ok)
1384 // {
1428 // t_print("problem with hostname: ");
1385 // printf("problem with hostname: ");
1429 // printSocketResult(aResult);
1430 //
1431 // }
1432
1433 if (! aSocketAddr.is())
1434 {
1386 // printSocketResult(aResult);
1387 //
1388 // }
1389
1390 if (! aSocketAddr.is())
1391 {
1435 t_print("SocketAddr::is() failed.\n");
1392 printf("SocketAddr::is() failed.\n");
1436 return;
1437 }
1438
1439 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1440 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1441
1442 aResult = aSocket.connect( aSocketAddr );
1443 if ( aResult != osl_Socket_Ok )
1444 {
1393 return;
1394 }
1395
1396 //if has not set this option, socket addr can not be binded in some time(maybe 2 minutes) by another socket
1397 aSocket.setOption( osl_Socket_OptionReuseAddr, 1 ); //sal_True;
1398
1399 aResult = aSocket.connect( aSocketAddr );
1400 if ( aResult != osl_Socket_Ok )
1401 {
1445 t_print("# send_Connector: connect failed. \n" );
1402 printf("# send_Connector: connect failed. \n" );
1446 }
1447 else
1448 {
1449 // blocking mode, if read/recv failed, block until success
1450// aSocket.enableNonBlockingMode( sal_False );
1451
1452// _aCondition.set();
1453

--- 9 unchanged lines hidden (view full) ---

1463 while ( nReadNumber != 0)
1464 {
1465 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
1466 if (nReadNumber > 0)
1467 {
1468 char *pBuffer2 = (char*) malloc(nReadNumber + 1);
1469 sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
1470 pBuffer2[nReadNumberReal] = '\0';
1403 }
1404 else
1405 {
1406 // blocking mode, if read/recv failed, block until success
1407// aSocket.enableNonBlockingMode( sal_False );
1408
1409// _aCondition.set();
1410

--- 9 unchanged lines hidden (view full) ---

1420 while ( nReadNumber != 0)
1421 {
1422 nReadNumber = ssStreamConnection.recv( pBufferPeek, 1024, osl_Socket_MsgPeek);
1423 if (nReadNumber > 0)
1424 {
1425 char *pBuffer2 = (char*) malloc(nReadNumber + 1);
1426 sal_Int32 nReadNumberReal = ssStreamConnection.read( pBuffer2, nReadNumber );
1427 pBuffer2[nReadNumberReal] = '\0';
1471 t_print("%s", pBuffer2);
1428 printf("%s", pBuffer2);
1472 free(pBuffer2);
1473 }
1474 }
1475 free(pBufferPeek);
1476
1477 // char * pBuffer3 = "quit\n";
1478 // nBufferSize = strlen(pBuffer3);
1479 // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1480
1481 rtl::OUString suError = ssStreamConnection.getErrorAsString();
1482 }
1483 aSocket.shutdown(osl_Socket_DirReadWrite);
1484 aSocket.close();
1485 }
1486
1429 free(pBuffer2);
1430 }
1431 }
1432 free(pBufferPeek);
1433
1434 // char * pBuffer3 = "quit\n";
1435 // nBufferSize = strlen(pBuffer3);
1436 // nWriteNumber = ssStreamConnection.write( pBuffer3, nBufferSize );
1437
1438 rtl::OUString suError = ssStreamConnection.getErrorAsString();
1439 }
1440 aSocket.shutdown(osl_Socket_DirReadWrite);
1441 aSocket.close();
1442 }
1443
1487// -----------------------------------------------------------------------------
1488
1444
1489 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::ctors, "osl_StreamSocket");
1490 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::send_recv, "osl_StreamSocket");
1491 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::shutdown, "osl_StreamSocket");
1492 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::isExceptionPending, "osl_StreamSocket");
1493
1494 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_StreamSocket::justtest, "osl_StreamSocket");
1495
1496} // namespace osl_StreamSocket
1497
1445} // namespace osl_StreamSocket
1446
1498// -----------------------------------------------------------------------------
1447static oslSignalAction SAL_CALL signalHandler(void* pData, oslSignalInfo* pInfo)
1448{
1449 return osl_Signal_ActCallNextHdl;
1450}
1499
1451
1500// this macro creates an empty function, which will called by the RegisterAllFunctions()
1501// to let the user the possibility to also register some functions by hand.
1502NOADDITIONAL;
1452int main(int argc, char **argv)
1453{
1454 osl_addSignalHandler(signalHandler, NULL);
1455 ::testing::InitGoogleTest(&argc, argv);
1456 return RUN_ALL_TESTS();
1457}