public void HandleEncapsulatedPacketRoute(EncapsulatedPacket packet) { if (this.Manager == null) { return; } int id = packet.Buffer[0]; RakNetPacket pk = this.Manager.GetRakNetPacket(id, packet.Buffer); if (pk != null) { if (id < 0x86) { if (this.State == SessionState.Connecting) { if (id == RakNetProtocol.ClientConnectDataPacket) { ClientConnectDataPacket ccd = (ClientConnectDataPacket)pk; ServerHandShakeDataPacket shd = new ServerHandShakeDataPacket(); shd.EndPoint = this.EndPoint; shd.SendPing = ccd.SendPing; shd.SendPong = ccd.SendPing + 1000; this.QueueConnectedPacket(shd, RakNetPacketReliability.UNRELIABLE, -1, RakNetProtocol.FlagImmediate); } else if (id == RakNetProtocol.ClientHandShakeDataPacket) { ClientHandShakeDataPacket chsd = (ClientHandShakeDataPacket)pk; if (chsd.EndPoint.Port == Server.Instance.EndPoint.Port) { this.State = SessionState.Connected; } } } else if (id == RakNetProtocol.ClientDisconnectDataPacket) { this.Disconnect("clientDisconnect"); } else if (id == RakNetProtocol.OnlinePing) { OnlinePing ping = (OnlinePing)pk; OnlinePong pong = new OnlinePong(); pong.PingID = ping.PingID; this.LastPingTime = SendPingTime; this.QueueConnectedPacket(pong, RakNetPacketReliability.UNRELIABLE, -1, RakNetProtocol.FlagImmediate); } else if (id == RakNetProtocol.OnlinePong) { } } else if (this.State == SessionState.Connected) { if (id == RakNetProtocol.BatchPacket) { this.HandleBatchPacket((BatchPacket)pk); } } } }
public void OnUpdate() { if (this.LastUpdateTime < 0) { this.Disconnect("timedout"); return; } if (this.ACKQueue.Count > 0) { Ack pk = new Ack(); List <int> pks = new List <int>(); foreach (KeyValuePair <int, int> kv in this.ACKQueue.ToArray()) { pks.Add(kv.Value); } pks.Sort(); pk.Packets = pks.ToArray(); this.SendPacket(pk); this.ACKQueue.Clear(); } if (this.NACKQueue.Count > 0) { Nack pk = new Nack(); List <int> pks = new List <int>(); foreach (KeyValuePair <int, int> kv in this.NACKQueue.ToArray()) { pks.Add(kv.Value); } pks.Sort(); pk.Packets = pks.ToArray(); this.SendPacket(pk); this.NACKQueue.Clear(); } if (this.ResendQueue.Count > 0) { for (int i = 0; i < this.ResendQueue.Count; ++i) { DataPacket pk; this.ResendQueue.TryDequeue(out pk); pk.SeqNumber = this.LastSendSeqNumber++; this.SendPacket(pk); Logger.Debug("%server.network.dataPacket.resend"); } } if (this.SendedPacket.Count > 0) { foreach (DataPacket pk in this.SendedPacket.Values) { if (pk.SendTimedOut < 0) { DataPacket remove; this.ResendQueue.Enqueue(pk); this.SendedPacket.TryRemove(pk.SeqNumber, out remove); } --pk.SendTimedOut; } } if (this.LastPingTime < 0) { OnlinePing ping = new OnlinePing(); ping.PingID = DateTime.Now.Ticks; this.QueueConnectedPacket(ping, RakNetPacketReliability.UNRELIABLE, -1, RakNetProtocol.FlagImmediate); } this.SendQueuePacket(); --this.LastPingTime; --this.LastUpdateTime; }