CRioTCP Class Reference

#include <RioTCP.h>

Public Member Functions

 CRioTCP ()
 ~CRioTCP ()
RioResult Connect (const char *MachineName, const unsigned short TCPport)
RioResult Connect (const char *MachineName)
RioResult InitServer (const unsigned short TCPport)
RioResult AcceptConnection (const SOCKET sock, int TimeOut=0)
RioResult CloseSocket ()
RioResult Disconnect ()
RioResult Call (const unsigned int Class, const unsigned int Method, const unsigned int ParameterSize, const char *Parameter, unsigned int *ResultSize, char **Result)
RioResult WaitConnection (SOCKET *sock)
RioResult ReceiveCall (unsigned int *Class, unsigned int *Method, char *Parameter, unsigned int ParameterSize)
RioResult SendResult (const char *Result, const unsigned int ResultSize)
RioResult SendCallError (const int Status)
bool isConnected ()

Data Fields

struct sockaddr_in m_RemoteAddress

Private Member Functions

bool ReceiveSocket (VOID *buffer, const unsigned int nBytes)
bool SendSocket (const VOID *buffer, const unsigned int nBytes)
bool ReceiveHeader (RioTCPHeader *Header)
bool SendMessage (const RioTCPHeader *Header, const void *Data)

Private Attributes

SOCKET m_Socket
bool m_connected
int m_Mode
CMutexm_Mutex

Detailed Description

Definition at line 65 of file RioTCP.h.


Constructor & Destructor Documentation

CRioTCP::CRioTCP (  ) 

Definition at line 60 of file RioTCP.cpp.

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 }

CRioTCP::~CRioTCP (  ) 

Definition at line 76 of file RioTCP.cpp.

00077 {
00078     #ifdef RIO_DEBUG1
00079     RioErr << "### [RioTCP - Destructor] Single" << endl;
00080     #endif
00081 }


Member Function Documentation

RioResult CRioTCP::AcceptConnection ( const SOCKET  sock,
int  TimeOut = 0 
)

Definition at line 697 of file RioTCP.cpp.

