Ejemplo n.º 1
0
        /** 是否数据一致 */
        public bool dataEquals(BaseData data)
        {
            if (data == null)
            {
                return(false);
            }

            if (data._dataID != _dataID)
            {
                return(false);
            }

            try
            {
                return(toDataEquals(data));
            }
            catch (Exception e)
            {
                Ctrl.errorLog(e);
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 读取字节流(不加长度标记)(完整版,为DB)
        /// </summary>
        public void readBytesFullWithoutLen(BytesReadStream stream)
        {
            //空读标记
            // _isEmptyRead=stream.isEmpty();

            try
            {
                toReadBytesFull(stream);
            }
            catch (Exception e)
            {
                Ctrl.errorLog(e);
            }

            try
            {
                afterRead();
            }
            catch (Exception e)
            {
                Ctrl.errorLog(e);
            }
        }
Ejemplo n.º 3
0
        /** 主线程 */
        private void toConnectOnce()
        {
            Ctrl.print("connectOnce");

            createNewContent();

            if (_currentInfo == null)
            {
                Ctrl.errorLog("此时不该没有currentInfo");
                toGetDNS();
                return;
            }

            _hostAddress = _currentInfo.getOnce();

            _state       = Connecting;
            _connectTime = 0;
            ++_currentTryCount;

            Ctrl.printForIO("toConnect一次:" + _hostAddress, _content.GetHashCode());

            _content.connect(_hostAddress, _port);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 写入字节流(完整版,为DB)
        /// </summary>
        public void writeBytesFull(BytesWriteStream stream)
        {
            try
            {
                beforeWrite();
            }
            catch (Exception e)
            {
                Ctrl.errorLog(e);
            }

            stream.startWriteObj();

            try
            {
                toWriteBytesFull(stream);
            }
            catch (Exception e)
            {
                Ctrl.errorLog(e);
            }

            stream.endWriteObj();
        }
Ejemplo n.º 5
0
        private void run()
        {
            try
            {
                SFuncQueue queue = _queue;

                _running = true;

                long lastTime = Ctrl.getTimer();
                long time;
                int  delay;
                int  tickMax  = _tickDelay;
                int  tickTime = 0;

                while (_running)
                {
                    time     = Ctrl.getTimer();
                    delay    = (int)(time - lastTime);
                    lastTime = time;

                    //防止系统时间改小
                    if (delay < 0)
                    {
                        delay = 0;
                    }

                    if (delay > 0)
                    {
                        if ((tickTime += delay) >= tickMax)
                        {
                            try
                            {
                                tick(tickTime);
                            }
                            catch (Exception e)
                            {
                                Ctrl.errorLog("线程tick出错", e);
                            }

                            tickTime = 0;
                        }
                    }

                    try
                    {
                        runEx();
                    }
                    catch (Exception e)
                    {
                        Ctrl.errorLog("线程runEx出错", e);
                    }

                    //再事务
                    queue.runOnce();

                    //最后睡
                    threadSleep();
                }
            }
            catch (ThreadAbortException)
            {
                //线程结束
            }
            catch (Exception e)
            {
                Ctrl.printExceptionForIO(e);
            }
        }
Ejemplo n.º 6
0
        public void tick(int delay)
        {
            IntObjectMap <TimeExecuteData> timeExDic;

            if (!(timeExDic = _timeExDic).isEmpty())
            {
                foreach (TimeExecuteData tData in timeExDic)
                {
                    if (!tData.pause)
                    {
                        tData.time += delay;

                        if (tData.time >= tData.timeMax)
                        {
                            tData.intArg = tData.time;

                            if (tData.isCut)
                            {
                                tData.time = 0;
                            }
                            else
                            {
                                tData.time -= tData.timeMax;
                            }

                            if (!tData.isGoOn)
                            {
                                //移除
                                timeExDic.remove(tData.index);
                            }

                            executeTimeExFunc(tData);
                        }
                    }
                }
            }


            //frame

            IntObjectMap <Action <int> > frameDic;

            if (!(frameDic = _frameDic).isEmpty())
            {
                foreach (Action <int> v in frameDic)
                {
                    try
                    {
                        v(delay);
                    }
                    catch (Exception e)
                    {
                        Ctrl.errorLog(e);
                    }
                }
            }

            //callLater(放在最下面,用来在一次tick的最后执行逻辑)

            SList <Action> callLaters;

            if (!(callLaters = _callLaters).isEmpty())
            {
                _callLaterForEaching = true;

                Action[] values = callLaters.getValues();

                for (int i = 0, len = callLaters.size(); i < len; ++i)
                {
                    try
                    {
                        values[i]();
                    }
                    catch (Exception e)
                    {
                        Ctrl.errorLog(e);
                    }

                    values[i] = null;
                }

                callLaters.justClearSize();

                _callLaterForEaching = false;

                SList <Action> callLaterTemps;

                if (!(callLaterTemps = _callLaterTemps).isEmpty())
                {
                    callLaters.addAll(callLaterTemps);

                    callLaterTemps.clear();
                }
            }
        }
Ejemplo n.º 7
0
        private void onSecond()
        {
            if (_reconnecting)
            {
                if (--_reconnectLastTime <= 0)
                {
                    _reconnectLastTime = 0;

                    Ctrl.log("重连超时,连接关闭");

                    reconnectFailed();
                }
            }

            // Ctrl.print("newResponseNum",_newResponseNum);
            _newResponseNum = 0;

            if (ShineSetting.messageUsePool)
            {
                long nowTime = Ctrl.getFixedTimer();
                int  key;
                for (short i = _sendCheckIndex; i != _sendMsgIndex; i++)
                {
                    BaseRequest request = _sendCacheRequestQueue[key = (i & ShineSetting.requestCacheMark)];

                    if (request == null)
                    {
                        Ctrl.errorLog("不该找不到消息", i);
                        break;
                    }
                    else
                    {
                        if ((nowTime - request.sendTime) > ShineSetting.requestCacheTime)
                        {
                            if (!request.sended)
                            {
                                Ctrl.warnLog("消息还未发送", request.getDataID());
                                break;
                            }

                            request.doRelease();
                            _sendCacheRequestQueue[key] = null;
                            _sendCheckIndex++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            switch (_state)
            {
            case Connected:
            {
                // if(!_socket.Connected)
                // {
                //  //连接被关闭
                //  beClose();
                // }

                ++_pingIndex;

                if (_pingIndex >= ShineSetting.pingTime)
                {
                    _pingIndex = 0;

                    sendPingRequest();
                }

                if (ShineSetting.needPingCut)
                {
                    ++_pingTimePass;

                    if (_pingTimePass > ShineSetting.pingKeepTime)
                    {
                        Ctrl.warnLog("连接超时,强制关闭");
                        _pingTimePass = 0;
                        closeForIO(Close_TimeOut);
                    }
                }
            }
            break;

            case Connecting:
            {
                //连接超时
                if ((++_connectTime) >= _connectTimeMax)
                {
                    Ctrl.log("连接中超时");

                    _connectTime = 0;
                    preConnectFailedForIO();
                }
            }
            break;

            case Closed:
            {
                _tryConnectTime.onSecond();
            }
            break;
            }
        }