udp.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. ** Command & Conquer Generals(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. #ifndef UDP_HEADER
  19. #define UDP_HEADER
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #ifdef _WINDOWS
  26. #include <winsock.h>
  27. #include <io.h>
  28. #define close _close
  29. #define read _read
  30. #define write _write
  31. #else //UNIX
  32. #include <netdb.h>
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <netinet/in.h>
  36. #include <arpa/inet.h>
  37. #include <unistd.h>
  38. #include <sys/time.h>
  39. #include <fcntl.h>
  40. #include <limits.h>
  41. #endif
  42. #ifdef AIX
  43. #include <sys/select.h>
  44. #endif
  45. #define DEFAULT_PROTOCOL 0
  46. #include <wlib/wstypes.h>
  47. #include <wlib/wtime.h>
  48. class UDP
  49. {
  50. // DATA
  51. private:
  52. sint32 fd;
  53. uint32 myIP;
  54. uint16 myPort;
  55. struct sockaddr_in addr;
  56. // These defines specify a system independent way to
  57. // get error codes for socket services.
  58. enum sockStat
  59. {
  60. OK = 0, // Everything's cool
  61. UNKNOWN = -1, // There was an error of unknown type
  62. ISCONN = -2, // The socket is already connected
  63. INPROGRESS = -3, // The socket is non-blocking and the operation
  64. // isn't done yet
  65. ALREADY = -4, // The socket is already attempting a connection
  66. // but isn't done yet
  67. AGAIN = -5, // Try again.
  68. ADDRINUSE = -6, // Address already in use
  69. ADDRNOTAVAIL = -7, // That address is not available on the remote host
  70. BADF = -8, // Not a valid FD
  71. CONNREFUSED = -9, // Connection was refused
  72. INTR =-10, // Operation was interrupted
  73. NOTSOCK =-11, // FD wasn't a socket
  74. PIPE =-12, // That operation just made a SIGPIPE
  75. WOULDBLOCK =-13, // That operation would block
  76. INVAL =-14, // Invalid
  77. TIMEDOUT =-15 // Timeout
  78. };
  79. // CODE
  80. private:
  81. sint32 SetBlocking(bit8 block);
  82. public:
  83. UDP();
  84. ~UDP();
  85. sint32 Bind(uint32 IP,uint16 port);
  86. sint32 Bind(char *Host,uint16 port);
  87. sint32 Write(uint8 *msg,uint32 len,uint32 IP,uint16 port);
  88. sint32 Read(uint8 *msg,uint32 len,sockaddr_in *from);
  89. sockStat GetStatus(void);
  90. void ClearStatus(void);
  91. int Wait(sint32 sec,sint32 usec,fd_set &returnSet);
  92. int Wait(sint32 sec,sint32 usec,fd_set &givenSet,fd_set &returnSet);
  93. bit8 getLocalAddr(uint32 &ip, uint16 &port);
  94. sint32 getFD(void) { return(fd); }
  95. bit8 SetInputBuffer(uint32 bytes);
  96. bit8 SetOutputBuffer(uint32 bytes);
  97. int GetInputBuffer(void);
  98. int GetOutputBuffer(void);
  99. };
  100. #endif