diff options
author | rubidium <rubidium@openttd.org> | 2009-04-07 20:27:13 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-04-07 20:27:13 +0000 |
commit | 2a6e9288fd9e238c2f0b68243fced9e3cf794100 (patch) | |
tree | 6cab21c9baf3fb1ca74033e132561b40ed015d91 /src/network/core | |
parent | 6db7f15b3ad8addb7187bec3d309a4e6481ac431 (diff) | |
download | openttd-2a6e9288fd9e238c2f0b68243fced9e3cf794100.tar.xz |
(svn r15971) -Codechange: make it possible for UDP socket handlers to bind to multiple sockets.
Diffstat (limited to 'src/network/core')
-rw-r--r-- | src/network/core/core.h | 11 | ||||
-rw-r--r-- | src/network/core/tcp.cpp | 5 | ||||
-rw-r--r-- | src/network/core/tcp.h | 7 | ||||
-rw-r--r-- | src/network/core/udp.cpp | 131 | ||||
-rw-r--r-- | src/network/core/udp.h | 11 |
5 files changed, 97 insertions, 68 deletions
diff --git a/src/network/core/core.h b/src/network/core/core.h index bc348255d..4fca89f54 100644 --- a/src/network/core/core.h +++ b/src/network/core/core.h @@ -38,11 +38,8 @@ 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(); } @@ -58,12 +55,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 * data), the socket in not closed; only the packet is dropped. diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp index 1ed8b618c..a80defb4c 100644 --- a/src/network/core/tcp.cpp +++ b/src/network/core/tcp.cpp @@ -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) { } diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h index 90edba014..7e4964813 100644 --- a/src/network/core/tcp.h +++ b/src/network/core/tcp.h @@ -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(); diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp index df5a68cb5..96ece0ace 100644 --- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -16,29 +16,39 @@ #include "udp.h" /** + * Create an UDP socket but don't listen yet. + * @param bind the addresses to bind to. + */ +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. - * @param address the host to listen on - * @param broadcast whether to allow broadcast sending/receiving - * @return true if the listening succeeded + * @return true if at least one port is listening */ -bool NetworkUDPSocketHandler::Listen(NetworkAddress address, bool broadcast) +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)); - - socklen_t client_len; - int nbytes; - Packet p(this); - int packet_len; - - if (!this->IsConnected()) return; - - packet_len = sizeof(p.buffer); - client_len = sizeof(client_addr); - - /* 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); - - /* We got some bytes for the base header of the packet. */ - if (nbytes > 2) { - NetworkAddress address(client_addr, client_len); - p.PrepareToRead(); + for (SocketList::iterator s = this->sockets.Begin(); s != this->sockets.End(); s++) { + struct sockaddr_storage client_addr; + memset(&client_addr, 0, sizeof(client_addr)); + + Packet p(this); + int packet_len = sizeof(p.buffer); + socklen_t client_len = sizeof(client_addr); + + /* 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); + + /* 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; + } - /* 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); } } diff --git a/src/network/core/udp.h b/src/network/core/udp.h index 20e228100..2302dab0d 100644 --- a/src/network/core/udp.h +++ b/src/network/core/udp.h @@ -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); |