/** 连接失败一次(主线程) */ public void preConnectFailedForIO() { if (!isConnecting()) { return; } Ctrl.log("socket连接失败一次", _doIndex); if (_currentInfo != null) { //记录一次失败 _currentInfo.failOnce(); } toCloseForConnect(); //超出次数限制 if (_tryCountMax > 0 && _currentTryCount >= _tryCountMax) { _connectTime = 0; if (_connectFailedCall != null) { _connectFailedCall(); } } else { if (_currentInfo != null) { if (_currentInfo.isCircle()) { _tryConnectTime.start(); } else { //此次不计次数 --_currentTryCount; retryConnect(); } } else { //dns没获取到 toGetDNS(); } } }
/** 执行关闭(主线程) */ 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(); } }
/// <summary> /// 写入一个非空数据(完整版) /// </summary> public void writeDataSimpleNotNull(BaseData data) { int dataID = data.getDataID(); if (dataID <= 0) { Ctrl.log("不该找不到dataID", dataID); this.writeShort(-1); } else { this.writeShort(dataID); data.writeBytesSimple(this); } }
//--data--// /// <summary> /// 写入一个非空数据(可继承的)(完整版) /// </summary> public void writeDataFullNotNull(BaseData data) { startWriteObj(); int dataID = data.getDataID(); if (dataID <= 0) { Ctrl.log("不该找不到dataID", dataID); this.writeShort(-1); } else { this.writeShort(dataID); data.writeBytesFullWithoutLen(this); } endWriteObj(); }
/** 重连的下一步(主线程) */ protected void doReconnectNext(short lastReceiveIndex, bool needReMsg) { short dd = indexD(_sendMsgIndex, lastReceiveIndex); if (dd < 0 || dd >= ShineSetting.requestCacheLen) { Ctrl.warnLog("重连时,消息缓存已失效"); closeForIO(Close_ReconnectFailed, false); return; } if (needReMsg) { sendAbs(SocketReconnectSuccessRequest.create(_receiveMsgIndex)); } for (short i = lastReceiveIndex; i != _sendMsgIndex; i++) { BaseRequest request = _sendCacheRequestQueue[i & ShineSetting.requestCacheMark]; if (request == null) { Ctrl.warnLog("找不到缓存消息,或是超时被释放"); closeForIO(Close_ReconnectFailed, false); return; } else { doWriteRequestToStream(request, i); } } _reconnecting = false; _disConnectLastSendIndex = 0; //推送一次 toFlush(); Ctrl.log("socket重连成功:"); }
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; } }