protected override void PostProcessEvent(int deltaTime) { if (_peer == null) { return; } if (!_connected) { _connectTimer += deltaTime; if (_connectTimer > ReconnectDelay) { _connectTimer = 0; _connectAttempts++; if (_connectAttempts > MaxConnectAttempts) { CloseConnection(true, DisconnectReason.ConnectionFailed, 0); return; } //else send connect again SendConnectRequest(); } } else if (_peer.TimeSinceLastPacket > DisconnectTimeout) { CloseConnection(true, DisconnectReason.Timeout, 0); return; } _peer.Update(deltaTime); }
protected override void PostProcessEvent(int deltaTime) { if (_peer == null) { return; } if (!_connected) { _connectTimer += deltaTime; if (_connectTimer > _reconnectDelay) { _connectTimer = 0; _connectAttempts++; if (_connectAttempts > _maxConnectAttempts) { var netEvent = CreateEvent(NetEventType.Disconnect); netEvent.AdditionalInfo = "connection timeout"; EnqueueEvent(netEvent); Stop(); return; } //else send connect again SendConnectRequest(); } } else if (_peer.TimeSinceLastPacket > DisconnectTimeout) { Stop(); var netEvent = CreateEvent(NetEventType.Disconnect); netEvent.AdditionalInfo = "Timeout"; EnqueueEvent(netEvent); return; } _peer.Update(deltaTime); }
//Update function private void UpdateLogic() { long startNowMs = NetTime.NowMs; NetPacket receiveBuffer = NetPacketPool.Get(PacketProperty.Sequenced, 0, NetConstants.MaxPacketSize); while (IsRunning) { #if DEBUG if (SimulateLatency) { var time = DateTime.UtcNow; lock (_pingSimulationList) { for (int i = 0; i < _pingSimulationList.Count; i++) { var incomingData = _pingSimulationList[i]; if (incomingData.TimeWhenGet <= time) { DataReceived(incomingData.Data, incomingData.Data.Length, incomingData.EndPoint); _pingSimulationList.RemoveAt(i); i--; } } } } #endif #if STATS_ENABLED ulong totalPacketLoss = 0; #endif // Flush disconnection first lock (_peers) { for (int i = 0; i < _peers.Count; i++) { NetPeer netPeer = _peers[i]; if (netPeer.ConnectionState == ConnectionState.Disconnected) { _peers.Remove(netPeer); } } //Process acks for (int i = 0; i < _peers.Count; i++) { NetPeer netPeer = _peers[i]; netPeer.Update(UpdateTime); #if STATS_ENABLED totalPacketLoss += netPeer.Statistics.PacketLoss; #endif } //Process ping for (int i = 0; i < _peers.Count; i++) { _peers[i].ProcessPong(UpdateTime); } } _socket.Receive(false, receiveBuffer.RawData); _socket.Receive(true, receiveBuffer.RawData); #if STATS_ENABLED Statistics.PacketLoss = totalPacketLoss; #endif Thread.Sleep(UpdateTime); long currentNowMs = NetTime.NowMs; long elapsedNowMs = currentNowMs - startNowMs; startNowMs = currentNowMs; AvgUpdateTime = (long)((elapsedNowMs * 6.0f + _updateTimeFilter[0] * 3.0f + _updateTimeFilter[1] * 2.0f + _updateTimeFilter[2] * 1.0f) / 12.0f); _updateTimeFilter[2] = _updateTimeFilter[1]; _updateTimeFilter[1] = _updateTimeFilter[0]; _updateTimeFilter[0] = elapsedNowMs; } }