/// <inheritdoc /> public void Update() { if (_mode.IsServerEnabled()) { var state = _net.Server.Update(); // If we encounter an error during update then the best we can do is slam the connection shut and try to open a new one. Since this is the server it... // ...will cause all of the clients to error too (they will communicate with the new server, and get back an incorrect session reponse). They will... // ...disconnect themselves and connect to the new server. if (state == ServerState.Error) { _net.Log.Warn("Server update encountered an error - restarting server"); // The server should have already shut itself down internally when it detected an error. Call this to perform clean up at the BaseCommsNetwork level. // It's safe to call Disconnect on the server multiple times so it's not a problem that it may have already been called by the server itself. _net.StopServer(); StartServer(); } } if (_mode.IsClientEnabled()) { if (_net.Client != null) { var state = _net.Client.Update(); // If we encounter an error during update we instantly slam shut the connection and, after a small delay, attempt to create a new client. Call... // ...StopClient to perform clean up at the BaseCommsNetwork level. This will call Disconnect on the client (which should have already been called... // ...by the client itself) but that's fine, it's safe to call multiple times. if (state == ClientStatus.Error) { _net.Log.Warn("Client update encountered an error - shutting down client"); _net.StopClient(); } else { //While the client is running without any errors reduce the reconnection interval linearly. _reconnectionAttemptInterval = Math.Max(0, _reconnectionAttemptInterval - Time.deltaTime); } } if (_net.Client == null && ShouldAttemptReconnect()) { _net.Log.Info("Attempting to restart client"); StartClient(); //Every time we start a new client increase the interval until we're allowed to start another client (linear backoff) _reconnectionAttemptInterval = Math.Min(3, _reconnectionAttemptInterval + 0.5f); } } }
public void Update() { if (_mode.IsServerEnabled()) { var state = _net.Server.Update(); if (state == ServerState.Error) { _net.Log.Trace("Restarting server"); _net.StopServer(); StartServer(); } } if (_mode.IsClientEnabled()) { if (_net.Client != null) { var state = _net.Client.Update(); if (state == ClientStatus.Error) { _net.StopClient(); } else { _reconnectionAttemptInterval = Math.Max(0, _reconnectionAttemptInterval - Time.deltaTime); } } if (_net.Client == null && ShouldAttemptReconnect()) { _net.Log.Trace("Restarting client"); StartClient(); _reconnectionAttemptInterval = Math.Min(3, _reconnectionAttemptInterval + 0.5f); } } }