packet_peer_udp_posix.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*************************************************************************/
  2. /* packet_peer_udp_posix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "packet_peer_udp_posix.h"
  30. #ifdef UNIX_ENABLED
  31. #include <errno.h>
  32. #include <unistd.h>
  33. #include <netdb.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <netinet/in.h>
  37. #include <stdio.h>
  38. #ifndef NO_FCNTL
  39. #ifdef __HAIKU__
  40. #include <fcntl.h>
  41. #else
  42. #include <sys/fcntl.h>
  43. #endif
  44. #else
  45. #include <sys/ioctl.h>
  46. #endif
  47. #ifdef JAVASCRIPT_ENABLED
  48. #include <arpa/inet.h>
  49. #endif
  50. int PacketPeerUDPPosix::get_available_packet_count() const {
  51. Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false);
  52. if (err!=OK)
  53. return 0;
  54. return queue_count;
  55. }
  56. Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size) const{
  57. Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false);
  58. if (err!=OK)
  59. return err;
  60. if (queue_count==0)
  61. return ERR_UNAVAILABLE;
  62. uint32_t size;
  63. rb.read((uint8_t*)&packet_ip.host,4,true);
  64. rb.read((uint8_t*)&packet_port,4,true);
  65. rb.read((uint8_t*)&size,4,true);
  66. rb.read(packet_buffer,size,true);
  67. --queue_count;
  68. *r_buffer=packet_buffer;
  69. r_buffer_size=size;
  70. return OK;
  71. }
  72. Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
  73. int sock = _get_socket();
  74. ERR_FAIL_COND_V( sock == -1, FAILED );
  75. struct sockaddr_in addr;
  76. addr.sin_family = AF_INET;
  77. addr.sin_port = htons(peer_port);
  78. addr.sin_addr = *((struct in_addr*)&peer_addr.host);
  79. errno = 0;
  80. int err;
  81. while ( (err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, sizeof(addr))) != p_buffer_size) {
  82. if (errno != EAGAIN) {
  83. return FAILED;
  84. }
  85. }
  86. return OK;
  87. }
  88. int PacketPeerUDPPosix::get_max_packet_size() const{
  89. return 512; // uhm maybe not
  90. }
  91. Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size){
  92. close();
  93. int sock = _get_socket();
  94. if (sock == -1 )
  95. return ERR_CANT_CREATE;
  96. sockaddr_in addr = {0};
  97. addr.sin_family = AF_INET;
  98. addr.sin_port = htons(p_port);
  99. addr.sin_addr.s_addr = INADDR_ANY;
  100. if (bind(sock, (struct sockaddr*)&addr, sizeof(sockaddr_in)) == -1 ) {
  101. close();
  102. return ERR_UNAVAILABLE;
  103. }
  104. rb.resize(nearest_shift(p_recv_buffer_size));
  105. return OK;
  106. }
  107. void PacketPeerUDPPosix::close(){
  108. if (sockfd != -1)
  109. ::close(sockfd);
  110. sockfd=-1;
  111. rb.resize(8);
  112. queue_count=0;
  113. }
  114. Error PacketPeerUDPPosix::wait() {
  115. return _poll(true);
  116. }
  117. Error PacketPeerUDPPosix::_poll(bool p_wait) {
  118. struct sockaddr_in from = {0};
  119. socklen_t len = sizeof(struct sockaddr_in);
  120. int ret;
  121. while ( (ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-12, 0)), p_wait?0:MSG_DONTWAIT, (struct sockaddr*)&from, &len)) > 0) {
  122. rb.write((uint8_t*)&from.sin_addr, 4);
  123. uint32_t port = ntohs(from.sin_port);
  124. rb.write((uint8_t*)&port, 4);
  125. rb.write((uint8_t*)&ret, 4);
  126. rb.write(recv_buffer, ret);
  127. len = sizeof(struct sockaddr_in);
  128. ++queue_count;
  129. };
  130. // TODO: Should ECONNRESET be handled here?
  131. if (ret == 0 || (ret == -1 && errno != EAGAIN) ) {
  132. close();
  133. return FAILED;
  134. };
  135. return OK;
  136. }
  137. bool PacketPeerUDPPosix::is_listening() const{
  138. return sockfd!=-1;
  139. }
  140. IP_Address PacketPeerUDPPosix::get_packet_address() const {
  141. return packet_ip;
  142. }
  143. int PacketPeerUDPPosix::get_packet_port() const{
  144. return packet_port;
  145. }
  146. int PacketPeerUDPPosix::_get_socket() {
  147. if (sockfd != -1)
  148. return sockfd;
  149. sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  150. ERR_FAIL_COND_V( sockfd == -1, -1 );
  151. //fcntl(sockfd, F_SETFL, O_NONBLOCK);
  152. return sockfd;
  153. }
  154. void PacketPeerUDPPosix::set_send_address(const IP_Address& p_address,int p_port) {
  155. peer_addr=p_address;
  156. peer_port=p_port;
  157. }
  158. PacketPeerUDP* PacketPeerUDPPosix::_create() {
  159. return memnew(PacketPeerUDPPosix);
  160. };
  161. void PacketPeerUDPPosix::make_default() {
  162. PacketPeerUDP::_create = PacketPeerUDPPosix::_create;
  163. };
  164. PacketPeerUDPPosix::PacketPeerUDPPosix() {
  165. sockfd=-1;
  166. packet_port=0;
  167. queue_count=0;
  168. peer_port=0;
  169. }
  170. PacketPeerUDPPosix::~PacketPeerUDPPosix() {
  171. close();
  172. }
  173. #endif