int ConnectNode::cmdNotify()

in nlsCppSdk/transport/connectNode.cpp [1579:1662]


int ConnectNode::cmdNotify(CmdType type, const char *message) {
  int ret = Success;

  LOG_DEBUG("Node(%p) invoke CmdNotify: %s with %s", this,
            getCmdTypeString(type).c_str(),
            getConnectNodeStatusString().c_str());

  if (type == CmdStop) {
#ifdef ENABLE_REQUEST_RECORDING
    updateNodeProcess("stop", NodeStop, true, 0);
#endif
    addRemainAudioData();
    _exitStatus = ExitStopping;
    if (_workStatus == NodeStarted) {
      size_t length = evbuffer_get_length(_binaryEvBuffer);
      if (length == 0) {
        ret = sendControlDirective();
      } else {
        LOG_DEBUG(
            "Node(%p) invoke CmdNotify: %s, and continue send audio data "
            "%zubytes.",
            this, getCmdTypeString(type).c_str(), length);
      }
    }
  } else if (type == CmdCancel) {
#ifdef ENABLE_REQUEST_RECORDING
    updateNodeProcess("cancel", NodeCancel, true, 0);
#endif
    _exitStatus = ExitCancel;
  } else if (type == CmdStControl) {
#ifdef ENABLE_REQUEST_RECORDING
    updateNodeProcess("ctrl", NodeSendControl, true, 0);
#endif
    addCmdDataBuffer(CmdStControl, message);
    if (_workStatus == NodeStarted) {
      size_t length = evbuffer_get_length(_binaryEvBuffer);
      if (length == 0) {
        ret = nlsSendFrame(_cmdEvBuffer);
      }
    }
  } else if (type == CmdWarkWord) {
    _isWakeStop = true;
    size_t length = evbuffer_get_length(_wwvEvBuffer);
    if (length == 0) {
      addCmdDataBuffer(CmdWarkWord);
      ret = nlsSendFrame(_cmdEvBuffer);
    }
  } else if (type == CmdSendText) {
#ifdef ENABLE_REQUEST_RECORDING
    updateNodeProcess("send_text", NodeSendText, true, 0);
#endif
    addCmdDataBuffer(CmdSendText, message);
    if (_workStatus == NodeStarted) {
      ret = nlsSendFrame(_cmdEvBuffer);
      _isSendSingleRoundText = false;
    }
  } else if (type == CmdSendPing) {
    addCmdDataBuffer(CmdSendPing, NULL);
    ret = nlsSendFrame(_cmdEvBuffer);
  } else if (type == CmdSendFlush) {
    addCmdDataBuffer(CmdSendFlush, message);
    ret = nlsSendFrame(_cmdEvBuffer);
  } else {
    LOG_ERROR("Node(%p) invoke unknown command.", this);
  }

  if (ret < 0) {
    disconnectProcess();
    handlerTaskFailedEvent(getErrorMsg());
  }

#ifdef ENABLE_REQUEST_RECORDING
  if (type == CmdStop) {
    updateNodeProcess("stop", NodeStop, false, 0);
  } else if (type == CmdCancel) {
    updateNodeProcess("cancel", NodeCancel, false, 0);
  } else if (type == CmdStControl) {
    updateNodeProcess("ctrl", NodeSendControl, false, 0);
  } else if (type == CmdSendText) {
    updateNodeProcess("send_text", NodeSendText, false, 0);
  }
#endif
  return ret;
}