int shift_buf_2_msg()

in eventmesh-sdks/eventmesh-sdk-c/src/rmb_msg.c [579:726]


int shift_buf_2_msg (StRmbMsg * pStMsg, const char *cBuf, unsigned int uiLen)
{
  //StRmbMsg msg;
  const char *p = cBuf;

  unsigned int uiBufLen =
    3 * sizeof (char) + sizeof (pStMsg->sysHeader) +
    2 * sizeof (pStMsg->dest) + 2 * sizeof (unsigned long) +
    3 * sizeof (int) + sizeof (StFlow *) + sizeof (pStMsg->strTargetDcn) +
    sizeof (pStMsg->strServiceId) + sizeof (pStMsg->strScenarioId) +
    sizeof (int) + sizeof (int);
  if (uiBufLen > uiLen)
  {
    rmb_errno = RMB_ERROR_BUF_2_MSG_FAIL;
    return -1;
  }

  //copy msg src
  memcpy (&pStMsg->iMsgMode, p, sizeof (int));
  p += sizeof (int);

  //copy cPkgType
  pStMsg->cPkgType = *p;
  p += sizeof (char);

  //copy cLogicType
  pStMsg->cLogicType = *p;
  p += sizeof (char);

  //copy cApiType
  pStMsg->cApiType = *p;
  p += sizeof (char);

  //copy sysHeader
  memcpy (&pStMsg->sysHeader, p, sizeof (pStMsg->sysHeader));
  p += sizeof (pStMsg->sysHeader);

  //copy dest
  memcpy (&pStMsg->dest, p, sizeof (pStMsg->dest));
  p += sizeof (pStMsg->dest);

  //copy replyTo
  memcpy (&pStMsg->replyTo, p, sizeof (pStMsg->replyTo));
  p += sizeof (pStMsg->replyTo);

  //copy msgid
  memcpy (&pStMsg->ulMsgId, p, sizeof (unsigned long));
  p += sizeof (unsigned long);

  //copy msgLiveTime
  memcpy (&pStMsg->ulMsgLiveTime, p, sizeof (unsigned long));
  p += sizeof (unsigned long);

  //copy AppHeader  -- len
  memcpy (&pStMsg->iAppHeaderLen, p, sizeof (int));
  p += sizeof (int);

  uiBufLen += pStMsg->iAppHeaderLen;
  if (pStMsg->iAppHeaderLen >= pStMsg->iMallocAppHeaderLength)
  {
    int iFitSize =
      rmb_get_fit_size (pStMsg->iAppHeaderLen, MAX_APPHEADER_LENGTH);
    if (iFitSize < 0)
    {
      LOGRMB (RMB_LOG_ERROR, "appheader len=%u too large!max_limit=%u\n",
              pStMsg->iAppHeaderLen, MAX_APPHEADER_LENGTH);
      rmb_errno = RMB_ERROR_BUF_2_MSG_FAIL;
      return rmb_errno;
    }
    free (pStMsg->cAppHeader);
    pStMsg->cAppHeader = NULL;
    pStMsg->cAppHeader = (char *) malloc (iFitSize);
    pStMsg->iMallocAppHeaderLength = iFitSize;
  }
  if (uiBufLen > uiLen)
  {
    rmb_errno = RMB_ERROR_BUF_2_MSG_FAIL;
    return -2;
  }

  //copy AppHeader  -- data
  memcpy (pStMsg->cAppHeader, p, pStMsg->iAppHeaderLen);
  pStMsg->cAppHeader[pStMsg->iAppHeaderLen] = 0;
  p += pStMsg->iAppHeaderLen;

  //copy content
  memcpy (&pStMsg->iContentLen, p, sizeof (int));
  p += sizeof (int);

  uiBufLen += pStMsg->iContentLen;
  if (pStMsg->iContentLen >= pStMsg->iMallocContentLength)
  {
    int iFitSize =
      rmb_get_fit_size (pStMsg->iContentLen, MAX_MSG_CONTENT_SIZE);
    if (iFitSize < 0)
    {
      LOGRMB (RMB_LOG_ERROR, "content len=%u too large!max_limit=%u\n", uiLen,
              MAX_MSG_CONTENT_SIZE);
      rmb_errno = RMB_ERROR_BUF_2_MSG_FAIL;
      return -3;
    }
    free (pStMsg->cContent);
    pStMsg->cContent = NULL;
    pStMsg->cContent = (char *) malloc (iFitSize);
    pStMsg->iMallocContentLength = iFitSize;
  }

  if (uiBufLen > uiLen)
  {
    rmb_errno = RMB_ERROR_BUF_2_MSG_FAIL;
    return -3;
  }

  //copy content
  memcpy (pStMsg->cContent, p, pStMsg->iContentLen);
  pStMsg->cContent[pStMsg->iContentLen] = 0;
  p += pStMsg->iContentLen;

  //copy corrid
  memcpy (&pStMsg->iCorrLen, p, sizeof (int));
  p += sizeof (int);

  uiBufLen += pStMsg->iCorrLen;
  if (uiBufLen > uiLen)
  {
    rmb_errno = RMB_ERROR_BUF_2_MSG_FAIL;
    return -4;
  }

  memcpy (pStMsg->cCorrId, p, pStMsg->iCorrLen);
  p += pStMsg->iCorrLen;

  memcpy (&pStMsg->iEventOrService, p, sizeof (int));
  p += sizeof (int);

  memcpy (pStMsg->strTargetDcn, p, sizeof (pStMsg->strTargetDcn));
  p += sizeof (pStMsg->strTargetDcn);

  memcpy (pStMsg->strServiceId, p, sizeof (pStMsg->strServiceId));
  p += sizeof (pStMsg->strServiceId);

  memcpy (pStMsg->strScenarioId, p, sizeof (pStMsg->strScenarioId));
  p += sizeof (pStMsg->strScenarioId);

  //memcpy(pStMsg, &msg, sizeof(StRmbMsg));

  return 0;
}