예제 #1
0
		private Task Client_OnSent(object Sender, byte[] Buffer, int Offset, int Count)
		{
			if (this.HasSniffers)
				this.TransmitBinary(BinaryTcpClient.ToArray(Buffer, Offset, Count));

			return Task.FromResult<bool>(true);
		}
예제 #2
0
		private async Task<bool> Client_OnReceived(object Sender, byte[] Buffer, int Offset, int Count)
		{
			if (this.HasSniffers)
				this.ReceiveBinary(BinaryTcpClient.ToArray(Buffer, Offset, Count));

			try
			{
				await this.ParseIncoming(Buffer, Offset, Count);
				return true;
			}
			catch (Exception ex)
			{
				Log.Critical(ex);
				return false;
			}
		}
예제 #3
0
        private Task <bool> Client_OnReceived(object Sender, byte[] Buffer, int Offset, int Count)
        {
            if (this.HasSniffers)
            {
                this.ReceiveBinary(BinaryTcpClient.ToArray(Buffer, Offset, Count));
            }

            try
            {
                this.ParseIncoming(Buffer, Offset, Count);
            }
            catch (Exception ex)
            {
                Log.Critical(ex);
            }

            return(Task.FromResult <bool>(true));
        }
예제 #4
0
        private async Task <bool> Client_OnReceived(object Sender, byte[] Buffer, int Offset, int Count)
        {
            if (this.HasSniffers)
            {
                this.ReceiveBinary(BinaryTcpClient.ToArray(Buffer, Offset, Count));
            }

            byte b;
            bool Result = true;

            while (Count-- > 0)
            {
                b = Buffer[Offset++];

                switch (this.inputState)
                {
                case 0:
                    this.inputPacket = new MemoryStream();
                    this.inputPacket.WriteByte(b);
                    this.inputPacketType      = (MqttControlPacketType)(b >> 4);
                    this.inputRemainingLength = 0;
                    this.inputOffset          = 0;
                    this.inputState++;
                    break;

                case 1:
                    this.inputRemainingLength |= ((b & 127) << this.inputOffset);
                    this.inputOffset          += 7;

                    this.inputPacket.WriteByte(b);
                    if ((b & 128) == 0)
                    {
                        switch (this.inputPacketType)
                        {
                        case MqttControlPacketType.CONNECT:
                        case MqttControlPacketType.CONNACK:
                        case MqttControlPacketType.PINGREQ:
                        case MqttControlPacketType.PINGRESP:
                        case MqttControlPacketType.DISCONNECT:
                        case MqttControlPacketType.PUBLISH:
                        default:
                            if (this.inputRemainingLength == 0)
                            {
                                this.inputState = 0;
                                if (!await this.ProcessInputPacket())
                                {
                                    Result = false;
                                }
                            }
                            else
                            {
                                this.inputState += 3;
                            }
                            break;

                        case MqttControlPacketType.PUBACK:
                        case MqttControlPacketType.PUBREC:
                        case MqttControlPacketType.PUBREL:
                        case MqttControlPacketType.PUBCOMP:
                        case MqttControlPacketType.SUBSCRIBE:
                        case MqttControlPacketType.SUBACK:
                        case MqttControlPacketType.UNSUBSCRIBE:
                        case MqttControlPacketType.UNSUBACK:
                            this.inputState++;
                            break;
                        }
                    }
                    break;

                case 2:
                    this.inputPacket.WriteByte(b);
                    this.inputState++;
                    this.inputRemainingLength--;
                    break;

                case 3:
                    this.inputPacket.WriteByte(b);
                    this.inputRemainingLength--;

                    if (this.inputRemainingLength == 0)
                    {
                        this.inputState = 0;
                        if (!await this.ProcessInputPacket())
                        {
                            Result = false;
                        }
                    }
                    else
                    {
                        this.inputState++;
                    }

                    break;

                case 4:
                    this.inputPacket.WriteByte(b);
                    this.inputRemainingLength--;

                    if (this.inputRemainingLength == 0)
                    {
                        this.inputState = 0;
                        if (!await this.ProcessInputPacket())
                        {
                            Result = false;
                        }
                    }
                    break;
                }
            }

            return(Result);
        }
