/// <summary>
        /// <see cref="IDisposable.Dispose"/>
        /// </summary>
        public async Task DisposeAsync()
        {
            await this.CloseMqtt();

            await this.SetState(MultiPlayerState.Closed);

            this.p2pNetwork?.Dispose();
            this.p2pNetwork = null;

            this.ready?.Dispose();
            this.ready = null;

            this.error?.Dispose();
            this.error = null;

            if (!(this.remotePlayersByEndpoint is null))
            {
                lock (this.remotePlayersByEndpoint)
                {
                    this.playersById.Clear();
                    this.remotePlayersByIndex.Clear();

                    foreach (Player Player in this.remotePlayersByEndpoint.Values)
                    {
                        if (Player.Connection != null)
                        {
                            Player.Connection.Dispose();
                        }
                    }

                    this.remotePlayersByEndpoint.Clear();
                    this.remotePlayers = null;
                }
            }
        }
Example #2
0
 internal PeerConnection(TcpClient TcpConnection, PeerToPeerNetwork Network, IPEndPoint RemoteEndpoint,
                         bool EncapsulatePackets)
 {
     this.network            = Network;
     this.remoteEndpoint     = RemoteEndpoint;
     this.tcpConnection      = TcpConnection;
     this.stream             = this.tcpConnection.GetStream();
     this.encapsulatePackets = EncapsulatePackets;
 }
Example #3
0
 internal IPEndPoint GetExpectedEndpoint(PeerToPeerNetwork Network)
 {
     if (IPAddress.Equals(this.publicEndpoint.Address, Network.ExternalAddress))
     {
         return(this.localEndpoint);
     }
     else
     {
         return(this.publicEndpoint);
     }
 }
Example #4
0
        internal PeerConnection(BinaryTcpClient TcpConnection, PeerToPeerNetwork Network, IPEndPoint RemoteEndpoint,
                                bool EncapsulatePackets)
        {
            this.network            = Network;
            this.remoteEndpoint     = RemoteEndpoint;
            this.tcpConnection      = TcpConnection;
            this.encapsulatePackets = EncapsulatePackets;

            this.tcpConnection.OnDisconnected += TcpConnection_OnDisconnected;
            this.tcpConnection.OnError        += TcpConnection_OnError;
            this.tcpConnection.OnReceived     += TcpConnection_OnReceived;
            this.tcpConnection.OnSent         += TcpConnection_OnSent;
        }
Example #5
0
        /// <summary>
        /// <see cref="IDisposable.Dispose"/>
        /// </summary>
        public void Dispose()
        {
            this.CloseMqtt();

            this.State = MultiPlayerState.Closed;

            if (this.p2pNetwork != null)
            {
                this.p2pNetwork.Dispose();
                this.p2pNetwork = null;
            }

            if (this.ready != null)
            {
                this.ready.Dispose();
                this.ready = null;
            }

            if (this.error != null)
            {
                this.error.Dispose();
                this.error = null;
            }

            if (this.remotePlayersByEndpoint != null)
            {
                lock (this.remotePlayersByEndpoint)
                {
                    this.playersById.Clear();
                    this.remotePlayersByIndex.Clear();

                    foreach (Player Player in this.remotePlayersByEndpoint.Values)
                    {
                        if (Player.Connection != null)
                        {
                            Player.Connection.Dispose();
                        }
                    }

                    this.remotePlayersByEndpoint.Clear();
                    this.remotePlayers = null;
                }
            }
        }
Example #6
0
        /// <summary>
        /// Manages a multi-player environment.
        /// </summary>
        /// <param name="ApplicationName">Name of application.</param>
        /// <param name="AllowMultipleApplicationsOnSameMachine">Allow multiple application on the same machine.</param>
        /// <param name="MqttServer">MQTT server host.</param>
        /// <param name="MqttPort">MQTT port to use.</param>
        /// <param name="MqttTls">If TLS is to be used for the MQTT connection.</param>
        /// <param name="MqttUserName">MQTT user name.</param>
        /// <param name="MqttPassword">MQTT password.</param>
        /// <param name="MqttNegotiationTopic">MQTT topic to use for multiplayer negotiation.</param>
        /// <param name="EstimatedMaxNrPlayers">Estimated number of maximum players.</param>
        /// <param name="PlayerId">Player ID.</param>
        /// <param name="PlayerMetaInfo">Meta-information about player.</param>
        public MultiPlayerEnvironment(string ApplicationName, bool AllowMultipleApplicationsOnSameMachine,
                                      string MqttServer, int MqttPort, bool MqttTls, string MqttUserName, string MqttPassword,
                                      string MqttNegotiationTopic, int EstimatedMaxNrPlayers, Guid PlayerId, params KeyValuePair <string, string>[] PlayerMetaInfo)
        {
            this.localPlayer           = new Player(PlayerId, new IPEndPoint(IPAddress.Any, 0), new IPEndPoint(IPAddress.Any, 0), PlayerMetaInfo);
            this.playersById[PlayerId] = this.localPlayer;
            this.applicationName       = ApplicationName;

            this.mqttServer           = MqttServer;
            this.mqttPort             = MqttPort;
            this.mqttTls              = MqttTls;
            this.mqttUserName         = MqttUserName;
            this.mqttPassword         = MqttPassword;
            this.mqttNegotiationTopic = MqttNegotiationTopic;

            this.p2pNetwork = new PeerToPeerNetwork(AllowMultipleApplicationsOnSameMachine ? this.applicationName + " (" + PlayerId.ToString() + ")" :
                                                    this.applicationName, 0, 0, EstimatedMaxNrPlayers);
            this.p2pNetwork.OnStateChange         += this.P2PNetworkStateChange;
            this.p2pNetwork.OnPeerConnected       += new PeerConnectedEventHandler(P2pNetwork_OnPeerConnected);
            this.p2pNetwork.OnUdpDatagramReceived += new UdpDatagramEvent(P2pNetwork_OnUdpDatagramReceived);
        }