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

📄 cmpp.c

📁 cmpp 发送接收短信息程序!是在linux下c 实现的。
💻 C
📖 第 1 页 / 共 3 页
字号:

/*******************************************************
    NAME:      cmpp.c
    PURPOSE:   China Mobile Peer to Peer Protocol 2.0
               Permanent connection implementation to 
               submit data to Internet Short Message Gateway
               or deliver data from ISMG to upper layer.
               routine implementation.
    VERSION:   0.0.1
    AUTHOR:    Ke Heng Zhong
    DATE:      2002/06/17 12:52
    MODIFIED:  2000/06/17 21:12
 ********************************************************/

#include "kevopsall.ext"
#include "cmpp.h"
#include "cmppif.h"

VTASK * gEntity = NULL;


CRITICAL_SECTION seqidCS = PTHREAD_MUTEX_INITIALIZER;

uint32 cmpp_alloc_sequence_id ()
{
    static uint32 gseqid = 0, tempid;

    EnterCriticalSection(&seqidCS);
    tempid = gseqid++;
    LeaveCriticalSection(&seqidCS);

    return tempid;
}



/* CMPP permanent TCP connection instance initialized */
void cmppcon_init (VTASK * vtask)
{
    CmppCon * ccon = NULL;
    SPCMPP  * spc = NULL;
    int i;

    spc = (SPCMPP *)vtask->cvopsState->ref;

    vtask->state = cmpp_null;

    ccon = (CmppCon *)vtask->var;
    ccon->local_ip.s_addr = 0;
    ccon->local_port      = 0;
    ccon->fd              = -1;
    ccon->devid           = -1;

    ccon->concreated      = 0;
    ccon->conpdu          = NULL;
    ccon->retrynum        = 0;

    ccon->rcvbuf          = NULL;

    ccon->is_mo           = 0;

    ccon->sliding_len = spc->slidingwin;

    /*ccon->sliding_len = getConfInt("Flow Control", "Sliding Window");*/
    if (ccon->sliding_len > MAX_SLIDING_WIN || ccon->sliding_len <= 0)
        ccon->sliding_len = MAX_SLIDING_WIN;

    for (i=0; i<ccon->sliding_len; i++) {
        ccon->subpdu[i].status      = SUBMIT_AVAIL;
        ccon->subpdu[i].retrynum    = 0;
        ccon->subpdu[i].submit      = NULL;
        emptyFrame(ccon->subpdu[i].submitframe);
    }
    ccon->availwin = ccon->sliding_len;

    /* deliver message handling */
    ccon->deliverresp = NULL;
    if (ccon->drespdu)
        emptyFrame(ccon->drespdu);

    /* active test message */
    if (ccon->actpdu)
        emptyFrame(ccon->actpdu);
    if (ccon->actrespdu)
        emptyFrame(ccon->actrespdu);
    ccon->acttest = NULL;
    ccon->actretry = 0;

    /*ccon->version = (((uint8)getConfInt ("General", "Major_Version")) << 4) & 0xF0;;
    ccon->version |= ((uint8)getConfInt("General","Minor_Version") & 0x0F);*/

    strncpy(ccon->sp_id, spc->spid, sizeof(ccon->sp_id));
    strncpy(ccon->sharedsecret, spc->secret, sizeof(ccon->sharedsecret));

    /*strncpy(ccon->sp_id, getConfString("General","SP_Id"), 
              sizeof(ccon->sp_id));
    strncpy(ccon->sharedsecret, getConfString("General", "SharedSecret"),
                sizeof(ccon->sharedsecret));*/
}



/* CMPP permanent TCP connection instance destructed */
void cmppcon_destruct (VTASK * vtask)
{
    CmppCon * ccon = NULL;
    SubmitPending  * item = NULL;
    CmppEntity * cent = NULL;
    int i, unsuccnum = 0;

    cent = (CmppEntity *)((VTASK *)getEntity(vtask))->var;

    ccon = (CmppCon *)vtask->var;
    if (ccon->devid != -1) {
        removeDevice(vtask->cvopsState, ccon->devid);
        ccon->devid = -1;
    }
    if (ccon->fd != -1) {
        close(ccon->fd);
        ccon->fd = -1;
    }

    ccon->concreated      = 0;
    deleteFrame(&ccon->conpdu);
    ccon->retrynum        = 0;

    /* used for data_recv buffer */
    deleteFrame(&ccon->rcvbuf);

    /* deliver message */
    cmpdu_free(ccon->deliverresp);
    deleteFrame(&ccon->drespdu);

    /* active test message */
    deleteFrame(&ccon->actpdu);
    deleteFrame(&ccon->actrespdu);
    cmpdu_free(ccon->acttest);
    ccon->actretry = 0;

    for (i=0, unsuccnum=0; i<ccon->sliding_len; i++) {
        item = &ccon->subpdu[i];

        if (item->status == SUBMIT_AVAIL)
            continue;

        item->status = SUBMIT_AVAIL;

        store_unsucc_submit (vtask, item->submit);
        item->submit = NULL;
        emptyFrame(item->submitframe);
        deleteFrame(&item->submitframe);
        stopDynTimer(item->submtimer);

        unsuccnum += 1;
    }

    cent->consumedwin -= unsuccnum;

    if (sk_delete_ptr (cent->init_list, vtask)) {
        cent->total_avail -= ccon->sliding_len;
    } else
        sk_delete_ptr (cent->mo_list, vtask);

    /* only when the CMPP connection exists, the distribute 
     * procedure hook is started. this will reduce the risk
     * of frequent calling hook and finite loop to calm down
     * system overhead. */
    if (unsuccnum > 0 && sk_num(cent->init_list) > 0)
        startHook(cent->hDistribute);

    stopTimer(vtask, t_acttest);
}


