コード例 #1
0
        public Connection <T> GetConnection(IPEndPoint endPoint)
        {
            Guid storeKey = DarkUtils.GuidFromIPEndpoint(endPoint);

            if (connections.ContainsKey(storeKey))
            {
                return(connections[storeKey]);
            }
            return(null);
        }
コード例 #2
0
        internal void HandleRaw(byte[] data, int length, IPEndPoint endPoint)
        {
            if (length < 12)
            {
                return;
            }
            //Magic header, DARK
            if (data[0] != 68 || data[1] != 65 || data[2] != 82 || data[3] != 75)
            {
                return;
            }
            Connection <T> connection   = null;
            Guid           messageOwner = DarkUtils.GuidFromIPEndpoint(endPoint);

            if (!connections.ContainsKey(messageOwner))
            {
                if (connection == null)
                {
                    connection         = new Connection <T>();
                    connection.handler = this;
                    connection.reliableMessageHandler = new ReliableMessageHandler <T>(connection, this);
                    if (network.clientMode && serverConnection == null)
                    {
                        serverConnection = connection;
                    }
                }
                connection.lastReceiveTime = DateTime.UtcNow.Ticks;
                connection.remoteEndpoint  = endPoint;
                lock (connections)
                {
                    connections.Add(messageOwner, connection);
                }
                if (connectCallback != null)
                {
                    connection.state = connectCallback(connection);
                }
            }
            connection = connections[messageOwner];
            int messageType   = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 4));
            int messageLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(data, 8));

            if (length < messageLength + 12)
            {
                //Malformed message
                return;
            }
            NetworkMessage nm = NetworkMessage.Create(messageType, messageLength, NetworkMessageType.UNORDERED_UNRELIABLE);

            if (nm.data != null && nm.data.Length > 0)
            {
                Array.Copy(data, 12, nm.data.data, 0, nm.data.Length);
            }
            Handle(nm, connection);
        }