PolySocket.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyGlobals.h"
  21. #include "PolyEvent.h"
  22. #include "stdio.h"
  23. #define MAX_PACKET_SIZE 1400
  24. // if set to 1, will create a thread for each network socket
  25. // DO NOT USE FOR PRODUCTION
  26. #define USE_THREADED_SOCKETS 0
  27. // Socket poll interval time in msecs
  28. #define SOCKET_POLL_INTERVAL 5
  29. #define PACKET_TYPE_USERDATA 0
  30. #define PACKET_TYPE_SETCLIENT_ID 1
  31. #define PACKET_TYPE_CLIENT_READY 2
  32. #define PACKET_TYPE_DISONNECT 3
  33. #define PACKET_TYPE_CLIENT_DATA 4
  34. #define PACKET_TYPE_SERVER_DATA 5
  35. #if PLATFORM == PLATFORM_WINDOWS
  36. #include <winsock2.h>
  37. #elif PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
  38. #include <sys/socket.h>
  39. #include <sys/types.h>
  40. #include <netinet/in.h>
  41. #include <fcntl.h>
  42. #endif
  43. #include "PolyEventDispatcher.h"
  44. #if PLATFORM == PLATFORM_WINDOWS
  45. typedef int socklen_t;
  46. #endif
  47. namespace Polycode {
  48. /**
  49. * A typical network address, defined by IP and port.
  50. */
  51. class _PolyExport Address {
  52. public:
  53. /**
  54. * Constructor.
  55. * @param ipAsString An IP address represented as string,
  56. * for example "127.0.0.1"
  57. * @param port The UDP/TCP port of the address, given in
  58. * host byte order.
  59. */
  60. Address(String ipAsString, unsigned int port);
  61. /**
  62. * Constructor.
  63. * @param ip An IP address given as integer in host byte
  64. * order.
  65. * @param port The UDP/TCP port of the address, given in
  66. * host byte order.
  67. */
  68. Address(unsigned int ip, unsigned int port);
  69. /**
  70. * Constructor, leaving the address uninitalized.
  71. *
  72. * Address IP and port will default to 0.
  73. */
  74. Address();
  75. ~Address();
  76. /**
  77. * Copy the address IP and port from add2 into this.
  78. * @param add2 The address to copy into this.
  79. */
  80. inline void operator = (const Address &add2) {
  81. setAddress(add2.uintAddress, add2.port);
  82. }
  83. /**
  84. * @return 1 if the address IP and port match, 0 otherwise.
  85. */
  86. inline bool operator == ( const Address& add2) {
  87. return (uintAddress == add2.uintAddress && port == add2.port);
  88. }
  89. /**
  90. * Update the address IP and port.
  91. * @param ipAsString An IP address represented as string,
  92. * for example "197.0.0.1"
  93. * @param port The UDP/TCP port of the address, given in
  94. * host byte order.
  95. */
  96. void setAddress(String ipAsString, unsigned int port);
  97. /**
  98. * Update the address IP and port.
  99. * @param ip An IP address given as integer in host byte
  100. * order.
  101. * @param port The UDP/TCP port of the address, given in
  102. * host byte order.
  103. */
  104. void setAddress(unsigned int ip, unsigned int port);
  105. // TODO: A way to get the IP/port without exposing internal members.
  106. // The problem here is that the IP internally is converted to
  107. // network byte order, but the API seems to work with host byte
  108. // order, so I'm unsure in which byte order the IP/port should
  109. // be returned.
  110. protected:
  111. unsigned int uintAddress;
  112. unsigned int port;
  113. sockaddr_in sockAddress;
  114. // This class already uses socket API structs internally,
  115. // so it's no far stretch to make it tied to the Socket class.
  116. friend class Socket;
  117. };
  118. class _PolyExport SocketEvent : public Event {
  119. public:
  120. SocketEvent(){}
  121. ~SocketEvent(){}
  122. char data[MAX_PACKET_SIZE];
  123. unsigned int dataSize;
  124. Address fromAddress;
  125. static const int EVENTBASE_SOCKETEVENT = 0x500;
  126. static const int EVENT_ERROR = EVENTBASE_SOCKETEVENT+0;
  127. static const int EVENT_DATA_RECEIVED = EVENTBASE_SOCKETEVENT+1;
  128. };
  129. class _PolyExport Socket : public EventDispatcher {
  130. public:
  131. Socket(int port);
  132. ~Socket();
  133. int receiveData();
  134. bool sendData(const Address &address, char *data, unsigned int packetSize);
  135. void socketError(String error);
  136. private:
  137. int sockId;
  138. };
  139. }