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

📄 ss_msg.c

📁 中国石油二期加油站IC系统后台通讯软件
💻 C
📖 第 1 页 / 共 5 页
字号:


/********************************************************************20**
 
     Name:     System Services -- Message manipulation functions
 
     Type:     C source file
 
     Desc:     Source Code for message related functions.
 
     File:     ss_msg.c
 
     Sid:      ss_msg.c 1.3  -  10/14/98 14:12:52
 
     Prg:      sn
 
*********************************************************************21*/



/* header include files (.h) */

#include "envopt.h"        /* environment options */
#include "envdep.h"        /* environment dependent */
#include "envind.h"        /* environment independent */

#include "gen.h"           /* general layer */
#include "ssi.h"           /* system services */

#include "ss_err.h"        /* errors */
#include "ss_dep.h"        /* implementation-specific */
#include "ss_queue.h"      /* queues */
#include "ss_strm.h"       /* STREAMS */
#include "ss_msg.h"        /* messaging */
#include "ss_mem.h"        /* memory management interface */
#include "ss_gen.h"        /* general */



/* header/extern include files (.x) */

#include "gen.x"           /* general layer */
#include "ssi.x"           /* system services */

#include "ss_dep.x"        /* implementation-specific */
#include "ss_queue.x"      /* queues */
#include "ss_task.x"       /* tasking */
#include "ss_timer.x"      /* timers */
#include "ss_strm.x"       /* STREAMS */
#include "ss_msg.x"        /* messaging */
#include "ss_mem.x"        /* memory management interface */
#include "ss_drvr.x"       /* driver tasks */
#include "ss_gen.x"        /* general */



/* forward declarations */
PRIVATE S16 ssGetDBufOfSize ARGS((Region region, Size size, Buffer **dBuf));



/* local defines */
#define MBSIZE   (sizeof(SsMblk))
#define MDBSIZE  (sizeof(SsDblk) + sizeof(SsMblk))
#define MSGSIZE  (MDBSIZE + sizeof(SsMsgInfo))

#define INITMBLK(mp, dp, dat)   {\
   mp->b_next = NULLP;\
   mp->b_prev = NULLP;\
   mp->b_cont = NULLP;\
   mp->b_rptr = dat;\
   mp->b_wptr = dat;\
   mp->b_datap = dp;\
}

#define INITDBLK(dp, dat, size, frtn)   {\
   dp->db_frtnp = frtn;\
   dp->db_base = dat;\
   dp->db_lim = (dat == NULLP ? dat : (dat + size));\
   ssLockTsk(); \
   dp->db_ref = 1;\
   ssUnlockTsk(); \
   dp->db_type = SS_M_DATA;\
}

#define INITB(mp, dp, dat, size, frtn)   {\
   INITMBLK(mp, dp, dat)\
   INITDBLK(dp, dat, size, frtn)\
}


/*
*
*       Fun:   ssGetDBufOfSize
*
*       Desc:  This function gets a message buffer from specified region and
*              size
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*              ROUTRES  - failed, out of resources (optional)
*
*       Notes: message is created. message is returned via message buffer
*              pointer. Buffer type is SS_M_DATA.
*              return is ok.
*
*       File:  ss_msg.c
*
*/
#ifdef ANSI
PRIVATE S16 ssGetDBufOfSize
(
Region region,
Size size,
Buffer **dBuf
)
#else
PRIVATE S16 ssGetDBufOfSize(region, size, dBuf)
Region region;
Size size;
Buffer **dBuf;
#endif
{
    Size mdsize;
    Data *data;
    SsDblk *dptr;

    TRC1(ssGetDBufOfSize)

#if (ERRCLASS & ERRCLS_INT_PAR)
    /* check buffer pointer */
    if (!dBuf)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS216, ERRZERO, "ssGetDBufOfSize : Null\
                 Buffer");
        RETVALUE(RFAILED);
    }
    if (region >= SS_MAX_REGS)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS217, ERRZERO, "ssGetDBufOfSize : Invalid\
                region id");
        RETVALUE(RFAILED);
    }

    if (size <= 0)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS218, ERRZERO, "ssGetDBufOfSize : Invalid\
                 size");
        RETVALUE(RFAILED);
    }
#endif

    mdsize = MDBSIZE;

    if (osCp.regionTbl[region].flags & SS_OUTBOARD_FLAG)
    {
        /* allocate memory for data buffer */
        if (SAlloc(region, &size, 0, &data) != ROK)
            RETVALUE(ROUTRES);
        if (SAlloc(SS_DFLT_REGION, &mdsize, 0, (Data **) dBuf) != ROK)
        {
            SFree(region, (Data *) data, size);
            RETVALUE(ROUTRES);
        }
    } else
    {
        mdsize += size;
        if (SAlloc(region, &mdsize, 0, (Data **) dBuf) != ROK)
        {
            RETVALUE(ROUTRES);
        }
        data = (Data *) (*dBuf) + MDBSIZE;
        size = mdsize - MDBSIZE;
    }

    dptr = (SsDblk*) (((Data *) (*dBuf)) + MBSIZE);

    INITB((*dBuf), dptr, data, size, NULLP)

    RETVALUE(ROK);
} /* ssGetDBufOfSize */


