tcp.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //-----------------------------------------------------------------------------
  2. // ----------------
  3. // File ....: tcp.h
  4. // ----------------
  5. // Author...: Gus J Grubba
  6. // Date ....: September 1995
  7. // O.S. ....: Windows NT 3.51
  8. //
  9. // Note ....: Copyright 1991, 1995 Gus J Grubba
  10. //
  11. // History .: Sep, 03 1995 - Ported to C++ / WinNT
  12. //
  13. //-----------------------------------------------------------------------------
  14. #ifndef _TCPINCLUDE_
  15. #define _TCPINCLUDE_
  16. #include <winsock.h>
  17. //-- Constants ----------------------------------------------------------------
  18. #define MAXUDPLEN 512
  19. #define LOCALHOSTADDRESSH 0x0100007F //-- (Host Order)
  20. #define LOCALHOSTADDRESSN 0x7F000001 //-- (Network Order)
  21. //-----------------------------------------------------------------------------
  22. //-- Error Codes
  23. #define GCRES_SERVICEERROR 0x1000
  24. #define GCRES_GETHOSTERROR 0x1001
  25. #define GCRES_CANNOTCREATESOCKET 0x1002
  26. #define GCRES_CANNOTCONNECT 0x1003
  27. #define GCRES_BINDERROR 0x1004
  28. #define GCRES_CANNOTSERVE 0x1005
  29. #define GCRES_DISCONNECTED 0x1006
  30. #define GCRES_READERROR 0x1007
  31. #define GCRES_INVALIDSERVERTHREAD 0x1008
  32. #define GCRES_INVALIDPORT 0x1009
  33. #define GCRES_NOTINITIALIZED 0x100A
  34. #define GCRES_TOOBIG 0x100B
  35. #define GCRES_WRITEERROR 0x100C
  36. #define GCRES_TIMEOUT 0x100D
  37. //-- Forward References ------------------------------------------------------
  38. class ConnectionInfo;
  39. class TCPcomm;
  40. //-----------------------------------------------------------------------------
  41. //-- Server Thread
  42. typedef void (WINAPI *SERVER_THREAD)(
  43. ConnectionInfo *ci
  44. );
  45. typedef struct tag_tcpSRV {
  46. ConnectionInfo *ci;
  47. TCPcomm *tcp;
  48. } tcpSRV;
  49. //-----------------------------------------------------------------------------
  50. //-- Connection data
  51. class ConnectionInfo {
  52. //-- All kept in host order
  53. void *ptr, *param; //-- Generic Pointers
  54. BOOL blocking;
  55. SOCKET c_sock; //-- The "client" socket
  56. DWORD c_address; //-- The "client" inet address
  57. char c_name[MAX_PATH]; //-- The "client" hostname
  58. WORD c_port; //-- The "client" port
  59. SOCKET s_sock; //-- The "server" socket
  60. DWORD s_address; //-- The "server" inet address
  61. char s_name[MAX_PATH]; //-- The "server" hostname
  62. WORD s_port; //-- The "server" port
  63. DWORD bytes_s, bytes_r;
  64. public:
  65. GCOMMEXPORT ConnectionInfo ( ) { Reset(); }
  66. GCOMMEXPORT void Reset( ) {
  67. ptr = NULL;
  68. param = NULL;
  69. bytes_s = 0;
  70. bytes_r = 0;
  71. ResetClient();
  72. ResetServer();
  73. }
  74. GCOMMEXPORT void ResetClient ( ) {
  75. c_sock = INVALID_SOCKET;
  76. c_address = 0;
  77. c_name[0] = 0;
  78. }
  79. GCOMMEXPORT void ResetServer ( ) {
  80. s_sock = INVALID_SOCKET;
  81. s_address = 0;
  82. s_name[0] = 0;
  83. }
  84. GCOMMEXPORT void SetClientSocket ( SOCKET s) { c_sock = s; }
  85. GCOMMEXPORT void SetClientAddress ( DWORD a) { c_address = a; }
  86. GCOMMEXPORT void SetClientName ( char *n) { strcpy(c_name,n); }
  87. GCOMMEXPORT void SetClientPort ( WORD p) { c_port = p; }
  88. GCOMMEXPORT void SetServerSocket ( SOCKET s) { s_sock = s; }
  89. GCOMMEXPORT void SetServerAddress ( DWORD a) { s_address = a; }
  90. GCOMMEXPORT void SetServerName ( char *n) { strcpy(s_name,n); }
  91. GCOMMEXPORT void SetServerPort ( WORD p) { s_port = p; }
  92. GCOMMEXPORT SOCKET ClientSocket ( ) { return c_sock; }
  93. GCOMMEXPORT DWORD ClientAddress ( ) { return c_address; }
  94. GCOMMEXPORT WORD ClientPort ( ) { return c_port; }
  95. GCOMMEXPORT char *ClientName ( ) { return c_name; }
  96. GCOMMEXPORT SOCKET ServerSocket ( ) { return s_sock; }
  97. GCOMMEXPORT WORD ServerPort ( ) { return s_port; }
  98. GCOMMEXPORT DWORD ServerAddress ( ) { return s_address; }
  99. GCOMMEXPORT char *ServerName ( ) { return s_name; }
  100. GCOMMEXPORT void AddToBytesSent ( DWORD b ) { bytes_s += b; }
  101. GCOMMEXPORT void AddToBytesReceived ( DWORD b ) { bytes_r += b; }
  102. GCOMMEXPORT DWORD BytesSent ( ) { return bytes_s; }
  103. GCOMMEXPORT DWORD BytesReceived ( ) { return bytes_r; }
  104. GCOMMEXPORT void ResetBytesSent ( ) { bytes_s = 0; }
  105. GCOMMEXPORT void ResetBytesReceived ( ) { bytes_r = 0; }
  106. GCOMMEXPORT void *Ptr ( ) { return ptr; }
  107. GCOMMEXPORT void SetPtr ( void * p ) { ptr = p; }
  108. GCOMMEXPORT void *Param ( ) { return param; }
  109. GCOMMEXPORT void SetParam ( void * p ) { param = p; }
  110. GCOMMEXPORT void SetBlocking ( BOOL b ) { blocking = b; }
  111. GCOMMEXPORT BOOL Blocking ( ) { return blocking; }
  112. };
  113. //-----------------------------------------------------------------------------
  114. //-- BSD Socket Class Definition ---------------------------------------------
  115. //-----------------------------------------------------------------------------
  116. // #> TCPcomm
  117. //
  118. class TCPcomm : public tcCOMM {
  119. private:
  120. BOOL initialized;
  121. WSADATA WSAData;
  122. SOCKET sSocket;
  123. BOOL HandleStandardErrors ( int err, TCHAR *msg );
  124. public:
  125. GCOMMEXPORT TCPcomm ( );
  126. GCOMMEXPORT ~TCPcomm ( );
  127. //-- House Keeping --------------------------------
  128. GCOMMEXPORT BOOL Init ( HWND hWnd );
  129. GCOMMEXPORT BOOL Setup ( void * ) { return TRUE; }
  130. GCOMMEXPORT void Close ( );
  131. GCOMMEXPORT BOOL SaveSession ( void *ptr ) { return TRUE; }
  132. GCOMMEXPORT BOOL LoadSession ( void *ptr ) { return TRUE; }
  133. GCOMMEXPORT DWORD EvaluateDataSize ( ) { return 0; }
  134. //-- Helpers --------------------------------------
  135. GCOMMEXPORT GCRES GetHostAddress ( DWORD *addr, char *name, char *fullname = NULL );
  136. //-- TCP Transport --------------------------------
  137. GCOMMEXPORT GCRES Serve ( WORD port, SERVER_THREAD func, void *param );
  138. GCOMMEXPORT void StopServer ( );
  139. GCOMMEXPORT GCRES Send ( ConnectionInfo *ci, void *buf, int len, float timeout = 5.0f );
  140. GCOMMEXPORT GCRES Receive ( ConnectionInfo *ci, void *buf, int len, float timeout = 5.0f );
  141. GCOMMEXPORT GCRES rlogin ( ConnectionInfo *ci );
  142. GCOMMEXPORT GCRES Connect ( ConnectionInfo *ci );
  143. GCOMMEXPORT void Disconnect ( ConnectionInfo *ci );
  144. //-- UDP Transport --------------------------------
  145. #define UDP_BLOCK 1
  146. #define UDP_NONBLOCK 0
  147. GCOMMEXPORT GCRES CreateUDPServer ( ConnectionInfo *ci, BOOL block = UDP_NONBLOCK );
  148. GCOMMEXPORT GCRES CloseUDPServer ( ConnectionInfo *ci );
  149. GCOMMEXPORT GCRES SendUDP ( ConnectionInfo *ci, void *buf, int len, int *written, float timeout = 5.0f);
  150. GCOMMEXPORT GCRES ReceiveUDP ( ConnectionInfo *ci, void *buf, int len, int *read, float timeout = 5.0f);
  151. };
  152. #endif
  153. //-- EOF: tcp.h ---------------------------------------------------------------