Exemplo n.º 1
0
        private void P2pNetwork_OnPeerConnected(object Listener, PeerConnection Peer)
        {
            IPEndPoint Endpoint = (IPEndPoint)Peer.Tcp.Client.RemoteEndPoint;

#if LineListener
            Console.Out.WriteLine("Receiving connection from " + Endpoint.ToString());
#endif

            lock (this.remotePlayersByEndpoint)
            {
                if (!this.remotePlayerIPs.ContainsKey(Endpoint.Address))
                {
                    Peer.Dispose();
                    return;
                }
            }

            Peer.OnClosed   += new EventHandler(Peer_OnClosed);
            Peer.OnReceived += new BinaryEventHandler(Peer_OnReceived);

            BinaryOutput Output = new BinaryOutput();

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

            Peer.SendTcp(Output.GetPacket());
        }
Exemplo n.º 2
0
        private void CloseMqtt()
        {
            if (this.mqttConnection != null)
            {
                if (this.mqttConnection.State == MqttState.Connected)
                {
                    BinaryOutput Output = new BinaryOutput();
                    Output.WriteByte(2);
                    Output.WriteString(this.applicationName);
                    Output.WriteGuid(this.localPlayer.PlayerId);

                    this.mqttTerminatedPacketIdentifier = this.mqttConnection.PUBLISH(this.mqttNegotiationTopic, MqttQualityOfService.AtLeastOnce, false, Output);
                    this.mqttConnection.OnPublished    += new PacketAcknowledgedEventHandler(MqttConnection_OnPublished);

#if LineListener
                    Console.Out.WriteLine("Tx: BYE(" + this.localPlayer.ToString() + ")");
#endif
                }
                else
                {
                    this.mqttConnection.Dispose();
                    this.mqttConnection = null;
                }
            }
        }
Exemplo n.º 3
0
        private void Serialize(Player Player, BinaryOutput Output)
        {
            Output.WriteString(Player.PublicEndpoint.Address.ToString());
            Output.WriteUInt16((ushort)Player.PublicEndpoint.Port);

            Output.WriteString(Player.LocalEndpoint.Address.ToString());
            Output.WriteUInt16((ushort)Player.LocalEndpoint.Port);

            Output.WriteGuid(Player.PlayerId);
            Output.WriteUInt((uint)Player.Count);

            foreach (KeyValuePair <string, string> P in Player)
            {
                Output.WriteString(P.Key);
                Output.WriteString(P.Value);
            }
        }
Exemplo n.º 4
0
        private void Connection_OnReceived(object Sender, byte[] Packet)
        {
            PeerConnection Connection = (PeerConnection)Sender;
            Guid           PlayerId;
            IPAddress      PlayerRemoteAddress;
            IPEndPoint     PlayerRemoteEndpoint;

            try
            {
                BinaryInput Input = new BinaryInput(Packet);

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

            Player Player = (Player)Connection.StateObject;

            lock (this.remotePlayersByEndpoint)
            {
                if (!this.playersById.TryGetValue(PlayerId, out Player Player2) || Player2.PlayerId != Player.PlayerId)
                {
                    Connection.Dispose();
                    return;
                }

                Player.Connection = Connection;
            }

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

            Connection.OnReceived -= new BinaryEventHandler(Connection_OnReceived);
            Connection.OnReceived += new BinaryEventHandler(Peer_OnReceived);

            Connection.OnSent += new BinaryEventHandler(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);
                }
            }
        }
Exemplo n.º 5
0
        private async 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(true);
            }

            Player Player = (Player)Connection.StateObject;

            lock (this.remotePlayersByEndpoint)
            {
                if (!this.playersById.TryGetValue(PlayerId, out Player Player2) || Player2.PlayerId != Player.PlayerId)
                {
                    Connection.Dispose();
                    return(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);

            await Connection.SendTcp(Output.GetPacket());

            MultiPlayerEnvironmentPlayerInformationEventHandler h = this.OnPlayerConnected;

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

            return(true);
        }