/*
*
*       Fun:   SGetMsg
*
*       Desc:  This function gets a message
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*              ROUTRES  - failed, out of resources (optional)
*
*       Notes: message is created. message is set to type SS_M_PROTO.
*              message is returned via message buffer pointer.
*              return is ok.
*
*       File:  ss_msg.c
*
*/

#ifdef ANSI
PUBLIC S16 SGetMsg
(
Region region,              /* region id */
Pool pool,                  /* pool id */
Buffer **mBuf               /* pointer to message buffer */
)
#else
PUBLIC S16 SGetMsg(region, pool, mBuf)
Region region;              /* region id */
Pool pool;                  /* pool id */
Buffer **mBuf;              /* pointer to message buffer */
#endif
{
    SsMsgInfo *minfo;
    Size size;
    SsDblk *dptr;
    Data *data;

    TRC1(SGetMsg)

#if (ERRCLASS & ERRCLS_INT_PAR)
    /* check buffer pointer */
    if (!mBuf)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS219, ERRZERO, "SGetMsg : Null Buffer");
        RETVALUE(RFAILED);
    }
    if (region >= SS_MAX_REGS)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS220, ERRZERO, "SGetMsg : Invalid region\
                                                   id");
        RETVALUE(RFAILED);
    }

    if (pool >= SS_MAX_POOLS_PER_REG)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS221, ERRZERO, "SGetMsg : Invalid pool id");
        RETVALUE(RFAILED);
    }
#endif
    size = MSGSIZE;

    if (SAlloc(SS_DFLT_REGION, &size, 0, (Data**) mBuf) != ROK)
    {
        SSLOGERROR(ERRCLS_DEBUG, ESS222, size, "SGetMsg:Failed in SAlloc");
        RETVALUE(ROUTRES);
    }

    dptr = (SsDblk*) (((Data *) (*mBuf)) + MBSIZE);
    data = (Data*) (((Data *) (*mBuf)) + MDBSIZE);

    /* INITB initialises and sets up the message blk */
    INITB((*mBuf), dptr, data, size, NULLP)

    (*mBuf)->b_datap->db_type = SS_M_PROTO;
    (*mBuf)->b_wptr = (*mBuf)->b_rptr + sizeof(SsMsgInfo);

    /* initialise message info of mBuf */
    minfo = (SsMsgInfo*) (*mBuf)->b_rptr;

    minfo->region = region;
    minfo->pool = pool;
    minfo->len = 0;
    minfo->endptr = NULLP;
    minfo->eventInfo.event = SS_EVNT_DATA;

    RETVALUE(ROK);
}

/*
*
*       Fun:   SPutMsg
*
*       Desc:  This function deallocates a message back.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*
*       Notes: all data attached to message is returned to memory.
*              message is returned to memory. return is ok.
*
*       File:  ss_msg.c
*
*/

#ifdef ANSI
PUBLIC S16 SPutMsg
(
Buffer *mBuf
)
#else
PUBLIC S16 SPutMsg(mBuf)
Buffer *mBuf;
#endif
{
    Buffer *tmp;
    SsMsgInfo *minfo;

    TRC1(SPutMsg)

#if (ERRCLASS & ERRCLS_INT_PAR)
    if (mBuf == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS223, ERRZERO, "SPutMsg: Null Buffer");
        RETVALUE(RFAILED);
    }
    if (mBuf->b_datap->db_type != SS_M_PROTO)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS224, ERRZERO, "SPutMsg: Incorrect buffer\
                                                   type");
        RETVALUE(RFAILED);
    }
#endif

    /* get the message info */
    minfo = (SsMsgInfo *) mBuf->b_rptr;

    while ((tmp = mBuf->b_cont))
    {
        /* set b_cont of mBuf to point to the b_cont of tmp */
        mBuf->b_cont = tmp->b_cont;
        (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
    }
    SFree(SS_DFLT_REGION, (Data *) mBuf, MSGSIZE);

    RETVALUE(ROK);
}

/*
*
*       Fun:   SInitMsg
*
*       Desc:  This function deallocates a message back and then
*              reinitializes the message.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*
*       Notes: all data attached to message is returned to memory.
*              message is set to empty. message is not returned to
*              memory. return is ok.
*
*       File:  ss_msg.c
*
*/

