示例#1
0
        private void AcceptCallback(IAsyncResult result)
        {
            var listener = (TcpListener)result.AsyncState;

            try
            {
                var socket = listener.EndAcceptSocket(result);
                var client = new TextCommandClient(this, socket);

                Log.Debug("Accepting connection from {RemoteEndPoint}", client.Socket.RemoteEndPoint);

                lock (sync)
                {
                    clients.Add(client);
                }

                listener.BeginAcceptSocket(AcceptCallback, listener);
                socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, client);
            }
            catch (SocketException ex)
            {
                Log.Error("Accept error", ex);
            }
            catch (ObjectDisposedException)
            {
            }
        }
示例#2
0
        public void CloseClient(TextCommandClient client)
        {
            Log.Debug("Closing client {RemoteEndPoint}", client.Socket.RemoteEndPoint);

            lock (sync)
            {
                clients.Remove(client);
            }

            client.Dispose();
        }