tcp.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /****************************************************************************\
  19. TCP Neal Kettler [email protected]
  20. \****************************************************************************/
  21. #ifndef TCP_HEADER
  22. #define TCP_HEADER
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <ctype.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <assert.h>
  29. #ifdef _WINDOWS
  30. #include <winsock.h>
  31. #include <io.h>
  32. #define close _close
  33. #define read _read
  34. #define write _write
  35. #else //UNIX
  36. #include <netdb.h>
  37. #include <sys/types.h>
  38. #include <sys/socket.h>
  39. #include <netinet/in.h>
  40. #include <arpa/inet.h>
  41. #include <unistd.h>
  42. #include <sys/time.h>
  43. #include <fcntl.h>
  44. #include <limits.h>
  45. typedef signed int SOCKET;
  46. #endif
  47. #ifdef AIX
  48. #include <sys/select.h>
  49. #endif
  50. #define DEFAULT_PROTOCOL 0
  51. #include "wlib/wstypes.h"
  52. #include "wlib/wdebug.h"
  53. #include "wlib/wtime.h"
  54. class TCP
  55. {
  56. // DATA ---------------
  57. private:
  58. int mode; // client or server
  59. sint32 fd; // the primary FD
  60. uint32 myIP; // after bind myIP & myPort will be
  61. uint16 myPort; // whatever we bound to
  62. struct sockaddr_in addr;
  63. int maxFD; // value of the biggest FD
  64. int clientCount; // how many clients open
  65. sint32 inputDelay; // default delay for semi-blocking reads
  66. sint32 outputDelay; // default delay for semi-blocking writes
  67. enum ConnectionState
  68. {
  69. CLOSED,
  70. CONNECTING,
  71. CONNECTED
  72. } connectionState; // What state is client FD in
  73. public:
  74. enum
  75. {
  76. CLIENT = 1,
  77. SERVER = 2
  78. };
  79. // These defines specify a system independent way to
  80. // get error codes for socket services.
  81. enum
  82. {
  83. OK, // Everything's cool
  84. UNKNOWN, // There was an error of unknown type
  85. ISCONN, // The socket is already connected
  86. INPROGRESS, // The socket is non-blocking and the operation
  87. // isn't done yet
  88. ALREADY, // The socket is already attempting a connection
  89. // but isn't done yet
  90. AGAIN, // Try again.
  91. ADDRINUSE, // Address already in use
  92. ADDRNOTAVAIL, // That address is not available on the remote host
  93. BADF, // Not a valid FD
  94. CONNREFUSED, // Connection was refused
  95. INTR, // Operation was interrupted
  96. NOTSOCK, // FD wasn't a socket
  97. PIPE, // That operation just made a SIGPIPE
  98. WOULDBLOCK, // That operation would block
  99. INVAL, // Invalid
  100. TIMEDOUT // Timeout
  101. };
  102. // for client list (if this is a server)
  103. fd_set clientList;
  104. // CODE ----------------
  105. public:
  106. TCP(int newMode);
  107. TCP(int newMode,sint16 socket);
  108. ~TCP();
  109. bit8 Bind(uint32 IP,uint16 port,bit8 reuseAddr=FALSE);
  110. bit8 Bind(char *Host,uint16 port,bit8 reuseAddr=FALSE);
  111. sint32 GetMaxFD(void);
  112. bit8 Connect(uint32 IP,uint16 port);
  113. bit8 Connect(char *Host,uint16 port);
  114. bit8 ConnectAsync(uint32 IP,uint16 port);
  115. bit8 ConnectAsync(char *Host,uint16 port);
  116. bit8 IsConnected(sint32 whichFD=0);
  117. sint32 GetFD(void);
  118. sint32 GetClientCount(void) { return(clientCount); }
  119. // Get IP or Port of a connected endpoint
  120. uint32 GetRemoteIP(sint32 whichFD=0);
  121. uint16 GetRemotePort(sint32 whichFD=0);
  122. sint32 GetConnection(void);
  123. sint32 GetConnection(struct sockaddr *clientAddr);
  124. void WaitWrite(sint32 whichFD=0);
  125. bit8 CanWrite(sint32 whichFD=0);
  126. sint32 Write(const uint8 *msg,uint32 len,sint32 whichFD=0);
  127. sint32 WriteNB(uint8 *msg,uint32 len,sint32 whichFD=0);
  128. sint32 EncapsulatedWrite(uint8 *msg,uint32 len,sint32 whichFD=0);
  129. sint32 WriteString(char *msg,sint32 whichFD=0);
  130. sint32 Printf(sint32 whichFD,const char *format,...);
  131. sint32 Read(uint8 *msg,uint32 len,sint32 whichFD=0);
  132. sint32 TimedRead(uint8 *msg,uint32 len,int seconds,sint32 whichFD=0);
  133. sint32 Peek(uint8 *msg,uint32 len,sint32 whichFD=0);
  134. sint32 EncapsulatedRead(uint8 *msg,uint32 len,sint32 whichFD=0);
  135. char *Gets(char *string,int n,int whichFD=0);
  136. // Wait on all sockets (or a specified one)
  137. // return when ready for reading (or timeout occurs)
  138. int Wait(sint32 sec,sint32 usec,fd_set &returnSet,sint32 whichFD=0);
  139. int Wait(sint32 sec,sint32 usec,fd_set &inputSet,fd_set &returnSet);
  140. int GetStatus(void);
  141. void ClearStatus(void);
  142. //sint32 GetSockStatus(sint32 whichFD=0);
  143. // give up ownership of the socket without closing it
  144. void DisownSocket(void);
  145. sint32 Close(sint32 whichFD=0);
  146. sint32 CloseAll(void); // close all sockets (same as close for client)
  147. sint32 SetBlocking(bit8 block,sint32 whichFD=0);
  148. // Set default delays for semi-blocking reads & writes
  149. // default input = 5, output = 5
  150. // this is new and not used everywhere
  151. //
  152. bit8 SetInputDelay(sint32 delay) { inputDelay=delay; return(TRUE); };
  153. bit8 SetOutputDelay(sint32 delay) { outputDelay=delay; return(TRUE); };
  154. };
  155. #endif