/// <summary>Used internally by the netcode to process a received connection request.</summary> private void ReceiveRequest(IPEndPoint remote, ArraySegment <byte> packet) { // Make sure packet is not too short for header if (packet.Count < 4) { throw new FormatException(string.Format( "Connection request from {0} with length {1} is too short for header", remote, packet.Count )); } // Extract header ushort lengthKey = Serializer.ReadUInt16(packet.Array, packet.Offset); ushort lengthData = Serializer.ReadUInt16(packet.Array, packet.Offset + 2); int lengthMessage = packet.Count - lengthKey - lengthData - 4; // Make sure packet is not too short for data if (packet.Count < 4 + lengthKey + lengthData) { throw new FormatException(string.Format( "Connection request from {0} with length {1} is less than header (4) + key {2} + data {3}", remote, packet.Count, lengthKey, lengthData )); } // Extract data ArraySegment <byte> key = new ArraySegment <byte>(packet.Array, packet.Offset + 4, lengthKey); ArraySegment <byte> random = new ArraySegment <byte>(packet.Array, packet.Offset + 4 + lengthKey, lengthData); ArraySegment <byte> message = new ArraySegment <byte>(packet.Array, 4 + lengthKey + lengthData, lengthMessage); // Process connection request using (ConnectionRequest request = new ConnectionRequest(this, remote, key, random)) using (Reader reader = new Reader(message)) { Peer peer = FindPeer(remote); if (peer == null) { // Peer does not yet exist, notify listener Listener.OnHostReceiveRequest(request, reader); } else { // Peer already exists, automatically accept // This is done because connection requests can be resent peer.Accept(request); } } }