public static GetUnconnectedData ( byte raw, int count ) : byte[] | ||
raw | byte | |
count | int | |
Результат | byte[] |
private void DataReceived(byte[] reusableBuffer, int count, NetEndPoint remoteEndPoint) { //Try get packet property PacketProperty property; if (!NetPacket.GetPacketProperty(reusableBuffer, out property)) { return; } //Check unconnected switch (property) { case PacketProperty.UnconnectedMessage: if (UnconnectedMessagesEnabled) { var netEvent = CreateEvent(NetEventType.ReceiveUnconnected); netEvent.RemoteEndPoint = remoteEndPoint; netEvent.DataReader.SetSource(NetPacket.GetUnconnectedData(reusableBuffer, count)); EnqueueEvent(netEvent); } return; case PacketProperty.NatIntroduction: case PacketProperty.NatIntroductionRequest: case PacketProperty.NatPunchMessage: if (NatPunchEnabled) { NatPunchModule.ProcessMessage(remoteEndPoint, property, NetPacket.GetUnconnectedData(reusableBuffer, count)); } return; } //other ReceiveFromSocket(reusableBuffer, count, remoteEndPoint); }
private void DataReceived(byte[] reusableBuffer, int count, NetEndPoint remoteEndPoint) { #if STATS_ENABLED PacketsReceived++; BytesReceived += (uint)count; #endif //Try get packet property PacketProperty property; if (!NetPacket.GetPacketProperty(reusableBuffer, out property)) { return; } //Check unconnected switch (property) { case PacketProperty.DiscoveryRequest: if (DiscoveryEnabled) { var netEvent = CreateEvent(NetEventType.DiscoveryRequest); netEvent.RemoteEndPoint = remoteEndPoint; netEvent.DataReader.SetSource(NetPacket.GetUnconnectedData(reusableBuffer, count)); EnqueueEvent(netEvent); } return; case PacketProperty.DiscoveryResponse: { var netEvent = CreateEvent(NetEventType.DiscoveryResponse); netEvent.RemoteEndPoint = remoteEndPoint; netEvent.DataReader.SetSource(NetPacket.GetUnconnectedData(reusableBuffer, count)); EnqueueEvent(netEvent); } return; case PacketProperty.UnconnectedMessage: if (UnconnectedMessagesEnabled) { var netEvent = CreateEvent(NetEventType.ReceiveUnconnected); netEvent.RemoteEndPoint = remoteEndPoint; netEvent.DataReader.SetSource(NetPacket.GetUnconnectedData(reusableBuffer, count)); EnqueueEvent(netEvent); } return; case PacketProperty.NatIntroduction: case PacketProperty.NatIntroductionRequest: case PacketProperty.NatPunchMessage: { if (NatPunchEnabled) { NatPunchModule.ProcessMessage(remoteEndPoint, property, NetPacket.GetUnconnectedData(reusableBuffer, count)); } return; } } //Check normal packets NetPacket packet; NetPeer netPeer; //Check peers Monitor.Enter(_peers); int peersCount = _peers.Count; if (_peers.TryGetValue(remoteEndPoint, out netPeer)) { Monitor.Exit(_peers); packet = netPeer.GetPacketFromPool(init: false); //Bad packet check if (!packet.FromBytes(reusableBuffer, 0, count)) { netPeer.Recycle(packet); return; } //Send if (packet.Property == PacketProperty.Disconnect) { if (BitConverter.ToInt64(packet.RawData, 1) != netPeer.ConnectId) { //Old or incorrect disconnect netPeer.Recycle(packet); return; } var netEvent = CreateEvent(NetEventType.Disconnect); netEvent.Peer = netPeer; netEvent.DataReader.SetSource(packet.RawData, 5, packet.RawData.Length - 5); netEvent.DisconnectReason = DisconnectReason.RemoteConnectionClose; EnqueueEvent(netEvent); lock (_peersToRemove) { _peersToRemove.Enqueue(netPeer.EndPoint); } //do not recycle because no sense) } else if (packet.Property == PacketProperty.ConnectAccept) { if (netPeer.ProcessConnectAccept(packet)) { var connectEvent = CreateEvent(NetEventType.Connect); connectEvent.Peer = netPeer; EnqueueEvent(connectEvent); } netPeer.Recycle(packet); } else { netPeer.ProcessPacket(packet); } return; } try { //Else add new peer packet = new NetPacket(); if (!packet.FromBytes(reusableBuffer, 0, count)) { //Bad packet return; } if (peersCount < _maxConnections && packet.Property == PacketProperty.ConnectRequest) { int protoId = BitConverter.ToInt32(packet.RawData, 1); if (protoId != NetConstants.ProtocolId) { NetUtils.DebugWrite(ConsoleColor.Cyan, "[NS] Peer connect reject. Invalid protocol ID: " + protoId); return; } string peerKey = Encoding.UTF8.GetString(packet.RawData, 13, packet.RawData.Length - 13); if (peerKey != _connectKey) { NetUtils.DebugWrite(ConsoleColor.Cyan, "[NS] Peer connect reject. Invalid key: " + peerKey); return; } //Getting new id for peer long connectionId = BitConverter.ToInt64(packet.RawData, 5); //response with id NetUtils.DebugWrite(ConsoleColor.Cyan, "[NS] Received peer connect request Id: {0}, EP: {1}", netPeer.ConnectId, remoteEndPoint); netPeer = new NetPeer(this, remoteEndPoint, connectionId); //clean incoming packet netPeer.Recycle(packet); _peers.Add(remoteEndPoint, netPeer); var netEvent = CreateEvent(NetEventType.Connect); netEvent.Peer = netPeer; EnqueueEvent(netEvent); } } finally { Monitor.Exit(_peers); } }