示例#1
0
        public void Active(IPEndPoint remoteEndPoint)
        {
            m_lastActiveTime = (int)SGFTime.GetTimeSinceStartup();
            m_active         = true;

            this.remoteEndPoint = remoteEndPoint as IPEndPoint;
        }
示例#2
0
        private void CheckTimeout()
        {
            float curTime = SGFTime.GetTimeSinceStartup();

            if (curTime - m_lastCheckTimeoutStamp >= 5)
            {
                m_lastCheckTimeoutStamp = curTime;

                var list = m_listRspListener.ToArray();
                for (int i = 0; i < list.Length; i++)
                {
                    var   helper = list[i];
                    float dt     = curTime - helper.timestamp;
                    if (dt >= helper.timeout)
                    {
                        m_listRspListener.Remove(helper.index);
                        if (helper.onErr != null)
                        {
                            helper.onErr.DynamicInvoke(NetErrorCode.Timeout);
                        }

                        Debuger.LogWarning("cmd:{0} Is Timeout!", helper.cmd);
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <typeparam name="TRsp"></typeparam>
        /// <param name="cmd"></param>
        /// <param name="req"></param>
        /// <param name="onRsp"></param>
        /// <param name="timeout"></param>
        /// <param name="onErr"></param>
        /// <returns>返回唯一的发送Index</returns>
        public uint Send <TRsp>(uint cmd, object req, Action <uint, TRsp> onRsp, float timeout = 30,
                                Action <NetErrorCode> onErr = null)
        {
            Debuger.LogVerbose("cmd:{0}, timeout:{1}", cmd, timeout);
            uint           index  = MessageIndexGenerator.NewIndex();
            ListenerHelper helper = new ListenerHelper()
            {
                cmd       = cmd,
                index     = index,
                TMsg      = typeof(TRsp),
                onErr     = onErr,
                onMsg0    = onRsp,
                timeout   = timeout,
                timestamp = SGFTime.GetTimeSinceStartup()
            };

            m_listRspListener.Add(index, helper);


            NetMessage msg = new NetMessage();

            msg.head.index    = index;
            msg.head.cmd      = cmd;
            msg.head.token    = m_token;
            msg.content       = PBSerializer.NSerialize(req);
            msg.head.dataSize = (uint)msg.content.Length;

            m_conn.Send(msg);
            return(index);
        }
示例#4
0
        public void Active(IPEndPoint remoteEndPoint)
        {
            m_lastActiveTime = (int)SGFTime.GetTimeSinceStartup();
            m_active         = true;

            if (this.RemoteEndPoint == null || !this.RemoteEndPoint.Equals(remoteEndPoint))
            {
                IsEndPointChanged   = true;
                this.RemoteEndPoint = remoteEndPoint;
            }
        }
示例#5
0
        private void OnUpdate(float dt)
        {
            float current = SGFTime.GetTimeSinceStartup();

            if (current - m_lastHeartBeatTime > 5.0f)
            {
                m_lastHeartBeatTime = current;

                HeartBeatReq req = new HeartBeatReq();
                req.ping      = (ushort)m_ping;
                req.timestamp = (uint)TimeUtils.GetTotalMillisecondsSince1970();
                m_net.Send <HeartBeatReq, HeartBeatRsp>(ProtoCmd.HeartBeatReq, req, OnHeartBeatRsp, 15, OnHeartBeatError);
            }
        }
示例#6
0
        public bool IsActived()
        {
            if (!m_active)
            {
                return(false);
            }

            int dt = (int)SGFTime.GetTimeSinceStartup() - m_lastActiveTime;

            if (dt > ActiveTimeout)
            {
                m_active = false;
            }
            return(m_active);
        }
示例#7
0
        private void AddListener(uint cmd, Type TRsp, Delegate onRsp, uint index, float timeout, Action <NetErrorCode> onErr)
        {
            ListenerHelper helper = new ListenerHelper()
            {
                cmd       = cmd,
                index     = index,
                TMsg      = TRsp,
                onErr     = onErr,
                onMsg     = onRsp,
                timeout   = timeout,
                timestamp = SGFTime.GetTimeSinceStartup()
            };

            m_listRspListener.Add(index, helper);
        }
示例#8
0
 public void Start()
 {
     m_lastHeartBeatTime = SGFTime.GetTimeSinceStartup() + SGFRandom.Default.Range(5.0f);
     GlobalEvent.onUpdate.AddListener(OnUpdate);
 }