Beispiel #1
0
        /// <summary>
        /// Disconnects a client
        /// </summary>
        /// <param name="baseClient">Client to be disconnected</param>
        /// <returns>True if the client was disconnected, false if it doesn't exist</returns>
        public virtual bool Disconnect(BaseClient baseClient)
        {
            lock (_clients)
            {
                if (!_clients.ContainsKey(baseClient))
                    return false;

                _clients.Remove(baseClient);
            }

            try
            {
                baseClient.OnDisconnect();
                baseClient.CloseConnections();
            }
            catch (Exception e)
            {
                if (Log.IsErrorEnabled)
                    Log.Error("Exception", e);

                return false;
            }

            return true;
        }
Beispiel #2
0
        /// <summary>
        /// Called when a client is trying to connect to the server
        /// </summary>
        /// <param name="ar">Async result of the operation</param>
        private void AcceptCallback(IAsyncResult ar)
        {
            Socket sock = null;

            try
            {
                if (_listen == null)
                {
                    return;
                }

                sock = _listen.EndAccept(ar);

                sock.SendBufferSize    = Constants.SendBufferSize;
                sock.ReceiveBufferSize = Constants.ReceiveBufferSize;
                sock.NoDelay           = Constants.UseNoDelay;

                BaseClient baseClient = null;

                try
                {
                    // Removing this message in favor of connection message in GameClient
                    // This will also reduce spam when server is pinged with 0 bytes - Tolakram
                    //string ip = sock.Connected ? sock.RemoteEndPoint.ToString() : "socket disconnected";
                    //Log.Info("Incoming connection from " + ip);

                    baseClient        = GetNewClient();
                    baseClient.Socket = sock;

                    lock (_clients)
                        _clients.Add(baseClient, baseClient);

                    baseClient.OnConnect();
                    baseClient.BeginReceive();
                }
                catch (SocketException)
                {
                    Log.Error("BaseServer SocketException");
                    if (baseClient != null)
                    {
                        Disconnect(baseClient);
                    }
                }
                catch (Exception e)
                {
                    Log.Error("Client creation", e);

                    if (baseClient != null)
                    {
                        Disconnect(baseClient);
                    }
                }
            }
            catch
            {
                Log.Error("AcceptCallback: Catch");

                if (sock != null)                 // don't leave the socket open on exception
                {
                    try
                    {
                        sock.Close();
                    }
                    catch
                    {
                    }
                }
            }
            finally
            {
                if (_listen != null)
                {
                    _listen.BeginAccept(_asyncAcceptCallback, this);
                }
            }
        }