Example #1
0
        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;
        }
Example #2
0
        private async Task <NetworkPacket> ProcessRequest(StreamSocket socket, NetworkPacket packet)
        {
            AcknowledgePacket acknowledgement = new AcknowledgePacket();

            if (packet.Type == PacketType.LobbyCommand)
            {
                LobbyCommandPacket command  = packet as LobbyCommandPacket;
                string             playerID = command.PlayerID;

                switch ((int)command.Command)
                {
                case (int)LobbyCommands.EnterLobby:
                    string playerIPAddress = command.Data;
                    await ProcessEnterLobbyCommand(playerID, playerIPAddress);

                    break;

                case (int)LobbyCommands.LeaveLobby:
                    await ProcessLeaveLobbyCommand(playerID);

                    break;

                case (int)LobbyCommands.HostGame:
                    string hostIPAddress = command.Data;
                    await ProcessHostGameCommand(playerID, hostIPAddress);

                    break;

                case (int)LobbyCommands.JoinGame:
                    string hostID = command.Data;
                    await ProcessJoinGameCommand(playerID, hostID);

                    break;

                case (int)LobbyCommands.LeaveGame:
                    await ProcessLeaveGameCommand(playerID);

                    break;

                case (int)LobbyCommands.SetupGame:
                    await ProcessSetupGameCommand(playerID);

                    break;
                }
                await UpdateAllClients();
            }
            return(acknowledgement);
        }
Example #3
0
        private async Task <NetworkPacket> SendLobbyCommand(string playerID, LobbyCommands command, string args = null)
        {
            try
            {
                LobbyCommandPacket commandPacket = new LobbyCommandPacket(playerID, command, args);
                return(await _connection.ConnectAndWaitResponse(_serverAddress, NetworkPorts.LobbyServerPort, commandPacket));
            } catch (Exception)
            {
                // TODO throw custom exception for gameview to deal with
                // for now, fire LobbyCommand.Disconnected.
                LobbyCommandPacket packet = new LobbyCommandPacket("Client", LobbyCommands.Disconnected);
                OnLobbyCommand(packet);
            }

            return(null as NetworkPacket);
        }
Example #4
0
 private async Task ProcessEnterLobbyCommand(string playerID, string ipAddress)
 {
     if (_playerList.ContainsKey(playerID))
     {
         // TODO: need to handle case where we are logging in a second time, possibly after disconnecting.
         // For now, simply tell the old client to close.
         LobbyCommandPacket ejectUser = new LobbyCommandPacket(playerID, LobbyCommands.EjectThisUser);
         string             address   = _playerList[playerID].IPAddress;
         NetworkPacket      reply     = await _connection.ConnectAndWaitResponse(address, NetworkPorts.LobbyClientPort, ejectUser);
     }
     else
     {
         PlayerData player = new PlayerData(new Player(playerID), ipAddress, NetworkPorts.GameClientPort);
         _playerList.Add(playerID, player);
     }
 }
Example #5
0
        private async Task <NetworkPacket> ProcessRequest(StreamSocket socket, NetworkPacket packet)
        {
            AcknowledgePacket acknowledgement = new AcknowledgePacket();

            if (packet.Type == PacketType.LobbyCommand)
            {
                LobbyCommandPacket command = packet as LobbyCommandPacket;
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                              () => { OnLobbyCommand(command); });
            }
            else if (packet.Type == PacketType.LobbyData)
            {
                LobbyData lobbyData = packet as LobbyData;
                _playerList = lobbyData._playerList;
                _gameList   = lobbyData._gameList;
                OnPropertyChanged("playerList");
                OnPropertyChanged("gamesList");
            }
            return(acknowledgement);
        }
Example #6
0
        private async void ProcessLobbyCommands(object sender, LobbyCommandEventArgs e)
        {
            LobbyCommandPacket packet = e.Packet;

            switch (packet.Command)
            {
            case LobbyCommands.EjectThisUser:
                ReportOutput("Ejecting " + packet.PlayerID + ".");
                Application.Current.Exit();
                break;

            case LobbyCommands.LeaveGame:
                ReportOutput(packet.PlayerID + " leaves his game.");
                await client.LeaveGame(packet.PlayerID);

                break;

            case LobbyCommands.Disconnected:
                ReportOutput("Unable to reach Lobby service.");
                break;
            }
        }
Example #7
0
 public void OnLobbyCommand(LobbyCommandPacket command)
 {
     LobbyCommand?.Invoke(this, new LobbyCommandEventArgs(command));
 }
Example #8
0
 private async Task SendLobbyCommandToClient(PlayerData player, LobbyCommands command)
 {
     LobbyCommandPacket commandPacket = new LobbyCommandPacket(player.PlayerID, command);
     await _connection.ConnectAndWaitResponse(player.IPAddress, NetworkPorts.LobbyClientPort, commandPacket);
 }
Example #9
0
 internal LobbyCommandEventArgs(LobbyCommandPacket packet)
 {
     Packet = packet;
 }