Ejemplo n.º 1
0
        public void Add(TcpClient tcpClient)
        {
            var client = new LineBufferedClient(tcpClient, this);

            if (!_clients.TryAdd(client, client))
            {
                throw new InvalidOperationException("Tried to add connection twice");
            }

            client.Stream.ReadLineAsync().ContinueWith(client.HandleLine);
        }
Ejemplo n.º 2
0
        public void SendToAllBut(LineBufferedClient client, string line)
        {
            Console.WriteLine(line);
            Thread.Sleep(2000);
            var buffer = Encoding.ASCII.GetBytes(line + "\n");

            foreach (var entry in _clients)
            {
                var connectedClient = entry.Value;
                if (connectedClient != client)
                {
                    try
                    {
                        connectedClient.Client.GetStream().Write(buffer, 0, buffer.Length);
                    }
                    catch (Exception ex) when(ex is InvalidOperationException || ex is System.IO.IOException)
                    {
                        RemoveClient(connectedClient);
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void RemoveClient(LineBufferedClient client)
        {
            LineBufferedClient ignored;

            _clients.TryRemove(client, out ignored);
        }