Пример #1
0
 public void Run()
 {
     if (!TryStartListening())
     {
         return;
     }
     while (true)
     {
         try
         {
             IConnectionClient client = server.Accept();
             if (client == null)
             {
                 break;
             }
             ManagedClient managedClient = new ManagedClient(client, nextId++);
             lock (clientsLocker)
             {
                 if (state.Value == CSState.Listening || state.Value == CSState.AgentsAccepting)
                 {
                     clients.Add(managedClient);
                     tasks.Add(Task.Run(() => HandleCommunication(managedClient)));
                 }
             }
         }
         catch (SocketException)
         {
             CSLogger.LogError("Some error with TCPListener occured.\n");
             Kill();
         }
     }
     CSLogger.Log($"Communication Server stopped listening for clients.\n");
     Task.WaitAll(tasks.ToArray());
 }
Пример #2
0
        private void HandleCommunication(ManagedClient client)
        {
            CSLogger.Log($"Client {client.Id} connected ");

            while (client.IsConnected())
            {
                Message message = client.GetMessage();


                switch (state.Value)
                {
                case CSState.Listening:
                    ProcessClientMessageDuringListening(message, client);
                    break;

                case CSState.AgentsAccepting:
                    ProcessAgentMessageDuringAgentsAccepting(message, client);
                    break;

                case CSState.GameInProgress:
                    ProcessAgentMessageDuringGameInProgress(message, client);
                    break;
                }
            }

            if (client.IsInGame)
            {
                HandlePlayerDisconnected(client);
            }

            CSLogger.Log($"Client {client.Id} disconnected ");
        }
Пример #3
0
        private void ProcessGmMessageDuringGameInProgress(Message message, ManagedClient client)
        {
            switch (message)
            {
            case ConnectPlayerResMsg _:
            case DiscoverResMsg _:
            case GameStartMsg _:
            case MoveResMsg _:
            case PickUpResMsg _:
            case PlaceResMsg _:
            case ReadyResMsg _:
            case SetupResMsg _:
            case TestResMsg _:
                ForwardMessageFromGM(message);
                break;

            //case GameOver msg:
            //    break;

            default:
                CSLogger.LogMessage(message, state.Value);
                Kill();
                break;
            }
        }
Пример #4
0
        private void HandleGmCommunication(ManagedClient client)
        {
            while (client.IsConnected())
            {
                Message message = client.GetMessage();


                switch (state.Value)
                {
                case CSState.AgentsAccepting:
                    ProcessGmMessageDuringAgentsAccepting(message, client);
                    break;

                case CSState.GameInProgress:
                    ProcessGmMessageDuringGameInProgress(message, client);
                    break;

                case CSState.GameFinished:
                    ProcessGmMessageDuringGameFinished(message, client);
                    break;
                }
            }

            if (client.Id == gmId.Value)
            {
                HandleGMDisconnected();
            }
        }
Пример #5
0
        private void ProcessGmMessageDuringGameFinished(Message message, ManagedClient client)
        {
            switch (message)
            {
            //case GameOver msg:
            //    break;

            default:
                CSLogger.LogMessage(message, state.Value);
                Kill();
                break;
            }
        }
Пример #6
0
 private bool RegisterGM(ManagedClient client)
 {
     //return false;
     lock (gmRegisteringLocker)
     {
         if (state.Value == CSState.Listening)
         {
             gmId.Value  = client.Id;
             state.Value = CSState.AgentsAccepting;
             CSLogger.Log($"GM registered and agents accepting started.");
             client.SendMessage(new ConnectGMResMsg("127.0.0.1", "OK"));
             return(true);
         }
         else
         {
             client.SendMessage(new ConnectGMResMsg("127.0.0.1", "DENIED"));
             return(false);
         }
     }
 }
Пример #7
0
        private void ProcessAgentMessageDuringAgentsAccepting(Message message, ManagedClient client)
        {
            switch (message)
            {
            case ConnectPlayerMsg msg:
                playerGuids.Add(msg.playerGuid.g);
                clients[0].SendMessage(message);
                //client.SendMessage(new ConnectPlayerResMsg(msg.portNumber, msg.playerGuid, "OK"));
                break;

            case ReadyMsg msg:
                client.SendMessage(new ReadyResMsg(msg.playerGuid, "YES"));
                break;

            default:
                CSLogger.LogMessage(message, state.Value);
                Kill();
                break;
            }
        }
Пример #8
0
        private void ProcessClientMessageDuringListening(Message message, ManagedClient client)
        {
            switch (message)
            {
            case ConnectPlayerMsg _:
                //client.SendMessage(new GmNotConnectedYet());
                break;

            case ConnectGMMsg _:
                if (RegisterGM(client))
                {
                    HandleGmCommunication(client);
                }
                break;

            default:
                CSLogger.LogMessage(message, state.Value);
                Kill();
                break;
            }
        }
Пример #9
0
        private void ProcessAgentMessageDuringGameInProgress(Message message, ManagedClient client)
        {
            switch (message)
            {
            case ConnectPlayerMsg _:
            case DiscoverMsg _:
            case GameStartMsg _:
            case MoveMsg _:
            case PickUpMsg _:
            case PlaceMsg _:
            case ReadyMsg _:
            case SetupMsg _:
            case TestMsg _:
                clients[0].SendMessage(message);
                break;

            default:
                CSLogger.LogMessage(message, state.Value);
                Kill();
                break;
            }
        }
Пример #10
0
        private void ProcessGmMessageDuringAgentsAccepting(Message message, ManagedClient client)
        {
            switch (message)
            {
            case ConnectPlayerResMsg msg:
                ForwardMessageFromGM(msg);
                break;

            case GameStartMsg msg:
                //StartGame();
                ForwardMessageFromGM(msg);
                break;

            case SetupMsg _:
                StartGame();
                break;

            default:
                CSLogger.LogMessage(message, state.Value);
                Kill();
                break;
            }
        }
Пример #11
0
 private void HandlePlayerDisconnected(ManagedClient client)
 {
     client.Disconnect();
 }