public void Process(BaseMessage message, ProtocolType protocol, IPEndPoint endpoint = null, TcpClient client = null)
        {
            BroadcastMessage msg = (BroadcastMessage)message;

            if (msg.To == "HUB")
            {
                List <Entities.HubInfo> connections = connectionManager.Connections.GetBroadcastConnections(false);

                foreach (Entities.HubInfo connection in connections)
                {
                    // Make sure we don't broadcast back to source.
                    //if (connection.Id == msg.From)
                    //{
                    //   continue;
                    //}

                    // Should we only send TCP, and not UDP? Should we check if either is available?
                    connectionManager.SendTCP(new Broadcast(msg), connection.Client);
                }
            }
            else if (msg.To == "GATEWAY")
            {
                Console.WriteLine($"Gateway receive a message from {msg.From} with content {msg.Content}.");
            }
            else
            {
                Console.WriteLine($"Gateway receive a message without a correct \"To\" value. Must be \"HUB\" or \"GATEWAY\".");
            }
        }
Ejemplo n.º 2
0
        public void Process(BaseMessage message, ProtocolType Protocol, IPEndPoint EP = null, TcpClient Client = null)
        {
            ReqMessage req = (ReqMessage)message;

            HubInfo hubInfo = manager.Connections.GetConnection(req.RecipientId);

            if (hubInfo != null)
            {
                manager.SendTCP(new Req(req), hubInfo.Client);
            }
        }
        public void Process(BaseMessage message, ProtocolType protocol, IPEndPoint endpoint = null, TcpClient client = null)
        {
            HubInfo hubInfo = connections.GetConnection(message.Id);

            if (hubInfo == null)
            {
                hubInfo = new HubInfo((HubInfoMessage)message);
                connections.AddConnection(hubInfo);

                if (endpoint != null)
                {
                    log.LogInformation("Client Added: UDP EP: {0}:{1}, Name: {2}", endpoint.Address, endpoint.Port, hubInfo.Name);
                }
                else if (client != null)
                {
                    log.LogInformation("Client Added: TCP EP: {0}:{1}, Name: {2}", ((IPEndPoint)client.Client.RemoteEndPoint).Address, ((IPEndPoint)client.Client.RemoteEndPoint).Port, hubInfo.Name);
                }
            }
            else
            {
                hubInfo.Update((HubInfoMessage)message);

                if (endpoint != null)
                {
                    log.LogInformation("Client Updated: UDP EP: {0}:{1}, Name: {2}", endpoint.Address, endpoint.Port, hubInfo.Name);
                }
                else if (client != null)
                {
                    log.LogInformation("Client Updated: TCP EP: {0}:{1}, Name: {2}", ((IPEndPoint)client.Client.RemoteEndPoint).Address, ((IPEndPoint)client.Client.RemoteEndPoint).Port, hubInfo.Name);
                }
            }

            if (endpoint != null)
            {
                hubInfo.ExternalEndpoint = endpoint;
            }

            if (client != null)
            {
                hubInfo.Client = client;
            }

            manager.BroadcastTCP(hubInfo);

            if (!hubInfo.Initialized)
            {
                if (hubInfo.ExternalEndpoint != null & protocol == ProtocolType.Udp)
                {
                    manager.SendUDP(new Message("Server", hubInfo.Name, "UDP Communication Test"), hubInfo.ExternalEndpoint);
                }

                if (hubInfo.Client != null & protocol == ProtocolType.Tcp)
                {
                    manager.SendTCP(new Message("Server", hubInfo.Name, "TCP Communication Test"), hubInfo.Client);
                }

                if (hubInfo.Client != null & hubInfo.ExternalEndpoint != null)
                {
                    foreach (HubInfo ci in connections.GetBroadcastConnections(false))
                    {
                        manager.SendUDP(ci, hubInfo.ExternalEndpoint);
                    }

                    hubInfo.Initialized = true;
                }
            }
        }