Esempio n. 1
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);
            }
        }
Esempio n. 2
0
        static public void Q2P_IP_INFO_Handle(Session session, int opcode, object msg)
        {
            Q2P_IP_INFO qNode    = msg as Q2P_IP_INFO;
            R2P_IP_INFO response = new R2P_IP_INFO()
            {
                address = session.RemoteAddress.Address.ToString()
            };

            session.Reply(qNode, response);
        }
Esempio n. 3
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);
                }
            }
        }