private void AddClient(SFClientHandler newClient) { mutex.WaitOne(); clients.Add(newClient); clientId++; mutex.ReleaseMutex(); }
private void ListenForClients() { bool listen = true; while (listen) { try { //Esperar conexion de cliente TcpClient newTCPClient = tcpListener.AcceptTcpClient(); WriteLine("New client connected to Serial forwarder (id = " + id.ToString() + ") on port " + port.ToString(), prompt.eventTextColor); SFClientHandler newClient = new SFClientHandler(newTCPClient, mote, clientId); newClient.RxPacket += newClient_RxPacket; newClient.TxPacket += newClient_TxPacket; AddClient(newClient); newClient.ClientClosedEvent += ClientDC; newClient.MoteIFClosedEvent += newClient_MoteIFClosedEvent; } catch (Exception e) { // WSACancelBlockingCall issue: // When tcpListener.Close() is called from another // thread, WSACancelBlockingCall exception is thrown // from the AcceptTcpClient() method. // Catching the exception is just a 'workaround', as this // shouldn't happen. listen = false; Debug.WriteLine(e.Message); } } }
private void ClientDC(Object src, ClientClosedEvtArg arg) { WriteLine("Client disconnected from Serial forwarder (id = " + id.ToString() + ") on port " + port.ToString(), prompt.eventTextColor); SFClientHandler c = (SFClientHandler)src; RemoveClient(c); }
private void RemoveClient(SFClientHandler client) { mutex.WaitOne(); clients.Remove(client); mutex.ReleaseMutex(); }