diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2017-03-24 22:47:18 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-24 22:47:18 +0100 |
commit | 296ece2c6a61630bfdb9b31dce46e5e2d1cda7cf (patch) | |
tree | 934c3f266fa8a76bab60f68a6ab825236b7e7959 /drivers/unix/packet_peer_udp_posix.cpp | |
parent | f1e05d31444f46906e8bd155c0dd7d7a9a018451 (diff) | |
parent | 0450e6539ea77f8c56b244aef27ec45c69e1dcdb (diff) |
Merge pull request #7985 from Faless/enet_godot_sock_squash
Update ENet to use Godot sockets.
Diffstat (limited to 'drivers/unix/packet_peer_udp_posix.cpp')
-rw-r--r-- | drivers/unix/packet_peer_udp_posix.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/drivers/unix/packet_peer_udp_posix.cpp b/drivers/unix/packet_peer_udp_posix.cpp index 98883f3267..e70e4fc349 100644 --- a/drivers/unix/packet_peer_udp_posix.cpp +++ b/drivers/unix/packet_peer_udp_posix.cpp @@ -107,10 +107,14 @@ Error PacketPeerUDPPosix::put_packet(const uint8_t *p_buffer, int p_buffer_size) errno = 0; int err; + _set_sock_blocking(blocking); + while ((err = sendto(sock, p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) { if (errno != EAGAIN) { return FAILED; + } else if (!blocking) { + return ERR_UNAVAILABLE; } } @@ -173,10 +177,12 @@ Error PacketPeerUDPPosix::_poll(bool p_wait) { return FAILED; } + _set_sock_blocking(p_wait); + struct sockaddr_storage from = { 0 }; socklen_t len = sizeof(struct sockaddr_storage); int ret; - while ((ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), p_wait ? 0 : MSG_DONTWAIT, (struct sockaddr *)&from, &len)) > 0) { + while ((ret = recvfrom(sockfd, recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) { uint32_t port = 0; @@ -243,9 +249,35 @@ int PacketPeerUDPPosix::_get_socket() { sockfd = _socket_create(sock_type, SOCK_DGRAM, IPPROTO_UDP); + if (sockfd != -1) + _set_sock_blocking(false); + return sockfd; } +void PacketPeerUDPPosix::_set_sock_blocking(bool p_blocking) { + + if (sock_blocking == p_blocking) + return; + + sock_blocking = p_blocking; + +#ifndef NO_FCNTL + int opts = fcntl(sockfd, F_GETFL); + int ret = 0; + if (sock_blocking) + ret = fcntl(sockfd, F_SETFL, opts & ~O_NONBLOCK); + else + ret = fcntl(sockfd, F_SETFL, opts | O_NONBLOCK); + if (ret == -1) + perror("setting non-block mode"); +#else + int bval = sock_blocking ? 0 : 1; + if (ioctl(sockfd, FIONBIO, &bval) == -1) + perror("setting non-block mode"); +#endif +} + void PacketPeerUDPPosix::set_dest_address(const IP_Address &p_address, int p_port) { peer_addr = p_address; @@ -264,6 +296,8 @@ void PacketPeerUDPPosix::make_default() { PacketPeerUDPPosix::PacketPeerUDPPosix() { + blocking = true; + sock_blocking = true; sockfd = -1; packet_port = 0; queue_count = 0; |