void ConnectNode::closeConnectNode()

in nlsCppSdk/transport/connectNode.cpp [990:1105]


void ConnectNode::closeConnectNode() {
  bool lock_ret = true;
  MUTEX_TRY_LOCK(_mtxCloseNode, 2000, lock_ret);
  if (!lock_ret) {
    LOG_ERROR("Node(%p) closeConnectNode, deadlock has occurred", this);

    if (_releasingFlag || _exitStatus == ExitCancel) {
      LOG_ERROR(
          "Node(%p) in the process of releasing/canceling, skip "
          "closeConnectNode.",
          this);

      _isConnected = false;

      if (_audioFrame) {
        free(_audioFrame);
        _audioFrame = NULL;
      }
      _audioFrameSize = 0;
      _maxFrameSize = 0;
      _isFirstAudioFrame = true;

      LOG_INFO(
          "Node(%p) closeConnectNode done, current node status:%s exit "
          "status:%s.",
          this, getConnectNodeStatusString().c_str(),
          getExitStatusString().c_str());
      return;
    }
  }

  LOG_DEBUG(
      "Node(%p) closeConnectNode begin, current node status:%s exit status:%s.",
      this, getConnectNodeStatusString().c_str(),
      getExitStatusString().c_str());

  if (_socketFd != INVALID_SOCKET) {
    if (_sslHandle == _nativeSslHandle) {
      if (_url._isSsl) {
        _sslHandle->sslClose();
      }
      evutil_closesocket(_socketFd);
      _socketFd = INVALID_SOCKET;
    } else {
      LOG_INFO(
          "Node(%p) _sslHandle:%p and _socketFd:%d does not belong to it with "
          "_nativeSslHandle:%p, should not close SSL, which will close and "
          "release by itself.",
          this, _sslHandle, _socketFd, _nativeSslHandle);
    }
  }

  _isConnected = false;

  if (_url._enableSysGetAddr && _dnsEvent) {
    event_del(_dnsEvent);
    event_free(_dnsEvent);
    _dnsEvent = NULL;
  }
  if (_readEvent) {
    event_del(_readEvent);
    event_free(_readEvent);
    _readEvent = NULL;
  }
  if (_writeEvent) {
    event_del(_writeEvent);
    event_free(_writeEvent);
    _writeEvent = NULL;
  }
  if (_connectEvent) {
    event_del(_connectEvent);
    event_free(_connectEvent);
    _connectEvent = NULL;
  }
  if (_launchEvent) {
    event_del(_launchEvent);
    event_free(_launchEvent);
    _launchEvent = NULL;
  }

#ifdef ENABLE_PRECONNECTED_POOL
  if (_startWithPoolEvent) {
    event_del(_startWithPoolEvent);
    event_free(_startWithPoolEvent);
    _startWithPoolEvent = NULL;
  }
#endif

#ifdef ENABLE_HIGH_EFFICIENCY
  if (_connectTimerEvent != NULL) {
    if (_connectTimerFlag) {
      evtimer_del(_connectTimerEvent);
      _connectTimerFlag = false;
    }
    event_free(_connectTimerEvent);
    _connectTimerEvent = NULL;
  }
#endif

  if (_audioFrame) {
    free(_audioFrame);
    _audioFrame = NULL;
  }
  _audioFrameSize = 0;
  _maxFrameSize = 0;
  _isFirstAudioFrame = true;

  LOG_INFO(
      "Node(%p) closeConnectNode done, current node status:%s exit status:%s.",
      this, getConnectNodeStatusString().c_str(),
      getExitStatusString().c_str());

  if (lock_ret) {
    MUTEX_UNLOCK(_mtxCloseNode);
  }
}