예제 #5
0
        private Task <bool> Connection_OnReceived(object Sender, byte[] Buffer, int Offset, int Count)
        {
            PeerConnection Connection = (PeerConnection)Sender;
            Guid           PlayerId;
            IPAddress      PlayerRemoteAddress;
            IPEndPoint     PlayerRemoteEndpoint;

            try
            {
                BinaryInput Input = new BinaryInput(BinaryTcpClient.ToArray(Buffer, Offset, Count));

                PlayerId             = Input.ReadGuid();
                PlayerRemoteAddress  = IPAddress.Parse(Input.ReadString());
                PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16());
            }
            catch (Exception)
            {
                Connection.Dispose();
                return(Task.FromResult <bool>(true));
            }

            Player Player = (Player)Connection.StateObject;

            lock (this.remotePlayersByEndpoint)
            {
                if (!this.playersById.TryGetValue(PlayerId, out Player Player2) || Player2.PlayerId != Player.PlayerId)
                {
                    Connection.Dispose();
                    return(Task.FromResult <bool>(true));
                }

                Player.Connection = Connection;
            }

            Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

            Connection.OnReceived -= this.Connection_OnReceived;
            Connection.OnReceived += this.Peer_OnReceived;
            Connection.OnSent     += this.Connection_OnSent;

            BinaryOutput Output = new BinaryOutput();

            Output.WriteGuid(this.localPlayer.PlayerId);
            Output.WriteString(this.ExternalAddress.ToString());
            Output.WriteUInt16((ushort)this.ExternalEndpoint.Port);

            Connection.SendTcp(Output.GetPacket());

            MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected;

            if (h != null)
            {
                try
                {
                    h(this, Player);
                }
                catch (Exception ex)
                {
                    Events.Log.Critical(ex);
                }
            }

            return(Task.FromResult <bool>(true));
        }
예제 #6
0
        private Task <bool> Peer_OnReceived(object Sender, byte[] Buffer, int Offset, int Count)
        {
            PeerConnection Connection = (PeerConnection)Sender;
            Player         Player;

            byte[] Packet;

            if (Connection.StateObject is null)
            {
                BinaryInput Input = new BinaryInput(BinaryTcpClient.ToArray(Buffer, Offset, Count));
                Guid        PlayerId;
                IPAddress   PlayerRemoteAddress;
                IPEndPoint  PlayerRemoteEndpoint;

                try
                {
                    PlayerId             = Input.ReadGuid();
                    PlayerRemoteAddress  = IPAddress.Parse(Input.ReadString());
                    PlayerRemoteEndpoint = new IPEndPoint(PlayerRemoteAddress, Input.ReadUInt16());
                }
                catch (Exception)
                {
                    Connection.Dispose();
                    return(Task.FromResult <bool>(true));
                }

                if (Input.BytesLeft == 0)
                {
                    Packet = null;
                }
                else
                {
                    Packet = Input.GetRemainingData();
                }

                bool AllConnected;

                lock (this.remotePlayersByEndpoint)
                {
                    if (!this.playersById.TryGetValue(PlayerId, out Player))
                    {
                        Connection.Dispose();
                        return(Task.FromResult <bool>(true));
                    }

                    if (Player.Connection is null)
                    {
                        this.connectionCount++;
                    }
                    else
                    {
                        Player.Connection.Dispose();
                    }

                    Player.Connection         = Connection;
                    Connection.StateObject    = Player;
                    Connection.RemoteEndpoint = Player.GetExpectedEndpoint(this.p2pNetwork);

                    AllConnected = this.connectionCount + 1 == this.playerCount;
                }

                MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected;
                if (h != null)
                {
                    try
                    {
                        h(this, Player);
                    }
                    catch (Exception ex)
                    {
                        Events.Log.Critical(ex);
                    }
                }

                if (AllConnected)
                {
                    this.State = MultiPlayerState.Ready;
                }

                if (Packet is null)
                {
                    return(Task.FromResult <bool>(true));
                }
            }
            else
            {
                Player = (Player)Connection.StateObject;
                Packet = BinaryTcpClient.ToArray(Buffer, Offset, Count);
            }

            this.GameDataReceived(Player, Connection, Packet);

            return(Task.FromResult <bool>(true));
        }