⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ss_msg.c

📁 中国石油二期加油站IC系统后台通讯软件
💻 C
📖 第 1 页 / 共 5 页
字号:
Buffer **mBuf2;             /* message 2 */
#endif
{
    SsMsgInfo *minfo1;
    SsMsgInfo *minfo2;
    Buffer *tmp;
    Buffer *prev;
    Buffer *next;

    TRC1(SSegMsg)

#if (ERRCLASS & ERRCLS_INT_PAR)
    /* check message buffer 1 */
    if (mBuf1 == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS260, ERRZERO, "SSegMsg : Null Buffer");
        RETVALUE(RFAILED);
    }
    /* check message buffer 2 */
    if (mBuf2 == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS261, ERRZERO, "SSegMsg : Null Buffer");
        RETVALUE(RFAILED);
    }
    if (idx < 0)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS262, ERRZERO, "SSegMsg : Invalid index");
        RETVALUE(RFAILED);
    }
    if (mBuf1->b_datap->db_type != SS_M_PROTO)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS263, ERRZERO, "SSegMsg : Incorrect buffer\
                                                   type");
        RETVALUE(RFAILED);
    }
#endif

    /* get the SsMsgInfo of mBuf1 */
    minfo1 = (SsMsgInfo*) mBuf1->b_rptr;

    /* if index > length of mBuf, return */
    if (idx >= minfo1->len)
    {
        *mBuf2 = NULLP;
        RETVALUE(ROKDNA);
    }
    /* allocate message buffer */
    if (SGetMsg(minfo1->region, minfo1->pool, mBuf2) != ROK)
    {
        SSLOGERROR(ERRCLS_DEBUG, ESS264, ERRZERO, "SSegMsg : SGetMsg failed");
        RETVALUE(RFAILED);
    }

    /* get the SsMsgInfo of mBuf2 */
    minfo2 = (SsMsgInfo*) (*mBuf2)->b_rptr;

    /* adjust the lengths of mBuf1, mBuf2 */
    minfo2->len = minfo1->len - idx;
    minfo1->len = idx;

    /* set the endptr of mBuf2 to mBuf1 */
    minfo2->endptr = minfo1->endptr;

    /* if index is zero ... */
    if (!idx)
    {
        (*mBuf2)->b_cont = mBuf1->b_cont;

        /* set the endptr and b_cont of mBuf1 to NULLP */
        minfo1->endptr = NULLP;
        mBuf1->b_cont = NULLP;

        RETVALUE(ROK);
    }

    /* get the first SS_M_DATA blk */
    tmp = mBuf1->b_cont;
    prev = mBuf1;

    FIND_OFFSET_AND_PREV(prev, tmp, idx)

    /* segmented at the start of a blk */
    if (!idx)
    {
        (*mBuf2)->b_cont = tmp;
        prev->b_cont = NULLP;
        minfo1->endptr = prev;
    } else
    {
        /* allocate a message blk without a data blk */
        /* ssDupB internally increments the reference count */
        if (!(next = ssDupB(tmp)))
        {
            /* reset length */
            minfo1->len += minfo2->len;
            (Void) SPutMsg(*mBuf2);
            RETVALUE(ROUTRES);
        }
        (*mBuf2)->b_cont = next;

        tmp->b_cont = NULLP;

        tmp->b_wptr = tmp->b_rptr + idx;
        next->b_rptr = tmp->b_wptr;

        if (minfo1->endptr == tmp)
        {
            minfo2->endptr = next;
        } else
        {
            minfo1->endptr = tmp;
        }
    }

    RETVALUE(ROK);
}

/*
*
*       Fun:   SCpyFixMsg
*
*       Desc:  This function copies data from a fixed buffer to a
*              message.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*
*       Notes: None
*
*       File:  ss_msg.c
*
*/

#ifdef ANSI
PUBLIC S16 SCpyFixMsg
(
Data *srcBuf,               /* source buffer */
Buffer *dstMbuf,            /* destination message buffer */
MsgLen dstIdx,              /* destination index */
MsgLen cnt,                 /* count */
MsgLen *cCnt
)
#else
PUBLIC S16 SCpyFixMsg(srcBuf, dstMbuf, dstIdx, cnt, cCnt)
Data *srcBuf;               /* source buffer */
Buffer *dstMbuf;            /* destination message buffer */
MsgLen dstIdx;              /* destination index */
MsgLen cnt;                 /* count */
MsgLen *cCnt;               /* copied count */
#endif
{
    S16 ret;
    SsMsgInfo *minfo;
    Buffer *right;

    TRC1(SCpyFixMsg)

#if (ERRCLASS & ERRCLS_INT_PAR)
    /* check source message buffer */
    if (srcBuf == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS265, ERRZERO, "SCpyFixMsg : Null Buffer");
        RETVALUE(RFAILED);
    }
    /* check destination message buffer */
    if (dstMbuf == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS266, ERRZERO, "SCpyFixMsg : Null Buffer");
        RETVALUE(RFAILED);
    }
    /* check copied count buffer */
    if (cCnt == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS267, ERRZERO, "SCpyFixMsg : Null Buffer");
        RETVALUE(RFAILED);
    }
    /* check copy count */
    if (cnt <= 0)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS268, ERRZERO, "SCpyFixMsg : Invalid count");
        RETVALUE(RFAILED);
    }
    if (dstMbuf->b_datap->db_type != SS_M_PROTO)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS269, ERRZERO, "SCpyFixMsg : Incorrect\
                                                   buffer type");
        RETVALUE(RFAILED);
    }
