Exemplo n.º 1
0
        //[MessageMethod(NetOpcode.O2G_New_Node)]
        void Q2P_New_Node_Handle(Session session, int opcode, object msg)
        {
            Q2P_New_Node new_Node = msg as Q2P_New_Node;
            //Log.Debug($"Q2P_New_Nod {new_Node.ActorId} \r\nHash: {new_Node.HashCode}");

            NodeData data = new NodeData();

            data.nodeId     = new_Node.ActorId;
            data.address    = new_Node.address;
            data.ipEndPoint = new_Node.ipEndPoint;
            data.kIndex     = GetkIndex();
            AddNode(data);

            R2P_New_Node response = new R2P_New_Node()
            {
                Nodes = "", sendTime = new_Node.sendTime, nodeTime = TimeHelper.Now()
            };

            if (StringHelper.HashCode(JsonHelper.ToJson(nodes)) != new_Node.HashCode)
            {
                response.Nodes = JsonHelper.ToJson(nodes);
                session.Send(response);
            }
            session.Reply(new_Node, response);
        }
Exemplo n.º 2
0
        //[MessageMethod(NetOpcode.Q2P_New_Node)]
        async void Q2P_New_Node_Handle(Session session, int opcode, object msg)
        {
            Q2P_New_Node new_Node = msg as Q2P_New_Node;

            try
            {
                if (new_Node.version != BlockMgr.networkID)
                {
                    R2P_New_Node response = new R2P_New_Node()
                    {
                        Nodes = "", nodeTime = TimeHelper.Now()
                    };
                    response.Message = $"Network Version Not Compatible your:{new_Node.version} cur:{BlockMgr.networkID}";
                    session.Reply(new_Node, response);
                    return;
                }

                //Log.Debug($"Q2P_New_Node_Handle {new_Node.address} ipEndPoint: {new_Node.ipEndPoint}  1");
                Session sessionNew = await networkInner.Get(NetworkHelper.ToIPEndPoint(new_Node.ipEndPoint), 2);

                Q2P_IP_INFO qIPNode = new Q2P_IP_INFO();
                R2P_IP_INFO rIPNode = (R2P_IP_INFO)await sessionNew.Query(qIPNode, 0.3f);

                if (rIPNode != null)
                {
                    NodeData data = new NodeData();
                    data.nodeId     = new_Node.ActorId;
                    data.address    = new_Node.address;
                    data.ipEndPoint = new_Node.ipEndPoint;
                    data.state      = new_Node.state;
                    data.version    = new_Node.version;
                    data.kIndex     = GetkIndex();
                    AddNode(data);

                    R2P_New_Node response = new R2P_New_Node()
                    {
                        Nodes = "", nodeTime = TimeHelper.Now()
                    };
                    string nodesjson = JsonHelper.ToJson(nodes);
                    if (StringHelper.HashCode(nodesjson) != new_Node.HashCode)
                    {
                        response.Nodes = nodesjson;
                        //session.Send(response);
                    }
                    session.Reply(new_Node, response);
                    return;
                }
            }
            catch (Exception)
            {
            }
            {
                R2P_New_Node response = new R2P_New_Node()
                {
                    Nodes = "", nodeTime = TimeHelper.Now()
                };
                response.Message = "LAN not supported or Your network has a firewall";
                session.Reply(new_Node, response);
            }
        }
Exemplo n.º 3
0
        void Q2P_New_Node_Handle(Session session, int opcode, object msg)
        {
            Q2P_New_Node new_Node = msg as Q2P_New_Node;

            //Log.Debug($"Q2P_New_Nod {new_Node.ActorId} \r\nHash: {new_Node.HashCode}");
            if (nodeManager != null)
            {
                //Internet
                var nodes = nodeManager.GetNodeList();
                nodes = nodes.FindAll((n) => { return((n.state & NodeManager.EnumState.RelayNetwork) == NodeManager.EnumState.RelayNetwork); });

                //// LAN
                //var node = new NodeManager.NodeData();
                //node.address = Wallet.GetWallet().GetCurWallet().ToAddress();
                //node.nodeId = nodeManager.GetMyNodeId();
                //node.ipEndPoint = $"{relayNetworkInner.ipEndPoint.Address}:{Entity.Root.GetComponent<ComponentNetworkInner>().ipEndPoint.Port}";
                //node.state = 7;
                //var nodes = new List<NodeManager.NodeData>();
                //nodes.Add(node);

                //
                R2P_New_Node response = new R2P_New_Node()
                {
                    Nodes = JsonHelper.ToJson(nodes), nodeTime = TimeHelper.Now()
                };
                session.Reply(new_Node, response);
            }
        }
