PolySocket.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 400
  24. // if set to 1, will create a thread for each network socket
  25. #define USE_THREADED_SOCKETS 0
  26. // Socket poll interval time in msecs
  27. #define SOCKET_POLL_INTERVAL 5
  28. #define PACKET_TYPE_USERDATA 0
  29. #define PACKET_TYPE_SETCLIENT_ID 1
  30. #define PACKET_TYPE_CLIENT_READY 2
  31. #define PACKET_TYPE_DISONNECT 3
  32. #define PACKET_TYPE_CLIENT_DATA 4
  33. #define PACKET_TYPE_SERVER_DATA 5
  34. #if PLATFORM == PLATFORM_WINDOWS
  35. #include <winsock2.h>
  36. #elif PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
  37. #include <sys/socket.h>
  38. #include <sys/types.h>
  39. #include <netinet/in.h>
  40. #include <fcntl.h>
  41. #endif
  42. #include "PolyEventDispatcher.h"
  43. #if PLATFORM == PLATFORM_WINDOWS
  44. typedef int socklen_t;
  45. #endif
  46. namespace Polycode {
  47. class _PolyExport Address {
  48. public:
  49. Address(String ipAsString, unsigned int port);
  50. Address(unsigned int ip, unsigned int port);
  51. Address();
  52. ~Address();
  53. inline void operator = (const Address &add2) {
  54. setAddress(add2.uintAddress, add2.port);
  55. }
  56. inline bool operator == ( const Address& add2) {
  57. return (uintAddress == add2.uintAddress && port == add2.port);
  58. }
  59. void setAddress(String ipAsString, unsigned int port);
  60. void setAddress(unsigned int ip, unsigned int port);
  61. unsigned int uintAddress;
  62. unsigned int port;
  63. sockaddr_in sockAddress;
  64. };
  65. class _PolyExport SocketEvent : public Event {
  66. public:
  67. SocketEvent(){}
  68. ~SocketEvent(){}
  69. char data[MAX_PACKET_SIZE];
  70. unsigned int dataSize;
  71. Address fromAddress;
  72. static const int EVENT_ERROR = 0;
  73. static const int EVENT_DATA_RECEIVED = 1;
  74. };
  75. class _PolyExport Socket : public EventDispatcher {
  76. public:
  77. Socket(int port);
  78. ~Socket();
  79. int receiveData();
  80. bool sendData(const Address &address, char *data, unsigned int packetSize);
  81. void socketError(String error);
  82. private:
  83. int sockId;
  84. };
  85. }