Exemple #1
0
 public void JoinRoom(string roomName)
 {
     _writer.Reset();
     _writer.Write((byte)0);
     _writer.Write(roomName);
     _connection.Send(_writer.ToArray());
 }
        public TResponse Execute <TResponse>(IProtocolRequest <TResponse> request)
            where TResponse : class, IProtocolResponse, new()
        {
            if (_connection == null)
            {
                throw new ConnectionRequiredException();
            }

            MessageHeadersDictionary headers = new MessageHeadersDictionary();

            byte[] requestHeaders    = ProtocolMessageHeaderSerializer.Serialize(headers);
            byte[] requestBody       = ProtocolBodySerializer.Serialize(request);
            byte[] requestIdentifier = _encoder.Encode(request.GetIdentifier());
            IList <ArraySegment <byte> > serializedRequest = ProtocolSerializer.Serialize(requestIdentifier, requestHeaders, requestBody);

            byte[] responseEncoded = _connection.Send(serializedRequest);

            var    deserializedObject = ProtocolSerializer.Deserialize(responseEncoded);
            string expectedIdentifier = new TResponse().GetIdentifier();
            string identifier         = _encoder.Decode(deserializedObject.ControlCommand);

            if (identifier != expectedIdentifier)
            {
                if (identifier == ErrorResponse.Identifier)
                {
                    ErrorResponse errorResponse = ProtocolBodySerializer.Deserialize <ErrorResponse>(deserializedObject.Body);
                    throw new CustomException(errorResponse.Message);
                }

                throw new InvalidResponseException();
            }

            TResponse response = ProtocolBodySerializer.Deserialize <TResponse>(deserializedObject.Body);

            return(response);
        }
Exemple #3
0
            public bool ProcessIncomingData(TunnelState tunnelState, PacketData data)
            {
                string ipaddress = tunnelState.Connection.RemoteEndPoint.ToString();

                //Console.WriteLine(ipaddress);

                if (!Engine.PlayersByTunnelEndPoint.ContainsKey(ipaddress))
                {
                    //Console.WriteLine("This is from anonymous");

                    IServerConnection connection = tunnelState.Connection;
                    //Console.WriteLine("Inform client about its external port: {0}", connection.RemoteEndPoint);
                    connection.Send(connection.RemoteEndPoint.Port.ToString());

                    return(true);
                }
                PlayerState player = Engine.PlayersByTunnelEndPoint[ipaddress];
                Room        room   = player.CurrentRoom;

                if (room == Engine.Lobby)
                {
                    return(true);
                }

                byte[] packet = data.RawBytes;
                //Console.WriteLine(BitConverter.ToString(packet));
                //Console.WriteLine();
                //Console.WriteLine(packet.Length);

                if (!Utility.IsPspPacket(packet, Utility.HEADER_OFFSET))
                {
                    //Console.WriteLine("This is not PSP Packet");
                    return(true);
                }

                byte[] buff = new byte[6];
                Array.Copy(packet, Utility.HEADER_OFFSET, buff, 0, 6);
                PhysicalAddress destMac = new PhysicalAddress(buff);

                Array.Copy(packet, Utility.HEADER_OFFSET + 6, buff, 0, 6);
                PhysicalAddress srcMac = new PhysicalAddress(buff);

                string destMacStr = destMac.ToString();
                string srcMacStr  = srcMac.ToString();

                lock (Engine.PlayersByMacAddress)
                    Engine.PlayersByMacAddress[srcMacStr] = player;

                //Console.WriteLine("Tick = {0}", BitConverter.ToUInt64(packet, 0));
                //Console.WriteLine("Tunnel packet: {0} -> {1} size={2}", srcMacStr, destMacStr, packet.Length - Utility.HEADER_OFFSET);

                if (destMac.Equals(Utility.GetBroadcastAddress()))
                {
                    //Console.WriteLine("Broadcast packet from {0}", player.Name);
                    // send to other players in room except me
                    room.ForEach((ForEachPlayerAction) delegate(PlayerState sendTo)
                    {
                        IPEndPoint remoteEP = sendTo.RemoteTunnelEndPoint;
                        if (remoteEP != null)
                        {
                            //Console.WriteLine("Broadcasting from {0} to {1}", player.Name, sendTo.Name);
                            Engine.TunnelServer.Send(remoteEP, packet, packet.Length);
                        }
                        else
                        {
                            //Console.WriteLine("But {0} doesn't have UDP tunnel port;", sendTo.Name);
                        }
                    }, player);
                }
                else
                {
                    // send to specific player
                    if (Engine.PlayersByMacAddress.ContainsKey(destMacStr))
                    {
                        PlayerState sendTo = Engine.PlayersByMacAddress[destMacStr];
                        if (sendTo.CurrentRoom != room)
                        {
                            //Console.WriteLine("Players are not in the same room: {0} [{1}] => {2} [{3}]",
                            //    player.Name, srcMacStr, sendTo.Name, destMacStr);
                            return(true);
                        }

                        //Console.WriteLine("Tunnel: {0} [{1}] => {2} [{3}]", player.Name, srcMacStr, sendTo.Name, destMacStr);
                        IPEndPoint remoteEP = sendTo.RemoteTunnelEndPoint;
                        if (remoteEP != null)
                        {
                            Engine.TunnelServer.Send(remoteEP, packet, packet.Length);
                        }
                        else
                        {
                            //Console.WriteLine("But {0} doesn't have UDP tunnel port;", sendTo.Name);
                        }
                    }
                    else
                    {
                        //Console.WriteLine("Player not found from MAC Address: {0}", destMac.ToString());
                    }
                }
                return(false);
            }