/// <summary> /// Process the client connection. /// </summary> /// <param name="ar">Ar.</param> void DoAcceptSocketCallback(IAsyncResult ar){ lock (listenLocker) { if (!IsListening) return; // Get the listener that handles the client request. TcpListener listener = (TcpListener)ar.AsyncState; // End the operation and... TcpClient client = null; try { client = listener.EndAcceptTcpClient(ar); } catch(Exception sex) { if (OnEnd != null) OnEnd(this, sex); IsListening = false; } if (client != null){ //...Registrating the client var qClient = new LClient(client); addClient(qClient); listener.BeginAcceptTcpClient(new AsyncCallback(DoAcceptSocketCallback), Listener); } else{ IsListening = false; if (OnEnd != null) OnEnd(this, null); } } }
void addClient(LClient client){ lock (clients) { clients.Add (client); } client.OnDisconnect+= client_OnDisconnect; ConnectInfo info = new ConnectInfo (client); if (OnConnect != null) OnConnect (this, client, info); if (!info.AllowConnection) client.Close (); }
public ConnectInfo(LClient client) { this.Client = client; }
void client_OnDisconnect (LClient obj){ lock (clients) { clients.Remove (obj); } obj.OnDisconnect -= client_OnDisconnect; if (OnDisconnect != null) OnDisconnect (this, obj); }