Ejemplo n.º 1
0
        public void ConnectAgents()
        {
            Console.WriteLine("Agent connect");
            IPAddress   ipAddress   = IPAddress.Parse(IpAddress);
            TcpListener tcpListener = new TcpListener(ipAddress, PortCSforAgents);

            tcpListener.Start();
            PortCSforAgents = ((IPEndPoint)tcpListener.LocalEndpoint).Port;
            int i = 0;

            while (_acceptingAgents)
            {
                if (tcpListener.Pending() && _acceptingAgents)
                {
                    TcpClient       agentClient = tcpListener.AcceptTcpClient();
                    AgentDescriptor agent       = new AgentDescriptor(agentClient);
                    _agentsConnections.Add(agent);
                    agent.StartReceiving(GetAgentMessage, HandleConnectionError);
                    Console.WriteLine("Agent connected: " + ++i);
                    Log.Information("New agent connected.");
                }
                else if (!_acceptingAgents)
                {
                    continue;
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }
            tcpListener.Stop();
            Console.WriteLine("Agent end");
        }
Ejemplo n.º 2
0
        private void GetGMMessage(Message message)
        {
            if (message.IsGameStarted())
            {
                _acceptingAgents = false;
            }
            if (message.IsEndGame())
            {
                HandleEndGame(message);
                //_gameMasterConnection.Dispose();
                return;
            }

            Console.WriteLine("I've got such message: " + message.GetPayload());
            Log.Information("GetGMMessege: {@m}", message);
            AgentDescriptor agent = _agentsConnections.Find(x => x.Id == message.AgentId);

            if (message.MessageId == MessageType.JoinGameResponse)
            {
                JoinGameResponse resp = (JoinGameResponse)message.GetPayload();
                if (resp.Accepted == false)
                {
                    var removedConnection = _agentsConnections.Find(a => a.Id == message.AgentId);
                    _agentsConnections.Remove(removedConnection);
                    SendMessageWithErrorHandling(agent, message);
                    removedConnection.Dispose();
                    return;
                }
            }
            SendMessageWithErrorHandling(agent, message);
        }