#endif

    minfo = (SsMsgInfo*) dstMbuf->b_rptr;

    if (minfo->len < dstIdx)
    {
#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS270, ERRZERO, "SCpyFixMsg : Invalid Index");
#endif
        RETVALUE(RFAILED);
    }

    /* add data at the start of dst buffer */
    if (!dstIdx)
    {
        if ((ret = SAddPreMsgMult(srcBuf, cnt, dstMbuf)) != ROK)
        {
#if (ERRCLASS & ERRCLS_DEBUG)
            SSLOGERROR(ERRCLS_DEBUG, ESS271, ERRZERO, "SCpyFixMsg : Failed in\
                                                    SAddPreMsgMult");
#endif
            RETVALUE(ret);
        }
        *cCnt = cnt;

        RETVALUE(ROK);
    }

    /* add data at the end of the dst buffer */
    if (minfo->len == dstIdx)
    {
        if ((ret = SAddPstMsgMult(srcBuf, cnt, dstMbuf)) != ROK)
        {
            RETVALUE(ret);
        }
        *cCnt = cnt;

        RETVALUE(ROK);
    }

    /* segment the message into dstMbuf and right */
    if ((ret = SSegMsg(dstMbuf, dstIdx, &right)) != ROK)
    {
        RETVALUE(ret);
    }

    /* append data at the end of dstMbuf */
    if ((ret = SAddPstMsgMult(srcBuf, cnt, dstMbuf)) != ROK)
    {
        RETVALUE(ret);
    }

    /* cancatenate dstMbuf and right */
    if ((ret = SCatMsg(dstMbuf, right, M1M2)) != ROK)
    {
        RETVALUE(ret);
    }

    *cCnt = cnt;

    (Void) SPutMsg(right);

    RETVALUE(ROK);
}

/*
*
*       Fun:   SCpyMsgFix
*
*       Desc:  This function copies data from a message
*              into a fixed buffer.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*
*       Notes: None
*
*       File:  ss_msg.c
*
*/

#ifdef ANSI
PUBLIC S16 SCpyMsgFix
(
Buffer *srcMbuf,            /* source message buffer */
MsgLen srcIdx,              /* source index */
MsgLen cnt,                 /* count */
Data *dstBuf,               /* destination buffer */
MsgLen *cCnt
)
#else
PUBLIC S16 SCpyMsgFix(srcMbuf, srcIdx, cnt, dstBuf, cCnt)
Buffer *srcMbuf;            /* source message buffer */
MsgLen srcIdx;              /* source index */
MsgLen cnt;                 /* count */
Data *dstBuf;               /* destination buffer */
MsgLen *cCnt;               /* copied count */
#endif
{
    Data *cptr;
    Buffer *tmp;
    SsMsgInfo *minfo;
    MsgLen numBytes;

    TRC1(SCpyMsgFix)

#if (ERRCLASS & ERRCLS_INT_PAR)
    /* check source message buffer */
    if (srcMbuf == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS272, ERRZERO, "SCpyMsgFix : Null Buffer");
        RETVALUE(RFAILED);
    }
    /* check destination message buffer */
    if (dstBuf == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS273, ERRZERO, "SCpyMsgFix : Null Buffer");
        RETVALUE(RFAILED);
    }
    if (cnt <= 0)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS274, ERRZERO, "SCpyMsgFix : Invalid Index");
        RETVALUE(RFAILED);
    }

    if (srcIdx < 0)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS275, ERRZERO, "SCpyMsgFix : Invalid Index");
        RETVALUE(RFAILED);
    }
    if (!cCnt)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS276, ERRZERO, "SCpyMsgFix : Null Buffer");
        RETVALUE(RFAILED);
    }
    if (srcMbuf->b_datap->db_type != SS_M_PROTO)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS277, ERRZERO, "SCpyMsgFix : Incorrect\
                                                   buffer type");
        RETVALUE(RFAILED);
    }
