BF2Statistics.Gamespy.GpcmClient.Disconnect C# (CSharp) Method

Disconnect() public method

Logs the client out of the game client, and closes the stream
Codes: 0 => Client sends the "logout" command 1 => The login timer elapsed and the client wasnt logged in or this object was disposed, forcefully disconnected 2 => Invalid login query, or username was incorrect 3 => Incorrect Password 4 => An error occured while trying to login the client (could be database related) 5 => Cant create account, username exists already 6 => Error Creating new account in database 7 => Invalid query for account creation, or an exception was thrown while trying to create account 8 => Remote Connection closed the Stream or was un-readchable 9 => Forced server shutdown [No events called, database sessions are not updated, and EventArgs are disposed]
public Disconnect ( int code ) : void
code int /// The disconnect code. If set to 9, the OnDisconect event will not be called, the database /// will not be updated to reset everyone's session code, and the EventArgs objects will NOT /// be returned to the IO pool. You should only set to 9 for a planned server shutdown. ///
return void
        public void Disconnect(int code)
        {
            // Make sure we arent disposed
            if (Disposed) return;

            // Update database session
            if (Status == LoginStatus.Completed && code < 9)
            {
                try
                {
                    using (GamespyDatabase Database = new GamespyDatabase())
                        Database.Execute("UPDATE accounts SET session=0 WHERE id=" + PlayerId);
                }
                catch
                {
                    // We could be shutting this server down because of DB connection issues, don't do anything here.
                }
            }

            // Unregister for stream events and close the connection
            Stream.OnDisconnect -= Stream_OnDisconnect;
            Stream.DataReceived -= Stream_DataReceived;
            Stream.Close(code == 9);

            // Set status and log
            if (code == 1 && Status == LoginStatus.Processing)
                GpcmServer.Log("Login Timeout:  {0} - {1} - {2}", PlayerNick, PlayerId, RemoteEndPoint);
            else if (Status != LoginStatus.Disconnected)
                GpcmServer.Log("Client Logout:  {0} - {1} - {2}, Code={3}", PlayerNick, PlayerId, RemoteEndPoint, code);

            // Preapare to be unloaded from memory
            Status = LoginStatus.Disconnected;
            Disposed = true;

            // Call disconnect event
            if (OnDisconnect != null)
                OnDisconnect(this);
        }

Usage Example

Example #1
0
        /// <summary>
        /// Checks the timeout on a client connection. This method is used to detect hanging connections, and
        /// forcefully disconnects them.
        /// </summary>
        /// <param name="client"></param>
        protected void CheckTimeout(GpcmClient client)
        {
            // Setup vars
            DateTime   expireTime = client.Created.AddSeconds(Timeout);
            GpcmClient oldC;

            // Remove all processing connections that are hanging
            if (client.Status != LoginStatus.Completed && expireTime <= DateTime.Now)
            {
                try
                {
                    client.Disconnect(1);
                    Processing.TryRemove(client.ConnectionId, out oldC);
                }
                catch (Exception ex)
                {
                    // Log the error
                    L.LogError("NOTICE: [GpcmServer.CheckTimeout] Error removing client from processing queue. Generating Excpetion Log");
                    ExceptionHandler.GenerateExceptionLog(ex);
                }
            }
            else if (client.Status == LoginStatus.Completed)
            {
                Processing.TryRemove(client.ConnectionId, out oldC);
            }
        }
All Usage Examples Of BF2Statistics.Gamespy.GpcmClient::Disconnect