private void p2pNetwork_OnPeerConnected(PeerToPeerNetwork 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());
        }
        public async Task SctpError()
        {
            // Setup
            var config = new PeerConnectionConfiguration();
            var pc1    = new PeerConnection();
            var pc2    = new PeerConnection();
            await pc1.InitializeAsync(config);

            await pc2.InitializeAsync(config);

            pc1.LocalSdpReadytoSend += async(string type, string sdp) =>
            {
                await pc2.SetRemoteDescriptionAsync(type, sdp);

                if (type == "offer")
                {
                    pc2.CreateAnswer();
                }
            };
            pc2.LocalSdpReadytoSend += async(string type, string sdp) =>
            {
                await pc1.SetRemoteDescriptionAsync(type, sdp);

                if (type == "offer")
                {
                    pc1.CreateAnswer();
                }
            };
            pc1.IceCandidateReadytoSend += (string candidate, int sdpMlineindex, string sdpMid)
                                           => pc2.AddIceCandidate(sdpMid, sdpMlineindex, candidate);
            pc2.IceCandidateReadytoSend += (string candidate, int sdpMlineindex, string sdpMid)
                                           => pc1.AddIceCandidate(sdpMid, sdpMlineindex, candidate);

            // Connect
            {
                var c1 = new ManualResetEventSlim(false);
                var c2 = new ManualResetEventSlim(false);
                pc1.Connected += () => c1.Set();
                pc2.Connected += () => c2.Set();
                Assert.True(pc1.CreateOffer());
                Assert.True(c1.Wait(TimeSpan.FromSeconds(60.0)));
                Assert.True(c2.Wait(TimeSpan.FromSeconds(60.0)));
                Assert.True(pc1.IsConnected);
                Assert.True(pc1.IsConnected);
            }

            // Try to add a data channel. This should fail because SCTP was not negotiated.
            Assert.ThrowsAsync <SctpNotNegotiatedException>(async() => await pc1.AddDataChannelAsync("dummy", false, false));
            Assert.ThrowsAsync <SctpNotNegotiatedException>(async() => await pc1.AddDataChannelAsync(42, "dummy", false, false));

            // Clean-up
            pc1.Close();
            pc1.Dispose();
            pc2.Close();
            pc2.Dispose();
        }
Beispiel #3
0
 public void Dispose()
 {
     if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0)
     {
         // Order matters! note that PeerConnection must be closed first by calling Close() above.
         _peerConnectionImpl.Dispose();
         // Observer later, cuz PeerConnection uses it
         _peerConnectionObserverImpl.IceCandidateAdded   -= IceCandidateAdded;
         _peerConnectionObserverImpl.RenegotiationNeeded -= RenegotationNeeded;
         _peerConnectionObserverImpl.Dispose();
     }
 }
Beispiel #4
0
        public void Dispose()
        {
            ConnectionState = IceConnectionState.Disconnected;

            webrtc.VideoFrameReady -= OnFrameReady;
            webrtc.Unregister(this);

            cancelSession.Cancel();

            source.Dispose();

            peer.Dispose();
        }
        public void Disposing()
        {
            var closeCount = 0;
            var stream     = new MemoryStream();
            var connection = new PeerConnection {
                Stream = stream
            };

            connection.Closed += (s, e) => { ++closeCount; };
            Assert.IsTrue(connection.IsActive);
            Assert.IsNotNull(connection.Stream);

            connection.Dispose();
            Assert.IsFalse(connection.IsActive);
            Assert.IsNull(connection.Stream);

            // Can be disposed multiple times.
            connection.Dispose();

            Assert.IsFalse(connection.IsActive);
            Assert.AreEqual(1, closeCount);
        }
        //////////////////////////////////////////////////
        // For video streaming
        //////////////////////////////////////////////////

        private void App_Suspending(object sender, SuspendingEventArgs e)
        {
            if (_peerConnection != null)
            {
                _peerConnection.Close();
                _peerConnection.Dispose();
                _peerConnection = null;
            }
            //localVideoPlayerElement.SetMediaPlayer(null);
            if (_signaler != null)
            {
                _signaler.StopPollingAsync();
                _signaler = null;
            }
            remoteVideoPlayerElement.SetMediaPlayer(null);
        }
