diff options
author | rubidium <rubidium@openttd.org> | 2010-11-30 20:01:26 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2010-11-30 20:01:26 +0000 |
commit | fd752ca2b0cc6741c348a7fbb8b7a6a7575e0f64 (patch) | |
tree | fcb2824a6f0df8ec0056b377b4fc05ae8ea0c67c /src/network | |
parent | a0f0f5e2e1133138eaa8da256be4929e16c69db6 (diff) | |
download | openttd-fd752ca2b0cc6741c348a7fbb8b7a6a7575e0f64.tar.xz |
(svn r21363) -Add: support for limiting the amount of (accepted) incoming data
Diffstat (limited to 'src/network')
-rw-r--r-- | src/network/core/tcp.h | 2 | ||||
-rw-r--r-- | src/network/network_server.cpp | 19 | ||||
-rw-r--r-- | src/network/network_server.h | 2 |
3 files changed, 22 insertions, 1 deletions
diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h index 899eba771..0f6eb8f5d 100644 --- a/src/network/core/tcp.h +++ b/src/network/core/tcp.h @@ -39,7 +39,7 @@ public: bool SendPackets(bool closing_down = false); bool IsPacketQueueEmpty(); - Packet *ReceivePacket(); + virtual Packet *ReceivePacket(); bool CanSendReceive(); diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp index aa0cd7944..5437cd006 100644 --- a/src/network/network_server.cpp +++ b/src/network/network_server.cpp @@ -62,6 +62,7 @@ ServerNetworkGameSocketHandler::ServerNetworkGameSocketHandler(SOCKET s) : Netwo { this->status = STATUS_INACTIVE; this->client_id = _network_client_id++; + this->receive_limit = _settings_client.network.bytes_per_frame_burst; NetworkClientInfo *ci = new NetworkClientInfo(this->client_id); this->SetInfo(ci); ci->client_playas = COMPANY_INACTIVE_CLIENT; @@ -77,6 +78,19 @@ ServerNetworkGameSocketHandler::~ServerNetworkGameSocketHandler() OrderBackup::ResetUser(this->client_id); } +Packet *ServerNetworkGameSocketHandler::ReceivePacket() +{ + /* Only allow receiving when we have some buffer free; this value + * can go negative, but eventually it will become positive again. */ + if (this->receive_limit <= 0) return NULL; + + /* We can receive a packet, so try that and if needed account for + * the amount of received data. */ + Packet *p = this->NetworkTCPSocketHandler::ReceivePacket(); + if (p != NULL) this->receive_limit -= p->size; + return p; +} + NetworkRecvStatus ServerNetworkGameSocketHandler::CloseConnection(NetworkRecvStatus status) { assert(status != NETWORK_RECV_STATUS_OKAY); @@ -1540,6 +1554,11 @@ void NetworkServer_Tick(bool send_frame) /* Now we are done with the frame, inform the clients that they can * do their frame! */ FOR_ALL_CLIENT_SOCKETS(cs) { + /* We allow a number of bytes per frame, but only to the burst amount + * to be available for packet receiving at any particular time. */ + cs->receive_limit = min(cs->receive_limit + _settings_client.network.bytes_per_frame, + _settings_client.network.bytes_per_frame_burst); + /* Check if the speed of the client is what we can expect from a client */ if (cs->status == NetworkClientSocket::STATUS_ACTIVE) { /* 1 lag-point per day */ diff --git a/src/network/network_server.h b/src/network/network_server.h index 0185d69bd..89d94a3cc 100644 --- a/src/network/network_server.h +++ b/src/network/network_server.h @@ -70,10 +70,12 @@ public: uint32 last_token_frame; ///< The last frame we received the right token ClientStatus status; ///< Status of this client CommandQueue outgoing_queue; ///< The command-queue awaiting delivery + int receive_limit; ///< Amount of bytes that we can receive at this moment ServerNetworkGameSocketHandler(SOCKET s); ~ServerNetworkGameSocketHandler(); + virtual Packet *ReceivePacket(); NetworkRecvStatus CloseConnection(NetworkRecvStatus status); void GetClientName(char *client_name, size_t size) const; |