(svn r15971) -Codechange: make it possible for UDP socket handlers to bind to multiple sockets.
This commit is contained in:
@@ -37,12 +37,9 @@ struct Packet;
|
||||
*/
|
||||
class NetworkSocketHandler {
|
||||
bool has_quit; ///< Whether the current client has quit/send a bad packet
|
||||
public:
|
||||
/* TODO: make socket protected once the TCP stuff is in a real class too */
|
||||
SOCKET sock; ///< The socket currently connected to
|
||||
public:
|
||||
/** Create a new unbound socket */
|
||||
NetworkSocketHandler(SOCKET s = INVALID_SOCKET) { this->sock = s; this->has_quit = false; }
|
||||
NetworkSocketHandler() { this->has_quit = false; }
|
||||
|
||||
/** Close the socket when distructing the socket handler */
|
||||
virtual ~NetworkSocketHandler() { this->Close(); }
|
||||
@@ -57,12 +54,6 @@ public:
|
||||
*/
|
||||
virtual NetworkRecvStatus CloseConnection() { this->has_quit = true; return NETWORK_RECV_STATUS_OKAY; }
|
||||
|
||||
/**
|
||||
* Whether this socket is currently bound to a socket.
|
||||
* @return true when the socket is bound, false otherwise
|
||||
*/
|
||||
bool IsConnected() const { return this->sock != INVALID_SOCKET; }
|
||||
|
||||
/**
|
||||
* Whether the current client connected to the socket has quit.
|
||||
* In the case of UDP, for example, once a client quits (send bad
|
||||
|
@@ -13,8 +13,9 @@
|
||||
#include "tcp.h"
|
||||
|
||||
NetworkTCPSocketHandler::NetworkTCPSocketHandler(SOCKET s) :
|
||||
NetworkSocketHandler(s),
|
||||
packet_queue(NULL), packet_recv(NULL), writable(false)
|
||||
NetworkSocketHandler(),
|
||||
packet_queue(NULL), packet_recv(NULL),
|
||||
sock(s), writable(false)
|
||||
{
|
||||
}
|
||||
|
||||
|
@@ -20,8 +20,15 @@ private:
|
||||
Packet *packet_queue; ///< Packets that are awaiting delivery
|
||||
Packet *packet_recv; ///< Partially received packet
|
||||
public:
|
||||
SOCKET sock; ///< The socket currently connected to
|
||||
bool writable; ///< Can we write to this socket?
|
||||
|
||||
/**
|
||||
* Whether this socket is currently bound to a socket.
|
||||
* @return true when the socket is bound, false otherwise
|
||||
*/
|
||||
bool IsConnected() const { return this->sock != INVALID_SOCKET; }
|
||||
|
||||
virtual NetworkRecvStatus CloseConnection();
|
||||
void Send_Packet(Packet *packet);
|
||||
bool Send_Packets();
|
||||
|
@@ -16,29 +16,39 @@
|
||||
#include "udp.h"
|
||||
|
||||
/**
|
||||
* Start listening on the given host and port.
|
||||
* @param address the host to listen on
|
||||
* @param broadcast whether to allow broadcast sending/receiving
|
||||
* @return true if the listening succeeded
|
||||
* Create an UDP socket but don't listen yet.
|
||||
* @param bind the addresses to bind to.
|
||||
*/
|
||||
bool NetworkUDPSocketHandler::Listen(NetworkAddress address, bool broadcast)
|
||||
NetworkUDPSocketHandler::NetworkUDPSocketHandler(NetworkAddressList *bind)
|
||||
{
|
||||
if (bind != NULL) {
|
||||
for (NetworkAddress *addr = bind->Begin(); addr != bind->End(); addr++) {
|
||||
*this->bind.Append() = *addr;
|
||||
}
|
||||
} else {
|
||||
*this->bind.Append() = NetworkAddress(NULL, 0, AF_INET);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Start listening on the given host and port.
|
||||
* @return true if at least one port is listening
|
||||
*/
|
||||
bool NetworkUDPSocketHandler::Listen()
|
||||
{
|
||||
/* Make sure socket is closed */
|
||||
this->Close();
|
||||
|
||||
this->sock = address.Listen(AF_INET, SOCK_DGRAM);
|
||||
|
||||
if (broadcast) {
|
||||
/* Enable broadcast */
|
||||
unsigned long val = 1;
|
||||
#ifndef BEOS_NET_SERVER /* will work around this, some day; maybe. */
|
||||
setsockopt(this->sock, SOL_SOCKET, SO_BROADCAST, (char *) &val, sizeof(val));
|
||||
#endif
|
||||
for (NetworkAddress *addr = this->bind.Begin(); addr != this->bind.End(); addr++) {
|
||||
addr->Listen(AF_UNSPEC, SOCK_DGRAM, &this->sockets);
|
||||
}
|
||||
|
||||
DEBUG(net, 1, "[udp] listening on port %s", address.GetAddressAsString());
|
||||
for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
|
||||
DEBUG(net, 1, "[udp] listening on port %s", s->first.GetAddressAsString());
|
||||
}
|
||||
|
||||
return true;
|
||||
return this->sockets.Length() != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,10 +56,10 @@ bool NetworkUDPSocketHandler::Listen(NetworkAddress address, bool broadcast)
|
||||
*/
|
||||
void NetworkUDPSocketHandler::Close()
|
||||
{
|
||||
if (!this->IsConnected()) return;
|
||||
|
||||
closesocket(this->sock);
|
||||
this->sock = INVALID_SOCKET;
|
||||
for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
|
||||
closesocket(s->second);
|
||||
}
|
||||
this->sockets.Clear();
|
||||
}
|
||||
|
||||
NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection()
|
||||
@@ -62,18 +72,35 @@ NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection()
|
||||
* Send a packet over UDP
|
||||
* @param p the packet to send
|
||||
* @param recv the receiver (target) of the packet
|
||||
* @param all send the packet using all sockets that can send it
|
||||
* @param broadcast whether to send a broadcast message
|
||||
*/
|
||||
void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv)
|
||||
void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv, bool all, bool broadcast)
|
||||
{
|
||||
int res;
|
||||
if (this->sockets.Length() == 0) this->Listen();
|
||||
|
||||
p->PrepareToSend();
|
||||
for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
|
||||
/* Not the same type */
|
||||
if (s->first.GetAddress()->ss_family != recv->GetAddress()->ss_family) continue;
|
||||
|
||||
/* Send the buffer */
|
||||
res = sendto(this->sock, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv->GetAddress(), recv->GetAddressLength());
|
||||
p->PrepareToSend();
|
||||
|
||||
/* Check for any errors, but ignore it otherwise */
|
||||
if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR());
|
||||
#ifndef BEOS_NET_SERVER /* will work around this, some day; maybe. */
|
||||
if (broadcast) {
|
||||
/* Enable broadcast */
|
||||
unsigned long val = 1;
|
||||
setsockopt(s->second, SOL_SOCKET, SO_BROADCAST, (char *) &val, sizeof(val));
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Send the buffer */
|
||||
int res = sendto(s->second, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv->GetAddress(), recv->GetAddressLength());
|
||||
|
||||
/* Check for any errors, but ignore it otherwise */
|
||||
if (res == -1) DEBUG(net, 1, "[udp] sendto(%s) failed with: %i", recv->GetAddressAsString(), GET_LAST_ERROR());
|
||||
|
||||
if (!all) break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,37 +108,33 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, NetworkAddress *recv)
|
||||
*/
|
||||
void NetworkUDPSocketHandler::ReceivePackets()
|
||||
{
|
||||
struct sockaddr_storage client_addr;
|
||||
memset(&client_addr, 0, sizeof(client_addr));
|
||||
for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) {
|
||||
struct sockaddr_storage client_addr;
|
||||
memset(&client_addr, 0, sizeof(client_addr));
|
||||
|
||||
socklen_t client_len;
|
||||
int nbytes;
|
||||
Packet p(this);
|
||||
int packet_len;
|
||||
Packet p(this);
|
||||
int packet_len = sizeof(p.buffer);
|
||||
socklen_t client_len = sizeof(client_addr);
|
||||
|
||||
if (!this->IsConnected()) return;
|
||||
/* Try to receive anything */
|
||||
SetNonBlocking(s->second); // Some OSes seem to lose the non-blocking status of the socket
|
||||
int nbytes = recvfrom(s->second, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
|
||||
|
||||
packet_len = sizeof(p.buffer);
|
||||
client_len = sizeof(client_addr);
|
||||
/* We got some bytes for the base header of the packet. */
|
||||
if (nbytes > 2) {
|
||||
NetworkAddress address(client_addr, client_len);
|
||||
p.PrepareToRead();
|
||||
|
||||
/* Try to receive anything */
|
||||
SetNonBlocking(this->sock); // Some OSes seem to lose the non-blocking status of the socket
|
||||
nbytes = recvfrom(this->sock, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len);
|
||||
/* If the size does not match the packet must be corrupted.
|
||||
* Otherwise it will be marked as corrupted later on. */
|
||||
if (nbytes != p.size) {
|
||||
DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString());
|
||||
return;
|
||||
}
|
||||
|
||||
/* We got some bytes for the base header of the packet. */
|
||||
if (nbytes > 2) {
|
||||
NetworkAddress address(client_addr, client_len);
|
||||
p.PrepareToRead();
|
||||
|
||||
/* If the size does not match the packet must be corrupted.
|
||||
* Otherwise it will be marked as corrupted later on. */
|
||||
if (nbytes != p.size) {
|
||||
DEBUG(net, 1, "received a packet with mismatching size from %s", address.GetAddressAsString());
|
||||
return;
|
||||
/* Handle the packet */
|
||||
this->HandleUDPPacket(&p, &address);
|
||||
}
|
||||
|
||||
/* Handle the packet */
|
||||
this->HandleUDPPacket(&p, &address);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -96,6 +96,11 @@ enum PacketUDPType {
|
||||
/** Base socket handler for all UDP sockets */
|
||||
class NetworkUDPSocketHandler : public NetworkSocketHandler {
|
||||
protected:
|
||||
/** The address to bind to. */
|
||||
NetworkAddressList bind;
|
||||
/** The opened sockets. */
|
||||
SocketList sockets;
|
||||
|
||||
NetworkRecvStatus CloseConnection();
|
||||
|
||||
/* Declare all possible packets here. If it can be received by the
|
||||
@@ -124,13 +129,15 @@ protected:
|
||||
*/
|
||||
virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) { NOT_REACHED(); }
|
||||
public:
|
||||
NetworkUDPSocketHandler(NetworkAddressList *bind = NULL);
|
||||
|
||||
/** On destructing of this class, the socket needs to be closed */
|
||||
virtual ~NetworkUDPSocketHandler() { this->Close(); }
|
||||
|
||||
bool Listen(NetworkAddress address, bool broadcast);
|
||||
bool Listen();
|
||||
void Close();
|
||||
|
||||
void SendPacket(Packet *p, NetworkAddress *recv);
|
||||
void SendPacket(Packet *p, NetworkAddress *recv, bool all = false, bool broadcast = false);
|
||||
void ReceivePackets();
|
||||
|
||||
void Send_NetworkGameInfo(Packet *p, const NetworkGameInfo *info);
|
||||
|
Reference in New Issue
Block a user