public void Listen() { try { TcpListener server = null; server = new TcpListener(IPAddress.Parse(_host), Protocol.TCP_PORT); server.Start(); while (_flag.Retrieve((x) => x)) { if (!server.Pending()) { Protocol.Yield(); continue; } var client = new ProtocolTcpClient(); client.Configure(server.AcceptTcpClient()); var message = client.RecvMessage(); if (message.Length > 0 && message.Code == MessageCode.CONNECT && Equals(Protocol.VERSION, message.GetReader().ReadString())) { var name = RandomNameGen.GetName(); Console.WriteLine("Connection: {0} => {1}", client.Address, name); _clients.Act(x => x.Add(new UserTcpConnection(name, client))); } else { client.Close(); } } server.Stop(); } catch (Exception e) { Console.WriteLine(e); } }
public void Disconnect() => _client.Close();