static int rmb_pub_encode_header_for_wemq()

in eventmesh-sdks/eventmesh-sdk-c/src/rmb_pub.c [99:308]


static int rmb_pub_encode_header_for_wemq (unsigned int uiCmd,
                                           StWemqThreadMsg * ptThreadMsg,
                                           StRmbMsg * ptSendMsg)
{
  if (ptThreadMsg == NULL || ptSendMsg == NULL)
  {
    LOGRMB (RMB_LOG_ERROR, "ptThreadMsg is null or ptSendMsg is null");
    return -1;
  }

  switch (uiCmd)
  {
  case THREAD_MSG_CMD_SEND_MSG:
    {
      ptThreadMsg->m_iCmd = THREAD_MSG_CMD_SEND_MSG;

      WEMQJSON *jsonHeader = json_object_new_object ();

      // ptSendMsg->strServiceId[3] == '3' 多播使用ASYNC_MESSAGE_TO_SERVER
      if (ptSendMsg->strServiceId[3] == '4')
      {
        json_object_object_add (jsonHeader, MSG_HEAD_COMMAND_STR,
                                json_object_new_string
                                (BROADCAST_MESSAGE_TO_SERVER));
      }
      else
      {
        json_object_object_add (jsonHeader, MSG_HEAD_COMMAND_STR,
                                json_object_new_string
                                (ASYNC_MESSAGE_TO_SERVER));
      }
      json_object_object_add (jsonHeader, MSG_HEAD_SEQ_INT,
                              json_object_new_int (++g_iSendReqForEvent));
      LOGRMB (RMB_LOG_DEBUG, "put seq:%ld in pkg", g_iSendReqForEvent);
      json_object_object_add (jsonHeader, MSG_HEAD_CODE_INT,
                              json_object_new_int (0));
      const char *header_str = json_object_get_string (jsonHeader);
      if (header_str == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "get thread msg header failed");
        json_object_put (jsonHeader);
        return -1;
      }
      ptThreadMsg->m_iHeaderLen = strlen (header_str);

      LOGRMB (RMB_LOG_DEBUG, "Get thread msg header succ, len=%d,%s\n",
              ptThreadMsg->m_iHeaderLen, header_str);
      ptThreadMsg->m_pHeader =
        (char *) malloc ((ptThreadMsg->m_iHeaderLen + 1) * sizeof (char));
      if (ptThreadMsg->m_pHeader == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "malloc for ptThreadMsg->m_pHeader failed");
        json_object_put (jsonHeader);
        return -2;
      }
      memcpy (ptThreadMsg->m_pHeader, header_str, ptThreadMsg->m_iHeaderLen);
      ptThreadMsg->m_pHeader[ptThreadMsg->m_iHeaderLen] = '\0';

      json_object_put (jsonHeader);
      return 0;
    }
  case THREAD_MSG_CMD_SEND_REQUEST:
    {
      ptThreadMsg->m_iCmd = THREAD_MSG_CMD_SEND_REQUEST;

      WEMQJSON *jsonHeader = json_object_new_object ();
      json_object_object_add (jsonHeader, MSG_HEAD_COMMAND_STR,
                              json_object_new_string (REQUEST_TO_SERVER));
      json_object_object_add (jsonHeader, MSG_HEAD_SEQ_INT,
                              json_object_new_int (g_iSendReq++));
      json_object_object_add (jsonHeader, MSG_HEAD_CODE_INT,
                              json_object_new_int (0));
      const char *header_str = json_object_get_string (jsonHeader);
      if (header_str == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "Get thread msg header failed");
        json_object_put (jsonHeader);
        return -1;
      }
      ptThreadMsg->m_iHeaderLen = strlen (header_str);

      LOGRMB (RMB_LOG_DEBUG, "Get thread msg header succ, len=%d,%s\n",
              ptThreadMsg->m_iHeaderLen, header_str);
      ptThreadMsg->m_pHeader =
        (char *) malloc ((ptThreadMsg->m_iHeaderLen + 1) * sizeof (char));
      if (ptThreadMsg->m_pHeader == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "malloc for ptThreadMsg->m_pHeader failed\n");
        json_object_put (jsonHeader);
        return -2;
      }
      memcpy (ptThreadMsg->m_pHeader, header_str, ptThreadMsg->m_iHeaderLen);
      ptThreadMsg->m_pHeader[ptThreadMsg->m_iHeaderLen] = '\0';
      json_object_put (jsonHeader);
      return 0;
    }
  case THREAD_MSG_CMD_SEND_REQUEST_ASYNC:
    {
      ptThreadMsg->m_iCmd = THREAD_MSG_CMD_SEND_REQUEST_ASYNC;

      WEMQJSON *jsonHeader = json_object_new_object ();
      json_object_object_add (jsonHeader, MSG_HEAD_COMMAND_STR,
                              json_object_new_string (REQUEST_TO_SERVER));
      json_object_object_add (jsonHeader, MSG_HEAD_SEQ_INT,
                              json_object_new_int (g_iSendReq++));
      json_object_object_add (jsonHeader, MSG_HEAD_CODE_INT,
                              json_object_new_int (0));
      const char *header_str = json_object_get_string (jsonHeader);
      if (header_str == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "Get thread msg header failed");
        json_object_put (jsonHeader);
        return -1;
      }
      ptThreadMsg->m_iHeaderLen = strlen (header_str);

      LOGRMB (RMB_LOG_DEBUG, "Get thread msg header succ, len=%d,%s\n",
              ptThreadMsg->m_iHeaderLen, header_str);
      ptThreadMsg->m_pHeader =
        (char *) malloc ((ptThreadMsg->m_iHeaderLen + 1) * sizeof (char));
      if (ptThreadMsg->m_pHeader == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "malloc for ptThreadMsg->m_pHeader failed\n");
        json_object_put (jsonHeader);
        return -2;
      }
      memcpy (ptThreadMsg->m_pHeader, header_str, ptThreadMsg->m_iHeaderLen);
      ptThreadMsg->m_pHeader[ptThreadMsg->m_iHeaderLen] = '\0';
      json_object_put (jsonHeader);
      return 0;
    }
  case THREAD_MSG_CMD_SEND_REPLY:
    {
      ptThreadMsg->m_iCmd = THREAD_MSG_CMD_SEND_REPLY;

      WEMQJSON *jsonHeader = json_object_new_object ();
      json_object_object_add (jsonHeader, MSG_HEAD_COMMAND_STR,
                              json_object_new_string (RESPONSE_TO_SERVER));
      json_object_object_add (jsonHeader, MSG_HEAD_CODE_INT,
                              json_object_new_int (0));
      json_object_object_add (jsonHeader, MSG_HEAD_SEQ_INT,
                              json_object_new_int (g_iSendReq++));

      const char *header_str = json_object_get_string (jsonHeader);
      if (header_str == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "Get thread msg header failed");
        json_object_put (jsonHeader);
        return -1;
      }
      ptThreadMsg->m_iHeaderLen = strlen (header_str);

      //LOGRMB(RMB_LOG_DEBUG, "Get thread msg header succ, len=%d,%s\n", ptThreadMsg->m_iHeaderLen, header_str);
      ptThreadMsg->m_pHeader =
        (char *) malloc ((ptThreadMsg->m_iHeaderLen + 1) * sizeof (char));
      if (ptThreadMsg->m_pHeader == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "malloc for ptThreadMsg->m_pHeader failed\n");
        json_object_put (jsonHeader);
        return -2;
      }
      memcpy (ptThreadMsg->m_pHeader, header_str, ptThreadMsg->m_iHeaderLen);
      ptThreadMsg->m_pHeader[ptThreadMsg->m_iHeaderLen] = '\0';

      json_object_put (jsonHeader);
      return 0;
    }
  case THREAD_MSG_CMD_SEND_MSG_ACK:
    {
      ptThreadMsg->m_iCmd = THREAD_MSG_CMD_SEND_MSG_ACK;

      WEMQJSON *jsonHeader = json_object_new_object ();
      json_object_object_add (jsonHeader, MSG_HEAD_COMMAND_STR,
                              json_object_new_string (MSG_ACK));
      json_object_object_add (jsonHeader, MSG_HEAD_CODE_INT,
                              json_object_new_int (0));
      json_object_object_add (jsonHeader, MSG_HEAD_SEQ_INT,
                              json_object_new_int (g_iSendReq++));

      const char *header_str = json_object_get_string (jsonHeader);
      if (header_str == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "Get thread msg header failed");
        json_object_put (jsonHeader);
        return -1;
      }
      ptThreadMsg->m_iHeaderLen = strlen (header_str);

      //LOGRMB(RMB_LOG_DEBUG, "Get thread msg header succ, len=%d,%s\n", ptThreadMsg->m_iHeaderLen, header_str);
      ptThreadMsg->m_pHeader =
        (char *) malloc ((ptThreadMsg->m_iHeaderLen + 1) * sizeof (char));
      if (ptThreadMsg->m_pHeader == NULL)
      {
        LOGRMB (RMB_LOG_ERROR, "malloc for ptThreadMsg->m_pHeader failed\n");
        json_object_put (jsonHeader);
        return -2;
      }
      memcpy (ptThreadMsg->m_pHeader, header_str, ptThreadMsg->m_iHeaderLen);
      ptThreadMsg->m_pHeader[ptThreadMsg->m_iHeaderLen] = '\0';

      json_object_put (jsonHeader);
      return 0;
    }
  default:
    LOGRMB (RMB_LOG_ERROR, "unknown cmd:%u", uiCmd);
    return -1;
  }

  return 0;
}