00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef WINDOWS
00025
00026
00027 #include "stdafx.h"
00028
00029 #else
00030
00031 #include <netdb.h>
00032 #include <string.h>
00033 #include <errno.h>
00034 #include <unistd.h>
00035 #include "RioUnix.h"
00036 #include <netinet/in.h>
00037 #endif
00038
00039 #include <stdio.h>
00040 #include <unistd.h>
00041 #include <fcntl.h>
00042 #include <sys/time.h>
00043 #include <time.h>
00044
00045 #include "RioTCP.h"
00046 #include "RioProxy.h"
00047 #include "RioTCPTypes.h"
00048 #include "RioError.h"
00049
00050 #ifndef WINDOWS
00051
00052 static void closesocket( SOCKET sock )
00053 {
00054 shutdown( sock, 2 );
00055 close( sock );
00056 }
00057 #endif
00058
00059
00060 CRioTCP::CRioTCP()
00061 {
00062 #ifdef RIO_DEBUG1
00063 RioErr << "### [RioTCP - Constructor] Start" << endl;
00064 #endif
00065
00066 m_Socket = INVALID_SOCKET;
00067 m_connected = false;
00068 m_Mode = ModeInvalid;
00069 m_Mutex = NULL;
00070
00071 #ifdef RIO_DEBUG1
00072 RioErr << "### [RioTCP - Constructor] Finish" << endl;
00073 #endif
00074 }
00075
00076 CRioTCP::~CRioTCP()
00077 {
00078 #ifdef RIO_DEBUG1
00079 RioErr << "### [RioTCP - Destructor] Single" << endl;
00080 #endif
00081 }
00082
00083
00084 RioResult CRioTCP::Connect(const char* ServerName )
00085 {
00086 #ifdef RIO_DEBUG1
00087 RioErr << "### [RioTCP - Connect1] Start" << endl;
00088 #endif
00089
00090
00091 if(m_connected)
00092 {
00093 #ifdef RIO_DEBUG1
00094 RioErr << "### [RioTCP - Connect1] Finish1" << endl;
00095 #endif
00096
00097 return ERROR_RIOTCP + ERROR_CONNECTED;
00098 }
00099
00100 m_Mutex = new CMutex;
00101 if( m_Mutex == 0 )
00102 {
00103 #ifdef RIO_DEBUG1
00104 RioErr << "### [RioTCP - Connect1] Finish2" << endl;
00105 #endif
00106
00107 return ERROR_RIOTCP + ERROR_MEMORY;
00108 }
00109 if( !(m_Mutex->IsOpen()) )
00110 {
00111 #ifdef RIO_DEBUG1
00112 RioErr << "### [RioTCP - Connect1] Finish3" << endl;
00113 #endif
00114
00115 return ERROR_RIOTCP + ERROR_CREATE_MUTEX;
00116 }
00117
00118 SOCKET sock;
00119
00120 #ifdef WINDOWS
00121
00122
00123 WSADATA winSockInfo;
00124 if( WSAStartup( MAKEWORD( 2,0 ),&winSockInfo )!= 0 )
00125 {
00126 #ifdef RIO_DEBUG1
00127 RioErr << "### [RioTCP - Connect1] Finish4" << endl;
00128 #endif
00129
00130 return ERROR_RIOTCP + ERROR_SOCKET_LIBRARY;
00131 }
00132 #endif
00133
00134 sock = socket( AF_INET, SOCK_STREAM, 0 );
00135 if( sock == INVALID_SOCKET )
00136 {
00137 delete m_Mutex;
00138 m_Mutex = 0;
00139
00140 #ifdef RIO_DEBUG1
00141 RioErr << "### [RioTCP - Connect1] Finish5" << endl;
00142 #endif
00143
00144 return ERROR_RIOTCP + ERROR_SOCKET_CREATE;
00145 }
00146
00147
00148
00149
00150 if( fcntl( sock, F_SETFD, FD_CLOEXEC ) < 0 )
00151 {
00152 close( sock );
00153 delete m_Mutex;
00154 m_Mutex = 0;
00155 RioErr << " RioTCP - Error setting socket as exclusive.. " << endl;
00156
00157 #ifdef RIO_DEBUG1
00158 RioErr << "### [RioTCP - Connect1] Finish6" << endl;
00159 #endif
00160
00161 return ERROR_RIOTCP + ERROR_SOCKET_OPTIONS;
00162 }
00163
00164
00165
00166
00167
00168 struct timeval timeout;
00169 timeout.tv_sec = TCPTIMEOUTSECONDS;
00170 timeout.tv_usec = 0;
00171
00172
00173 if( setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
00174 sizeof( timeout ) ) < 0 )
00175 {
00176 closesocket( sock );
00177 delete m_Mutex;
00178 m_Mutex = 0;
00179 Rioperror( "RioTCP setsockopt" );
00180
00181 #ifdef RIO_DEBUG1
00182 RioErr << "### [RioTCP - Connect1] Finish7" << endl;
00183 #endif
00184
00185 return ERROR_RIOTCP + ERROR_SOCKET_OPTIONS;
00186 }
00187
00188
00189 if( setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, &timeout,
00190 sizeof( timeout ) ) < 0 )
00191 {
00192 closesocket( sock );
00193 delete m_Mutex;
00194 m_Mutex = 0;
00195 Rioperror( "RioTCP setsockopt" );
00196
00197 #ifdef RIO_DEBUG1
00198 RioErr << "### [RioTCP - Connect1] Finish8" << endl;
00199 #endif
00200
00201 return ERROR_RIOTCP + ERROR_SOCKET_OPTIONS;
00202 }
00203
00204
00205 struct hostent* hostaddress;
00206 hostaddress = gethostbyname(ServerName);
00207
00208 if( hostaddress == NULL )
00209 {
00210 delete m_Mutex;
00211 m_Mutex = 0;
00212 closesocket( sock );
00213
00214 #ifdef RIO_DEBUG1
00215 RioErr << "### [RioTCP - Connect1] Finish9" << endl;
00216 #endif
00217
00218 return ERROR_RIOTCP + ERROR_HOST_NOTFOUND;
00219 }
00220
00221
00222 SOCKADDR_IN RemoteAddress;
00223 memset( &RemoteAddress,0x00,sizeof( RemoteAddress ) );
00224 RemoteAddress.sin_family = AF_INET;
00225 memcpy ((char*)&RemoteAddress.sin_addr,
00226 hostaddress->h_addr_list[0],
00227 hostaddress->h_length);
00228 RemoteAddress.sin_port = htons( RioTCPport );
00229
00230 if( connect( sock,(SOCKADDR*) &RemoteAddress,sizeof( RemoteAddress ) )
00231 == SOCKET_ERROR )
00232 {
00233 closesocket( sock );
00234 delete m_Mutex;
00235 m_Mutex = 0;
00236 Rioperror( "RioTCP Connect 1" );
00237
00238 #ifdef RIO_DEBUG1
00239 RioErr << "### [RioTCP - Connect1] Finish10" << endl;
00240 #endif
00241
00242 return ERROR_RIOTCP + ERROR_SOCKET_CONNECT;
00243 }
00244
00245 m_Socket = sock;
00246
00247
00248
00249 RioTCPHeader Header;
00250 Header.Tag = RioTCPTag;
00251 Header.Version = RioTCPVersion;
00252 Header.Command = RioTCPCommandConnect;
00253 Header.Status = S_OK;
00254 Header.Class = 0;
00255 Header.Method = 0;
00256 Header.DataSize = 0;
00257
00258 if(!SendMessage(&Header,NULL))
00259 {
00260 m_Socket = INVALID_SOCKET;
00261 closesocket (sock);
00262 delete m_Mutex;
00263 m_Mutex = 0;
00264
00265 #ifdef RIO_DEBUG1
00266 RioErr << "### [RioTCP - Connect1] Finish11" << endl;
00267 #endif
00268
00269 return ERROR_RIOTCP + ERROR_SOCKET_SEND;
00270 }
00271
00272 if(!ReceiveHeader(&Header))
00273 {
00274 m_Socket = INVALID_SOCKET;
00275 closesocket (sock);
00276 delete m_Mutex;
00277 m_Mutex = 0;
00278
00279 #ifdef RIO_DEBUG1
00280 RioErr << "### [RioTCP - Connect1] Finish12" << endl;
00281 #endif
00282
00283 return ERROR_RIOTCP + ERROR_SOCKET_RECEIVE;
00284 }
00285
00286 if( Header.Command != RioTCPCommandAccept )
00287 {
00288 m_Socket = INVALID_SOCKET;
00289 delete m_Mutex;
00290 m_Mutex = 0;
00291 closesocket(sock);
00292 if( Header.Command == RioTCPCommandReject )
00293 {
00294 if(Header.Status != S_OK)
00295 {
00296 #ifdef RIO_DEBUG1
00297 RioErr << "### [RioTCP - Connect1] Finish13" << endl;
00298 #endif
00299
00300 return Header.Status;
00301 }
00302 }
00303
00304 #ifdef RIO_DEBUG1
00305 RioErr << "### [RioTCP - Connect1] Finish14" << endl;
00306 #endif
00307
00308 return ERROR_RIOTCP + ERROR_SOCKET_REFUSED_CONNECTION;
00309 }
00310
00311 m_connected = true;
00312 m_Mode = ModeClient;
00313
00314 #ifdef RIO_DEBUG1
00315 RioErr << "### [RioTCP - Connect1] Finish15" << endl;
00316 #endif
00317
00318 return S_OK;
00319 }
00320
00321
00322 RioResult CRioTCP::Connect(const char* ServerName,
00323 const unsigned short TCPport )
00324 {
00325 #ifdef RIO_DEBUG1
00326 RioErr << "### [RioTCP - Connect2] Start" << endl;
00327 #endif
00328
00329
00330 if( m_connected )
00331 {
00332 #ifdef RIO_DEBUG1
00333 RioErr << "### [RioTCP - Connect2] Finish1" << endl;
00334 #endif
00335
00336 return ERROR_RIOTCP + ERROR_CONNECTED;
00337 }
00338
00339 #ifdef RIO_DEBUG2
00340 RioErr << "### [RioTCP - Connect2] Creating mutex" << endl;
00341 #endif
00342
00343 m_Mutex = new CMutex;
00344 if( m_Mutex == 0 )
00345 {
00346 #ifdef RIO_DEBUG1
00347 RioErr << "### [RioTCP - Connect2] Finish2" << endl;
00348 #endif
00349
00350 return ERROR_RIOTCP + ERROR_MEMORY;
00351 }
00352
00353 #ifdef RIO_DEBUG2
00354 RioErr << "### [RioTCP - Connect2] Checking if mutex is open" << endl;
00355 #endif
00356
00357 if( !( m_Mutex->IsOpen() ) )
00358 {
00359 #ifdef RIO_DEBUG1
00360 RioErr << "### [RioTCP - Connect2] Finish3" << endl;
00361 #endif
00362
00363 return ERROR_RIOTCP + ERROR_CREATE_MUTEX;
00364 }
00365
00366 #ifdef RIO_DEBUG2
00367 RioErr << "### [RioTCP - Connect2] Creating socket" << endl;
00368 #endif
00369
00370 SOCKET sock;
00371
00372 #ifdef WINDOWS
00373
00374
00375 WSADATA winSockInfo;
00376 if( WSAStartup( MAKEWORD( 2, 0 ), &winSockInfo ) != 0 )
00377 {
00378 #ifdef RIO_DEBUG1
00379 RioErr << "### [RioTCP - Connect2] Finish4" << endl;
00380 #endif
00381
00382 return ERROR_RIOTCP + ERROR_SOCKET_LIBRARY;
00383 }
00384 #endif
00385
00386 sock = socket( AF_INET, SOCK_STREAM, 0 );
00387 if( sock == INVALID_SOCKET )
00388 {
00389 delete m_Mutex;
00390 m_Mutex = 0;
00391 RioErr << " RioTCP - Error creating socket .. " << endl;
00392
00393 #ifdef RIO_DEBUG1
00394 RioErr << "### [RioTCP - Connect2] Finish5" << endl;
00395 #endif
00396
00397 return ERROR_RIOTCP + ERROR_SOCKET_CREATE;
00398 }
00399
00400
00401
00402
00403 if( fcntl( sock, F_SETFD, FD_CLOEXEC ) < 0 )
00404 {
00405 close( sock );
00406 delete m_Mutex;
00407 m_Mutex = 0;
00408 RioErr << " RioTCP - Error setting socket as exclusive.. " << endl;
00409
00410 #ifdef RIO_DEBUG1
00411 RioErr << "### [RioTCP - Connect2] Finish6" << endl;
00412 #endif
00413
00414 return ERROR_RIOTCP + ERROR_SOCKET_OPTIONS;
00415 }
00416
00417
00418
00419
00420
00421 struct timeval timeout;
00422 timeout.tv_sec = TCPTIMEOUTSECONDS;
00423 timeout.tv_usec = 0;
00424
00425
00426 if( setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
00427 sizeof( timeout ) ) < 0 )
00428 {
00429 closesocket( sock );
00430 delete m_Mutex;
00431 m_Mutex = 0;
00432 Rioperror( "RioTCP setsockopt" );
00433
00434 #ifdef RIO_DEBUG1
00435 RioErr << "### [RioTCP - Connect2] Finish7" << endl;
00436 #endif
00437
00438 return ERROR_RIOTCP + ERROR_SOCKET_OPTIONS;
00439 }
00440
00441
00442 if( setsockopt( sock, SOL_SOCKET, SO_SNDTIMEO, &timeout,
00443 sizeof( timeout ) ) < 0 )
00444 {
00445 closesocket( sock );
00446 delete m_Mutex;
00447 m_Mutex = 0;
00448 Rioperror( "RioTCP setsockopt" );
00449
00450 #ifdef RIO_DEBUG1
00451 RioErr << "### [RioTCP - Connect2] Finish8" << endl;
00452 #endif
00453
00454 return ERROR_RIOTCP + ERROR_SOCKET_OPTIONS;
00455 }
00456
00457 #ifdef RIO_DEBUG2
00458 RioErr << "### [RioTCP - Connect2] Resolving host name" << endl;
00459 #endif
00460
00461
00462 struct hostent* hostaddress;
00463 hostaddress = gethostbyname( ServerName );
00464
00465 if( hostaddress == NULL )
00466 {
00467 delete m_Mutex;
00468 m_Mutex = 0;
00469 closesocket( sock );
00470
00471 #ifdef RIO_DEBUG1
00472 RioErr << "### [RioTCP - Connect2] Finish9" << endl;
00473 #endif
00474
00475 return ERROR_RIOTCP + ERROR_HOST_NOTFOUND;
00476 }
00477
00478 #ifdef RIO_DEBUG2
00479 RioErr << "### [RioTCP - Connect2] Making TCP connection to server... " << endl;
00480 #endif
00481
00482
00483 SOCKADDR_IN RemoteAddress;
00484 memset( &RemoteAddress,0x00,sizeof( RemoteAddress ) );
00485 RemoteAddress.sin_family = AF_INET;
00486 memcpy((char*)&RemoteAddress.sin_addr,
00487 hostaddress->h_addr_list[0],
00488 hostaddress->h_length);
00489
00490 #ifdef RIO_DEBUG2
00491 RioErr << "### [RioTCP - Connect2] Port: " << TCPport << endl;
00492 #endif
00493
00494 RemoteAddress.sin_port = htons( TCPport );
00495
00496 if( connect( sock,(SOCKADDR*) &RemoteAddress,sizeof( RemoteAddress ) )
00497 == SOCKET_ERROR )
00498 {
00499 closesocket (sock);
00500 delete m_Mutex;
00501 m_Mutex = 0;
00502 RioErr << " RioTCP - Error connecting socket ... " << endl;
00503 Rioperror( "RioTCP Connect 2" );
00504
00505 #ifdef RIO_DEBUG1
00506 RioErr << "### [RioTCP - Connect2] Finish10" << endl;
00507 #endif
00508
00509 return ERROR_RIOTCP + ERROR_SOCKET_CONNECT;
00510 }
00511
00512 m_Socket = sock;
00513
00514
00515 RioTCPHeader Header;
00516 Header.Tag = RioTCPTag;
00517 Header.Version = RioTCPVersion;
00518 Header.Command = RioTCPCommandConnect;
00519 Header.Status = S_OK;
00520 Header.Class = 0;
00521 Header.Method = 0;
00522 Header.DataSize = 0;
00523
00524 #ifdef RIO_DEBUG2
00525 RioErr << "### [RioTCP - Connect2] Sending TCP Message" << endl;
00526 #endif
00527
00528 if( !SendMessage( &Header, NULL ) )
00529 {
00530 m_Socket = INVALID_SOCKET;
00531 closesocket (sock);
00532 delete m_Mutex;
00533 m_Mutex = 0;
00534
00535 #ifdef RIO_DEBUG1
00536 RioErr << "### [RioTCP - Connect2] Finish11" << endl;
00537 #endif
00538
00539 return ERROR_RIOTCP + ERROR_SOCKET_SEND;
00540 }
00541
00542 #ifdef RIO_DEBUG2
00543 RioErr << "### [RioTCP - Connect2] Receiving TCP Header" << endl;
00544 #endif
00545
00546 if( !ReceiveHeader( &Header ) )
00547 {
00548 m_Socket = INVALID_SOCKET;
00549 closesocket (sock);
00550 delete m_Mutex;
00551 m_Mutex = 0;
00552
00553 #ifdef RIO_DEBUG1
00554 RioErr << "### [RioTCP - Connect2] Finish12" << endl;
00555 #endif
00556
00557 return ERROR_RIOTCP + ERROR_SOCKET_RECEIVE;
00558 }
00559
00560 #ifdef RIO_DEBUG2
00561 RioErr << "### [RioTCP - Connect2] Analysing Header" << endl;
00562 #endif
00563
00564 if( Header.Command != RioTCPCommandAccept )
00565 {
00566 m_Socket = INVALID_SOCKET;
00567 delete m_Mutex;
00568 m_Mutex = 0;
00569 closesocket (sock);
00570 if( Header.Command == RioTCPCommandReject )
00571 {
00572 if( Header.Status != S_OK )
00573 {
00574 #ifdef RIO_DEBUG1
00575 RioErr << "### [RioTCP - Connect2] Finish13" << endl;
00576 #endif
00577
00578 return Header.Status;
00579 }
00580 }
00581
00582 #ifdef RIO_DEBUG1
00583 RioErr << "### [RioTCP - Connect2] Finish14" << endl;
00584 #endif
00585
00586 return ERROR_RIOTCP + ERROR_SOCKET_REFUSED_CONNECTION;
00587 }
00588
00589 m_connected = true;
00590 m_Mode = ModeClient;
00591
00592 #ifdef RIO_DEBUG1
00593 RioErr << "### [RioTCP - Connect2] Finish15" << endl;
00594 #endif
00595
00596 return S_OK;
00597 }
00598
00599
00600 RioResult CRioTCP::InitServer( const unsigned short TCPport )
00601 {
00602 #ifdef RIO_DEBUG1
00603 RioErr << "### [RioTCP - InitServer] Start" << endl;
00604 #endif
00605
00606
00607 if( m_connected )
00608 {
00609 #ifdef RIO_DEBUG1
00610 RioErr << "### [RioTCP - InitServer] Finish1" << endl;
00611 #endif
00612
00613 return ERROR_RIOTCP + ERROR_CONNECTED;
00614 }
00615
00616 SOCKET sock;
00617
00618 #ifdef WINDOWS
00619
00620
00621 WSADATA winSockInfo;
00622 if( WSAStartup( MAKEWORD( 2, 0 ),&winSockInfo )!= 0 )
00623 {
00624 #ifdef RIO_DEBUG1
00625 RioErr << "### [RioTCP - InitServer] Finish2" << endl;
00626 #endif
00627
00628 return ERROR_RIOTCP + ERROR_SOCKET_LIBRARY;
00629 }
00630 #endif
00631
00632
00633 sock = socket( AF_INET,SOCK_STREAM,0 );
00634 if(sock == INVALID_SOCKET)
00635 {
00636 RioErr << "RioTCP: IF SOCK" << endl;
00637
00638 #ifdef RIO_DEBUG1
00639 RioErr << "### [RioTCP - InitServer] Finish3" << endl;
00640 #endif
00641
00642 return ERROR_RIOTCP + ERROR_SOCKET_CREATE;
00643 }
00644
00645
00646 int flag = 1;
00647 int rc = setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
00648
00649 if( rc != 0)
00650 RioErr << "RioTCP: Error on setsockopt (sock, SOL_SOCKET,"
00651 << "SO_REUSEADDR, ...): " << rc << endl;
00652
00653
00654 SOCKADDR_IN ourAddress;
00655 memset( &ourAddress,0x00, sizeof( ourAddress ) );
00656 ourAddress.sin_family = AF_INET;
00657 ourAddress.sin_addr.s_addr = htonl( INADDR_ANY );
00658 ourAddress.sin_port = htons( TCPport );
00659 if( bind( sock,(SOCKADDR*) &ourAddress,sizeof( ourAddress ) )
00660 == SOCKET_ERROR )
00661 {
00662 RioErr << "RioTCP: ERRO BIND: " << strerror( errno ) << endl;
00663
00664 #ifdef RIO_DEBUG1
00665 RioErr << "### [RioTCP - InitServer] Finish4" << endl;
00666 #endif
00667
00668 return ERROR_RIOTCP + ERROR_SOCKET_BIND;
00669 }
00670 if( listen( sock, SOMAXCONN )!= 0 )
00671 {
00672 RioErr << "RioTCP: ERRO LISTEN" << endl;
00673
00674 #ifdef RIO_DEBUG1
00675 RioErr << "### [RioTCP - InitServer] Finish5" << endl;
00676 #endif
00677
00678 return ERROR_RIOTCP + ERROR_SOCKET_LISTEN;
00679 }
00680
00681 m_connected = true;
00682 m_Mode = ModeServerMaster;
00683 m_Socket = sock;
00684
00685 #ifdef RIO_DEBUG1
00686 RioErr << "### [RioTCP - InitServer] Finish6" << endl;
00687 #endif
00688
00689 return S_OK;
00690 }
00691
00692
00693
00694
00695
00696
00697 RioResult CRioTCP::AcceptConnection(const SOCKET sock, int TimeOut)
00698 {
00699 #ifdef RIO_DEBUG1
00700 RioErr << "### [RioTCP - AcceptConnection] Start" << endl;
00701 #endif
00702
00703
00704 if(m_connected)
00705 {
00706 #ifdef RIO_DEBUG1
00707 RioErr << "### [RioTCP - AcceptConnection] Finish1" << endl;
00708 #endif
00709
00710 return ERROR_RIOTCP + ERROR_CONNECTED;
00711 }
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726 m_Mode = ModeServer;
00727
00728 #ifdef WINDOWS
00729
00730
00731 WSADATA winSockInfo;
00732 if( WSAStartup( MAKEWORD( 2,0 ),&winSockInfo )!= 0 )
00733 {
00734 #ifdef RIO_DEBUG1
00735 RioErr << "### [RioTCP - AcceptConnection] Finish2" << endl;
00736 #endif
00737
00738 return ERROR_RIOTCP + ERROR_SOCKET_LIBRARY;
00739 }
00740 #endif
00741
00742 RioTCPHeader Header;
00743
00744
00745 m_Socket = sock;
00746
00747
00748
00749
00750
00751
00752
00753 struct timeval timeout;
00754
00755 if( TimeOut != 0 )
00756 {
00757 timeout.tv_sec = TimeOut;
00758 timeout.tv_usec = 0;
00759
00760
00761 if( setsockopt( sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
00762 sizeof( timeout ) ) < 0 )
00763 {
00764 closesocket( sock );
00765 delete m_Mutex;
00766 m_Mutex = 0;
00767 Rioperror( "RioTCP setsockopt" );
00768
00769 #ifdef RIO_DEBUG1
00770 RioErr << "### [RioTCP - Connect2] Finish7" << endl;
00771 #endif
00772
00773 return ERROR_RIOTCP + ERROR_SOCKET_OPTIONS;
00774 }
00775 }
00776
00777
00778
00779
00780 timeout.tv_sec = TCPTIMEOUTSECONDS;
00781 timeout.tv_usec = 0;
00782
00783 if( setsockopt( m_Socket, SOL_SOCKET, SO_SNDTIMEO, &timeout,
00784 sizeof( timeout ) ) < 0 )
00785 {
00786 m_Socket = INVALID_SOCKET;
00787
00788 #ifdef RIO_DEBUG1
00789 RioErr << "### [RioTCP - AcceptConnection] Finish3" << endl;
00790 #endif
00791
00792 return ERROR_RIOTCP + ERROR_SOCKET_OPTIONS;
00793 }
00794
00795
00796 if( ReceiveHeader(&Header) )
00797 {
00798 if( (Header.Tag == RioTCPTag) &&
00799 (Header.Command == RioTCPCommandConnect) )
00800 {
00801 if(Header.Version == RioTCPVersion)
00802 {
00803
00804 Header.Command = RioTCPCommandAccept;
00805 Header.Status = S_OK;
00806 Header.DataSize = 0;
00807 SendMessage(&Header,NULL);
00808 m_connected = true;
00809 m_Mode = ModeServer;
00810
00811 #ifdef RIO_DEBUG1
00812 RioErr << "### [RioTCP - AcceptConnection] Finish4" << endl;
00813 #endif
00814
00815 return S_OK;
00816
00817 }
00818 else
00819 {
00820
00821 Header.Command = RioTCPCommandReject;
00822 Header.Status = ERROR_RIOTCP + ERROR_INVALID_VERSION;
00823 Header.DataSize = 0;
00824 SendMessage(&Header,NULL);
00825 m_Socket = INVALID_SOCKET;
00826
00827 #ifdef RIO_DEBUG1
00828 RioErr << "### [RioTCP - AcceptConnection] Finish5" << endl;
00829 #endif
00830
00831 return ERROR_RIOTCP + ERROR_INVALID_VERSION;
00832 }
00833 }
00834 else
00835 {
00836 m_Socket = INVALID_SOCKET;
00837
00838 #ifdef RIO_DEBUG1
00839 RioErr << "### [RioTCP - AcceptConnection] Finish6" << endl;
00840 #endif
00841
00842 return ERROR_RIOTCP + ERROR_RIO_PROTOCOL;
00843 }
00844 }
00845 else
00846 {
00847 #ifdef RIO_DEBUG1
00848 RioErr << "### [RioTCP - AcceptConnection] Finish7" << endl;
00849 #endif
00850
00851 return ERROR_RIOTCP + ERROR_SOCKET_RECEIVE;
00852 }
00853 }
00854
00855
00856 RioResult CRioTCP::CloseSocket()
00857 {
00858 #ifdef RIO_DEBUG1
00859 RioErr << "### [RioTCP - CloseSocket] Start" << endl;
00860 #endif
00861
00862
00863 if(!m_connected)
00864 {
00865 #ifdef RIO_DEBUG1
00866 RioErr << "### [RioTCP - CloseSocket] Finish1" << endl;
00867 #endif
00868
00869 return ERROR_RIOTCP + ERROR_NOTCONNECTED;
00870 }
00871
00872
00873
00874 if( m_Mode == ModeClient )
00875 closesocket( m_Socket );
00876 else
00877 close( m_Socket );
00878
00879 #ifdef RIO_DEBUG1
00880 RioErr << "### [RioTCP - CloseSocket] Finish2" << endl;
00881 #endif
00882
00883 return S_OK;
00884
00885 }
00886
00887 RioResult CRioTCP::Disconnect()
00888 {
00889 #ifdef RIO_DEBUG1
00890 RioErr << "### [RioTCP - Disconnect] Start" << endl;
00891 #endif
00892
00893
00894 if( !m_connected )
00895 {
00896 #ifdef RIO_DEBUG1
00897 RioErr << "### [RioTCP - Disconnect] Finish1" << endl;
00898 #endif
00899
00900 return ERROR_RIOTCP + ERROR_NOTCONNECTED;
00901 }
00902
00903
00904 if(m_Mode == ModeClient)
00905 {
00906 m_Mutex->Wait();
00907 RioTCPHeader Header;
00908
00909 Header.Tag = RioTCPTag;
00910 Header.Version = RioTCPVersion;
00911 Header.Command = RioTCPCommandDisconnect;
00912 Header.Status = S_OK;
00913 Header.Class = 0;
00914 Header.Method = 0;
00915 Header.DataSize = 0;
00916 SendMessage( &Header, NULL );
00917 delete m_Mutex;
00918 m_Mutex = 0;
00919 }
00920
00921 closesocket( m_Socket );
00922
00923 #ifdef WINDOWS
00924
00925
00926 WSACleanup();
00927 #endif
00928
00929 m_Socket = INVALID_SOCKET;
00930 m_Mode = ModeInvalid;
00931 m_connected = false;
00932
00933 #ifdef RIO_DEBUG1
00934 RioErr << "### [RioTCP - Disconnect] Finish2" << endl;
00935 #endif
00936
00937 return S_OK;
00938 }
00939
00940
00941
00942
00943 RioResult CRioTCP::Call( const unsigned int Class,
00944 const unsigned int Method,
00945 const unsigned int DataSize,
00946 const char *Parameter,
00947 unsigned int *ResultSize,
00948 char **Result )
00949 {
00950 #ifdef RIO_DEBUG1
00951 RioErr << "### [RioTCP - Call] Start" << endl;
00952 #endif
00953
00954
00955
00956
00957 bool closing = false;
00958
00959 *Result = NULL;
00960 *ResultSize = 0;
00961
00962
00963 if( !m_connected )
00964 {
00965 #ifdef RIO_DEBUG1
00966 RioErr << "### [RioTCP - Call] Finish1" << endl;
00967 #endif
00968
00969 #ifdef RIO_DEBUG2
00970 RioErr << "[RioTCP - Call] Error: Not connected!" << endl;
00971 #endif
00972
00973 return ERROR_RIOTCP + ERROR_NOTCONNECTED;
00974 }
00975
00976
00977 if( m_Mode != ModeClient )
00978 {
00979 #ifdef RIO_DEBUG1
00980 RioErr << "### [RioTCP - Call] Finish2" << endl;
00981 #endif
00982
00983 #ifdef RIO_DEBUG2
00984 RioErr << "[RioTCP - Call] Error: Not client!" << endl;
00985 #endif
00986
00987 return ERROR_RIOTCP + ERROR_NOT_CLIENT;
00988 }
00989
00990
00991
00992 if( !( ( ( Class == RioClassSessionManager ) &&
00993 ( Method == RioMethodSessionManagerClose )
00994 ) ||
00995 ( ( Class == RioClassStreamManager ) &&
00996 ( Method == RioMethodStreamManagerClose )
00997 ) ||
00998 ( ( Class == RioClassObjectManager ) &&
00999 ( Method == RioMethodObjectManagerClose )
01000 )
01001 )
01002 )
01003 m_Mutex->Wait();
01004 else
01005 closing = true;
01006
01007 RioTCPHeader Header;
01008
01009 Header.Tag = RioTCPTag;
01010 Header.Version = RioTCPVersion;
01011 Header.Command = RioTCPCommandCall;
01012 Header.Status = S_OK;
01013 Header.Class = Class;
01014 Header.Method = Method;
01015 Header.DataSize = DataSize;
01016
01017
01018 if( !SendMessage( &Header, (void*) Parameter ) )
01019 {
01020 if( !closing )
01021 m_Mutex->Release();
01022
01023 #ifdef RIO_DEBUG1
01024 RioErr << "### [RioTCP - Call] Finish3" << endl;
01025 #endif
01026
01027 #ifdef RIO_DEBUG2
01028 RioErr << "[RioTCP - Call] Error socket send!" << endl;
01029 #endif
01030
01031 return ERROR_RIOTCP + ERROR_SOCKET_SEND;
01032 }
01033
01034
01035 if( !ReceiveHeader( &Header ) )
01036 {
01037 if( !closing )
01038 m_Mutex->Release();
01039
01040 #ifdef RIO_DEBUG1
01041 RioErr << "### [RioTCP - Call] Finish4" << endl;
01042 #endif
01043
01044 #ifdef RIO_DEBUG2
01045 RioErr << "[RioTCP - Call] Error socket receive 1" << endl;
01046 #endif
01047
01048 return ERROR_RIOTCP + ERROR_SOCKET_RECEIVE;
01049 }
01050
01051 if( Header.Command == RioTCPCommandCallError )
01052 {
01053 if( !closing )
01054 m_Mutex->Release();
01055
01056 #ifdef RIO_DEBUG1
01057 RioErr << "### [RioTCP - Call] Finish5" << endl;
01058 #endif
01059
01060 #ifdef RIO_DEBUG2
01061 RioErr << "[RioTCP - Call] Call error(header): "
01062 << GetErrorDescription( Header.Status) << endl;
01063 #endif
01064
01065 return Header.Status;
01066 }
01067
01068 if( Header.Command != RioTCPCommandResult )
01069 {
01070 if( !closing )
01071 m_Mutex->Release();
01072
01073 #ifdef RIO_DEBUG1
01074 RioErr << "### [RioTCP - Call] Finish6" << endl;
01075 #endif
01076
01077 #ifdef RIO_DEBUG2
01078 RioErr << "[RioTCP - Call] Error: Rio Protocol error 1." << endl;
01079 #endif
01080
01081 return ERROR_RIOTCP + ERROR_RIO_PROTOCOL;
01082 }
01083
01084 unsigned int Rsize = Header.DataSize;
01085
01086 if( Rsize <= 0 )
01087 {
01088 if( !closing )
01089 m_Mutex->Release();
01090
01091 #ifdef RIO_DEBUG1
01092 RioErr << "### [RioTCP - Call] Finish7" << endl;
01093 #endif
01094
01095 #ifdef RIO_DEBUG2
01096 RioErr << "[RioTCP - Call] Error: Rio Protocol error 2." << endl;
01097 #endif
01098
01099 return ERROR_RIOTCP + ERROR_RIO_PROTOCOL;
01100 }
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118 *ResultSize = Rsize;
01119 *Result = ( char * ) malloc( Rsize * sizeof( char ) );
01120 if( *Result == NULL )
01121 {
01122 if( !closing )
01123 m_Mutex->Release();
01124
01125 #ifdef RIO_DEBUG1
01126 RioErr << "### [RioTCP - Call] Finish9" << endl;
01127 #endif
01128
01129 #ifdef RIO_DEBUG2
01130 RioErr << "[RioTCP - Call] Error: insufficient memory." << endl;
01131 #endif
01132
01133 return ERROR_RIOTCP + ERROR_MEMORY;
01134 }
01135
01136
01137 if( !ReceiveSocket( *Result, Rsize ) )
01138 {
01139
01140 if( !closing )
01141 m_Mutex->Release();
01142
01143 #ifdef RIO_DEBUG1
01144 RioErr << "### [RioTCP - Call] Finish10" << endl;
01145 #endif
01146
01147 #ifdef RIO_DEBUG2
01148 RioErr << "[RioTCP - Call] Error: socket receive 2." << endl;
01149 #endif
01150
01151 return ERROR_RIOTCP + ERROR_SOCKET_RECEIVE;
01152 }
01153
01154
01155 if( !closing )
01156 m_Mutex->Release();
01157
01158 #ifdef RIO_DEBUG1
01159 RioErr << "### [RioTCP - Call] Finish11" << endl;
01160 #endif
01161
01162 return S_OK;
01163 }
01164
01165
01166 RioResult CRioTCP::ReceiveCall( unsigned int *Class,
01167 unsigned int *Method,
01168 char *Parameter,
01169 unsigned int ParameterSize )
01170 {
01171 #ifdef RIO_DEBUG1
01172 RioErr << "### [RioTCP - ReceiveCall] Start" << endl;
01173 #endif
01174
01175
01176
01177
01178
01179
01180 if( !m_connected )
01181 {
01182 RioErr << "ERROR_RIOTCP + ERROR_NOTCONNECTED " << endl;
01183
01184 #ifdef RIO_DEBUG1
01185 RioErr << "### [RioTCP - ReceiveCall] Finish1" << endl;
01186 #endif
01187
01188 return ERROR_RIOTCP + ERROR_NOTCONNECTED;
01189 }
01190
01191
01192 if( m_Mode != ModeServer )
01193 {
01194 RioErr << "ERROR_RIOTCP + ERROR_NOT_SERVER " << endl;
01195
01196 #ifdef RIO_DEBUG1
01197 RioErr << "### [RioTCP - ReceiveCall] Finish2" << endl;
01198 #endif
01199
01200 return ERROR_RIOTCP + ERROR_NOT_SERVER;
01201 }
01202
01203 RioTCPHeader Header;
01204 if( ReceiveHeader( &Header ) )
01205 {
01206 if( ( Header.Tag == RioTCPTag ) &&
01207 ( Header.Version == RioTCPVersion ) &&
01208 ( Header.Command == RioTCPCommandCall )
01209 )
01210 {
01211 if( Header.DataSize > 0 )
01212 {
01213 if( Header.DataSize > ParameterSize )
01214 {
01215 #ifdef RIO_DEBUG1
01216 RioErr << "### [RioTCP - ReceiveCall] Finish3" << endl;
01217 #endif
01218
01219 return ERROR_RIOTCP + ERROR_RIO_PROTOCOL;
01220 }
01221 if( !ReceiveSocket( Parameter, Header.DataSize ) )
01222 {
01223 #ifdef RIO_DEBUG1
01224 RioErr << "### [RioTCP - ReceiveCall] Finish4" << endl;
01225 #endif
01226
01227 return ERROR_RIOTCP + ERROR_SOCKET_RECEIVE;
01228 }
01229 }
01230 else
01231 {
01232
01233
01234 }
01235
01236 *Class = Header.Class;
01237 *Method = Header.Method;
01238
01239 #ifdef RIO_DEBUG1
01240 RioErr << "### [RioTCP - ReceiveCall] Finish5" << endl;
01241 #endif
01242
01243 return S_OK;
01244 }
01245 else
01246 {
01247
01248
01249 if( ( Header.Tag == RioTCPTag ) &&
01250 ( Header.Version == RioTCPVersion ) &&
01251 ( Header.Command == RioTCPCommandDisconnect ) )
01252 {
01253 #ifdef RIO_DEBUG1
01254 RioErr << "### [RioTCP - ReceiveCall] Finish6" << endl;
01255 #endif
01256
01257 return ERROR_RIOTCP + ERROR_CLOSED_CONNECTION;
01258 }
01259 else
01260 {
01261 #ifdef RIO_DEBUG1
01262 RioErr << "### [RioTCP - ReceiveCall] Finish7" << endl;
01263 #endif
01264
01265 return ERROR_RIOTCP + ERROR_RIO_PROTOCOL;
01266 }
01267
01268 }
01269 }
01270 else
01271 {
01272 #ifdef RIO_DEBUG1
01273 RioErr << "### [RioTCP - ReceiveCall] Finish8" << endl;
01274 #endif
01275
01276 return ERROR_RIOTCP + ERROR_SOCKET_RECEIVE;
01277 }
01278 }
01279
01280
01281 RioResult CRioTCP::SendResult( const char* Result,
01282 const unsigned int ResultSize )
01283 {
01284 #ifdef RIO_DEBUG1
01285 RioErr << "### [RioTCP - SendResult] Start" << endl;
01286 #endif
01287
01288
01289 if(!m_connected)
01290 {
01291 #ifdef RIO_DEBUG1
01292 RioErr << "### [RioTCP - SendResult] Finish1" << endl;
01293 #endif
01294
01295 return ERROR_RIOTCP + ERROR_NOTCONNECTED;
01296 }
01297
01298
01299 if(m_Mode != ModeServer)
01300 {
01301 #ifdef RIO_DEBUG1
01302 RioErr << "### [RioTCP - SendResult] Finish2" << endl;
01303 #endif
01304
01305 return ERROR_RIOTCP + ERROR_NOT_SERVER;
01306 }
01307
01308 RioTCPHeader Header;
01309 Header.Tag = RioTCPTag;
01310 Header.Version = RioTCPVersion;
01311 Header.Command = RioTCPCommandResult;
01312 Header.Status = S_OK;
01313
01314 Header.Class = 0;
01315 Header.Method = 0;
01316 Header.DataSize = ResultSize;
01317
01318 if(SendMessage(&Header,(void*) Result))
01319 {
01320 #ifdef RIO_DEBUG1
01321 RioErr << "### [RioTCP - SendResult] Finish3" << endl;
01322 #endif
01323
01324 return S_OK;
01325 }
01326 else
01327 {
01328 #ifdef RIO_DEBUG1
01329 RioErr << "### [RioTCP - SendResult] Finish4" << endl;
01330 #endif
01331
01332 return ERROR_RIOTCP + ERROR_SOCKET_SEND;
01333 }
01334 }
01335
01336
01337 RioResult CRioTCP::SendCallError( const int Status )
01338 {
01339 #ifdef RIO_DEBUG1
01340 RioErr << "### [RioTCP - SendCallError] Start" << endl;
01341 #endif
01342
01343
01344 if( !m_connected )
01345 {
01346 #ifdef RIO_DEBUG1
01347 RioErr << "### [RioTCP - SendCallError] Finish1" << endl;
01348 #endif
01349
01350 return ERROR_RIOTCP + ERROR_NOTCONNECTED;
01351 }
01352
01353
01354 if( m_Mode != ModeServer )
01355 {
01356 #ifdef RIO_DEBUG1
01357 RioErr << "### [RioTCP - SendCallError] Finish2" << endl;
01358 #endif
01359
01360 return ERROR_RIOTCP + ERROR_NOT_SERVER;
01361 }
01362
01363 RioTCPHeader Header;
01364 Header.Tag = RioTCPTag;
01365 Header.Version = RioTCPVersion;
01366 Header.Command = RioTCPCommandCallError;
01367 Header.Status = Status;
01368
01369 Header.Class = 0;
01370 Header.Method = 0;
01371 Header.DataSize = 0;
01372
01373 if( SendMessage( &Header,NULL ) )
01374 {
01375 #ifdef RIO_DEBUG1
01376 RioErr << "### [RioTCP - SendCallError] Finish3" << endl;
01377 #endif
01378
01379 return S_OK;
01380 }
01381 else
01382 {
01383 #ifdef RIO_DEBUG1
01384 RioErr << "### [RioTCP - SendCallError] Finish4" << endl;
01385 #endif
01386
01387 return ERROR_RIOTCP + ERROR_SOCKET_SEND;
01388 }
01389 }
01390
01391
01392 RioResult CRioTCP::WaitConnection(SOCKET* sock )
01393 {
01394 #ifdef RIO_DEBUG1
01395 RioErr << "### [RioTCP - WaitConnection] Start" << endl;
01396 #endif
01397
01398
01399 if( !m_connected )
01400 {
01401 #ifdef RIO_DEBUG1
01402 RioErr << "### [RioTCP - WaitConnection] Finish1" << endl;
01403 #endif
01404
01405 return ERROR_RIOTCP + ERROR_NOTCONNECTED;
01406 }
01407
01408
01409 if( m_Mode != ModeServerMaster )
01410 {
01411 #ifdef RIO_DEBUG1
01412 RioErr << "### [RioTCP - WaitConnection] Finish2" << endl;
01413 #endif
01414
01415 return ERROR_RIOTCP + ERROR_NOT_SERVER;
01416 }
01417
01418 struct sockaddr_in remoteAddress;
01419 SOCKET ConnectedSocket;
01420
01421
01422 int remoteAddressl;
01423
01424 remoteAddressl = sizeof( remoteAddress );
01425 memset( &remoteAddress, 0, sizeof( remoteAddressl ));
01426 remoteAddress.sin_family = AF_INET;
01427
01428
01429
01430 ConnectedSocket = accept( m_Socket, (struct sockaddr *)&remoteAddress,
01431 (socklen_t *)&remoteAddressl);
01432
01433 if( ConnectedSocket == INVALID_SOCKET )
01434 {
01435 int error = errno;
01436
01437 RioErr << "[RioTCP - WaitConnection] Erro " << error << " ("
01438 << strerror( error ) << " retornado ao executar o accept!"
01439 << endl;
01440
01441 #ifdef RIO_DEBUG1
01442 RioErr << "### [RioTCP - WaitConnection] Finish3" << endl;
01443 #endif
01444
01445
01446 if( ( error == EBADF ) || ( error == EFAULT ) || ( error == EINTR ) ||
01447 ( error == EINVAL ) || ( error == ENOTSOCK ) ||
01448 ( error == EOPNOTSUPP ) )
01449 return ERROR_RIOTCP + ERROR_SOCKET_ACCEPT_CRITICAL;
01450 else
01451 return ERROR_RIOTCP + ERROR_SOCKET_ACCEPT;
01452 }
01453
01454 #ifdef RIO_DEBUG2
01455 RioErr << "[RioTCP] Nova conex�o de host vindo de "
01456 << inet_ntoa( remoteAddress.sin_addr ) << ":"
01457 << htons( remoteAddress.sin_port ) << endl;
01458 #endif
01459
01460
01461 m_RemoteAddress = remoteAddress;
01462
01463 *sock = ConnectedSocket;
01464
01465 #ifdef RIO_DEBUG1
01466 RioErr << "### [RioTCP - WaitConnection] Finish4" << endl;
01467 #endif
01468
01469 return S_OK;
01470 }
01471
01472
01473 bool CRioTCP::ReceiveSocket( VOID* buffer,
01474 const unsigned int nBytes )
01475 {
01476 #ifdef RIO_DEBUG1
01477 RioErr << "### [RioTCP - ReceiveSocket] Start" << endl;
01478 #endif
01479
01480 unsigned int TotalBytes = 0;
01481 char *buf = (char*)buffer;
01482 int n = 0;
01483
01484
01485
01486
01487 while( TotalBytes < nBytes )
01488 {
01489 n = recv(m_Socket,buf+TotalBytes,nBytes-TotalBytes,0);
01490
01491 if( ( n == SOCKET_ERROR ) || ( n == 0 ) )
01492 {
01493 #ifdef RIO_DEBUG1
01494 if( n < 0 )
01495 RioErr << "CRioTCP::ReceiveSocket rc " << n << " errno "
01496 << strerror(errno) << endl;
01497
01498 RioErr << "### [RioTCP - ReceiveSocket] Finish1" << endl;
01499 #endif
01500
01501 return false;
01502 }
01503
01504 TotalBytes+=n;
01505 }
01506
01507 #ifdef RIO_DEBUG1
01508 RioErr << "### [RioTCP - ReceiveSocket] Finish2" << endl;
01509 #endif
01510
01511 return true;
01512 }
01513
01514
01515 bool CRioTCP::SendSocket(const VOID* buffer,
01516 const unsigned int nBytes)
01517 {
01518 #ifdef RIO_DEBUG1
01519 RioErr << "### [RioTCP - SendSocket] Start" << endl;
01520 #endif
01521
01522 unsigned int TotalBytes = 0;
01523 const char *buf = (const char*)buffer;
01524 int n;
01525
01526
01527
01528
01529
01530 while( TotalBytes < nBytes )
01531 {
01532 n = send( m_Socket,buf+TotalBytes,nBytes-TotalBytes,MSG_NOSIGNAL );
01533 if( n == SOCKET_ERROR )
01534
01535 {
01536 if( errno == EINTR )
01537 {
01538 TotalBytes+=n;
01539 continue;
01540 }
01541 else
01542 {
01543 Rioperror( "RioTCP - receivesocket error " );
01544 RioErr << "RioTCP - receivesocket valor de n: " << n << endl;
01545
01546 if( errno == EPIPE )
01547 m_connected = false;
01548
01549 #ifdef RIO_DEBUG1
01550 RioErr << "### [RioTCP - SendSocket] Finish1" << endl;
01551 #endif
01552
01553 return false;
01554 }
01555 }
01556 else
01557 {
01558 if( errno == EINTR )
01559 {
01560 TotalBytes+=n;
01561 continue;
01562 }
01563 }
01564 TotalBytes+=n;
01565 }
01566
01567 #ifdef RIO_DEBUG1
01568 RioErr << "### [RioTCP - SendSocket] Finish2" << endl;
01569 #endif
01570
01571 return true;
01572 }
01573
01574
01575 bool CRioTCP::ReceiveHeader(RioTCPHeader *Header)
01576 {
01577 #ifdef RIO_DEBUG1
01578 RioErr << "### [RioTCP - ReceiveHeader] Start" << endl;
01579 #endif
01580
01581 RioTCPHeader LocalHeader;
01582
01583 bool result = ReceiveSocket(&LocalHeader,TCPHeaderSize);
01584
01585 if( result )
01586 {
01587
01588 Header->Tag = ntohl(LocalHeader.Tag);
01589 Header->Version = ntohl(LocalHeader.Version);
01590 Header->Command = ntohl(LocalHeader.Command);
01591 Header->Status = ntohl(LocalHeader.Status);
01592 Header->Class = ntohl(LocalHeader.Class);
01593 Header->Method = ntohl(LocalHeader.Method);
01594 Header->DataSize = ntohl(LocalHeader.DataSize);
01595
01596
01597 #if 0
01598 RioErr << "ReceiveHeader: "
01599 << "Tag: " << Header->Tag
01600 << " Version:" << Header->Version
01601 << " Command:" << Header->Command
01602 << " Status:" << Header->Status
01603 << " Class:" << Header->Class
01604 << " Method:" << Header->Method
01605 << " DataSize:" << Header->DataSize << endl;
01606 #endif
01607
01608 #ifdef RIO_DEBUG1
01609 RioErr << "### [RioTCP - ReceiveHeader] Finish1" << endl;
01610 #endif
01611
01612 return true;
01613 }
01614 else
01615 {
01616 #ifdef RIO_DEBUG1
01617 RioErr << "### [RioTCP - ReceiveHeader] Finish2" << endl;
01618 #endif
01619
01620 return false;
01621 }
01622
01623 }
01624
01625
01626 bool CRioTCP::SendMessage( const RioTCPHeader *Header, const void* Data )
01627 {
01628 #ifdef RIO_DEBUG1
01629 RioErr << "### [RioTCP - SendMessage] Start" << endl;
01630 #endif
01631
01632 bool rc = true;
01633 unsigned char *SendData;
01634
01635
01636
01637
01638
01639
01640
01641
01642
01643
01644 RioTCPHeader MessageHeader;
01645 MessageHeader.Tag = htonl(Header->Tag);
01646 MessageHeader.Version = htonl(Header->Version);
01647 MessageHeader.Command = htonl(Header->Command);
01648 MessageHeader.Status = htonl(Header->Status);
01649 MessageHeader.Class = htonl(Header->Class);
01650 MessageHeader.Method = htonl(Header->Method);
01651 MessageHeader.DataSize = htonl(Header->DataSize);
01652
01653
01654
01655 SendData = new unsigned char[ TCPHeaderSize + Header->DataSize ];
01656
01657 if( SendData == NULL )
01658 {
01659 RioErr << "### [RioTCP - SendMessage] insufficient memory" << endl;
01660 #ifdef RIO_DEBUG1
01661 RioErr << "### [RioTCP - SendMessage] Finish1" << endl;
01662 #endif
01663 return false;
01664 }
01665
01666
01667 memcpy( SendData, &MessageHeader, sizeof( RioTCPHeader ) *
01668 sizeof( unsigned char ) );
01669
01670 if( Header->DataSize > 0 )
01671 memcpy( SendData + sizeof( RioTCPHeader ), Data, Header->DataSize );
01672
01673
01674 #if 0
01675 RioErr << "SendMessage: "
01676 << " Command " << Header->Command
01677 << " Status " << (int *) Header->Status
01678 << " Class " << Header->Class
01679 << " Method " << Header->Method
01680 << " DataSize " << Header->DataSize << endl;
01681 #endif
01682
01683 rc = SendSocket( SendData, TCPHeaderSize + Header->DataSize );
01684
01685
01686 delete[] SendData;
01687
01688 #ifdef RIO_DEBUG1
01689 RioErr << "### [RioTCP - SendMessage] Finish2" << endl;
01690 #endif
01691
01692 return rc;
01693 }
01694
01695
01696 bool CRioTCP::isConnected( void )
01697 {
01698 #ifdef RIO_DEBUG1
01699 RioErr << "### [RioTCP - isConnected] Single" << endl;
01700 #endif
01701
01702 return m_connected;
01703 }