packet_peer_udp_posix.cpp 3.8 KB

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