public async Task Initialize() { if (_myAddress != null) { return; // already been initialized. } try { using (StreamSocket socket = await _connection.ConnectToTCP(_serverAddress, NetworkPorts.LobbyServerPort)) { _myAddress = socket.Information.LocalAddress.DisplayName; } await _connection.StartTCPListener(NetworkPorts.LobbyClientPort, ProcessRequest); } catch (Exception) { // Could not reach lobby. //TODO: Create custom exceptions to be thrown by lobby and caught and handled by the gameView. LobbyCommandPacket packet = new LobbyCommandPacket("Client", LobbyCommands.Disconnected); OnLobbyCommand(packet); } return; }
internal async Task CreateNetworkConnection() { if (_networkConnection == null) { EmpireSerializer serializer = new EmpireSerializer(); _networkConnection = new PacketConnection(serializer); await _networkConnection.StartTCPListener(_myPort, HandleRequest); await _networkConnection.StartUDPListener(_myPort, HandleUpdate); _serverSocket = await _networkConnection.ConnectToTCP(_gameData.HostIPAddress, _gameData.HostPort); _myAddress = _serverSocket.Information.LocalAddress.DisplayName; } }
private async Task HandlePlayerConnection(StreamSocket socket, SalutationPacket salutation) { string PlayerID = salutation.Name; if (_playerList.ContainsKey(PlayerID)) { PlayerData playerData = _playerList[PlayerID]; int shipID = _gameInstance.GameModel.NewShip(PlayerID); playerData.ShipID = shipID; StreamSocket clientSocket = await _networkConnection.ConnectToTCP(socket.Information.RemoteAddress.DisplayName, NetworkPorts.GameClientPort); playerData.ClientSocket = clientSocket; playerData.Connected = true; } }