Ejemplo n.º 1
0
 protected void InvokeTimeoutEventHandler()
 {
     Stop();
     if (TimeoutEventHandler != null)
     {
         TimeoutEventHandler.Invoke(this, EventArgs.Empty);
     }
     else
     {
         showIdleMessage();
     }
     Start();
 }
Ejemplo n.º 2
0
        public void PollEvents()
        {
            while (raiseConnectionEvent > 0)
            {
                if (connected > 0)
                {
                    OnConnectionSucces?.Invoke();
                }
                else
                {
                    OnConnectionFailed?.Invoke();
                }

                Interlocked.Exchange(ref raiseConnectionEvent, 0);
            }

            if (raiseDisconnectEvent > 0)
            {
                OnDisconnect?.Invoke();
                Interlocked.Exchange(ref raiseDisconnectEvent, 0);
            }

            if (raiseTimeoutEvent > 0)
            {
                OnTimeout?.Invoke();
                Interlocked.Exchange(ref raiseTimeoutEvent, 0);
            }

            while (!pendingReceiveEvents.IsEmpty)
            {
                OnReceiveData?.Invoke(pendingReceiveEvents.Dequeue());
            }

            while (!pendingErrorEvents.IsEmpty)
            {
                OnError?.Invoke(pendingErrorEvents.Dequeue());
            }

            while (!pendingNetEntityCreatedEvent.IsEmpty)
            {
                KeyValuePair <NetEntity, byte[]> pair = pendingNetEntityCreatedEvent.Dequeue();
                OnNetEntityCreated?.Invoke(pair.Key, pair.Value);
            }

            while (!netEntityReceiveQueue.IsEmpty)
            {
                var pair = netEntityReceiveQueue.Dequeue();
                OnNetEntityReceiveData?.Invoke(pair.Key, pair.Value);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Removes a client from the server
        /// </summary>
        /// <param name="client"></param>
        /// <param name="type"></param>
        public void RemoveClient(TCPServerClient client, TCPDisconnectType type = TCPDisconnectType.Disconnect)
        {
            if (Logging)
            {
                Logger.Write("REGION", "Method [RemoveClient]");
            }

            if (type == TCPDisconnectType.NoHandshake)
            {
                if (Logging)
                {
                    Logger.Write("INFO", "Client no handshake: " + client.UID);
                }
                OnNoHandshake?.Invoke(client);
            }
            else if (type == TCPDisconnectType.Disconnect)
            {
                if (Logging)
                {
                    Logger.Write("INFO", "Client disconnect: " + client.UID);
                }
                OnDisconnected?.Invoke(client);
            }
            else if (type == TCPDisconnectType.Timeout)
            {
                if (Logging)
                {
                    Logger.Write("INFO", "Client timeout: " + client.UID);
                }
                OnTimeout?.Invoke(client);
            }
            else if (type == TCPDisconnectType.Kick)
            {
                if (Logging)
                {
                    Logger.Write("INFO", "Client kick: " + client.UID);
                }
                OnKick?.Invoke(client);
            }

            lock (ClientsDict) ClientsDict.Remove(client.UID);
            lock (ClientsList) {
                for (int e = ClientsList.Count - 1; e >= 0; e--)
                {
                    if (ClientsList[e].UID == client.UID)
                    {
                        if (Logging)
                        {
                            Logger.Write("INFO", "Client found in ClientsList: " + client.UID);
                        }
                        ClientsList.RemoveAt(e);
                        break;
                    }
                }
            }

            try {
                client.Socket.Shutdown(SocketShutdown.Both);
                client.Socket.Close();
            } catch (Exception e) {
                if (Logging)
                {
                    Logger.Write("FAILED", "Socket shutdown/close", e);
                }
            }
        }