00698 {
00699     #ifdef RIO_DEBUG1
00700     RioErr << "### [RioTCP - AcceptConnection] Start" << endl;
00701     #endif
00702 
00703     // Check if not initialized
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     // set process priority to real-time
00715     if( SetPriorityClass( GetCurrentProcess(),
00716                           REALTIME_PRIORITY_CLASS ) == FALSE
00717       )
00718     {
00719     m_log << "AcceptConnection(): "
00720           << "Failed to set process priority to real-time " << endl;
00721     return ERROR_RIOTCP_SYS_REALTIME;
00722     }
00723     */
00724 
00725     //### init mode of this instance of RioTCP
00726     m_Mode = ModeServer;
00727 
00728     #ifdef WINDOWS
00729     // WINDOWS32 implementation
00730     // Initialize Windows Socket library
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     // Update socket info
00745     m_Socket = sock;
00746 
00747     // Configura o socket para abortar se a conexao fechar depois de um tempo,
00748     // se nao houver resposta de uns dos pontos da conexao depois de um certo
00749     // intervalo de tempo (TimeOut segundos e 0 microsegundos), se TimeOut for
00750     // diferente de 0, ao receber mensagens dos clientes. Se TimeOut for 0, nao
00751     // existira limite, e o servidor podera esperar para sempre por mensagens
00752     // do cliente.
00753     struct timeval timeout;
00754 
00755     if( TimeOut != 0 )
00756     {
00757         timeout.tv_sec = TimeOut;
00758         timeout.tv_usec = 0;
00759 
00760         // Tempo maximo de espera ao receber as mensagens.
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     // Configura o socket para abortar se a conexao fechar depois de um tempo,
00777     // se nao houver resposta de uns dos pontos da conexao depois de um certo
00778     // intervalo de tempo (TCPTIMEOUTSECONDS segundos e 0 microsegundos), ao
00779     // enviar mensagens ao cliente (respostas as requisicoes).
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     // Receive Connection message
00796     if( ReceiveHeader(&Header) )
00797     {
00798         if( (Header.Tag == RioTCPTag) &&
00799             (Header.Command == RioTCPCommandConnect) )
00800         {
00801             if(Header.Version == RioTCPVersion)
00802             {
00803                 // Accept connection
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                 // Reject connection if there is a version mismatch
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 }

RioResult CRioTCP::Call ( const unsigned int  Class,
const unsigned int  Method,
const unsigned int  ParameterSize,
const char *  Parameter,
unsigned int *  ResultSize,
char **  Result 
)

Definition at line 943 of file RioTCP.cpp.

00949 {
00950     #ifdef RIO_DEBUG1
00951     RioErr << "### [RioTCP - Call] Start" << endl;
00952     #endif
00953 
00954     //closing indica se Call foi chamada para fechar uma sess�o, stream ou
00955     //object. � utilizado pra que n�o se trave no mutex abaixo pois se esta
00956     //chamada n�o receber resposta o aplicativo deve prosseguir assim mesmo.
00957     bool closing = false;
00958     // Result is set to null if an error occurs
00959     *Result = NULL;
00960     *ResultSize = 0;
00961 
00962     // Check if  connected
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     // Check if in client mode
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     // Make sure there is only one client call at a time
00991     // If close Object/Stream/Session don't wait
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     // Send message with data
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     // receive reply header
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     /*if( Rsize > ResultSize )
01102     {
01103         if( !closing )
01104             m_Mutex->Release();
01105 
01106         #ifdef RIO_DEBUG1
01107         RioErr << "### [RioTCP - Call] Finish8" << endl;
01108         #endif
01109 
01110         #ifdef RIO_DEBUG2
01111         RioErr << "[RioTCP - Call] Error: Rio Protocol error 3." << endl;
01112         #endif
01113 
01114         return ERROR_RIOTCP + ERROR_RIO_PROTOCOL;
01115     }*/
01116     // Agora, alocamos o buffer na funcao, para podermos receber mensagens de
01117     // tamanhos variados.
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     //if( !ReceiveSocket( Result, Rsize ) )
01137     if( !ReceiveSocket( *Result, Rsize ) )
01138     {
01139         //delete[] ResultBuffer;
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     // If close Object/Stream/Session don't release
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 }

RioResult CRioTCP::CloseSocket (  ) 

Definition at line 856 of file RioTCP.cpp.

00857 {
00858     #ifdef RIO_DEBUG1
00859     RioErr << "### [RioTCP - CloseSocket] Start" << endl;
00860     #endif
00861 
00862     // Check if  connected
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     //### if server then might need to break waiting accept
00873     //    shutdown won't do this, use real close
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 }

RioResult CRioTCP::Connect ( const char *  MachineName  ) 

Definition at line 84 of file RioTCP.cpp.

00085 {
00086     #ifdef RIO_DEBUG1
00087     RioErr << "### [RioTCP - Connect1] Start" << endl;
00088     #endif
00089 
00090     // Check if not connected yet
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     // WINDOWS32 implementation
00122         // Initialize Windows Socket library
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     // Faz com que o socket seja exclusivo do processo, e nao seja passado 
00148     // para um outro processo criado pelo processo que abriu o socket.
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     // Configura o socket para abortar se a conexao fechar depois de um tempo,
00165     // se nao houver resposta de uns dos pontos da conexao depois de um certo
00166     // intervalo de tempo (TCPTIMEOUTSECONDS segundos e 0 microsegundos).
00167     
00168     struct timeval timeout;
00169     timeout.tv_sec = TCPTIMEOUTSECONDS;
00170     timeout.tv_usec = 0;
00171    
00172     // Tempo maximo de espera ao receber as mensagens. 
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     // Tempo maximo de espera ao enviar as mensagens. 
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     // Get host IP address for server from host name
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     // Make TCP connection to server
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     // Fill connect Message fields
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 }

RioResult CRioTCP::Connect ( const char *  MachineName,
const unsigned short  TCPport 
)

Definition at line 322 of file RioTCP.cpp.

00324 {
00325     #ifdef RIO_DEBUG1
00326     RioErr << "### [RioTCP - Connect2] Start" << endl;
00327     #endif
00328 
00329     // Check if not connected yet
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     // WINDOWS32 implementation
00374         // Initialize Windows Socket library
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     // Faz com que o socket seja exclusivo do processo, e nao seja passado 
00401     // para um outro processo criado pelo processo que abriu o socket.
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     // Configura o socket para abortar se a conexao fechar depois de um tempo,
00418     // se nao houver resposta de uns dos pontos da conexao depois de um certo
00419     // intervalo de tempo (TCPTIMEOUTSECONDS segundos e 0 microsegundos).
00420     
00421     struct timeval timeout;
00422     timeout.tv_sec = TCPTIMEOUTSECONDS;
00423     timeout.tv_usec = 0;
00424    
00425     // Tempo maximo de espera ao receber as mensagens. 
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     // Tempo maximo de espera ao enviar as mensagens. 
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     // Get host IP address for server from host name
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     // Make TCP connection to server
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     // Fill connect Message fields
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 }

RioResult CRioTCP::Disconnect (  ) 

Definition at line 887 of file RioTCP.cpp.

00888 {
00889     #ifdef RIO_DEBUG1
00890     RioErr << "### [RioTCP - Disconnect] Start" << endl;
00891     #endif
00892 
00893     // Check if  connected
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     // If client mode send disconnect message before closing socket
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     // WINDOWS32 implementation
00925         // Clean up windows socket library
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 }

RioResult CRioTCP::InitServer ( const unsigned short  TCPport  ) 

Definition at line 600 of file RioTCP.cpp.

00601 {
00602     #ifdef RIO_DEBUG1
00603     RioErr << "### [RioTCP - InitServer] Start" << endl;
00604     #endif
00605 
00606     // Check if not initialized
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     // WINDOWS32 implementation
00620     // setup winsock
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     // Create socket for receiving connections from clients
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     // ### set SO_REUSEADDR option so can reopen even if old sessions exist
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     // bind socket to port.
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 }

bool CRioTCP::isConnected ( void   ) 

Definition at line 1696 of file RioTCP.cpp.

01697 {
01698     #ifdef RIO_DEBUG1
01699     RioErr << "### [RioTCP - isConnected] Single" << endl;
01700     #endif
01701 
01702     return m_connected;
01703 }

RioResult CRioTCP::ReceiveCall ( unsigned int *  Class,
unsigned int *  Method,
char *  Parameter,
unsigned int  ParameterSize 
)

Definition at line 1166 of file RioTCP.cpp.

01170 {
01171     #ifdef RIO_DEBUG1
01172     RioErr << "### [RioTCP - ReceiveCall] Start" << endl;
01173     #endif
01174 
01175     //  // Parameter is set to null if an error occurs
01176     //  *Parameter = 0;
01177     //  *ParameterSize = 0;
01178 
01179     // Check if  connected
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     // Check if in server mode
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                 //*Parameter = 0;
01233                 //*ParameterSize = 0;
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             // test added to know when the user --------------------
01248             // disconnected or it was an error
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 }

bool CRioTCP::ReceiveHeader ( RioTCPHeader Header  )  [private]

Definition at line 1575 of file RioTCP.cpp.

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         // Convert network byte order of header to host byte order
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     // ### debug
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 }

bool CRioTCP::ReceiveSocket ( VOID buffer,
const unsigned int  nBytes 
) [private]

Definition at line 1473 of file RioTCP.cpp.

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     // Message can be received in several packets
01485     // Keep reading socket until expected number of bytes arrive
01486     // or an error occurs
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 }

RioResult CRioTCP::SendCallError ( const int  Status  ) 

Definition at line 1337 of file RioTCP.cpp.

01338 {
01339     #ifdef RIO_DEBUG1
01340     RioErr << "### [RioTCP - SendCallError] Start" << endl;
01341     #endif
01342 
01343     // Check if  connected
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     // Check if in server mode
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     // Class and method not necessary on call error message
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 }

bool CRioTCP::SendMessage ( const RioTCPHeader Header,
const void *  Data 
) [private]

Definition at line 1626 of file RioTCP.cpp.

01627 {
01628     #ifdef RIO_DEBUG1
01629     RioErr << "### [RioTCP - SendMessage] Start" << endl;
01630     #endif
01631 
01632     bool rc = true;
01633     unsigned char *SendData;
01634 
01635     // We copy the message to a Local Buffer where header and data are
01636     // stored continuously and thus can be sent on a single send() call
01637     // This add an additional copy of data.
01638     // We do this because sending Header and Data on two separate send()
01639     // calls cause a large latency between both messages (reason unknown)
01640     // Ideally we would like a send () function that could gather data
01641     // from non contiguous regions of memory and send them on the same
01642     // network packet.
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     // Aloca espaco na memoria para a mensagem a ser enviada.
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     // Copia o Header para o buffer.
01667     memcpy( SendData, &MessageHeader, sizeof( RioTCPHeader ) * 
01668                                       sizeof( unsigned char ) );
01669     // Copia os dados para o buffer.
01670     if( Header->DataSize > 0 )
01671         memcpy( SendData + sizeof( RioTCPHeader ), Data, Header->DataSize );
01672 
01673     // ### debugging
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     // Libera a memoria usada pela mensagem
01685 
01686     delete[] SendData;
01687 
01688     #ifdef RIO_DEBUG1
01689     RioErr << "### [RioTCP - SendMessage] Finish2" << endl;
01690     #endif
01691 
01692     return rc;
01693 }

RioResult CRioTCP::SendResult ( const char *  Result,
const unsigned int  ResultSize 
)

Definition at line 1281 of file RioTCP.cpp.

01283 {
01284     #ifdef RIO_DEBUG1
01285     RioErr << "### [RioTCP - SendResult] Start" << endl;
01286     #endif
01287 
01288     // Check if  connected
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     // Check if in server mode
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     // Class and method not necessary on call result message
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 }

bool CRioTCP::SendSocket ( const VOID buffer,
const unsigned int  nBytes 
) [private]

Definition at line 1515 of file RioTCP.cpp.

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     // Message can be sent in several packets
01527     // Keep writing socket until expected number of bytes are sent
01528     // or an error occurs
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 }

RioResult CRioTCP::WaitConnection ( SOCKET sock  ) 

Definition at line 1392 of file RioTCP.cpp.

01393 {
01394     #ifdef RIO_DEBUG1
01395     RioErr << "### [RioTCP - WaitConnection] Start" << endl;
01396     #endif
01397 
01398     // Check if  connected
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     // Check if in server master mode
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     // added to get remote client address --------------------------
01422     int remoteAddressl;
01423 
01424     remoteAddressl = sizeof( remoteAddress );
01425     memset( &remoteAddress, 0, sizeof( remoteAddressl ));
01426     remoteAddress.sin_family = AF_INET;
01427     // ------------------------------------------------------------------------
01428 
01429     // Wait for client connection
01430     ConnectedSocket = accept( m_Socket, (struct sockaddr *)&remoteAddress,
01431                              (socklen_t *)&remoteAddressl);
01432 
01433     if( ConnectedSocket == INVALID_SOCKET )
01434     {
01435         int error = errno; // Armazena o erro retornado pelo accept.
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         // Verifica se o erro e ou nao critico.
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     //A atribui��o abaixo � v�lida no C++ (n�o preciso do memcpy)
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 }


Field Documentation

bool CRioTCP::m_connected [private]

Definition at line 69 of file RioTCP.h.

int CRioTCP::m_Mode [private]

Definition at line 70 of file RioTCP.h.

Definition at line 71 of file RioTCP.h.

struct sockaddr_in CRioTCP::m_RemoteAddress

Definition at line 110 of file RioTCP.h.

Definition at line 68 of file RioTCP.h.


The documentation for this class was generated from the following files:
Generated on Wed Jul 4 16:03:33 2012 for RIO by  doxygen 1.6.3