Esempio n. 1
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);
        }
Esempio n. 2
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());
     }
 }
Esempio n. 3
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);
        }
Esempio n. 4
0
        public Server(string key, int port, int pollDifference, int maxClient, int disconnectTimeOut)
        {
            handlers   = new Dictionary <string, OnEventRecieved>();
            dataWriter = new NetDataWriter();
            listener   = new EventBasedNetListener();
            server     = new NetManager(listener, maxClient, key);
            server.Start(port);
            server.DisconnectTimeout = disconnectTimeOut;
            this.pollDifference      = pollDifference;

            listener.PeerConnectedEvent += peer =>
            {
                Console.WriteLine("peer connectd ===>", peer.EndPoint);
                onPeerConnected?.Invoke(peer);
            };

            listener.NetworkReceiveEvent += (peer, dataReader) =>
            {
                string x = dataReader.GetString();
                Console.WriteLine(x);
                JsonData jsonData = JsonMapper.ToObject(x);
                if (handlers.ContainsKey(jsonData[0].ToString()))
                {
                    if (jsonData.Count > 1)
                    {
                        handlers[jsonData[0].ToString()](peer, jsonData[1]);
                    }
                    else
                    {
                        handlers[jsonData[0].ToString()](peer, null);
                    }
                }
            };

            listener.PeerDisconnectedEvent += (peer, disconnectInfo) =>
            {
                Console.WriteLine("peer disconnectd ===>", peer.EndPoint); // Show peer ip
                onPeerDisconnected?.Invoke(peer);
            };

            pollEventThread = new Thread(pollEvent);
            pollEventThread.Start();
        }
Esempio n. 5
0
        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);
            }
        }
        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));
     }
 }
Esempio n. 8
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
                {
                }
            }
        }