diff options
author | rubidium <rubidium@openttd.org> | 2007-01-12 20:19:49 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2007-01-12 20:19:49 +0000 |
commit | 98e462e56444155a11fe92b9391c2faf1aea90d2 (patch) | |
tree | 23bff1b129004614fd68cb0c30436a14bbe240e9 /src/network/core | |
parent | 473ed1d351e04b8f000325219bf80f54bd96b7f1 (diff) | |
download | openttd-98e462e56444155a11fe92b9391c2faf1aea90d2.tar.xz |
(svn r8083) -Codechange: make a NetworkSocketHandler as base for all sockets and move a little of NetworkClientState functionality to the NetworkSocketHandler. Move the rest of the NetworkClientState to the new NetworkTCPSocketHandler class/struct, which is not yet implemented in an object oriented manner. The UDP socket handler now extends the NetworkSocketHandler instead of having a reference to a NetworkClientState.
Diffstat (limited to 'src/network/core')
-rw-r--r-- | src/network/core/core.h | 52 | ||||
-rw-r--r-- | src/network/core/packet.cpp | 23 | ||||
-rw-r--r-- | src/network/core/packet.h | 23 | ||||
-rw-r--r-- | src/network/core/tcp.cpp | 67 | ||||
-rw-r--r-- | src/network/core/tcp.h | 52 | ||||
-rw-r--r-- | src/network/core/udp.cpp | 96 | ||||
-rw-r--r-- | src/network/core/udp.h | 12 |
7 files changed, 208 insertions, 117 deletions
diff --git a/src/network/core/core.h b/src/network/core/core.h index c6dc05591..f88fd0116 100644 --- a/src/network/core/core.h +++ b/src/network/core/core.h @@ -5,9 +5,61 @@ #ifdef ENABLE_NETWORK +#include "os_abstraction.h" + bool NetworkCoreInitialize(void); void NetworkCoreShutdown(void); +typedef enum { + NETWORK_RECV_STATUS_OKAY, ///< Everything is okay + NETWORK_RECV_STATUS_DESYNC, ///< A desync did occur + NETWORK_RECV_STATUS_SAVEGAME, ///< Something went wrong (down)loading the savegame + NETWORK_RECV_STATUS_CONN_LOST, ///< The conection is 'just' lost + NETWORK_RECV_STATUS_MALFORMED_PACKET, ///< We apparently send a malformed packet + NETWORK_RECV_STATUS_SERVER_ERROR, ///< The server told us we made an error + NETWORK_RECV_STATUS_SERVER_FULL, ///< The server is full + NETWORK_RECV_STATUS_SERVER_BANNED, ///< The server has banned us + NETWORK_RECV_STATUS_CLOSE_QUERY, ///< Done quering the server +} NetworkRecvStatus; + +/** + * SocketHandler for all network sockets in OpenTTD. + */ +class NetworkSocketHandler { +public: + /* TODO: make socket & has_quit protected once the TCP stuff + *is in a real class too */ + bool has_quit; ///< Whether the current client has quit/send a bad packet + SOCKET sock; ///< The socket currently connected to +public: + NetworkSocketHandler() { this->sock = INVALID_SOCKET; this->has_quit = false; } + virtual ~NetworkSocketHandler() { this->Close(); } + + /** Really close the socket */ + virtual void Close() {} + + /** + * Close the current connection; for TCP this will be mostly equivalent + * to Close(), but for UDP it just means the packet has to be dropped. + * @return new status of the connection. + */ + 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() { 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. + * @return true when the current client has quit, false otherwise + */ + bool HasClientQuit() { return this->has_quit; } +}; + #endif /* ENABLE_NETWORK */ #endif /* NETWORK_CORE_H */ diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp index 511e0c675..fe10af48f 100644 --- a/src/network/core/packet.cpp +++ b/src/network/core/packet.cpp @@ -6,7 +6,6 @@ #include "../../macros.h" #include "../../string.h" #include "../../helpers.hpp" -#include "../network_data.h" #include "packet.h" @@ -111,17 +110,15 @@ void NetworkSend_string(Packet *packet, const char* data) */ -extern NetworkRecvStatus CloseConnection(NetworkClientState *cs); - /** Is it safe to read from the packet, i.e. didn't we run over the buffer ? */ -static inline bool CanReadFromPacket(NetworkClientState *cs, const Packet *packet, const uint bytes_to_read) +static inline bool CanReadFromPacket(NetworkSocketHandler *cs, const Packet *packet, const uint bytes_to_read) { - /* Don't allow reading from a closed socket */ - if (HasClientQuit(cs)) return false; + /* Don't allow reading from a quit client/client who send bad data */ + if (cs->HasClientQuit()) return false; /* Check if variable is within packet-size */ if (packet->pos + bytes_to_read > packet->size) { - CloseConnection(cs); + cs->CloseConnection(); return false; } @@ -138,7 +135,7 @@ void NetworkRecv_ReadPacketSize(Packet *packet) packet->size += (uint16)packet->buffer[1] << 8; } -uint8 NetworkRecv_uint8(NetworkClientState *cs, Packet *packet) +uint8 NetworkRecv_uint8(NetworkSocketHandler *cs, Packet *packet) { uint8 n; @@ -148,7 +145,7 @@ uint8 NetworkRecv_uint8(NetworkClientState *cs, Packet *packet) return n; } -uint16 NetworkRecv_uint16(NetworkClientState *cs, Packet *packet) +uint16 NetworkRecv_uint16(NetworkSocketHandler *cs, Packet *packet) { uint16 n; @@ -159,7 +156,7 @@ uint16 NetworkRecv_uint16(NetworkClientState *cs, Packet *packet) return n; } -uint32 NetworkRecv_uint32(NetworkClientState *cs, Packet *packet) +uint32 NetworkRecv_uint32(NetworkSocketHandler *cs, Packet *packet) { uint32 n; @@ -172,7 +169,7 @@ uint32 NetworkRecv_uint32(NetworkClientState *cs, Packet *packet) return n; } -uint64 NetworkRecv_uint64(NetworkClientState *cs, Packet *packet) +uint64 NetworkRecv_uint64(NetworkSocketHandler *cs, Packet *packet) { uint64 n; @@ -190,13 +187,13 @@ uint64 NetworkRecv_uint64(NetworkClientState *cs, Packet *packet) } /** Reads a string till it finds a '\0' in the stream */ -void NetworkRecv_string(NetworkClientState *cs, Packet *p, char *buffer, size_t size) +void NetworkRecv_string(NetworkSocketHandler *cs, Packet *p, char *buffer, size_t size) { PacketSize pos; char *bufp = buffer; /* Don't allow reading from a closed socket */ - if (HasClientQuit(cs)) return; + if (cs->HasClientQuit()) return; pos = p->pos; while (--size > 0 && pos < p->size && (*buffer++ = p->buffer[pos++]) != '\0') {} diff --git a/src/network/core/packet.h b/src/network/core/packet.h index e42c65d5c..65ddec72c 100644 --- a/src/network/core/packet.h +++ b/src/network/core/packet.h @@ -6,23 +6,12 @@ #ifdef ENABLE_NETWORK #include "config.h" +#include "core.h" /** * @file packet.h Basic functions to create, fill and read packets. */ -typedef struct NetworkClientState NetworkClientState; - -/** - * Queries the network client state struct to determine whether - * the client has quit. It indirectly also queries whether the - * packet is corrupt as the connection will be closed if it is - * reading beyond the boundary of the received packet. - * @param cs the state to query - * @param true if the connection should be considered dropped - */ -bool HasClientQuit(const NetworkClientState *cs); - typedef uint16 PacketSize; ///< Size of the whole packet. typedef uint8 PacketType; ///< Identifier for the packet @@ -58,11 +47,11 @@ void NetworkSend_uint64(Packet *packet, uint64 data); void NetworkSend_string(Packet *packet, const char* data); void NetworkRecv_ReadPacketSize(Packet *packet); -uint8 NetworkRecv_uint8 (NetworkClientState *cs, Packet *packet); -uint16 NetworkRecv_uint16(NetworkClientState *cs, Packet *packet); -uint32 NetworkRecv_uint32(NetworkClientState *cs, Packet *packet); -uint64 NetworkRecv_uint64(NetworkClientState *cs, Packet *packet); -void NetworkRecv_string(NetworkClientState *cs, Packet *packet, char* buffer, size_t size); +uint8 NetworkRecv_uint8 (NetworkSocketHandler *cs, Packet *packet); +uint16 NetworkRecv_uint16(NetworkSocketHandler *cs, Packet *packet); +uint32 NetworkRecv_uint32(NetworkSocketHandler *cs, Packet *packet); +uint64 NetworkRecv_uint64(NetworkSocketHandler *cs, Packet *packet); +void NetworkRecv_string(NetworkSocketHandler *cs, Packet *packet, char* buffer, size_t size); #endif /* ENABLE_NETWORK */ diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp index 98b5d8d5d..54271de57 100644 --- a/src/network/core/tcp.cpp +++ b/src/network/core/tcp.cpp @@ -18,6 +18,26 @@ * @file tcp.c Basic functions to receive and send TCP packets. */ +/** Very ugly temporary hack !!! */ +void NetworkTCPSocketHandler::Initialize() +{ + this->sock = INVALID_SOCKET; + + this->index = 0; + this->last_frame = 0; + this->last_frame_server = 0; + this->lag_test = 0; + + this->status = STATUS_INACTIVE; + this->has_quit = false; + this->writable = false; + + this->packet_queue = NULL; + this->packet_recv = NULL; + + this->command_queue = NULL; +} + /** * Functions to help NetworkRecv_Packet/NetworkSend_Packet a bit * A socket can make errors. When that happens this handles what to do. @@ -25,10 +45,11 @@ * For servers: close connection and that is it * @param cs the client to close the connection of * @return the new status + * TODO: needs to be splitted when using client and server socket packets */ -NetworkRecvStatus CloseConnection(NetworkClientState *cs) +NetworkRecvStatus NetworkTCPSocketHandler::CloseConnection() { - NetworkCloseClient(cs); + NetworkCloseClient(this); /* Clients drop back to the main menu */ if (!_network_server && _networking) { @@ -43,23 +64,13 @@ NetworkRecvStatus CloseConnection(NetworkClientState *cs) } /** - * Whether the client has quit or not (used in packet.c) - * @param cs the client to check - * @return true if the client has quit - */ -bool HasClientQuit(const NetworkClientState *cs) -{ - return cs->has_quit; -} - -/** * This function puts the packet in the send-queue and it is send as * soon as possible. This is the next tick, or maybe one tick later * if the OS-network-buffer is full) * @param packet the packet to send * @param cs the client to send to */ -void NetworkSend_Packet(Packet *packet, NetworkClientState *cs) +void NetworkSend_Packet(Packet *packet, NetworkTCPSocketHandler *cs) { Packet *p; assert(packet != NULL); @@ -89,31 +100,31 @@ void NetworkSend_Packet(Packet *packet, NetworkClientState *cs) * 3) sending took too long * @param cs the client to send the packets for */ -bool NetworkSend_Packets(NetworkClientState *cs) +bool NetworkSend_Packets(NetworkTCPSocketHandler *cs) { ssize_t res; Packet *p; /* We can not write to this socket!! */ if (!cs->writable) return false; - if (cs->socket == INVALID_SOCKET) return false; + if (!cs->IsConnected()) return false; p = cs->packet_queue; while (p != NULL) { - res = send(cs->socket, (const char*)p->buffer + p->pos, p->size - p->pos, 0); + res = send(cs->sock, (const char*)p->buffer + p->pos, p->size - p->pos, 0); if (res == -1) { int err = GET_LAST_ERROR(); if (err != EWOULDBLOCK) { /* Something went wrong.. close client! */ DEBUG(net, 0, "send failed with error %d", err); - CloseConnection(cs); + cs->CloseConnection(); return false; } return true; } if (res == 0) { /* Client/server has left us :( */ - CloseConnection(cs); + cs->CloseConnection(); return false; } @@ -139,14 +150,14 @@ bool NetworkSend_Packets(NetworkClientState *cs) * @param status the variable to store the status into * @return the received packet (or NULL when it didn't receive one) */ -Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status) +Packet *NetworkRecv_Packet(NetworkTCPSocketHandler *cs, NetworkRecvStatus *status) { ssize_t res; Packet *p; *status = NETWORK_RECV_STATUS_OKAY; - if (cs->socket == INVALID_SOCKET) return NULL; + if (!cs->IsConnected()) return NULL; if (cs->packet_recv == NULL) { cs->packet_recv = MallocT<Packet>(1); @@ -161,14 +172,14 @@ Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status) /* Read packet size */ if (p->pos < sizeof(PacketSize)) { while (p->pos < sizeof(PacketSize)) { - /* Read the size of the packet */ - res = recv(cs->socket, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0); + /* Read the size of the packet */ + res = recv(cs->sock, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0); if (res == -1) { int err = GET_LAST_ERROR(); if (err != EWOULDBLOCK) { /* Something went wrong... (104 is connection reset by peer) */ if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); - *status = CloseConnection(cs); + *status = cs->CloseConnection(); return NULL; } /* Connection would block, so stop for now */ @@ -176,7 +187,7 @@ Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status) } if (res == 0) { /* Client/server has left */ - *status = CloseConnection(cs); + *status = cs->CloseConnection(); return NULL; } p->pos += res; @@ -185,20 +196,20 @@ Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status) NetworkRecv_ReadPacketSize(p); if (p->size > SEND_MTU) { - *status = CloseConnection(cs); + *status = cs->CloseConnection(); return NULL; } } /* Read rest of packet */ while (p->pos < p->size) { - res = recv(cs->socket, (char*)p->buffer + p->pos, p->size - p->pos, 0); + res = recv(cs->sock, (char*)p->buffer + p->pos, p->size - p->pos, 0); if (res == -1) { int err = GET_LAST_ERROR(); if (err != EWOULDBLOCK) { /* Something went wrong... (104 is connection reset by peer) */ if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); - *status = CloseConnection(cs); + *status = cs->CloseConnection(); return NULL; } /* Connection would block */ @@ -206,7 +217,7 @@ Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status) } if (res == 0) { /* Client/server has left */ - *status = CloseConnection(cs); + *status = cs->CloseConnection(); return NULL; } diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h index ab5c9b8e0..c15f851aa 100644 --- a/src/network/core/tcp.h +++ b/src/network/core/tcp.h @@ -6,6 +6,7 @@ #ifdef ENABLE_NETWORK #include "os_abstraction.h" +#include "core.h" #include "packet.h" /** @@ -54,9 +55,54 @@ enum { PACKET_END ///< Must ALWAYS be on the end of this list!! (period) }; -void NetworkSend_Packet(Packet *packet, NetworkClientState *cs); -Packet *NetworkRecv_Packet(NetworkClientState *cs, NetworkRecvStatus *status); -bool NetworkSend_Packets(NetworkClientState *cs); +typedef struct CommandPacket { + struct CommandPacket *next; + PlayerByte player; ///< player that is executing the command + uint32 cmd; ///< command being executed + uint32 p1; ///< parameter p1 + uint32 p2; ///< parameter p2 + TileIndex tile; ///< tile command being executed on + char text[80]; + uint32 frame; ///< the frame in which this packet is executed + byte callback; ///< any callback function executed upon successful completion of the command +} CommandPacket; + +typedef enum { + STATUS_INACTIVE, ///< The client is not connected nor active + STATUS_AUTH, ///< The client is authorized + STATUS_MAP_WAIT, ///< The client is waiting as someone else is downloading the map + STATUS_MAP, ///< The client is downloading the map + STATUS_DONE_MAP, ///< The client has downloaded the map + STATUS_PRE_ACTIVE, ///< The client is catching up the delayed frames + STATUS_ACTIVE, ///< The client is an active player in the game +} ClientStatus; + +/** Base socket handler for all TCP sockets */ +class NetworkTCPSocketHandler : public NetworkSocketHandler { +/* TODO: rewrite into a proper class */ +public: + uint16 index; + uint32 last_frame; + uint32 last_frame_server; + byte lag_test; // This byte is used for lag-testing the client + + ClientStatus status; + bool writable; // is client ready to write to? + + Packet *packet_queue; // Packets that are awaiting delivery + Packet *packet_recv; // Partially received packet + + CommandPacket *command_queue; // The command-queue awaiting delivery + + NetworkRecvStatus CloseConnection(); + void Initialize(); +}; + + + +void NetworkSend_Packet(Packet *packet, NetworkTCPSocketHandler *cs); +Packet *NetworkRecv_Packet(NetworkTCPSocketHandler *cs, NetworkRecvStatus *status); +bool NetworkSend_Packets(NetworkTCPSocketHandler *cs); #endif /* ENABLE_NETWORK */ diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp index ddc4cadfb..c3709aa10 100644 --- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -13,12 +13,6 @@ * @file udp.c Basic functions to receive and send UDP packets. */ -/** Initialize the sockets with an INVALID_SOCKET */ -NetworkUDPSocketHandler::NetworkUDPSocketHandler() -{ - this->udp = INVALID_SOCKET; -} - /** * Start listening on the given host and port. * @param udp the place where the (references to the) UDP are stored @@ -34,8 +28,8 @@ bool NetworkUDPSocketHandler::Listen(const uint32 host, const uint16 port, const /* Make sure socket is closed */ this->Close(); - this->udp = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (this->udp == INVALID_SOCKET) { + this->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (!this->IsConnected()) { DEBUG(net, 0, "[udp] failed to start UDP listener"); return false; } @@ -44,9 +38,9 @@ bool NetworkUDPSocketHandler::Listen(const uint32 host, const uint16 port, const { unsigned long blocking = 1; #ifndef BEOS_NET_SERVER - ioctlsocket(this->udp, FIONBIO, &blocking); + ioctlsocket(this->sock, FIONBIO, &blocking); #else - setsockopt(this->udp, SOL_SOCKET, SO_NONBLOCK, &blocking, NULL); + setsockopt(this->sock, SOL_SOCKET, SO_NONBLOCK, &blocking, NULL); #endif } @@ -55,7 +49,7 @@ bool NetworkUDPSocketHandler::Listen(const uint32 host, const uint16 port, const sin.sin_addr.s_addr = host; sin.sin_port = htons(port); - if (bind(this->udp, (struct sockaddr*)&sin, sizeof(sin)) != 0) { + if (bind(this->sock, (struct sockaddr*)&sin, sizeof(sin)) != 0) { DEBUG(net, 0, "[udp] bind failed on %s:%i", inet_ntoa(*(struct in_addr *)&host), port); return false; } @@ -64,7 +58,7 @@ bool NetworkUDPSocketHandler::Listen(const uint32 host, const uint16 port, const /* Enable broadcast */ unsigned long val = 1; #ifndef BEOS_NET_SERVER // will work around this, some day; maybe. - setsockopt(this->udp, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val)); + setsockopt(this->sock, SOL_SOCKET, SO_BROADCAST, (char *) &val , sizeof(val)); #endif } @@ -79,12 +73,17 @@ bool NetworkUDPSocketHandler::Listen(const uint32 host, const uint16 port, const */ void NetworkUDPSocketHandler::Close() { - if (this->udp == INVALID_SOCKET) return; + if (!this->IsConnected()) return; - closesocket(this->udp); - this->udp = INVALID_SOCKET; + closesocket(this->sock); + this->sock = INVALID_SOCKET; } +NetworkRecvStatus NetworkUDPSocketHandler::CloseConnection() +{ + this->has_quit = true; + return NETWORK_RECV_STATUS_OKAY; +} /** * Send a packet over UDP @@ -99,7 +98,7 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, const struct sockaddr_in *re NetworkSend_FillPacketSize(p); /* Send the buffer */ - res = sendto(this->udp, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv)); + res = sendto(this->sock, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv)); /* Check for any errors, but ignore it otherwise */ if (res == -1) DEBUG(net, 1, "[udp] sendto failed with: %i", GET_LAST_ERROR()); @@ -117,13 +116,13 @@ void NetworkUDPSocketHandler::ReceivePackets() Packet p; int packet_len; - if (this->udp == INVALID_SOCKET) return; + if (!this->IsConnected()) return; packet_len = sizeof(p.buffer); client_len = sizeof(client_addr); /* Try to receive anything */ - nbytes = recvfrom(this->udp, (char*)p.buffer, packet_len, 0, (struct sockaddr *)&client_addr, &client_len); + 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) { @@ -170,9 +169,9 @@ void NetworkUDPSocketHandler::Send_GRFIdentifier(Packet *p, const GRFConfig *c) void NetworkUDPSocketHandler::Recv_GRFIdentifier(Packet *p, GRFConfig *c) { uint j; - c->grfid = NetworkRecv_uint32(&this->cs, p); + c->grfid = NetworkRecv_uint32(this, p); for (j = 0; j < sizeof(c->md5sum); j++) { - c->md5sum[j] = NetworkRecv_uint8(&this->cs, p); + c->md5sum[j] = NetworkRecv_uint8(this, p); } } @@ -246,7 +245,7 @@ void NetworkUDPSocketHandler::Send_NetworkGameInfo(Packet *p, const NetworkGameI */ void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *info) { - info->game_info_version = NetworkRecv_uint8(&this->cs, p); + info->game_info_version = NetworkRecv_uint8(this, p); /* * Please observe the order. @@ -260,7 +259,7 @@ void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *i case 4: { GRFConfig **dst = &info->grfconfig; uint i; - uint num_grfs = NetworkRecv_uint8(&this->cs, p); + uint num_grfs = NetworkRecv_uint8(this, p); for (i = 0; i < num_grfs; i++) { GRFConfig *c = CallocT<GRFConfig>(1); @@ -273,31 +272,31 @@ void NetworkUDPSocketHandler::Recv_NetworkGameInfo(Packet *p, NetworkGameInfo *i } } /* Fallthrough */ case 3: - info->game_date = NetworkRecv_uint32(&this->cs, p); - info->start_date = NetworkRecv_uint32(&this->cs, p); + info->game_date = NetworkRecv_uint32(this, p); + info->start_date = NetworkRecv_uint32(this, p); /* Fallthrough */ case 2: - info->companies_max = NetworkRecv_uint8 (&this->cs, p); - info->companies_on = NetworkRecv_uint8 (&this->cs, p); - info->spectators_max = NetworkRecv_uint8 (&this->cs, p); + info->companies_max = NetworkRecv_uint8 (this, p); + info->companies_on = NetworkRecv_uint8 (this, p); + info->spectators_max = NetworkRecv_uint8 (this, p); /* Fallthrough */ case 1: - NetworkRecv_string(&this->cs, p, info->server_name, sizeof(info->server_name)); - NetworkRecv_string(&this->cs, p, info->server_revision, sizeof(info->server_revision)); - info->server_lang = NetworkRecv_uint8 (&this->cs, p); - info->use_password = NetworkRecv_uint8 (&this->cs, p); - info->clients_max = NetworkRecv_uint8 (&this->cs, p); - info->clients_on = NetworkRecv_uint8 (&this->cs, p); - info->spectators_on = NetworkRecv_uint8 (&this->cs, p); + NetworkRecv_string(this, p, info->server_name, sizeof(info->server_name)); + NetworkRecv_string(this, p, info->server_revision, sizeof(info->server_revision)); + info->server_lang = NetworkRecv_uint8 (this, p); + info->use_password = NetworkRecv_uint8 (this, p); + info->clients_max = NetworkRecv_uint8 (this, p); + info->clients_on = NetworkRecv_uint8 (this, p); + info->spectators_on = NetworkRecv_uint8 (this, p); if (info->game_info_version < 3) { // 16 bits dates got scrapped and are read earlier - info->game_date = NetworkRecv_uint16(&this->cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR; - info->start_date = NetworkRecv_uint16(&this->cs, p) + DAYS_TILL_ORIGINAL_BASE_YEAR; + info->game_date = NetworkRecv_uint16(this, p) + DAYS_TILL_ORIGINAL_BASE_YEAR; + info->start_date = NetworkRecv_uint16(this, p) + DAYS_TILL_ORIGINAL_BASE_YEAR; } - NetworkRecv_string(&this->cs, p, info->map_name, sizeof(info->map_name)); - info->map_width = NetworkRecv_uint16(&this->cs, p); - info->map_height = NetworkRecv_uint16(&this->cs, p); - info->map_set = NetworkRecv_uint8 (&this->cs, p); - info->dedicated = (NetworkRecv_uint8(&this->cs, p) != 0); + NetworkRecv_string(this, p, info->map_name, sizeof(info->map_name)); + info->map_width = NetworkRecv_uint16(this, p); + info->map_height = NetworkRecv_uint16(this, p); + info->map_set = NetworkRecv_uint8 (this, p); + info->dedicated = (NetworkRecv_uint8(this, p) != 0); } } @@ -313,13 +312,12 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_i { PacketUDPType type; - /* Fake a client, so we can see when there is an illegal packet */ - this->cs.socket = INVALID_SOCKET; - this->cs.has_quit = false; + /* New packet == new client, which has not quit yet */ + this->has_quit = false; - type = (PacketUDPType)NetworkRecv_uint8(&this->cs, p); + type = (PacketUDPType)NetworkRecv_uint8(this, p); - switch (this->cs.has_quit ? PACKET_UDP_END : type) { + switch (this->HasClientQuit() ? PACKET_UDP_END : type) { UDP_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER); UDP_COMMAND(PACKET_UDP_SERVER_RESPONSE); UDP_COMMAND(PACKET_UDP_CLIENT_DETAIL_INFO); @@ -333,7 +331,7 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_i UDP_COMMAND(PACKET_UDP_SERVER_NEWGRFS); default: - if (!this->cs.has_quit) { + if (this->HasClientQuit()) { DEBUG(net, 0, "[udp] received invalid packet type %d from %s:%d", type, inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); } else { DEBUG(net, 0, "[udp] received illegal packet from %s:%d", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); @@ -351,8 +349,8 @@ void NetworkUDPSocketHandler::HandleUDPPacket(Packet *p, const struct sockaddr_i #define DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(type) \ void NetworkUDPSocketHandler::NetworkPacketReceive_## type ##_command(\ Packet *p, const struct sockaddr_in *client_addr) { \ - DEBUG(net, 0, "[udp] received packet on wrong port from %s:%d", \ - inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); \ + DEBUG(net, 0, "[udp] received packet type %d on wrong port from %s:%d", \ + type, inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port)); \ } DEFINE_UNAVAILABLE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER); diff --git a/src/network/core/udp.h b/src/network/core/udp.h index 755829600..9bb743841 100644 --- a/src/network/core/udp.h +++ b/src/network/core/udp.h @@ -6,11 +6,11 @@ #ifdef ENABLE_NETWORK #include "os_abstraction.h" +#include "core.h" #include "game.h" #include "packet.h" #include "../../newgrf_config.h" #include "../../debug.h" -#include "../network_data.h" /** * @file udp.h Basic functions to receive and send UDP packets. @@ -91,11 +91,11 @@ enum PacketUDPType { #define DECLARE_UDP_RECEIVE_COMMAND(type) virtual void NetworkPacketReceive_## type ##_command(Packet *p, const struct sockaddr_in *) -class NetworkUDPSocketHandler { -private: - SOCKET udp; +/** Base socket handler for all UDP sockets */ +class NetworkUDPSocketHandler : public NetworkSocketHandler { protected: - NetworkClientState cs; + NetworkRecvStatus CloseConnection(); + /* Declare all possible packets here. If it can be received by the * a specific handler, it has to be implemented. */ DECLARE_UDP_RECEIVE_COMMAND(PACKET_UDP_CLIENT_FIND_SERVER); @@ -121,10 +121,8 @@ protected: */ virtual void HandleIncomingNetworkGameInfoGRFConfig(GRFConfig *config) { NOT_REACHED(); } public: - NetworkUDPSocketHandler(); virtual ~NetworkUDPSocketHandler() { this->Close(); } - bool IsListening() { return this->udp != INVALID_SOCKET; } bool Listen(uint32 host, uint16 port, bool broadcast); void Close(); |