udp.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. #pragma once
  24. #ifndef UDP_HEADER
  25. #define UDP_HEADER
  26. #ifdef _UNIX
  27. #include <errno.h>
  28. #endif
  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. #endif
  46. #ifdef AIX
  47. #include <sys/select.h>
  48. #endif
  49. #include "Lib/BaseType.h"
  50. #define DEFAULT_PROTOCOL 0
  51. //#include "wlib/wstypes.h"
  52. //#include "wlib/wtime.h"
  53. class UDP
  54. {
  55. // DATA
  56. private:
  57. Int fd;
  58. UnsignedInt myIP;
  59. UnsignedShort myPort;
  60. struct sockaddr_in addr;
  61. public:
  62. // These defines specify a system independent way to
  63. // get error codes for socket services.
  64. enum sockStat
  65. {
  66. OK = 0, // Everything's cool
  67. UNKNOWN = -1, // There was an error of unknown type
  68. ISCONN = -2, // The socket is already connected
  69. INPROGRESS = -3, // The socket is non-blocking and the operation
  70. // isn't done yet
  71. ALREADY = -4, // The socket is already attempting a connection
  72. // but isn't done yet
  73. AGAIN = -5, // Try again.
  74. ADDRINUSE = -6, // Address already in use
  75. ADDRNOTAVAIL = -7, // That address is not available on the remote host
  76. BADF = -8, // Not a valid FD
  77. CONNREFUSED = -9, // Connection was refused
  78. INTR =-10, // Operation was interrupted
  79. NOTSOCK =-11, // FD wasn't a socket
  80. PIPE =-12, // That operation just made a SIGPIPE
  81. WOULDBLOCK =-13, // That operation would block
  82. INVAL =-14, // Invalid
  83. TIMEDOUT =-15 // Timeout
  84. };
  85. // CODE
  86. private:
  87. Int SetBlocking(Int block);
  88. Int m_lastError;
  89. public:
  90. UDP();
  91. ~UDP();
  92. Int Bind(UnsignedInt IP,UnsignedShort port);
  93. Int Bind(const char *Host,UnsignedShort port);
  94. Int Write(const unsigned char *msg,UnsignedInt len,UnsignedInt IP,UnsignedShort port);
  95. Int Read(unsigned char *msg,UnsignedInt len,sockaddr_in *from);
  96. sockStat GetStatus(void);
  97. void ClearStatus(void);
  98. //int Wait(Int sec,Int usec,fd_set &returnSet);
  99. //int Wait(Int sec,Int usec,fd_set &givenSet,fd_set &returnSet);
  100. Int getLocalAddr(UnsignedInt &ip, UnsignedShort &port);
  101. Int getFD(void) { return(fd); }
  102. Int SetInputBuffer(UnsignedInt bytes);
  103. Int SetOutputBuffer(UnsignedInt bytes);
  104. int GetInputBuffer(void);
  105. int GetOutputBuffer(void);
  106. Int AllowBroadcasts(Bool status);
  107. };
  108. #ifdef DEBUG_LOGGING
  109. AsciiString GetWSAErrorString( Int error );
  110. #endif
  111. #endif