2
0

PolySocket.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // @package Network
  2. #pragma once
  3. #include "PolyGlobals.h"
  4. #include "PolyUtil.h"
  5. #include "stdio.h"
  6. #if PLATFORM == PLATFORM_WINDOWS
  7. #include <winsock2.h>
  8. #elif PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
  9. #include <sys/socket.h>
  10. #include <sys/types.h>
  11. #include <netinet/in.h>
  12. #include <fcntl.h>
  13. #endif
  14. #include <string>
  15. #include "PolyEventDispatcher.h"
  16. using std::string;
  17. #if PLATFORM == PLATFORM_WINDOWS
  18. typedef int socklen_t;
  19. #endif
  20. namespace Polycode {
  21. class _PolyExport Address {
  22. public:
  23. Address(string ipAsString, unsigned int port);
  24. Address(unsigned int ip, unsigned int port);
  25. Address();
  26. ~Address();
  27. inline void operator = (const Address &add2) {
  28. setAddress(add2.uintAddress, add2.port);
  29. }
  30. inline bool operator == ( const Address& add2) {
  31. return (uintAddress == add2.uintAddress && port == add2.port);
  32. }
  33. void setAddress(string ipAsString, unsigned int port);
  34. void setAddress(unsigned int ip, unsigned int port);
  35. unsigned int uintAddress;
  36. unsigned int port;
  37. sockaddr_in sockAddress;
  38. };
  39. class _PolyExport SocketEvent : public Event {
  40. public:
  41. SocketEvent(){}
  42. ~SocketEvent(){}
  43. char data[MAX_PACKET_SIZE];
  44. unsigned int dataSize;
  45. Address fromAddress;
  46. static const int EVENT_ERROR = 0;
  47. static const int EVENT_DATA_RECEIVED = 1;
  48. };
  49. class _PolyExport Socket : public EventDispatcher {
  50. public:
  51. Socket(int port);
  52. ~Socket();
  53. int receiveData();
  54. bool sendData(const Address &address, char *data, unsigned int packetSize);
  55. void socketError(string error);
  56. private:
  57. int sockId;
  58. };
  59. }