Exemple #1
0
            // 在指定 id 的连接上发送请求
            public IWriteableBuffer BeginRequest(string componentName, Action <IReadableBuffer> cb, Action <bool> expiredProcess)
            {
                var buff = AllocWriteBuffer();

                buff.IsUsing = true;

                buff.Reserve(sizeof(bool));
                buff.Reserve(sizeof(int));
                long no = p();

                buff.Write(true);
                buff.Write(no);
                buff.Write(componentName);

                // 记录回调处理句柄
                CallbackNode cbn = new CallbackNode();

                cbn.time       = TimeUtils.NowSecond;
                cbn.cb         = cb;
                callbacks[-no] = cbn;

                if (expiredProcess != null)
                {
                    usrDefinedExpireProcess[-no] = expiredProcess;
                }

                return(buff);
            }
Exemple #2
0
        void CheckNextCallbackTimeout()
        {
            if (checkTimeoutLst.Count == 0)
            {
                if (callbacks.Count > 0)
                {
                    checkTimeoutLst.AddRange(callbacks.ToArray());
                }

                return;
            }

            long checkPoint = Utils.NowSecond - RequestExpireTime;
            KeyValuePair <long, CallbackNode> kv = checkTimeoutLst[0];

            checkTimeoutLst.RemoveAt(0);
            long          no          = kv.Key;
            CallbackNode  cb          = kv.Value;
            long          requestTime = cb.time;
            NetConnection nc          = cb.nc;

            if (!cb.expired && requestTime < checkPoint)
            {
                Action <bool> p = null;
                if (usrDefinedExpireProcess.TryGetValue(no, out p))
                {
                    usrDefinedExpireProcess.Remove(no);
                    if (p != null)
                    {
                        p(nc.IsConnected);
                    }
                }
                else if (OnRequestExpired != null)
                {
                    OnRequestExpired(nc.IsConnected);
                }

                // 每个消息只通知一次超时事件
                cb.expired = true;
            }
        }
Exemple #3
0
            // 在指定 id 的连接上发送请求
            public IWriteableBuffer BeginRequest(string componentName, Action <IReadableBuffer> cb, Action <bool> expiredProcess)
            {
                WriteBuffer buff = null;

                if (WriteBufferPool.Count > 0)
                {
                    buff = WriteBufferPool.Dequeue() as WriteBuffer;
                    buff.Clear();
                }
                else
                {
                    buff = new WriteBuffer(true);
                }

                buff.IsUsing = true;

                buff.Reserve(sizeof(int));
                long no = p();

                buff.Write(no);
                buff.Write(componentName);

                // 记录回调处理句柄
                CallbackNode cbn = new CallbackNode();

                cbn.time       = Utils.NowSecond;
                cbn.cb         = cb;
                callbacks[-no] = cbn;

                if (expiredProcess != null)
                {
                    usrDefinedExpireProcess[-no] = expiredProcess;
                }

                return(buff);
            }