PolySocket.cpp 4.4 KB

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