Esempio n. 1
0
        void OnGUI()
        {
            if (GUI.Button(new Rect(20, 150, 150, 40), "Test LoginMsg"))
            {
                LoginMsg msg = new LoginMsg();
                msg.passport = "ºôºô¹þ¹þ";
                msg.platform = 2;
                StringBuilder sb = new StringBuilder(512);
                sb.Length = 0;
                sb.Append("input string is ").Append(msg.passport).Append(" int is ").Append(msg.platform);
                Debug.LogError(sb.ToString());
                ByteArray array = new ByteArray();
                msg.Serialize(array);

                ByteArray receive = new ByteArray(array.Bytes);
                LoginMsg  ret     = new LoginMsg();
                ret.Deserialize(receive);
                sb.Length = 0;
                sb.Append("output string is ").Append(ret.passport).Append(" int is ").Append(ret.platform);
                Debug.LogError(sb.ToString());
            }
            if (GUI.Button(new Rect(20, 200, 150, 40), "RoleListInfoMsg"))
            {
                RoleListInfoMsg msg = new RoleListInfoMsg();
                msg.testClass.items.Add(100);
                msg.testClass.items.Add(1000);
                msg.testClass.testfloats.Add(101.01f);
                msg.testClass.testfloats.Add(1001.001f);

                msg.roles.Add(msg.testClass);
                msg.roles.Add(msg.testClass);

                ByteArray array = new ByteArray();
                msg.Serialize(array);

                ByteArray       receive = new ByteArray(array.Bytes);
                RoleListInfoMsg ret     = new RoleListInfoMsg();
                ret.Deserialize(receive);
                Debug.LogError("you can check the result by break point,oupt class object is too ma fan");
                byte[] none = receive.Bytes;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="e"></param>
        /// <param name="cmd"></param>
        internal static void Receive(TcpReceiveState e, PoolCommand cmd)
        {
            //TaskWork.Current.Add(new Task(() =>
            //{
            var loginMsg = new LoginMsg();
            int index    = 0;

            loginMsg.Deserialize(cmd.Payload, ref index);
            //验证矿工身份
            if (!MinerApi.ValidateMiner(loginMsg.WalletAddress, loginMsg.SerialNo))
            {
                RejectCommand.Send(e);
                return;
            }

            //TODO: address and SerialNo and account only for one Minner 第一个与条件匹配的矿工
            var miner = PoolCache.WorkingMiners.FirstOrDefault(m => m.WalletAddress == loginMsg.WalletAddress || m.ClientAddress == e.Address || m.SerialNo == loginMsg.SerialNo);

            //矿工不为空,发送stop命令
            if (miner != null)
            {
                StopMsg stopMsg = new StopMsg();
                stopMsg.Result = false;
                if (PoolCache.CurrentTask == null)
                {
                    return;
                }
                stopMsg.BlockHeight = PoolCache.CurrentTask.CurrentBlockHeight;
                stopMsg.StartTime   = PoolCache.CurrentTask.StartTime;
                stopMsg.StopTime    = Time.EpochTime;

                TcpSendState tcpSendState = new TcpSendState()
                {
                    Client = miner.Client, Stream = miner.Stream, Address = miner.ClientAddress
                };
                StopCommand.Send(tcpSendState, stopMsg);

                PoolCache.WorkingMiners.Remove(miner);
            }

            miner               = new Miner();
            miner.SerialNo      = loginMsg.SerialNo;
            miner.WalletAddress = loginMsg.WalletAddress;
            miner.ClientAddress = e.Address;
            miner.Client        = e.Client;
            miner.Stream        = e.Stream;

            Random random = new Random();

            miner.CheckScoopNumber = random.Next(0, POC.MAX_SCOOP_NUMBER + 1);
            PoolCache.WorkingMiners.Add(miner);

            miner.IsConnected         = true;
            miner.ConnectedTime       = Time.EpochTime;
            miner.LatestHeartbeatTime = Time.EpochTime;
            SendLoginResult(e, true);
            LogHelper.Info(miner.ClientAddress + " login success");

            MinerLoginMsg loginMinerMsg = new MinerLoginMsg();

            loginMinerMsg.Account  = loginMsg.WalletAddress;
            loginMinerMsg.SN       = loginMsg.SerialNo;
            loginMinerMsg.ServerId = Setting.PoolId;

            //MQApi.Current.SendLoginMsg(loginMinerMsg);
            RabbitMQApi.Current.SendLoginMsg(loginMinerMsg);
            if (PoolCache.CurrentTask != null)
            {
                StartCommand.Send(e, PoolCache.CurrentTask.CurrentStartMsg);
            }
            //}));
        }