packet_peer_udp_posix.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "packet_peer_udp_posix.h"
  2. #ifdef UNIX_ENABLED
  3. #include <errno.h>
  4. #include <unistd.h>
  5. #include <netdb.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <stdio.h>
  10. #ifndef NO_FCNTL
  11. #ifdef __HAIKU__
  12. #include <fcntl.h>
  13. #else
  14. #include <sys/fcntl.h>
  15. #endif
  16. #else
  17. #include <sys/ioctl.h>
  18. #endif
  19. #ifdef JAVASCRIPT_ENABLED
  20. #include <arpa/inet.h>
  21. #endif
  22. int PacketPeerUDPPosix::get_available_packet_count() const {
  23. Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false);
  24. if (err!=OK)
  25. return 0;
  26. return queue_count;
  27. }
  28. Error PacketPeerUDPPosix::get_packet(const uint8_t **r_buffer,int &r_buffer_size) const{
  29. Error err = const_cast<PacketPeerUDPPosix*>(this)->_poll(false);
  30. if (err!=OK)
  31. return err;
  32. if (queue_count==0)
  33. return ERR_UNAVAILABLE;
  34. uint32_t size;
  35. rb.read((uint8_t*)&packet_ip.host,4,true);
  36. rb.read((uint8_t*)&packet_port,4,true);
  37. rb.read((uint8_t*)&size,4,true);
  38. rb.read(packet_buffer,size,true);
  39. --queue_count;
  40. *r_buffer=packet_buffer;
  41. r_buffer_size=size;
  42. return OK;
  43. }
  44. Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer,int p_buffer_size){
  45. int sock = _get_socket();
  46. ERR_FAIL_COND_V( sock == -1, FAILED );
  47. struct sockaddr_in addr;
  48. addr.sin_family = AF_INET;
  49. addr.sin_port = htons(peer_port);
  50. addr.sin_addr = *((struct in_addr*)&peer_addr.host);
  51. errno = 0;
  52. int err;
  53. while ( (err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, sizeof(addr))) != p_buffer_size) {
  54. if (errno != EAGAIN) {
  55. return FAILED;
  56. }
  57. }
  58. return OK;
  59. }
  60. int PacketPeerUDPPosix::get_max_packet_size() const{
  61. return 512; // uhm maybe not
  62. }
  63. Error PacketPeerUDPPosix::listen(int p_port, int p_recv_buffer_size){
  64. close();
  65. int sock = _get_socket();
  66. if (sock == -1 )
  67. return ERR_CANT_CREATE;
  68. sockaddr_in addr = {0};
  69. addr.sin_family = AF_INET;
  70. addr.sin_port = htons(p_port);
  71. addr.sin_addr.s_addr = INADDR_ANY;
  72. if (bind(sock, (struct sockaddr*)&addr, sizeof(sockaddr_in)) == -1 ) {
  73. close();
  74. return ERR_UNAVAILABLE;
  75. }
  76. printf("UDP Connection listening on port %i bufsize %i \n", p_port,p_recv_buffer_size);
  77. rb.resize(nearest_shift(p_recv_buffer_size));
  78. return OK;
  79. }
  80. void PacketPeerUDPPosix::close(){
  81. if (sockfd != -1)
  82. ::close(sockfd);
  83. sockfd=-1;
  84. rb.resize(8);
  85. queue_count=0;
  86. }
  87. Error PacketPeerUDPPosix::wait() {
  88. return _poll(true);
  89. }
  90. Error PacketPeerUDPPosix::_poll(bool p_wait) {
  91. struct sockaddr_in from = {0};
  92. socklen_t len = sizeof(struct sockaddr_in);
  93. int ret;
  94. 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) {
  95. rb.write((uint8_t*)&from.sin_addr, 4);
  96. uint32_t port = ntohs(from.sin_port);
  97. rb.write((uint8_t*)&port, 4);
  98. rb.write((uint8_t*)&ret, 4);
  99. rb.write(recv_buffer, ret);
  100. len = sizeof(struct sockaddr_in);
  101. ++queue_count;
  102. };
  103. // TODO: Should ECONNRESET be handled here?
  104. if (ret == 0 || (ret == -1 && errno != EAGAIN) ) {
  105. close();
  106. return FAILED;
  107. };
  108. return OK;
  109. }
  110. bool PacketPeerUDPPosix::is_listening() const{
  111. return sockfd!=-1;
  112. }
  113. IP_Address PacketPeerUDPPosix::get_packet_address() const {
  114. return packet_ip;
  115. }
  116. int PacketPeerUDPPosix::get_packet_port() const{
  117. return packet_port;
  118. }
  119. int PacketPeerUDPPosix::_get_socket() {
  120. if (sockfd != -1)
  121. return sockfd;
  122. sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  123. ERR_FAIL_COND_V( sockfd == -1, -1 );
  124. //fcntl(sockfd, F_SETFL, O_NONBLOCK);
  125. return sockfd;
  126. }
  127. void PacketPeerUDPPosix::set_send_address(const IP_Address& p_address,int p_port) {
  128. peer_addr=p_address;
  129. peer_port=p_port;
  130. }
  131. PacketPeerUDP* PacketPeerUDPPosix::_create() {
  132. return memnew(PacketPeerUDPPosix);
  133. };
  134. void PacketPeerUDPPosix::make_default() {
  135. PacketPeerUDP::_create = PacketPeerUDPPosix::_create;
  136. };
  137. PacketPeerUDPPosix::PacketPeerUDPPosix() {
  138. sockfd=-1;
  139. packet_port=0;
  140. queue_count=0;
  141. peer_port=0;
  142. }
  143. PacketPeerUDPPosix::~PacketPeerUDPPosix() {
  144. close();
  145. }
  146. #endif