int ConnectNode::sslProcess()

in nlsCppSdk/transport/connectNode.cpp [3450:3527]


int ConnectNode::sslProcess() {
  EXIT_CANCEL_CHECK(_exitStatus, this);
  int ret = 0;
  NlsClientImpl *client = _instance;
  NlsNodeManager *node_manager = client->getNodeManger();
  int status = NodeStatusInvalid;
  ret = node_manager->checkNodeExist(this, &status);
  if (ret != Success) {
    LOG_ERROR("Node(%p) checkNodeExist failed, ret:%d", this, ret);
    return ret;
  }

  if (_url._isSsl) {
    ret = _sslHandle->sslHandshake(_socketFd, _url._host);
    if (ret == SSL_ERROR_WANT_READ || ret == SSL_ERROR_WANT_WRITE) {
// current status:NodeConnected
// LOG_DEBUG("Node(%p) status:%s, wait ssl process ...",
//     this, getConnectNodeStatusString().c_str());

// trigger connectEventCallback()
#ifdef ENABLE_HIGH_EFFICIENCY
      // connect回调和定时connect回调交替调用, 降低事件量的同时保证稳定
      if (!_connectTimerFlag) {
        if (NULL == _connectTimerEvent) {
          LOG_ERROR("Node(%p) event is nullptr.", this);
          return -(EventEmpty);
        }

        // LOG_DEBUG("Node(%p) add evtimer _connectTimerEvent.", this);
        evtimer_add(_connectTimerEvent, &_connectTimerTv);
        _connectTimerFlag = true;
      } else {
        if (NULL == _connectEvent) {
          LOG_ERROR("Node(%p) event is nullptr.", this);
          return -(EventEmpty);
        }

        // LOG_DEBUG("Node(%p) add events _connectEvent.", this);
        utility::TextUtils::GetTimevalFromMs(
            &_connectTv, _request->getRequestParam()->getTimeout());
        // set _connectEvent to pending status.
        event_add(_connectEvent, &_connectTv);
        _connectTimerFlag = false;
      }
#else
      if (NULL == _connectEvent) {
        LOG_ERROR("Node(%p) event is nullptr.", this);
        return -(EventEmpty);
      }
      utility::TextUtils::GetTimevalFromMs(
          &_connectTv, _request->getRequestParam()->getTimeout());
      // LOG_DEBUG("Node(%p) add events _connectEvent.", this);
      event_add(_connectEvent, &_connectTv);
#endif

      return 1;
    } else if (ret < 0) {
      _nodeErrMsg = _sslHandle->getFailedMsg();
      LOG_ERROR("Node(%p) _sslHandle(%p) sslHandshake failed(%d), %s.", this,
                _sslHandle, ret, _nodeErrMsg.c_str());
      return -(SslHandshakeFailed);
    } else {
      _workStatus = NodeHandshaking;
      node_manager->updateNodeStatus(this, NodeStatusHandshaking);
      LOG_DEBUG(
          "Node(%p) _sslHandle(%p) sslHandshake done, ret:%d, set "
          "node:NodeHandshaking.",
          this, _sslHandle, ret);
      return 0;
    }
  } else {
    _workStatus = NodeHandshaking;
    node_manager->updateNodeStatus(this, NodeStatusHandshaking);
    LOG_INFO("Node(%p) it's not ssl process, set node:NodeHandshaking.", this);
  }

  return 0;
}