/// <summary> /// 创建实例 /// </summary> public static PingRequest create(int index) { PingRequest re = (PingRequest)BytesControl.createRequest(dataID); re.index = index; return(re); }
//--data--// /// <summary> /// 读出一个非空数据(完整版) /// </summary> public BaseData readDataFullNotNull() { if (isEmpty()) { return(null); } startReadObj(); int dataID = this.readShort(); BaseData data; if (dataID <= 0) { Ctrl.log("不该找不到dataID", dataID); data = null; } else { data = BytesControl.getDataByID(dataID); if (data != null) { data.readBytesFullWithoutLen(this); } } endReadObj(); return(data); }
/// <summary> /// 读出一个非空数据(可继承的)(简版) /// </summary> public BaseData readDataSimpleNotNull() { if (isEmpty()) { return(null); } int dataID = this.readShort(); BaseData data; if (dataID <= 0) { Ctrl.log("不该找不到dataID", dataID); data = null; } else { data = BytesControl.getDataByID(dataID); if (data != null) { data.readBytesSimple(this); } } return(data); }
/** 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; } } }
/// <summary> /// 克隆数据 /// </summary> public BaseData clone() { BaseData re = BytesControl.getDataByID(_dataID); re.copy(this); return(re); }
/** 创建数据 */ public BaseData createData(int type) { if (_pool != null) { return(_pool.createData(type)); } return(BytesControl.getDataByID(type)); }
/// <summary> /// 启动 /// </summary> public static void setup(GameObject root, Action exitRun = null) { if (_inited) { return; } _inited = true; _root = root; GameObject.DontDestroyOnLoad(_root); _exitRun = exitRun; _rootBehavious = _root.AddComponent <ShineBehavious>(); SystemControl.init(); ThreadControl.init(); DateControl.init(); NetControl.init(); BytesControl.init(); //显示部分 if (ShineSetting.isWholeClient) { SKeyboardControl.init(); STouchControl.init(); UIControl.init(); CameraControl.init(); Tween.init(); LoadControl.init(); ResourceInfoControl.init(); AssetPoolControl.init(); } else { //gm指令所需 if (ShineSetting.needGMCommandUI) { SKeyboardControl.init(); STouchControl.init(); UIControl.init(); } ShineSetting.debugJumpResourceVersion = true; ResourceInfoControl.initBase(); } }
/** 发送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); } }
public static void setupForEditor() { SystemControl.init(); DateControl.init(); BytesControl.init(); if (ShineSetting.isWholeClient) { ResourceInfoControl.init(); } else { ResourceInfoControl.initBase(); } }
/** 实际执行写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; }
/** 执行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); } }
/** 执行释放(主线程) */ public void doRelease() { if (!ShineSetting.messageUsePool) { return; } //还没发送,就跳过回收 if (!sended) { Ctrl.warnLog("出现一次Request释放时还没发送的情况", getDataID()); return; } if (released) { Ctrl.errorLog("析构了两次"); } released = true; BytesControl.releaseRequest(this); }
/// <summary> /// 复制(深拷贝) /// </summary> protected override void toCopy(BaseData data) { if (!(data is UIObjectData)) { return; } UIObjectData mData = (UIObjectData)data; this.name = mData.name; this.type = mData.type; this.style = mData.style; if (mData.children != null) { if (this.children != null) { this.children.clear(); this.children.ensureCapacity(mData.children.size()); } else { this.children = new SList <UIObjectData>(); } SList <UIObjectData> childrenT = this.children; if (!mData.children.isEmpty()) { UIObjectData[] childrenVValues = mData.children.getValues(); for (int childrenVI = 0, childrenVLen = mData.children.length(); childrenVI < childrenVLen; ++childrenVI) { UIObjectData childrenV = childrenVValues[childrenVI]; UIObjectData childrenU; if (childrenV != null) { childrenU = (UIObjectData)BytesControl.createData(UIObjectData.dataID); childrenU.copy(childrenV); } else { childrenU = null; } childrenT.add(childrenU); } } } else { this.children = null; } if (mData.intArgs != null) { int[] intArgsR = mData.intArgs; int intArgsLen = intArgsR.Length; if (this.intArgs == null || this.intArgs.Length != intArgsLen) { this.intArgs = new int[intArgsLen]; } BytesControl.arrayCopy(mData.intArgs, this.intArgs, intArgsLen); } else { this.intArgs = null; } if (mData.strArgs != null) { string[] strArgsR = mData.strArgs; int strArgsLen = strArgsR.Length; if (this.strArgs == null || this.strArgs.Length != strArgsLen) { this.strArgs = new string[strArgsLen]; } string[] strArgsT = this.strArgs; for (int strArgsI = 0; strArgsI < strArgsLen; ++strArgsI) { string strArgsV = strArgsR[strArgsI]; string strArgsU; strArgsU = strArgsV; strArgsT[strArgsI] = strArgsU; } } else { this.strArgs = null; } }
/** 收到一个协议包(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); } } }