#endif

    /* get SsMsgInfo */
    minfo = (SsMsgInfo*) srcMbuf->b_rptr;

    if ((srcIdx + cnt) > minfo->len)
    {
        
		/* modify by shang 2002-7-17 23:01:41 */
		#if 0 /* last code */
		*cCnt = 0;
        RETVALUE(ROKDNA);
		
		#else /* new code */
		cnt = minfo->len;
		#endif /* end modify */
		/* modify by shang is over 2002-7-17 23:01:41*/
		
    }

    /* get the first SS_M_DATA blk */
    tmp = srcMbuf->b_cont;

    /* get to the srcIdx-th offset */
    FIND_OFFSET(tmp, srcIdx)

    *cCnt = cnt;

    /* set cptr to the read ptr of tmp + offset */
    cptr = tmp->b_rptr + srcIdx;

    while (cnt)
    {
        /* determine the number of bytes to be copied */
        numBytes = MIN(cnt, (tmp->b_wptr - cptr));

        /* decrement cnt */
        cnt -= numBytes;

        /* copy data */
        while (numBytes--)
            *dstBuf++ = *cptr++;

        /* get the next blk */
        if ((tmp = tmp->b_cont))
            /* set cptr to the read ptr of tmp */
            cptr = tmp->b_rptr;
        else
            break;
    }

    RETVALUE(ROK);
}

/*
*
*       Fun:   SCpyMsgMsg
*
*       Desc:  This function is used to copy a message into
*              a new region and or pool of memory.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*              ROUTRES  - failed, out of resources (optional)
*
*       Notes: None
*
*       File:  ss_msg.c
*
*/

#ifdef ANSI
PUBLIC S16 SCpyMsgMsg
(
Buffer *srcBuf,
Region dstRegion,
Pool dstPool,
Buffer **dstBuf
)
#else
PUBLIC S16 SCpyMsgMsg(srcBuf, dstRegion, dstPool, dstBuf)
Buffer *srcBuf;
Region dstRegion;
Pool dstPool;
Buffer **dstBuf;
#endif
{
    SsMsgInfo *minfo1;
    SsMsgInfo *minfo2;
    Buffer *tmp;
    Buffer *dBuf;
    Buffer *curblk;
    Buffer *newblk;
    Buffer *prevblk;
    Data *cptr;
    MsgLen numBytes;

    TRC1(SCpyMsgMsg)

#if (ERRCLASS & ERRCLS_INT_PAR)
    if (!srcBuf)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS278, ERRZERO, "SCpyMsgMsg : Null Buffer");
        RETVALUE(RFAILED);
    }
    if (srcBuf->b_datap->db_type != SS_M_PROTO)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS279, ERRZERO, "SCpyMsgMsg : Incorrect\
                                                   buffer type");
        RETVALUE(RFAILED);
    }
#endif

    /* allocate a message buffer */
    if (SGetMsg(dstRegion, dstPool, dstBuf) != ROK)
    {
#if (ERRCLASS & ERRCLS_DEBUG)
        SSLOGERROR(ERRCLS_DEBUG, ESS280, ERRZERO, "SCpyMsgMsg : SGetMsg failed");
#endif
        RETVALUE(RFAILED);
    }
    /* get the SsMsgInfo from srcBuf */
    minfo1 = (SsMsgInfo*) srcBuf->b_rptr;

    /* get the SsMsgInfo from srcBuf */
    minfo2 = (SsMsgInfo*) (*dstBuf)->b_rptr;

    /* get the first SS_M_DATA blk of srcBuf */
    tmp = srcBuf->b_cont;

    /* if srcBuf and dstBuf belong to the same region, increment the reference
     * count
     */
    if (dstRegion == minfo1->region)
    {
        newblk = NULLP;
        curblk = NULLP;
        prevblk = NULLP;

        while (tmp)
        {
            if ((curblk = ssDupB(tmp)) == NULLP)
            {
                while (newblk)
                {
                    curblk = newblk->b_cont;
                    (Void) SPutDBuf(minfo1->region, minfo1->pool, newblk);
                    newblk = curblk;
                }
                (Void) SPutMsg(*dstBuf);
                RETVALUE(ROUTRES);
            }

            if (!prevblk)
                newblk = curblk;
            else
                prevblk->b_cont = curblk;
            prevblk = curblk;

            tmp = tmp->b_cont;
        }
        if (curblk)
            curblk->b_cont = NULLP;

        minfo2->len = minfo1->len;
        minfo2->endptr = curblk;
        (*dstBuf)->b_cont = newblk;

        RETVALUE(ROK);
    }

    /* allocate a data buffer */
    if (ssGetDBufOfSize(dstRegion, minfo1->len, &dBuf) != ROK)
    {
        SSLOGERROR(ERRCLS_DEBUG, ESS281, ERRZERO, "SCpyMsgMsg : ssGetDBufOfSize\
                 failed");
        RETVALUE(ROUTRES);
    }
    dBuf->b_datap->db_type = SS_M_DATA;

    while (tmp)
    {
        numBytes = tmp->b_wptr - tmp->b_rptr;
        cptr = tmp->b_rptr;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -