/** 执行关闭(主线程) */ private void closeForIO(int reason, bool canRec) { ThreadControl.checkCurrentIsMainThread(); if (_state == Closed) { return; } //正在连接中 if (isConnecting()) { stopConnect(); } _state = Closed; Ctrl.print("连接断开,reason:", reason, _doIndex); _doIndex++; //序号加 bool isInitiative = reason == Close_Initiative; if (isInitiative) { toFlush(); } onDisconnect(); //重连中,会再次重连 if (_openReconnect && !isInitiative && canRec && canReconnect()) //!_reconnecting && { Ctrl.log("连接断线,进入重连阶段"); _reconnecting = true; _disConnectLastSendIndex = _sendMsgIndex; _reconnectLastTime = ShineSetting.socketReConnectKeepTime; _closeByInitiative = isInitiative; clearSend(); beginReconnect(); } else { _closeByInitiative = isInitiative; realClose(); } }
private void Update() { if (_testPause) { if (Ctrl.getTimer() >= _resumeTime) { Ctrl.print("暂停恢复"); _testPause = false; } else { return; } } Ctrl.makeFixDirty(); long now = Ctrl.getTimer(); int dd = (int)(now - _lastTime); _lastTime = now; if (dd > 0) { if ((_dateFixDelayTick -= dd) <= 0) { _dateFixDelayTick = ShineSetting.dateFixDelay; DateControl.makeFixDirty(); } //系统逻辑 SystemControl.onFrame(); //计时器 TimeDriver.instance.tick(dd); } TimeDriver.instance.update(); //线程事务最后执行 if (dd > 0) { //线程 ThreadControl.onFrame(); } }
private void onSocketConnect(IAsyncResult result) { bool isError = false; try { _socket.EndConnect(result); } catch (Exception e) { // Ctrl.printExceptionForIO(e); isError = true; } try { EndPoint localEndPoint = _socket.LocalEndPoint; if (localEndPoint != null) { Ctrl.debugLogForIO("本地端口:", localEndPoint.Serialize().ToString(), doIndex); } } catch (Exception e) { Ctrl.printExceptionForIO(e); } Ctrl.debugLogForIO("socketContent连接结果,success:", !isError, this.GetHashCode()); ThreadControl.addMainFunc(() => { if (isError) { connectFailedOnce(); } else { connectSuccess(); } }); }
/** 发送log */ private static void toSendLog(int type, string str) { if (!ShineSetting.needLogSendServer || _logPause) { return; } if (str.Length > _sendLogMax) { str = str.Substring(0, _sendLogMax) + "..."; } if (_sendLogFunc != null) { ThreadControl.addMainFunc(() => { _sendLogFunc(type, str); }); } }
/** 即将关闭(IO线程) */ protected void preBeClose(int way) { // Ctrl.debugLogForIO("preBeClose",way); doClose(); if (_clozed) { return; } _clozed = true; Ctrl.debugLogForIO("preBeClose2", way, this.GetHashCode()); ThreadControl.addMainFunc(() => { if (checkIsCurrent()) { _parent.beClose(); } }); }
public static void warnLogForIO(params object[] args) { ThreadControl.addMainFunc(() => { warnLog(args); }); }
private void toGetDNS() { _state = Connecting; _connectTime = 0; int index = ++_doIndex; _currentInfo = _ipInfoDic.get(_host); if (_currentInfo != null) { preConnectAfterAddress(); } else { IPAddress address; if (!IPAddress.TryParse(_host, out address)) { Ctrl.debugLog("dns上"); Dns.BeginGetHostAddresses(_host, v => { IPAddress[] addresses = null; try { addresses = Dns.EndGetHostAddresses(v); } catch (Exception e) { Ctrl.printExceptionForIO(e); } ThreadControl.addMainFunc(() => { if (index == _doIndex) { if (addresses != null) { foreach (IPAddress ipAddress in addresses) { Ctrl.print("dns看收到的上地址", ipAddress); } _currentInfo = registIPInfo(_host, addresses); preConnectAfterAddress(); } else { Ctrl.printForIO("解析dns失败"); } } else { Ctrl.print("dns获取完,已经到下个index"); } }); }, null); } else { Ctrl.debugLog("dns下"); _currentInfo = registIPInfo(_host, new[] { address }); preConnectAfterAddress(); } } }
/** 收到一个协议包(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); } } }