internal bool SendRawAndRecycle(NetPacket packet, NetEndPoint remoteEndPoint) { var result = SendRaw(packet.RawData, 0, packet.Size, remoteEndPoint); _netPacketPool.Recycle(packet); return(result); }
public void Recycle() { if (_packetPool != null) { _packetPool.Recycle(this); } }
internal void AddIncomingPacket(NetPacket p) { if (p.IsFragmented) { NetDebug.Write("Fragment. Id: {0}, Part: {1}, Total: {2}", p.FragmentId, p.FragmentPart, p.FragmentsTotal); //Get needed array from dictionary ushort packetFragId = p.FragmentId; IncomingFragments incomingFragments; if (!_holdedFragments.TryGetValue(packetFragId, out incomingFragments)) { incomingFragments = new IncomingFragments { Fragments = new NetPacket[p.FragmentsTotal], ChannelId = p.ChannelId }; _holdedFragments.Add(packetFragId, incomingFragments); } //Cache var fragments = incomingFragments.Fragments; //Error check if (p.FragmentPart >= fragments.Length || fragments[p.FragmentPart] != null || p.ChannelId != incomingFragments.ChannelId) { _packetPool.Recycle(p); NetDebug.WriteError("Invalid fragment packet"); return; } //Fill array fragments[p.FragmentPart] = p; //Increase received fragments count incomingFragments.ReceivedCount++; //Increase total size incomingFragments.TotalSize += p.Size - NetConstants.FragmentTotalSize; //Check for finish if (incomingFragments.ReceivedCount != fragments.Length) { return; } NetPacket resultingPacket = _packetPool.GetWithProperty(p.Property, incomingFragments.TotalSize); resultingPacket.ChannelId = incomingFragments.ChannelId; int resultingPacketOffset = resultingPacket.GetHeaderSize(); int firstFragmentSize = fragments[0].Size - NetConstants.FragmentTotalSize; for (int i = 0; i < incomingFragments.ReceivedCount; i++) { //Create resulting big packet int fragmentSize = fragments[i].Size - NetConstants.FragmentTotalSize; Buffer.BlockCopy( fragments[i].RawData, NetConstants.FragmentTotalSize, resultingPacket.RawData, resultingPacketOffset + firstFragmentSize * i, fragmentSize); //Free memory _packetPool.Recycle(fragments[i]); fragments[i] = null; } //Send to process NetManager.ReceiveFromPeer(resultingPacket, this); //Clear memory _holdedFragments.Remove(packetFragId); } else //Just simple packet { NetManager.ReceiveFromPeer(p, this); } }
internal void AddReliablePacket(DeliveryMethod method, NetPacket p) { if (p.IsFragmented) { NetDebug.Write("Fragment. Id: {0}, Part: {1}, Total: {2}", p.FragmentId, p.FragmentPart, p.FragmentsTotal); //Get needed array from dictionary ushort packetFragId = p.FragmentId; IncomingFragments incomingFragments; if (!_holdedFragments.TryGetValue(packetFragId, out incomingFragments)) { incomingFragments = new IncomingFragments { Fragments = new NetPacket[p.FragmentsTotal], ChannelId = p.ChannelId }; _holdedFragments.Add(packetFragId, incomingFragments); } //Cache var fragments = incomingFragments.Fragments; //Error check if (p.FragmentPart >= fragments.Length || fragments[p.FragmentPart] != null || p.ChannelId != incomingFragments.ChannelId) { _packetPool.Recycle(p); NetDebug.WriteError("Invalid fragment packet"); return; } //Fill array fragments[p.FragmentPart] = p; //Increase received fragments count incomingFragments.ReceivedCount++; //Increase total size incomingFragments.TotalSize += p.Size - NetConstants.FragmentedHeaderTotalSize; //Check for finish if (incomingFragments.ReceivedCount != fragments.Length) { return; } //just simple packet NetPacket resultingPacket = _packetPool.GetPacket(incomingFragments.TotalSize); int firstFragmentSize = fragments[0].Size - NetConstants.FragmentedHeaderTotalSize; for (int i = 0; i < incomingFragments.ReceivedCount; i++) { var fragment = fragments[i]; //Create resulting big packet Buffer.BlockCopy( fragment.RawData, NetConstants.FragmentedHeaderTotalSize, resultingPacket.RawData, firstFragmentSize * i, fragment.Size - NetConstants.FragmentedHeaderTotalSize); //Free memory _packetPool.Recycle(fragment); } Array.Clear(fragments, 0, incomingFragments.ReceivedCount); //Send to process NetManager.CreateReceiveEvent(resultingPacket, method, 0, this); //Clear memory _holdedFragments.Remove(packetFragId); } else //Just simple packet { NetManager.CreateReceiveEvent(p, method, NetConstants.ChanneledHeaderSize, this); } }
//from user thread, our thread, or recv? private void SendPacket(NetPacket packet) { NetUtils.DebugWrite("[RS]Packet: " + packet.Property); switch (packet.Property) { case PacketProperty.Reliable: _reliableUnorderedChannel.AddToQueue(packet); break; case PacketProperty.Sequenced: _sequencedChannel.AddToQueue(packet); break; case PacketProperty.ReliableOrdered: _reliableOrderedChannel.AddToQueue(packet); break; case PacketProperty.Unreliable: _simpleChannel.AddToQueue(packet); break; case PacketProperty.MtuCheck: //Must check result for MTU fix if (!_peerListener.SendRawAndRecycle(packet, _remoteEndPoint)) { _finishMtu = true; } break; case PacketProperty.AckReliable: case PacketProperty.AckReliableOrdered: case PacketProperty.Ping: case PacketProperty.Pong: case PacketProperty.Disconnect: case PacketProperty.MtuOk: SendRawData(packet); _packetPool.Recycle(packet); break; default: throw new Exception("Unknown packet property: " + packet.Property); } }
internal bool ProcessConnectAccept(NetPacket packet) { if (_connectionState != ConnectionState.InProgress) { return(false); } //check connection id if (BitConverter.ToInt64(packet.RawData, NetConstants.AcceptConnectIdIndex) != _connectId) { NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Invalid connectId: {0}", _connectId); return(false); } NetUtils.DebugWrite(ConsoleColor.Cyan, "[NC] Received connection accept"); _timeSinceLastPacket = 0; _connectionState = ConnectionState.Connected; _packetPool.Recycle(packet); return(true); }
//from user thread, our thread, or recv? private void SendPacket(NetPacket packet) { NetUtils.DebugWrite("[RS]Packet: " + packet.Property); switch (packet.Property) { case PacketProperty.ReliableUnordered: _reliableUnorderedChannel.AddToQueue(packet); break; case PacketProperty.Sequenced: _sequencedChannel.AddToQueue(packet); break; case PacketProperty.ReliableOrdered: _reliableOrderedChannel.AddToQueue(packet); break; case PacketProperty.Unreliable: _simpleChannel.AddToQueue(packet); break; case PacketProperty.ReliableSequenced: _reliableSequencedChannel.AddToQueue(packet); break; case PacketProperty.AckReliable: case PacketProperty.AckReliableOrdered: case PacketProperty.Ping: case PacketProperty.Pong: SendRawData(packet); _packetPool.Recycle(packet); break; default: throw new InvalidPacketException("Unknown packet property: " + packet.Property); } }