diff --git a/Src/C64_SDL.h b/Src/C64_SDL.h index a0216a5..9995085 100644 --- a/Src/C64_SDL.h +++ b/Src/C64_SDL.h @@ -556,19 +556,14 @@ void C64::Run(void) void C64::network_vblank() { + static uint32_t last_time_update; #if defined(GEKKO) Uint32 now = ticks_to_millisecs(gettime()); #else Uint32 now = SDL_GetTicks(); #endif - static int frms = 1; - frms--; - if (this->network_server && frms == 0) { - static uint32_t last_time_update; - static size_t bytes_sent; - - frms = 6; + if (this->network_server) { /* Perhaps accept a new connection */ this->network_server->CheckNewConnection(); @@ -576,6 +571,7 @@ void C64::network_vblank() Uint8 *master = this->TheDisplay->BitmapBase(); NetworkClient *remote = this->network_server->clients[i]; + remote->Tick( now - last_time_update ); /* Has the client sent any data? */ if (remote->ReceiveUpdate() == true) { @@ -589,7 +585,11 @@ void C64::network_vblank() this->network_server->RemoveClient(remote); continue; } - + remote->ResetNetworkUpdate(); + } + if (remote->ThrottleTraffic()) { + /* Skip this frame if the data rate is too high */ + continue; } remote->EncodeDisplay(master, remote->screen); if (remote->SendUpdate() == false) @@ -598,22 +598,19 @@ void C64::network_vblank() printf("Could not send update\n"); this->network_server->RemoveClient(remote); } - else - { - remote->ResetNetworkUpdate(); + remote->ResetNetworkUpdate(); - bytes_sent += remote->GetBytesSent(); + if (1) + { + static uint32_t last_traffic_update; + + if (last_time_update - last_traffic_update > 300) + { + TheDisplay->NetworkTrafficMeter(remote->GetKbps() / (8 * 1024.0)); + last_traffic_update = now; + } } } - if (now - last_time_update > 300) - { - TheDisplay->NetworkTrafficMeter(((bytes_sent * 1000.0) / - ((float)now - last_time_update)) / 1024.0); - for (int i = 0; i < this->network_server->n_clients; i++) - this->network_server->clients[i]->ResetBytesSent(); - bytes_sent = 0; - last_time_update = now; - } } else if (this->network_client) { Uint8 js = TheCIA1->Joystick2; @@ -632,6 +629,7 @@ void C64::network_vblank() this->network_client->EncodeJoystickUpdate(js); this->network_client->SendUpdate(); this->network_client->cur_joystick_data = js; + this->network_client->ResetNetworkUpdate(); } if (this->network_client->ReceiveUpdate()) @@ -651,6 +649,7 @@ void C64::network_vblank() } } } + last_time_update = now; } /* @@ -660,8 +659,8 @@ void C64::network_vblank() void C64::VBlank(bool draw_frame) { /* From Acorn port */ - static uint64_t lastFrame; - static uint32_t now; + static uint32_t lastFrame; + uint32_t now; uint8 j1, j2; #if defined(GEKKO) diff --git a/Src/Network.cpp b/Src/Network.cpp index 6fae160..2ad5361 100644 --- a/Src/Network.cpp +++ b/Src/Network.cpp @@ -46,7 +46,10 @@ Network::Network() assert(this->ud && this->tmp_ud); this->ResetNetworkUpdate(); - this->bytes_sent = 0; + this->traffic = 0; + this->last_traffic = 0; + this->target_kbps = 120000; /* kilobit per seconds */ + this->kbps = 0; this->raw_buf = (Uint8*)malloc(RAW_SIZE); this->rle_buf = (Uint8*)malloc(RLE_SIZE); @@ -68,6 +71,15 @@ Network::~Network() free(this->diff_buf); } +void Network::Tick(int ms) +{ + int last_kbps = ((this->traffic - this->last_traffic) * 8) * (1000 / ms); + + /* 1/3 of the new value, 2/3 of the old */ + this->kbps = 2 * (this->kbps / 3) + (last_kbps / 3); + this->last_traffic = this->traffic; +} + size_t Network::EncodeSoundRLE(struct NetworkUpdate *dst, Uint8 *buffer, size_t buf_len) { @@ -493,7 +505,7 @@ bool Network::SendUpdate(int sock) return false; if (this->SendData((void*)src, sock, sz) == false) return false; - this->bytes_sent += sz; + this->traffic += sz; return true; } diff --git a/Src/Network.h b/Src/Network.h index 7c76add..d2d045c 100644 --- a/Src/Network.h +++ b/Src/Network.h @@ -62,14 +62,20 @@ public: void DrawTransferredBlocks(SDL_Surface *screen); - size_t GetBytesSent() { - return this->bytes_sent; + size_t GetKbps() { + return this->kbps; + } + + bool ThrottleTraffic() { + return this->kbps > this->target_kbps; } void ResetBytesSent() { - this->bytes_sent = 0; + this->traffic = 0; } + void Tick(int ms); + void CloseSocket(int sock); bool SendUpdate(int sock); @@ -167,8 +173,12 @@ protected: Uint8 *raw_buf; Uint8 *rle_buf; Uint8 *diff_buf; - size_t bytes_sent; Uint32 *square_updated; + + size_t traffic, last_traffic; + int time_since_last_reset; + int target_kbps; + int kbps; }; class NetworkClient : public Network diff --git a/Src/NetworkUnix.h b/Src/NetworkUnix.h index 48b4623..ad4c2d4 100644 --- a/Src/NetworkUnix.h +++ b/Src/NetworkUnix.h @@ -158,6 +158,7 @@ bool Network::ReceiveData(void *dst, int sock, size_t sz) return false; received_sz += v; } + this->traffic += received_sz; return sz > 0; }