#ifdef ANSI
PUBLIC S16 SInitMsg
(
Buffer *mBuf
)
#else
PUBLIC S16 SInitMsg(mBuf)
Buffer *mBuf;
#endif
{
    SsMsgInfo *minfo;
    Buffer *tmp;

    TRC1(SInitMsg)

#if (ERRCLASS & ERRCLS_INT_PAR)
    /* check message buffer */
    if (mBuf == NULLP)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS225, ERRZERO, "SInitMsg: Null Buffer");
        RETVALUE(RFAILED);
    }
    if (mBuf->b_datap->db_type != SS_M_PROTO)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS226, ERRZERO, "SInitMsg: Incorrect buffer\
                                                   type");
        RETVALUE(RFAILED);
    }
#endif

    /* get the message info */
    minfo = (SsMsgInfo*) mBuf->b_rptr;

    /* free all SS_M_DATA blks */
    while ((tmp = mBuf->b_cont))
    {
        mBuf->b_cont = tmp->b_cont;
        (Void) SPutDBuf(minfo->region, minfo->pool, tmp);
    }
    /* initialise the length and endptr variables of mBuf */

    minfo->len = 0;
    minfo->endptr = NULLP;
    minfo->next = NULLP;

    RETVALUE(ROK);
}

/*
*
*       Fun:   SAddPreMsg
*
*       Desc:  This function copies one byte of data to the
*              beginning of a message.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*              ROUTRES  - failed, out of resources (optional)
*
*       Notes: if message is empty: data is placed in the message. message
*              length is incremented. return is ok.
*
*              if message is not empty: data is placed in front of all
*              other data in message. message length is incremented.
*              return is ok.
*
*       File:  ss_msg.c
*
*/


#ifdef ANSI
PUBLIC S16 SAddPreMsg
(
Data data,
Buffer *mBuf
)
#else
PUBLIC S16 SAddPreMsg (data, mBuf)
Data data;
Buffer *mBuf;
#endif
{
    SsMsgInfo *minfo;
    Buffer *tmp;
    Buffer *newb;

    TRC1(SAddPreMsg)

#if (ERRCLASS & ERRCLS_INT_PAR)
    /* check message buffer */
    if (!mBuf)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS227, ERRZERO, "SAddPreMsg: Null Buffer");
        RETVALUE(RFAILED);
    }
    if (mBuf->b_datap->db_type != SS_M_PROTO)
    {
        SSLOGERROR(ERRCLS_INT_PAR, ESS228, ERRZERO, "SAddPreMsg: Incorrect\
                                                   buffer type");
        RETVALUE(RFAILED); 
    }
#endif

    minfo = (SsMsgInfo *) mBuf->b_rptr;

    /*
     * allocate a message blk using SGetDBuf(), if there are no data blks in the
     * message, mBuf, or if the reference count of the first data blk is greater
     * than 1, or if there is no space to append databytes in front of the read
     * pointer of the first data blk
    */
    ssLockTsk();
    if (!(tmp = mBuf->b_cont) || (tmp->b_datap->db_ref > 1) ||
        (tmp->b_rptr == tmp->b_datap->db_base))
    {
        ssUnlockTsk();
        if (SGetDBuf(minfo->region, minfo->pool, &newb) != ROK)
        {
            SSLOGERROR(ERRCLS_DEBUG, ESS229, ERRZERO, "SAddPreMsg:Failed in\
                    SGetDBuf");
            RETVALUE(ROUTRES);
        }
        /* set the read and write pointers to end of data buffer */
        /* subsequent prepends have all the buffer to insert data into */
        newb->b_wptr = newb->b_datap->db_lim;
        newb->b_rptr = newb->b_datap->db_lim;

        /* insert newb before tmp */
        newb -> b_cont = tmp;
        mBuf->b_cont = newb;

        /* if endptr of mBuf is NULLP, set it to newb */
        if (tmp == NULLP)
            minfo->endptr = newb;
        tmp = newb;
    } else
        ssUnlockTsk();
    /* insert data, increment length */
    *--tmp->b_rptr = data;
    minfo->len++;

    RETVALUE(ROK);
}

/*
*
*       Fun:   SAddPstMsg
*
*       Desc:  This function copies one byte of data to the
*              end of a message.
*
*       Ret:   ROK      - ok
*              RFAILED  - failed, general (optional)
*              ROUTRES  - failed, out of resources (optional)
*
*       Notes: if message is empty: data is placed in the message. message
*              length is incremented. return is ok.
*
*              if message is not empty: data is placed in back of all
*              other data in message. message length is incremented.
*              return is ok.
*
*       File:  ss_msg.c
*

⌨️ 快捷键说明

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