00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef __RIOTCP_H_
00021 #define __RIOTCP_H_
00022
00023
00024
00025 #ifndef WINDOWS
00026
00027 #include <sys/types.h>
00028 #include <sys/socket.h>
00029 #include <netinet/in.h>
00030 #include <arpa/inet.h>
00031
00032 #else
00033
00034 #include <winsock2.h>
00035 #endif
00036
00037 #include "RioMutex.h"
00038 #include <fstream>
00039 #include <iostream>
00040 using namespace std;
00041
00042
00043
00044 #include "RioTypes.h"
00045
00046
00047
00048
00049 typedef struct
00050 {
00051 unsigned int Tag;
00052 unsigned int Version;
00053 unsigned int Command;
00054 int Status;
00055 unsigned int Class;
00056 unsigned int Method;
00057 unsigned int DataSize;
00058 } RioTCPHeader;
00059
00060 const int MaxTCPDataSize = 2048;
00061 const int TCPHeaderSize = sizeof ( RioTCPHeader );
00062 const int MaxTCPMessageSize = MaxTCPDataSize + TCPHeaderSize;
00063 const int TCPTIMEOUTSECONDS = 15;
00064
00065 class CRioTCP
00066 {
00067 private:
00068 SOCKET m_Socket;
00069 bool m_connected;
00070 int m_Mode;
00071 CMutex* m_Mutex;
00072 bool ReceiveSocket( VOID* buffer, const unsigned int nBytes );
00073 bool SendSocket( const VOID* buffer, const unsigned int nBytes );
00074 bool ReceiveHeader( RioTCPHeader *Header );
00075 bool SendMessage( const RioTCPHeader *Header, const void* Data );
00076
00077 public:
00078 CRioTCP();
00079 ~CRioTCP();
00080
00081
00082 RioResult Connect( const char* MachineName,
00083 const unsigned short TCPport );
00084 RioResult Connect( const char* MachineName );
00085
00086 RioResult InitServer( const unsigned short TCPport );
00087
00088
00089
00090
00091
00092 RioResult AcceptConnection( const SOCKET sock,
00093 int TimeOut = 0 );
00094 RioResult CloseSocket();
00095 RioResult Disconnect();
00096 RioResult Call( const unsigned int Class,
00097 const unsigned int Method,
00098 const unsigned int ParameterSize,
00099 const char* Parameter,
00100 unsigned int *ResultSize,
00101 char** Result );
00102 RioResult WaitConnection( SOCKET* sock );
00103 RioResult ReceiveCall( unsigned int* Class,
00104 unsigned int* Method,
00105 char* Parameter,
00106 unsigned int ParameterSize );
00107 RioResult SendResult( const char* Result,
00108 const unsigned int ResultSize );
00109 RioResult SendCallError( const int Status );
00110 struct sockaddr_in m_RemoteAddress;
00111 bool isConnected();
00112
00113 };
00114
00115 #endif // __RIOTCP_H_