Пример #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;
        }
Пример #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;
                if (command.Command == LobbyCommands.SetupGame)
                {
                    string   hostID = command.PlayerID;
                    GameData game   = _gameList[_playerList[hostID].GameID];
                    await GamePage.gameInstance.StartServer(hostID, _playerList, game);
                }
                else
                {
                    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;
                OnLobbyUpdated("playerList");
                OnLobbyUpdated("gamesList");
            }
            return(acknowledgement);
        }
Пример #3
0
        private async void OnRequestReceived(object sender, PacketReceivedEventArgs e)
        {
            StreamSocket  source = e.Source;
            NetworkPacket packet = e.Packet;

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

                if (Server)
                {
                    switch ((int)command.Command)
                    {
                    case (int)LobbyCommands.EnterLobby:
                        await ProcessEnterLobbyCommand(playerID, source.Information.RemoteAddress.DisplayName);

                        break;

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

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

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

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

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

                        break;
                    }

                    await _lobbyCommandConnection.SendPacketTo(source, new AcknowledgePacket());
                    await UpdateAllClients();

                    MenuManager.PlayerListChanged();
                }
                else
                {
                    switch ((int)command.Command)
                    {
                    case (int)LobbyCommands.EjectThisUser:
                        ProcessEjectThisUserCommand();
                        break;
                    }
                }
            }
        }
Пример #4
0
 private async Task ProcessEnterLobbyCommand(string playerID, string ipAddress)
 {
     if (!_playerList.ContainsKey(playerID))
     {
         PlayerData player = new PlayerData(new Player(playerID), ipAddress);
         _playerList.Add(playerID, player);
     }
     else
     {
         // TODO: need to handle case where we are logging in a second time, possibly after disconnecting.
         LobbyCommandPacket ejectUser = new LobbyCommandPacket(playerID, LobbyCommands.EjectThisUser);
         await _lobbyDataConnection.SendPacketToHost(ejectUser);
     }
 }
Пример #5
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(new AcknowledgePacket());
        }
Пример #6
0
 internal LobbyCommandEventArgs(LobbyCommandPacket packet)
 {
     Packet = packet;
 }
Пример #7
0
        internal async void OnPacketReceived(object network, PacketReceivedEventArgs args)
        {
            StreamSocket  source = args.Source;
            NetworkPacket packet = args.Packet;

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

                if (Server)
                {
                    switch ((int)command.Command)
                    {
                    case (int)LobbyCommands.EnterLobby:
                        await ProcessEnterLobbyCommand(playerID, source.Information.RemoteAddress.DisplayName);

                        break;

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

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

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

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

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

                        break;
                    }
                    await UpdateAllClients();

                    MenuManager.PlayerListChanged();
                }
                else
                {
                    switch ((int)command.Command)
                    {
                    case (int)LobbyCommands.EjectThisUser:
                        ProcessEjectThisUserCommand();
                        break;
                    }
                }
            }
            else if (packet.Type == PacketType.LobbyData && !Server)
            {
                LobbyData data = packet as LobbyData;
                _playerList = data._playerList;
                _gameList   = data._gameList;
                MenuManager.PlayerListChanged();
            }
        }
Пример #8
0
        public Task InitializeGame(string playerID)
        {
            LobbyCommandPacket command = new LobbyCommandPacket(playerID, LobbyCommands.SetupGame);

            return(_lobbyDataConnection.SendPacketToHost(command));
        }
Пример #9
0
        public Task JoinGame(string playerID, string hostID)
        {
            LobbyCommandPacket command = new LobbyCommandPacket(playerID, LobbyCommands.JoinGame, hostID);

            return(_lobbyDataConnection.SendPacketToHost(command));
        }
Пример #10
0
        public Task HostGame(string playerID)
        {
            LobbyCommandPacket command = new LobbyCommandPacket(playerID, LobbyCommands.HostGame, _myAddress);

            return(_lobbyDataConnection.SendPacketToHost(command));
        }
Пример #11
0
        public Task LeaveLobby(string playerID)
        {
            LobbyCommandPacket command = new LobbyCommandPacket(playerID, LobbyCommands.LeaveLobby);

            return(_lobbyDataConnection.SendPacketToHost(command));
        }
Пример #12
0
 public async Task EnterLobby(string playerID)
 {
     LobbyCommandPacket command = new LobbyCommandPacket(playerID, LobbyCommands.EnterLobby);
     await _lobbyCommandConnection.WaitResponse(command);
 }
Пример #13
0
 public void OnLobbyCommand(LobbyCommandPacket command)
 {
     LobbyCommand?.Invoke(this, new LobbyCommandEventArgs(command));
 }