MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / SendReceive

Method SendReceive

src/network/network_coordinator.cpp:732–813  ·  view source on GitHub ↗

* Check whether we received/can send some data from/to the Game Coordinator server and * when that's the case handle it appropriately. */

Source from the content-addressed store, hash-verified

730 * when that's the case handle it appropriately.
731 */
732void ClientNetworkCoordinatorSocketHandler::SendReceive()
733{
734 /* Private games are not listed via the Game Coordinator. */
735 if (_network_server && _settings_client.network.server_game_type == SERVER_GAME_TYPE_LOCAL) {
736 if (this->sock != INVALID_SOCKET) {
737 this->CloseConnection();
738 }
739 return;
740 }
741
742 static int last_attempt_backoff = 1;
743 static bool first_reconnect = true;
744
745 if (this->sock == INVALID_SOCKET) {
746 static std::chrono::steady_clock::time_point last_attempt = {};
747
748 /* Don't auto-reconnect when we are not a server. */
749 if (!_network_server) return;
750 /* Don't reconnect if we are connecting. */
751 if (this->connecting) return;
752 /* Throttle how often we try to reconnect. */
753 if (std::chrono::steady_clock::now() < last_attempt + std::chrono::seconds(1) * last_attempt_backoff) return;
754
755 last_attempt = std::chrono::steady_clock::now();
756 /* Delay reconnecting with up to 32 seconds. */
757 if (last_attempt_backoff < 32) {
758 last_attempt_backoff *= 2;
759 }
760
761 /* Do not reconnect on the first attempt, but only initialize the
762 * last_attempt variables. Otherwise after an outage all servers
763 * reconnect at the same time, potentially overwhelming the
764 * Game Coordinator. */
765 if (first_reconnect) {
766 first_reconnect = false;
767 return;
768 }
769
770 Debug(net, 1, "Connection with Game Coordinator lost; reconnecting...");
771 this->Register();
772 return;
773 }
774
775 last_attempt_backoff = 1;
776 first_reconnect = true;
777
778 if (_network_server && _network_server_connection_type != CONNECTION_TYPE_UNKNOWN && std::chrono::steady_clock::now() > this->next_update) {
779 this->SendServerUpdate();
780 }
781
782 if (!_network_server && std::chrono::steady_clock::now() > this->last_activity + IDLE_TIMEOUT) {
783 this->CloseConnection();
784 return;
785 }
786
787 if (this->CanSendReceive()) {
788 if (this->ReceivePackets()) {
789 this->last_activity = std::chrono::steady_clock::now();

Callers

nothing calls this directly

Calls 11

CloseConnectionMethod · 0.95
RegisterMethod · 0.95
SendServerUpdateMethod · 0.95
nowClass · 0.85
CanSendReceiveMethod · 0.80
SendPacketsMethod · 0.80
ReceivePacketsMethod · 0.45
beginMethod · 0.45
endMethod · 0.45
IsConnectedMethod · 0.45
eraseMethod · 0.45

Tested by

no test coverage detected