Пример #1
0
        /** IO线程tick */
        public void socketTick()
        {
            if (!ShineSetting.messageUsePool)
            {
                return;
            }

            if (_responseCacheQueue.isEmpty())
            {
                return;
            }

            BaseResponse response;

            for (int i = _responseCacheQueue.Count - 1; i >= 0; --i)
            {
                response = _responseCacheQueue.peek();

                //执行过了
                if (response.executed)
                {
                    _responseCacheQueue.poll();

                    BytesControl.releaseResponse(response);
                }
                else
                {
                    break;
                }
            }
        }
Пример #2
0
        /** 收到一个协议包(netIO线程) */
        private void onSocketDataT(byte[] data, int pos, int len)
        {
            _readStream.setBuf(data, pos, len);

            //客户端不需要检测

            int mid = _readStream.natureReadUnsignedShort();

            //检测序号
            if (!BytesControl.isIgnoreMessage(mid))
            {
                int receiveIndex = _readStream.natureReadShort();

                int nowIndex = _receiveMsgIndex;

                if (receiveIndex != nowIndex)
                {
                    ThreadControl.addMainFunc(() =>
                    {
                        Ctrl.warnLog("序号检测没对上," + " nowIndex:" + nowIndex + " receiveIndex:" + receiveIndex + " mid:" + mid);

                        //视为被动关闭
                        closeForIO(Close_Error);
                    });

                    return;
                }

                ++_receiveMsgIndex;
            }

            if (_createResponseFunc != null)
            {
                BaseResponse response = _createResponseFunc(mid);

                if (response == null)
                {
                    if (ShineSetting.needMessageExistCheck)
                    {
                        ThreadControl.addMainFunc(() =>
                        {
                            Ctrl.throwError("未解析mid为" + mid + "的协议");
                        });
                    }

                    return;
                }

                if (response.isNewOne)
                {
                    _newResponseNum++;
                }

                response.socket = this;

                BaseResponse response2 = response.readFromStream(_readStream);

                //变了
                if (response2 != response)
                {
                    //直接回收 IO
                    BytesControl.releaseResponse(response);
                }

                if (response2 != null)
                {
                    if (ShineSetting.messageUsePool && response2.needRelease())
                    {
                        _responseCacheQueue.offer(response2);
                    }

                    //入队
                    _receiveQueue.add(response2);
                }
            }
        }