Exemplo n.º 4
0
        public async override void Start()
        {
            networkInner.ipEndPoint = NetworkHelper.ToIPEndPoint(GetIpV4() + ":" + networkInner.ipEndPoint.Port);
            Log.Info($"Node:{networkInner.ipEndPoint.ToString()}");

            string        tmp  = Program.jdNode["NodeSessions"].ToString();
            List <string> list = JsonHelper.FromJson <List <string> >(tmp);

            Q2P_New_Node new_Node = new Q2P_New_Node();

            new_Node.ActorId    = GetMyNodeId();
            new_Node.address    = Wallet.GetWallet().GetCurWallet().ToAddress();
            new_Node.ipEndPoint = networkInner.ipEndPoint.ToString();

            Log.Debug($"NodeManager.Start");
            while (true && list.Count > 0)
            {
                try
                {
                    for (int ii = 0; ii < list.Count; ii++)
                    {
                        bool bResponse = false;
                        new_Node.HashCode = StringHelper.HashCode(JsonHelper.ToJson(nodes));
                        new_Node.sendTime = TimeHelper.Now();
                        Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(list[0]));

                        if (session != null && session.IsConnect())
                        {
                            //Log.Debug($"NodeSessions connect " + r2P_New_Node.ActorId);
                            //session.Send(new_Node);

                            R2P_New_Node r2P_New_Node = (R2P_New_Node)await session.Query(new_Node, 0.3f);

                            if (r2P_New_Node != null)
                            {
                                if (r2P_New_Node.Nodes != "")
                                {
                                    nodes = JsonHelper.FromJson <List <NodeData> >(r2P_New_Node.Nodes);
                                    long timeNow = TimeHelper.Now();
                                    nodeTimeOffset = (timeNow - new_Node.sendTime) / 2 + r2P_New_Node.nodeTime - timeNow;
                                }
                                bResponse = true;
                            }
                        }
                        if (bResponse)
                        {
                            break;
                        }
                    }

                    // 等待5秒后关闭连接
                    await Task.Delay(5 * 1000);
                }
                catch (Exception)
                {
                    await Task.Delay(5 * 1000);
                }
            }
        }
Exemplo n.º 5
0
        public async void Run(bool bRun)
        {
            await Task.Delay(1 * 1000);

            List <string> list = JsonHelper.FromJson <List <string> >(Program.jdNode["NodeSessions"].ToString());

            // Get Internet IP
            try
            {
                Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(list[0]));

                Q2P_IP_INFO qIPNode = new Q2P_IP_INFO();
                R2P_IP_INFO rIPNode = (R2P_IP_INFO)await session.Query(qIPNode, 0.3f);

                networkInner.ipEndPoint = NetworkHelper.ToIPEndPoint(GetIpV4() + ":" + networkInner.ipEndPoint.Port);
                if (rIPNode != null)
                {
                    networkInner.ipEndPoint = NetworkHelper.ToIPEndPoint(rIPNode.address + ":" + networkInner.ipEndPoint.Port);
                }
            }
            catch (Exception)
            {
            }
            Log.Info($"NodeManager  {networkInner.ipEndPoint.ToString()}");
            Log.Info($"NodeSessions {list[0]}");

            //
            Q2P_New_Node new_Node = new Q2P_New_Node();

            new_Node.ActorId    = GetMyNodeId();
            new_Node.address    = Wallet.GetWallet().GetCurWallet().ToAddress();
            new_Node.ipEndPoint = networkInner.ipEndPoint.ToString();

            long state     = 0;
            var  consensus = Entity.Root.GetComponent <Consensus>();

            if (consensus != null)
            {
                state |= consensus.transferShow ? EnumState.transferShow : 0;
                state |= consensus.openSyncFast ? EnumState.openSyncFast : 0;
            }
            state           |= Entity.Root.GetComponentInChild <RelayNetwork>() != null ? EnumState.RelayNetwork : 0;
            new_Node.state   = state;
            new_Node.version = BlockMgr.networkID;

            while (bRun && list.Count > 0)
            {
                try
                {
                    for (int ii = 0; ii < list.Count; ii++)
                    {
                        bool bResponse = false;
                        new_Node.HashCode = StringHelper.HashCode(JsonHelper.ToJson(nodes));
                        Session session = await networkInner.Get(NetworkHelper.ToIPEndPoint(list[ii]));

                        if (session != null && session.IsConnect())
                        {
                            //Log.Debug($"NodeSessions connect " + r2P_New_Node.ActorId);
                            //session.Send(new_Node);

                            long         sendTime     = TimeHelper.Now();
                            R2P_New_Node r2P_New_Node = (R2P_New_Node)await session.Query(new_Node, 3f);

                            if (r2P_New_Node != null)
                            {
                                long timeNow = TimeHelper.Now();
                                nodeTimeOffset = (timeNow - sendTime) / 2 + r2P_New_Node.nodeTime - timeNow;
                                if (!string.IsNullOrEmpty(r2P_New_Node.Nodes))
                                {
                                    nodes = JsonHelper.FromJson <List <NodeData> >(r2P_New_Node.Nodes);
                                }
                                if (!string.IsNullOrEmpty(r2P_New_Node.Message))
                                {
                                    Log.Warning($"NodeSessions: {r2P_New_Node.Message}");
                                }
                                bResponse = true;
                            }
                        }
                        if (bResponse)
                        {
                            break;
                        }
                    }

                    // 等待5秒后关闭连接
                    await Task.Delay(5 * 1000);
                }
                catch (Exception)
                {
                    await Task.Delay(5 * 1000);
                }
            }
        }