PolySocket.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include "PolySocket.h"
  2. using namespace Polycode;
  3. Address::Address(string ipAsString, unsigned int port) {
  4. setAddress(ipAsString, port);
  5. }
  6. Address::Address(unsigned int ip, unsigned int port) {
  7. setAddress(ip, port);
  8. }
  9. Address::Address() {
  10. }
  11. void Address::setAddress(unsigned int ip, unsigned int port) {
  12. unsigned short destination_port = port;
  13. sockAddress.sin_family = AF_INET;
  14. sockAddress.sin_addr.s_addr = htonl(ip);
  15. sockAddress.sin_port = htons( destination_port );
  16. uintAddress = ip;
  17. this->port = port;
  18. }
  19. void Address::setAddress(string ipAsString, unsigned int port) {
  20. unsigned int a,b,c,d;
  21. vector<string> values = StringUtil::split(ipAsString, ".");
  22. if(values.size() == 4) {
  23. a = atoi(values[0].c_str());
  24. b = atoi(values[1].c_str());
  25. c = atoi(values[2].c_str());
  26. d = atoi(values[3].c_str());
  27. } else {
  28. Logger::log("Invalid IP address!\n");
  29. }
  30. unsigned int destination_address = ( a << 24 ) | ( b << 16 ) | ( c << 8 ) | d;
  31. unsigned short destination_port = port;
  32. sockAddress.sin_family = AF_INET;
  33. sockAddress.sin_addr.s_addr = htonl( destination_address );
  34. sockAddress.sin_port = htons( destination_port );
  35. uintAddress = destination_address;
  36. this->port = port;
  37. }
  38. Address::~Address() {
  39. }
  40. Socket::Socket(int port) : EventDispatcher() {
  41. sockId = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  42. if (sockId < 0) {
  43. socketError("Error creating socket");
  44. }
  45. sockaddr_in address;
  46. address.sin_family = AF_INET;
  47. address.sin_addr.s_addr = INADDR_ANY;
  48. address.sin_port = htons( (unsigned short) port );
  49. if( bind(sockId, (const sockaddr*) &address, sizeof(sockaddr_in) ) < 0 ) {
  50. socketError( "failed to bind socket");
  51. }
  52. #if PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
  53. int nonBlocking = 1;
  54. if ( fcntl( sockId, F_SETFL, O_NONBLOCK, nonBlocking ) == -1 ) {
  55. socketError( "failed to set non-blocking socket");
  56. }
  57. #elif PLATFORM == PLATFORM_WINDOWS
  58. DWORD nonBlocking = 1;
  59. if ( ioctlsocket( sockId, FIONBIO, &nonBlocking ) != 0 ) {
  60. socketError( "failed to set non-blocking socket");
  61. }
  62. #endif
  63. }
  64. bool Socket::sendData(const Address &address, char *data, unsigned int packetSize) {
  65. int sent_bytes = sendto(sockId, (const char*)data, packetSize, 0, (sockaddr*)&address.sockAddress, sizeof(sockaddr_in));
  66. if ( sent_bytes != packetSize ) {
  67. socketError("failed to send packet");
  68. return false;
  69. }
  70. return true;
  71. }
  72. int Socket::receiveData() {
  73. SocketEvent *event = new SocketEvent();
  74. sockaddr_in from;
  75. socklen_t fromLength = sizeof( from );
  76. int received_bytes = recvfrom( sockId, (char*)event->data, MAX_PACKET_SIZE,
  77. 0, (sockaddr*)&from, &fromLength );
  78. if ( received_bytes <= 0 ) {
  79. delete event;
  80. return received_bytes;
  81. }
  82. event->dataSize = received_bytes;
  83. event->fromAddress = Address(ntohl( from.sin_addr.s_addr ), ntohs( from.sin_port ));
  84. dispatchEvent(event, SocketEvent::EVENT_DATA_RECEIVED);
  85. return received_bytes;
  86. }
  87. Socket::~Socket() {
  88. #if PLATFORM == PLATFORM_MAC || PLATFORM == PLATFORM_UNIX
  89. close( sockId );
  90. #elif PLATFORM == PLATFORM_WINDOWS
  91. closesocket( sockId );
  92. #endif
  93. }
  94. void Socket::socketError(string error) {
  95. Logger::log("%s\n",error.c_str());
  96. }