예제 #1
0
        /** 发送request(主线程) */
        protected void sendRequestForIO(BaseRequest request)
        {
            request.preSend();

            bool  needIndex = false;
            short index     = 0;

            if (!BytesControl.isIgnoreMessage(request.getDataID()))
            {
                needIndex = true;
                index     = _sendMsgIndex++;

                int key = index & ShineSetting.requestCacheMark;
                //存起
                BaseRequest cache;
                if ((cache = _sendCacheRequestQueue[key]) != null)
                {
                    cache.doRelease();

                    if (ShineSetting.messageUsePool)
                    {
                        //序号碰到
                        if (key == (_sendCheckIndex & ShineSetting.requestCacheMark))
                        {
                            Ctrl.warnLog("出现一次发送index超过检查index", _sendCheckIndex);
                            _sendCheckIndex++;
                        }
                    }
                }

                _sendCacheRequestQueue[key] = request;
            }

            if (_reconnecting)
            {
                if (needIndex)
                {
                    //失效
                    if (indexD(index, _disConnectLastSendIndex) >= ShineSetting.requestCacheLen)
                    {
                        reconnectFailed();
                    }
                }
            }
            else
            {
                doWriteRequestToStream(request, index);
            }
        }
예제 #2
0
        /** 实际执行写request到stream */
        private void toWriteRequestToStream(BytesWriteStream stream, BaseRequest request, int index)
        {
            stream.clearBooleanPos();

            int limit = request.getMessageLimit();

            stream.setWriteLenLimit(limit);
            int mid = request.getDataID();

            int startPos = stream.getPosition();

            //写协议ID(原生写)
            stream.natureWriteUnsignedShort(mid);

            if (!BytesControl.isIgnoreMessage(mid))
            {
                stream.natureWriteShort(index);
            }

            request.writeToStream(stream);

            int endPos = stream.getPosition();
            int len    = endPos - startPos;

            if (limit > 0 && len >= limit)
            {
                Ctrl.errorLog("request长度超出上限", len);
            }

            if (ShineSetting.needCustomLengthBasedFrameDecoder)
            {
                stream.insertLenToPos(startPos, len);
            }
            else
            {
                stream.setPosition(startPos);

                stream.insertVoidBytes(4, len);

                stream.natureWriteInt(len);

                stream.setPosition(endPos);
            }

            //写完的标记
            request.sended = true;
        }
예제 #3
0
        /** 执行response们(主线程) */
        private void toRunResponseOne(BaseResponse response)
        {
            int mid = response.getDataID();

            if (ShineSetting.needShowMessage && !BytesControl.isIgnoreMessage(mid))
            {
                Ctrl.debugLog("收消息:", "mid:" + mid, ShineSetting.needShowMessageDetail ? response.toDataString() : response.getDataClassName());
            }

            _server.checkResponseUnbind(mid);

            try
            {
                response.run();
            }
            catch (Exception e)
            {
                Ctrl.errorLogForIO(e);
            }
        }
예제 #4
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);
                }
            }
        }