Example #1
0
        private void Disconnect(SocketAsyncEventArgs e)
        {
            if (e.UserToken is TcpSession session)
            {
                try
                {
                    session.Socket.Shutdown(SocketShutdown.Both);
                }
                catch (SocketException)
                {
                    // this seems to randomly happen when the client disconnects. not always.
                    //  there are other examples online of this being caught and ignored like this.
                    //  see links at the top of the file
                    // TODO: log
                }

                session.Socket.Dispose();

                var disconnectEventArgs = new ClientDisconnectedEventArgs(session);
                OnClientDisconnected(disconnectEventArgs);
                session.OnDisconnected(disconnectEventArgs);

                Interlocked.Decrement(ref activeSessions);
            }

            if (e != null)
            {
                e.Completed -= FinishReceive;
                e.UserToken  = null;
                e.SetBuffer(null, 0, 0);
                socketEventPool.CheckIn(e);
            }
        }
Example #2
0
        private async Task SessionDisconnectedAsync(object sender, ClientDisconnectedEventArgs e)
        {
            // waiting for initialization proves self is non-null
            WaitForInitialization();
            await OnSessionDisconnected(e);

            // need to clean up event hooks here so that this object can be GC'd
            Session.DataReceived -= sessionDataReceived;
            Session.Disconnected -= sessionDisconnected;

            initialized.Dispose();
        }
Example #3
0
 protected virtual void OnClientDisconnected(ClientDisconnectedEventArgs e)
 {
     ClientDisconnected?.Invoke(this, e);
 }
Example #4
0
 internal virtual void OnDisconnected(ClientDisconnectedEventArgs e) => Disconnected?.Invoke(this, e);
Example #5
0
 protected abstract Task OnSessionDisconnected(ClientDisconnectedEventArgs e);