Example #1
0
 public static void Raise(this OnPeerDisconnected @event, object sender, ChannelEventArgs e)
 {
     if (@event != null)
     {
         @event(sender, e);
     }
 }
Example #2
0
 internal static void Raise(this OnPeerDisconnected @event, object sender, PeerEventArgs e)
 {
     if (@event != null)
     {
         @event(sender, e);
     }
 }
Example #3
0
        private void Listener_PeerDisconnectedEvent(NetPeer peer, DisconnectInfo disconnectInfo)
        {
            int id = peer.Id + 1;

            jitterBuffersByPeerId.Remove(id);

            NetDebug.Log($"Peer index <b>{peer.Id}</b> global ID {id} disconnected. Reason: {disconnectInfo.Reason}", LogType.Connection);
            OnPeerDisconnected?.Invoke(id);
        }
Example #4
0
 /// <summary>
 /// Remove peer
 /// </summary>
 /// <param name="peer">Peer</param>
 private void RemovePeer(Peer peer)
 {
     if (peerIDToPeerLookup.ContainsKey(peer.ID))
     {
         IPeer peer_peer = peerIDToPeerLookup[peer.ID];
         OnPeerDisconnected?.Invoke(peer_peer);
         peerIDToPeerLookup.Remove(peer.ID);
         peers.Remove(peer_peer.GUID.ToString());
     }
 }
Example #5
0
        protected virtual void OnDisconnect(int connectionId, Exception exception)
        {
            NetworkConnection connection;

            if (!connectionMap.TryGetValue(connectionId, out connection))
            {
                return;
            }
            connection.DisconnectInternal();
            OnPeerDisconnected?.Invoke(connection);
            RemoveConnection(connection);
        }
        public void Disconnect()
        {
            var groupArray = groups.ToArray();

            foreach (var group in groupArray)
            {
                UntrackGroup(group);
            }
            // positively disconnect only if peer is connected
            if (isConnected)
            {
                peer.Disconnect();
            }

            if (OnPeerDisconnected != null)
            {
                OnPeerDisconnected.Invoke(this);
            }
        }
Example #7
0
        private void AcceptCallback(IAsyncResult res)
        {
            if (!IsActive)
            {
                return;
            }
            var newSock = ChannelSocket.EndAccept(res);

            Peer.Peer newPeer;
            if (IsMainChannel)
            {
                newPeer = new Peer.Peer(new SocketIdentity(Manager.GeneratePeerId(), SocketIdentity.GenerateGuid()), newSock, BufferSize, this);
                newPeer.OnPeerDisconnected += (o, e2) =>
                {
                    OnPeerDisconnected.Raise(o, new ChannelEventArgs(this, newPeer));
                };
                newPeer.Receive();
                AcceptPeer(newPeer);
            }
            else
            {
                newPeer = new Peer.Peer(new SocketIdentity(Manager.GeneratePeerId()), newSock, BufferSize, this);
                PendingConnections.Add(new PendingPeerConnection(newPeer.Identity.Guid, newPeer));
                newPeer.OnPeerDisconnected += (o, e2) =>
                {
                    OnPeerDisconnected.Raise(o, new ChannelEventArgs(this, newPeer));
                };
                newPeer.Receive();
                newPeer.OnPeerRelocationRequest += (sender, e) =>
                {
                    if (PendingConnections.FirstOrDefault(pc => pc.Guid == e.PeerGuid && !pc.IsCancelled) != null)
                    {
                        AcceptPeer(newPeer);
                        OnPeerConnected.Raise(this, new ChannelEventArgs(this, e.Peer));

                        lock (_lockObj)
                            PendingConnections.Remove(PendingConnections.First(pc => pc.Guid == e.PeerGuid));
                    }
                };
            }
            ChannelSocket.BeginAccept(AcceptCallback, null);
        }
        void OnDisconnect(int connectionId, byte error)
        {
            UNETConnection connection;

            if (!connectionMap.TryGetValue(connectionId, out connection))
            {
                return;
            }
            var exception = UNETUtility.CreateError(error);

            if (exception == null)
            {
                connection.ConnectTask.TrySetResult(new object());
            }
            else
            {
                connection.ConnectTask.TrySetException(exception);
            }
            OnPeerDisconnected?.Invoke(connection);
            RemoveConnection(connection);
        }
 /// <summary>
 /// Processes all events
 /// </summary>
 public override void ProcessEvents()
 {
     while (localPeerConnectionAttemptMessages.TryDequeue(out LocalPeerConnectionAttemptMessage local_peer_connection_attempt_message))
     {
         IPeer peer = local_peer_connection_attempt_message.Peer;
         OnPeerConnectionAttempted?.Invoke(peer);
         if (IsConnectionAllowed(peer))
         {
             peers.Add(peer.GUID.ToString(), peer);
             OnPeerConnected?.Invoke(peer);
         }
         else
         {
             local_peer_connection_attempt_message.Peer.Disconnect(EDisconnectionReason.Banned);
         }
     }
     while (localPeerDisconnectionMessages.TryDequeue(out LocalPeerDisconnectionMessage local_peer_disconnection_message))
     {
         IPeer  peer = local_peer_disconnection_message.Peer;
         string key  = peer.GUID.ToString();
         if (peers.ContainsKey(key))
         {
             OnPeerDisconnected?.Invoke(peer);
             peers.Remove(key);
         }
         foreach (KeyValuePair <string, ILocalPeer> peer_peer in peerPeerLookup)
         {
             if (peer.GUID == peer_peer.Value.GUID)
             {
                 peerPeerLookup.Remove(peer_peer.Key);
                 break;
             }
         }
     }
     while (localPeerReceiveMessages.TryDequeue(out LocalPeerReceiveMessage local_peer_receive_message))
     {
         OnPeerMessageReceived?.Invoke(local_peer_receive_message.Peer, Compression.Decompress(local_peer_receive_message.Message, local_peer_receive_message.Index, local_peer_receive_message.Length));
     }
 }
Example #10
0
        private void CleanupConnection(Peer peer)
        {
            lock (this)
            {
                try
                {
                    try
                    {
                        OnPeerDisconnected?.Invoke(this, peer);

                        peer.Socket.Shutdown(SocketShutdown.Both);
                        peer.Socket.Close();
                    }
                    catch
                    {
                    }

                    _peers.Remove(peer);
                }
                catch
                {
                }
            }
        }
Example #11
0
 public void OnDisconnected(OnPeerDisconnected onPeerDisconnected)
 {
     this.onPeerDisconnected = onPeerDisconnected;
 }