public void Disconnect() { if (!Mode) { if (ClientsPool.ContainsKey(EnemyPoint.ToString())) { SendMessage(ClientsPool[EnemyPoint.ToString()].GetStream(), MessageCode.Fin); ClientsPool[EnemyPoint.ToString()].Close(); } } }
//by invoking this method server sends Playaccept message, to confirm players choose public void SelectPlayer(IPEndPoint player) { if (ClientsPool.ContainsKey(player.ToString())) { EnemyPoint = player; SendMessage(ClientsPool[EnemyPoint.ToString()].GetStream(), MessageCode.PlayAccept); } else { throw new FailedToConnectException("Хост с таким адресом недоступен, похоже он отключился"); } }
private void ClientOnTryConnect(IAsyncResult res) { try { TcpListener Server = res.AsyncState as TcpListener; TcpClient NewHost = Server.EndAcceptTcpClient(res); string key = ((IPEndPoint)NewHost.Client.RemoteEndPoint).ToString(); if (!ClientsPool.ContainsKey(key)) { Thread Handler = new Thread(new ParameterizedThreadStart(MaintainConnection)); ClientsPool.Add(key, NewHost); Handler.Start(NewHost); } Server.BeginAcceptTcpClient(new AsyncCallback(ClientOnTryConnect), Server); } catch (ObjectDisposedException) { return; } }