private void SendConnectRequest() { //Make initial packet var connectPacket = _packetPool.Get(PacketProperty.ConnectRequest, 0, sizeof(int) + sizeof(long) + _connectData.Length); //Add data FastBitConverter.GetBytes(connectPacket.RawData, 0, NetConstants.ProtocolId); FastBitConverter.GetBytes(connectPacket.RawData, sizeof(int), _connectId); Buffer.BlockCopy(_connectData.Data, 0, connectPacket.RawData, sizeof(int) + sizeof(long), _connectData.Length); //Send raw _netManager.SendRawAndRecycle(connectPacket, _remoteEndPoint); }
//Update function private void UpdateLogic() { long startNowMs = NetTime.NowMs; long timeout = startNowMs + UpdateTime; if (_receiveBuffer == null) { _receiveBuffer = NetPacketPool.Get(PacketProperty.Sequenced, 0, NetConstants.MaxPacketSize); } //while (true) { #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) { //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); } } #if STATS_ENABLED Statistics.PacketLoss = totalPacketLoss; #endif _socket.Receive(false, _receiveBuffer.RawData); _socket.Receive(true, _receiveBuffer.RawData); PollEvents(); long currentNowMs = NetTime.NowMs; int remainingTime = (int)(timeout - currentNowMs); Thread.Sleep(remainingTime > 0 ? remainingTime : 0); 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; //break; } }