2
0

UDPSocket.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <sys/socket.h>
  24. #include <sys/types.h>
  25. #include <netinet/in.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include "Assert.h"
  29. #include "Types.h"
  30. #include "OS.h"
  31. #include "UDPSocket.h"
  32. #include "NetAddress.h"
  33. namespace crown
  34. {
  35. namespace os
  36. {
  37. //-----------------------------------------------------------------------------
  38. UDPSocket::UDPSocket() :
  39. m_socket(0)
  40. {
  41. }
  42. //-----------------------------------------------------------------------------
  43. UDPSocket::~UDPSocket()
  44. {
  45. close();
  46. }
  47. //-----------------------------------------------------------------------------
  48. bool UDPSocket::open(uint16_t port)
  49. {
  50. CE_ASSERT(!is_open(), "Socket is already open");
  51. m_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  52. if (m_socket <= 0)
  53. {
  54. os::printf("Failed to create socket.\n");
  55. m_socket = 0;
  56. return false;
  57. }
  58. // Bind to port
  59. sockaddr_in address;
  60. address.sin_family = AF_INET;
  61. address.sin_addr.s_addr = INADDR_ANY;
  62. address.sin_port = htons(port);
  63. if (bind(m_socket, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0)
  64. {
  65. os::printf("Failed to bind socket\n");
  66. close();
  67. return false;
  68. }
  69. int non_blocking = 1;
  70. if (fcntl(m_socket, F_SETFL, O_NONBLOCK, non_blocking) == -1)
  71. {
  72. os::printf("Failed to set non-blocking socket\n");
  73. close();
  74. return false;
  75. }
  76. return true;
  77. }
  78. //-----------------------------------------------------------------------------
  79. bool UDPSocket::send(const NetAddress &receiver, const void* data, size_t size)
  80. {
  81. CE_ASSERT(data != NULL, "Data must be != NULL");
  82. sockaddr_in address;
  83. address.sin_family = AF_INET;
  84. address.sin_addr.s_addr = htonl(receiver.address());
  85. address.sin_port = htons(receiver.port());
  86. ssize_t sent_bytes = sendto(m_socket, (const char*) data, size, 0, (sockaddr*) &address, sizeof(sockaddr_in));
  87. if (sent_bytes < 0)
  88. {
  89. return false;
  90. }
  91. return (size_t) sent_bytes == size;
  92. }
  93. //-----------------------------------------------------------------------------
  94. size_t UDPSocket::receive(NetAddress& sender, void* data, size_t size)
  95. {
  96. CE_ASSERT(data != NULL, "Data must be != NULL");
  97. sockaddr_in from;
  98. socklen_t from_length = sizeof(from);
  99. ssize_t received_bytes = recvfrom(m_socket, (char*)data, size, 0, (sockaddr*)&from, &from_length);
  100. if (received_bytes <= 0)
  101. {
  102. return 0;
  103. }
  104. uint32_t address = ntohl(from.sin_addr.s_addr);
  105. uint16_t port = ntohs(from.sin_port);
  106. sender.set(address, port);
  107. return (size_t) received_bytes;
  108. }
  109. //-----------------------------------------------------------------------------
  110. void UDPSocket::close()
  111. {
  112. if (m_socket != 0)
  113. {
  114. ::close(m_socket);
  115. m_socket = 0;
  116. }
  117. }
  118. //-----------------------------------------------------------------------------
  119. bool UDPSocket::is_open()
  120. {
  121. return m_socket != 0;
  122. }
  123. } // namespace os
  124. } // namespace crown