/* CMPP permanent TCP connection entity instance initialized */
void cmppent_init (VTASK * vtask)
{
    CmppEntity * cent = NULL;
    SPCMPP * spc = NULL;

    gEntity = vtask;

    cent = (CmppEntity *)vtask->var;
    spc = (SPCMPP *)vtask->cvopsState->ref;

    strncpy(cent->remote_host, spc->ismgaddr, sizeof(cent->remote_host));
    cent->remote_port = (uint16)spc->ismgport;

    /*strncpy(cent->remote_host, getConfString("ISMG", "host"), 
             sizeof(cent->remote_host));
    cent->remote_port = (uint16)getConfInt("ISMG", "port");*/

    cent->mt_num = spc->mtnum;
    cent->mo_num = spc->monum;

    cent->mo_num = getConfInt("CMPP Connection", "MOConnection Num");*/

    cent->cmpp_con_num = cent->mt_num + cent->mo_num;

    cent->init_list = sk_new (NULL);
    cent->mo_list = sk_new (NULL);

    cent->sliding_len = spc->slidingwin;

    /*cent->sliding_len = getConfInt("Flow Control", "Sliding Window");*/
    if (cent->sliding_len > MAX_SLIDING_WIN || cent->sliding_len <= 0)
        cent->sliding_len = MAX_SLIDING_WIN;

    /*cent->max_cmpp_con = getConfInt("Flow Control", "Max CMPP Connection");
    if (cent->max_cmpp_con > MAX_CMPP_CON || cent->max_cmpp_con <= 0)
        cent->max_cmpp_con = MAX_CMPP_CON;*//*commented on 2002-07-20*/

    /* used for data sharing with upper layer */
    /*sem_init (&cent->sem_submit, 0, 0);    */
    /*sem_init (&cent->sem_submitresp, 0, 0);*/
    /*sem_init (&cent->sem_deliver, 0, 0);   */


    cent->total_avail = 0;
    cent->consumedwin = 0;

    cent->hAvailEvent   = CreateEvent();
    cent->hRespEvent    = CreateEvent();
    cent->hDeliverEvent = CreateEvent();

    InitializeCriticalSection (&cent->submitCS);
    InitializeCriticalSection (&cent->respCS);
    InitializeCriticalSection (&cent->deliverCS);
    InitializeCriticalSection (&cent->recycleCS);
    InitializeCriticalSection (&cent->recyclerespCS);
    InitializeCriticalSection (&cent->recycledeliverCS);

    cent->submits = lt_new (NULL);
    cent->unsucc_submits = lt_new (NULL);
    cent->submitresps = lt_new (NULL);
    cent->recycle_submits = lt_new (NULL);
    cent->recycle_submitresps = lt_new (NULL);
    cent->recycle_delivers = lt_new (NULL);


    cent->delivers = lt_new (NULL);

    cent->hBuildConn = createHook (vtask, cmpp_build_conn);
    cent->hDistribute = createHook (vtask, distribute_submit);

    startTimer (vtask, t_build, TBUILD);

    return;
}

/* CMPP permanent TCP connection entity instance destructed */
void cmppent_destruct (VTASK * vtask)
{
    CmppEntity * cent = NULL;
    int i, num;
    CmppPdu * pdu = NULL;

    cent = (CmppEntity *)vtask->var;

    sk_free (cent->init_list);
    sk_free (cent->mo_list);

    cent->total_avail = 0;
    cent->consumedwin = 0;

    DeleteCriticalSection (&cent->submitCS);
    DeleteCriticalSection (&cent->respCS);
    DeleteCriticalSection (&cent->deliverCS);
    DeleteCriticalSection (&cent->recycleCS);
    DeleteCriticalSection (&cent->recyclerespCS);
    DeleteCriticalSection (&cent->recycledeliverCS);

    /*sem_destroy (&cent->sem_submit);*/
    /*sem_destroy (&cent->sem_submitresp);*/
    /*sem_destroy (&cent->sem_deliver);*/

    num = lt_num(cent->submits);
    for (i=0; i<num; i++) {
        pdu = lt_rm_head(cent->submits);
        cmpdu_free(pdu);
    }
    lt_free (cent->submits);

    num = lt_num(cent->unsucc_submits);
    for (i=0; i<num; i++) {
        pdu = lt_rm_head(cent->unsucc_submits);
        cmpdu_free(pdu);
    }
    lt_free (cent->unsucc_submits);

    num = lt_num(cent->recycle_submits);
    for (i=0; i<num; i++) {
        pdu = lt_rm_head(cent->recycle_submits);
        cmpdu_free(pdu);
    }
    lt_free (cent->recycle_submits);

    num = lt_num(cent->submitresps);
    for (i=0; i<num; i++) {
        pdu = lt_rm_head(cent->submitresps);
        cmpdu_free(pdu);
    }
    lt_free (cent->submitresps);


    num = lt_num(cent->delivers);
    for (i=0; i<num; i++) {
        pdu = lt_rm_head(cent->delivers);
        cmpdu_free(pdu);
    }
    lt_free (cent->delivers);

    num = lt_num(cent->recycle_submitresps);
    for (i=0; i<num; i++) {
        pdu = lt_rm_head(cent->recycle_submitresps);
        cmpdu_free(pdu);
    }
    lt_free (cent->recycle_submitresps);

    num = lt_num(cent->recycle_delivers);
    for (i=0; i<num; i++) {
        pdu = lt_rm_head(cent->recycle_delivers);
        cmpdu_free(pdu);
    }
    lt_free (cent->recycle_delivers);

    destroyHook (cent->hBuildConn);

    SetEvent(cent->hAvailEvent, -10);
    SetEvent(cent->hRespEvent, -10);

⌨️ 快捷键说明

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