2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* packet_peer_udp_winsock.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2014-11-12 15:23:23 +01:00
|
|
|
#include "packet_peer_udp_winsock.h"
|
|
|
|
|
|
|
|
#include <winsock2.h>
|
2016-10-20 12:04:10 +02:00
|
|
|
#include <ws2tcpip.h>
|
|
|
|
|
|
|
|
#include "drivers/unix/socket_helpers.h"
|
2014-11-12 15:23:23 +01:00
|
|
|
|
|
|
|
int PacketPeerUDPWinsock::get_available_packet_count() const {
|
|
|
|
|
2014-11-13 04:53:12 +01:00
|
|
|
Error err = const_cast<PacketPeerUDPWinsock*>(this)->_poll(false);
|
2014-11-12 15:23:23 +01:00
|
|
|
if (err!=OK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return queue_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error PacketPeerUDPWinsock::get_packet(const uint8_t **r_buffer,int &r_buffer_size) const{
|
|
|
|
|
2014-11-13 04:53:12 +01:00
|
|
|
Error err = const_cast<PacketPeerUDPWinsock*>(this)->_poll(false);
|
2014-11-12 15:23:23 +01:00
|
|
|
if (err!=OK)
|
|
|
|
return err;
|
|
|
|
if (queue_count==0)
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
|
|
|
|
uint32_t size;
|
2016-10-20 12:04:10 +02:00
|
|
|
uint8_t type;
|
|
|
|
rb.read(&type, 1, true);
|
|
|
|
if (type == IP_Address::TYPE_IPV4) {
|
|
|
|
rb.read((uint8_t*)&packet_ip.field8,4,true);
|
|
|
|
packet_ip.type = IP_Address::TYPE_IPV4;
|
|
|
|
} else {
|
|
|
|
rb.read((uint8_t*)&packet_ip.field8,16,true);
|
|
|
|
packet_ip.type = IP_Address::TYPE_IPV6;
|
|
|
|
};
|
2014-11-12 15:23:23 +01:00
|
|
|
rb.read((uint8_t*)&packet_port,4,true);
|
2014-11-13 04:53:12 +01:00
|
|
|
rb.read((uint8_t*)&size,4,true);
|
2014-11-12 15:23:23 +01:00
|
|
|
rb.read(packet_buffer,size,true);
|
|
|
|
--queue_count;
|
|
|
|
*r_buffer=packet_buffer;
|
|
|
|
r_buffer_size=size;
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer,int p_buffer_size){
|
|
|
|
|
2016-11-30 20:45:19 +01:00
|
|
|
int sock = _get_socket();
|
2014-11-12 15:23:23 +01:00
|
|
|
ERR_FAIL_COND_V( sock == -1, FAILED );
|
2016-10-20 12:04:10 +02:00
|
|
|
struct sockaddr_storage addr;
|
2016-12-01 04:23:57 +01:00
|
|
|
size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, ip_type);
|
2014-11-13 04:53:12 +01:00
|
|
|
|
|
|
|
_set_blocking(true);
|
|
|
|
|
2014-11-12 15:23:23 +01:00
|
|
|
errno = 0;
|
|
|
|
int err;
|
2016-10-22 07:23:07 +02:00
|
|
|
while ( (err = sendto(sock, (const char*)p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, addr_size)) != p_buffer_size) {
|
2014-11-12 15:23:23 +01:00
|
|
|
|
|
|
|
if (WSAGetLastError() != WSAEWOULDBLOCK) {
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PacketPeerUDPWinsock::get_max_packet_size() const{
|
|
|
|
|
|
|
|
return 512; // uhm maybe not
|
|
|
|
}
|
|
|
|
|
2014-11-13 04:53:12 +01:00
|
|
|
|
|
|
|
void PacketPeerUDPWinsock::_set_blocking(bool p_blocking) {
|
2014-11-17 11:46:11 +01:00
|
|
|
//am no windows expert
|
|
|
|
//hope this is the right thing
|
2014-11-13 04:53:12 +01:00
|
|
|
|
|
|
|
if (blocking==p_blocking)
|
|
|
|
return;
|
|
|
|
|
|
|
|
blocking=p_blocking;
|
|
|
|
unsigned long par = blocking?0:1;
|
|
|
|
if (ioctlsocket(sockfd, FIONBIO, &par)) {
|
|
|
|
perror("setting non-block mode");
|
|
|
|
//close();
|
|
|
|
//return -1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:45:19 +01:00
|
|
|
Error PacketPeerUDPWinsock::listen(int p_port, int p_recv_buffer_size) {
|
2014-11-12 15:23:23 +01:00
|
|
|
|
|
|
|
close();
|
2016-11-30 20:45:19 +01:00
|
|
|
int sock = _get_socket();
|
2014-11-12 15:23:23 +01:00
|
|
|
if (sock == -1 )
|
|
|
|
return ERR_CANT_CREATE;
|
2016-10-20 12:04:10 +02:00
|
|
|
|
|
|
|
struct sockaddr_storage addr = {0};
|
2016-11-30 20:45:19 +01:00
|
|
|
size_t addr_size = _set_listen_sockaddr(&addr, p_port, ip_type, NULL);
|
2016-10-20 12:04:10 +02:00
|
|
|
|
2016-10-22 07:23:07 +02:00
|
|
|
if (bind(sock, (struct sockaddr*)&addr, addr_size) == -1 ) {
|
2014-11-12 15:23:23 +01:00
|
|
|
close();
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
2014-11-13 04:53:12 +01:00
|
|
|
|
|
|
|
blocking=true;
|
|
|
|
|
2014-11-12 15:23:23 +01:00
|
|
|
printf("UDP Connection listening on port %i\n", p_port);
|
2014-11-13 04:53:12 +01:00
|
|
|
rb.resize(nearest_shift(p_recv_buffer_size));
|
2014-11-12 15:23:23 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PacketPeerUDPWinsock::close(){
|
|
|
|
|
|
|
|
if (sockfd != -1)
|
|
|
|
::closesocket(sockfd);
|
|
|
|
sockfd=-1;
|
|
|
|
rb.resize(8);
|
|
|
|
queue_count=0;
|
|
|
|
}
|
|
|
|
|
2014-11-13 04:53:12 +01:00
|
|
|
|
|
|
|
Error PacketPeerUDPWinsock::wait() {
|
|
|
|
|
|
|
|
return _poll(true);
|
|
|
|
}
|
|
|
|
Error PacketPeerUDPWinsock::_poll(bool p_wait) {
|
|
|
|
|
|
|
|
|
|
|
|
_set_blocking(p_wait);
|
|
|
|
|
2014-11-12 15:23:23 +01:00
|
|
|
|
2016-10-20 12:04:10 +02:00
|
|
|
struct sockaddr_storage from = {0};
|
|
|
|
int len = sizeof(struct sockaddr_storage);
|
2014-11-12 15:23:23 +01:00
|
|
|
int ret;
|
2015-11-19 19:06:01 +01:00
|
|
|
while ( (ret = recvfrom(sockfd, (char*)recv_buffer, MIN((int)sizeof(recv_buffer),MAX(rb.space_left()-12, 0)), 0, (struct sockaddr*)&from, &len)) > 0) {
|
2016-10-20 12:04:10 +02:00
|
|
|
|
|
|
|
uint32_t port = 0;
|
|
|
|
|
|
|
|
if (from.ss_family == AF_INET) {
|
|
|
|
uint8_t type = (uint8_t)IP_Address::TYPE_IPV4;
|
|
|
|
rb.write(&type, 1);
|
|
|
|
struct sockaddr_in* sin_from = (struct sockaddr_in*)&from;
|
|
|
|
rb.write((uint8_t*)&sin_from->sin_addr, 4);
|
2016-10-26 18:27:01 +02:00
|
|
|
port = ntohs(sin_from->sin_port);
|
2016-10-20 12:04:10 +02:00
|
|
|
|
|
|
|
} else if (from.ss_family == AF_INET6) {
|
|
|
|
|
|
|
|
uint8_t type = (uint8_t)IP_Address::TYPE_IPV6;
|
|
|
|
rb.write(&type, 1);
|
|
|
|
|
|
|
|
struct sockaddr_in6* s6_from = (struct sockaddr_in6*)&from;
|
|
|
|
rb.write((uint8_t*)&s6_from->sin6_addr, 16);
|
|
|
|
|
2016-10-26 18:27:01 +02:00
|
|
|
port = ntohs(s6_from->sin6_port);
|
2016-10-20 12:04:10 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// WARN_PRINT("Ignoring packet with unknown address family");
|
|
|
|
uint8_t type = (uint8_t)IP_Address::TYPE_NONE;
|
|
|
|
rb.write(&type, 1);
|
|
|
|
};
|
|
|
|
|
2014-11-12 15:23:23 +01:00
|
|
|
rb.write((uint8_t*)&port, 4);
|
|
|
|
rb.write((uint8_t*)&ret, 4);
|
|
|
|
rb.write(recv_buffer, ret);
|
|
|
|
|
2016-10-20 12:04:10 +02:00
|
|
|
len = sizeof(struct sockaddr_storage);
|
2014-11-12 15:23:23 +01:00
|
|
|
++queue_count;
|
|
|
|
};
|
|
|
|
|
2015-11-09 01:33:16 +01:00
|
|
|
if (ret == SOCKET_ERROR){
|
|
|
|
int error = WSAGetLastError();
|
|
|
|
|
|
|
|
if (error == WSAEWOULDBLOCK){
|
|
|
|
// Expected when doing non-blocking sockets, retry later.
|
|
|
|
}
|
|
|
|
else if (error == WSAECONNRESET){
|
|
|
|
// If the remote target does not accept messages, this error may occur, but is harmless.
|
|
|
|
// Once the remote target gets available, this message will disappear for new messages.
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 04:53:12 +01:00
|
|
|
|
2015-11-09 01:33:16 +01:00
|
|
|
if (ret == 0) {
|
2014-11-12 15:23:23 +01:00
|
|
|
close();
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
2014-11-13 04:53:12 +01:00
|
|
|
|
2014-11-12 15:23:23 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2014-11-13 04:53:12 +01:00
|
|
|
|
2014-11-12 15:23:23 +01:00
|
|
|
bool PacketPeerUDPWinsock::is_listening() const{
|
|
|
|
|
|
|
|
return sockfd!=-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
IP_Address PacketPeerUDPWinsock::get_packet_address() const {
|
|
|
|
|
|
|
|
return packet_ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
int PacketPeerUDPWinsock::get_packet_port() const{
|
|
|
|
|
|
|
|
return packet_port;
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:45:19 +01:00
|
|
|
int PacketPeerUDPWinsock::_get_socket() {
|
2014-11-12 15:23:23 +01:00
|
|
|
|
|
|
|
if (sockfd != -1)
|
|
|
|
return sockfd;
|
|
|
|
|
2016-11-30 20:45:19 +01:00
|
|
|
sockfd = _socket_create(ip_type, SOCK_DGRAM, IPPROTO_UDP);
|
2014-11-12 15:23:23 +01:00
|
|
|
|
|
|
|
return sockfd;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PacketPeerUDPWinsock::set_send_address(const IP_Address& p_address,int p_port) {
|
|
|
|
|
|
|
|
peer_addr=p_address;
|
|
|
|
peer_port=p_port;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PacketPeerUDPWinsock::make_default() {
|
|
|
|
|
|
|
|
PacketPeerUDP::_create = PacketPeerUDPWinsock::_create;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PacketPeerUDP* PacketPeerUDPWinsock::_create() {
|
|
|
|
|
|
|
|
return memnew(PacketPeerUDPWinsock);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PacketPeerUDPWinsock::PacketPeerUDPWinsock() {
|
|
|
|
|
|
|
|
sockfd=-1;
|
|
|
|
packet_port=0;
|
|
|
|
queue_count=0;
|
|
|
|
peer_port=0;
|
2016-11-30 20:45:19 +01:00
|
|
|
ip_type = IP_Address::TYPE_ANY;
|
2014-11-12 15:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PacketPeerUDPWinsock::~PacketPeerUDPWinsock() {
|
|
|
|
|
|
|
|
close();
|
|
|
|
}
|