Beispiel #7
0
        public void PeerDisconnectedEvent_ConnectionClose()
        {
            var gotEvent = 0;
            var manager  = new ConnectionManager();

            manager.PeerDisconnected += (s, e) => gotEvent += 1;
            var peerA = new Peer {
                Id = aId
            };
            var a = new PeerConnection {
                RemotePeer = peerA, Stream = Stream.Null
            };

            manager.Add(a);
            a.Dispose();
            Assert.AreEqual(1, gotEvent);
        }
        //Suspending-EventHandler of app: executes when app turns into suspending-state
        private void App_Suspending(object sender, SuspendingEventArgs e)
        {
            if (peerConnection != null)
            {
                //Closing of PeerConnection
                peerConnection.Close();
                //Disposing of PeerConnection-Resources
                peerConnection.Dispose();
                peerConnection = null;

                Debugger.Log(0, "", "Peer conection disposed successfully\n");
            }

            //Disposing of MediaPlayers
            Mpe_localVideo.SetMediaPlayer(null);
            Mpe_remoteVideo.SetMediaPlayer(null);

            //Disposing of signaler
            if (signaler != null)
            {
                signaler.StopPollingAsync();
                signaler = null;
            }
        }
        public void TearDownConnection()
        {
            // Unregister all callbacks
            pc1_.LocalSdpReadytoSend     -= OnLocalSdpReady1;
            pc2_.LocalSdpReadytoSend     -= OnLocalSdpReady2;
            pc1_.IceCandidateReadytoSend -= OnIceCandidateReadytoSend1;
            pc2_.IceCandidateReadytoSend -= OnIceCandidateReadytoSend2;
            pc1_.IceStateChanged         -= OnIceStateChanged1;
            pc2_.IceStateChanged         -= OnIceStateChanged2;
            pc1_.RenegotiationNeeded     -= OnRenegotiationNeeded1;
            pc2_.RenegotiationNeeded     -= OnRenegotiationNeeded2;
            pc1_.DataChannelAdded        -= OnDataChannelAdded1;
            pc2_.DataChannelAdded        -= OnDataChannelAdded2;
            pc1_.DataChannelRemoved      -= OnDataChannelRemoved1;
            pc2_.DataChannelRemoved      -= OnDataChannelRemoved2;
            pc1_.AudioTrackAdded         -= OnAudioTrackAdded1;
            pc2_.AudioTrackAdded         -= OnAudioTrackAdded2;
            pc1_.AudioTrackRemoved       -= OnAudioTrackRemoved1;
            pc2_.AudioTrackRemoved       -= OnAudioTrackRemoved2;
            pc1_.VideoTrackAdded         -= OnVideoTrackAdded1;
            pc2_.VideoTrackAdded         -= OnVideoTrackAdded2;
            pc1_.VideoTrackRemoved       -= OnVideoTrackRemoved1;
            pc2_.VideoTrackRemoved       -= OnVideoTrackRemoved2;

            Assert.IsFalse(exchangePending_);
            exchangeCompleted_.Dispose();
            exchangeCompleted_ = null;

            // Clean-up callback events
            dataChannelAddedEvent1_.Dispose();
            dataChannelAddedEvent1_ = null;
            dataChannelRemovedEvent1_.Dispose();
            dataChannelRemovedEvent1_ = null;
            audioTrackAddedEvent1_.Dispose();
            audioTrackAddedEvent1_ = null;
            audioTrackRemovedEvent1_.Dispose();
            audioTrackRemovedEvent1_ = null;
            audioTrackAddedEvent2_.Dispose();
            audioTrackAddedEvent2_ = null;
            audioTrackRemovedEvent2_.Dispose();
            audioTrackRemovedEvent2_ = null;
            videoTrackAddedEvent1_.Dispose();
            videoTrackAddedEvent1_ = null;
            videoTrackRemovedEvent1_.Dispose();
            videoTrackRemovedEvent1_ = null;
            videoTrackAddedEvent2_.Dispose();
            videoTrackAddedEvent2_ = null;
            videoTrackRemovedEvent2_.Dispose();
            videoTrackRemovedEvent2_ = null;
            renegotiationEvent1_.Dispose();
            renegotiationEvent1_ = null;
            renegotiationEvent2_.Dispose();
            renegotiationEvent2_ = null;
            iceConnectedEvent1_.Dispose();
            iceConnectedEvent1_ = null;
            iceConnectedEvent2_.Dispose();
            iceConnectedEvent2_ = null;
            remoteDescAppliedEvent1_.Dispose();
            remoteDescAppliedEvent1_ = null;
            remoteDescAppliedEvent2_.Dispose();
            remoteDescAppliedEvent2_ = null;
            connectedEvent1_.Dispose();
            connectedEvent1_ = null;
            connectedEvent2_.Dispose();
            connectedEvent2_ = null;

            // Destroy peers
            pc1_.Close();
            pc1_.Dispose();
            pc1_ = null;
            pc2_.Close();
            pc2_.Dispose();
            pc2_ = null;

            Assert.AreEqual(0, Library.ReportLiveObjects());
        }
Beispiel #10
0
 private async void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     _peerConnection.Close();
     _peerConnection.Dispose();
     await _hubConnection.InvokeAsync("LeaveRoom", SignallerConstants.RoomName);
 }
        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;
            Player Player2;

            lock (this.remotePlayersByEndpoint)
            {
                if (!this.playersById.TryGetValue(PlayerId, out 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 is null))
            {
                try
                {
                    h(this, Player);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace.ToString());
                }
            }
        }
        private void Peer_OnReceived(object Sender, byte[] Packet)
        {
            PeerConnection Connection = (PeerConnection)Sender;
            Player         Player;

            if (Connection.StateObject is null)
            {
                BinaryInput Input = new BinaryInput(Packet);
                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;
                }

                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;
                    }

                    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 is null))
                {
                    try
                    {
                        h(this, Player);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        Debug.WriteLine(ex.StackTrace.ToString());
                    }
                }

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

                if (Packet is null)
                {
                    return;
                }
            }
            else
            {
                Player = (Player)Connection.StateObject;
            }

            this.GameDataReceived(Player, Connection, Packet);
        }
Beispiel #13
0
 public void Dispose()
 {
     connection.Dispose();
 }