ache.c

来自「Linux 下的socket客户端服务端代码」· C语言 代码 · 共 1,106 行 · 第 1/2 页

C
1,106
字号
            }                        for ( i=0 ; i< length ; i++)            {                achSta->ACHBuffer[achSta->windex]=buffer[i]&(achSta->perbyte);                achSta->windex++;                achSta->windex%=(achSta->MaxBufLen);            }                        if(achSta->writetype EQ ACHERW_TRANS)            {                achSta->ACHBuffer[achSta->windex]=(achSta->tail);                achSta->windex++;                achSta->windex%=(achSta->MaxBufLen);            }            else            {                achSta->writetype=ACHERW_NOTRANS;            }                        achSta->ACHBuffer[achSta->windex]=0;        };    }; /*switch(achSta->achemode)*/    achSta->ACHCOUNT+=length;    if(achSta->writetype EQ ACHERW_TRANS)        achSta->ACHCOUNT+=2;        return length ;}ACHE_MODE  SetAcheMode( ACHSTATUS *achSta, ACHE_MODE amode, int rtype, int wtype ){    ACHE_MODE ret=achSta->achemode;    achSta->achemode=amode;        if(rtype EQ ACHERW_NOTRANS)        achSta->readtype=ACHERW_NOTRANS;    else         achSta->readtype=ACHERW_TRANS;        if(wtype EQ ACHERW_NOTRANS)        achSta->writetype=ACHERW_NOTRANS;    else         achSta->writetype=ACHERW_TRANS;        return ret;}/* --------------------------------------------------------------------------------------  */int ResetPktACHSTATUS(PKTACHSTATUS *pktsta){    if(pktsta EQ NULL)        return 0;    pktsta->PKTCOUNT=0;    pktsta->ACHCOUNT=0;    pktsta->wid=0;    pktsta->rindex=pktsta->pkthead;    pktsta->windex=pktsta->pkthead;    return 1 ;}int CreatePktACHSTATUS(        PKTACHSTATUS *pktsta,        unsigned char *dest,        unsigned long MaxPktCount,        unsigned long MAXACHCOUNT        ){    unsigned long freei;    PACKETACHE *oldindex;    if(pktsta EQ NULL        ||dest EQ NULL        ||MaxPktCount<1)        return 0;    pktsta->ACHBuffer=dest;    pktsta->ACHCOUNT=0;    pktsta->MAXACHCOUNT=MAXACHCOUNT;    pktsta->wid=0;    pktsta->MaxPktCount=MaxPktCount;    pktsta->PKTCOUNT=0;        pktsta->pkthead=(PACKETACHE*)malloc(sizeof(PACKETACHE));    if(    pktsta->pkthead EQ NULL)    {        return 0;    }    pktsta->pkthead->prev=pktsta->pkthead;    pktsta->pkthead->next=pktsta->pkthead;        pktsta->rindex=pktsta->pkthead;    pktsta->windex=pktsta->pkthead;        for(freei=0;freei<MaxPktCount-1;freei++)    {        pktsta->windex->paklen=0;        pktsta->windex->id=0;        pktsta->windex->next=(PACKETACHE*)malloc(sizeof(PACKETACHE));        if(    pktsta->windex->next EQ NULL)        {            return 0;        }        oldindex=pktsta->windex;        pktsta->windex=pktsta->windex->next;        pktsta->windex->prev=oldindex;        pktsta->windex->next=pktsta->pkthead;    }        pktsta->windex->paklen=0;    pktsta->windex->id=0;    pktsta->pkthead->prev=pktsta->windex;        ResetPktACHSTATUS(pktsta);    return 1;}int  ReaderPktAche( PKTACHSTATUS *pktsta, unsigned char * buffer){    unsigned long ret=0,i,id;    if(pktsta EQ NULL||pktsta->ACHBuffer EQ NULL||pktsta->MaxPktCount<1)        return 0;    if(pktsta->PKTCOUNT<1||pktsta->rindex EQ 0 ||pktsta->ACHCOUNT<2)        return 0;    if(pktsta->rindex->paklen>0)    {        id=pktsta->rindex->id;        for(i=0;i<pktsta->rindex->paklen;i++)        {            buffer[i]=pktsta->ACHBuffer[id];            pktsta->ACHBuffer[id]=0;            id++;            id%=(pktsta->MAXACHCOUNT);        }        pktsta->PKTCOUNT--;        if(pktsta->ACHCOUNT>=pktsta->rindex->paklen)            pktsta->ACHCOUNT-=pktsta->rindex->paklen;                else pktsta->ACHCOUNT=0;        ret=pktsta->rindex->paklen;        pktsta->rindex=pktsta->rindex->next;    }        return ret;}int  WriterPktAche( PKTACHSTATUS *pktsta, unsigned char * buffer, int length){    int i;    if(pktsta EQ NULL||pktsta->ACHBuffer EQ NULL||pktsta->MaxPktCount<1)        return 0;        if(pktsta->PKTCOUNT>=pktsta->MaxPktCount||pktsta->ACHCOUNT+length>pktsta->MAXACHCOUNT)        return 0;        switch(pktsta->achemode)    {    case ACHE_AS_STACK:        {            pktsta->rindex=pktsta->rindex->prev;                    if(pktsta->rindex EQ 0)                return 0;            pktsta->rindex->paklen=length;            for ( i=length-1 ; i<=0 ; i++)            {                pktsta->wid--;                pktsta->wid%=(pktsta->MAXACHCOUNT);                pktsta->ACHBuffer[pktsta->wid]=buffer[i];            }            pktsta->rindex->id=pktsta->wid;        }break;    default :        {            pktsta->achemode=ACHE_AS_QUEUE;                        if(pktsta->windex EQ 0)                return 0;            pktsta->windex->id=pktsta->wid;            pktsta->windex->paklen=length;            for ( i=0 ; i< length ; i++)            {                pktsta->ACHBuffer[pktsta->wid]=buffer[i];                pktsta->wid++;                pktsta->wid%=(pktsta->MAXACHCOUNT);            }            pktsta->ACHBuffer[pktsta->wid]=0;            pktsta->windex=pktsta->windex->next;        };    }; /* switch(pktsta->achemode)*/        pktsta->ACHCOUNT+=length;    pktsta->PKTCOUNT++;    return length;}ACHE_MODE SetPktAcheMode( PKTACHSTATUS *achSta, ACHE_MODE amode){    ACHE_MODE ret=achSta->achemode;    achSta->achemode=amode;    return ret ;}/*----------------------------------------------------------------------*/int  ResetCircleAche(CIRCLEACHESTATUS *lpsta){    if(lpsta EQ NULL)        return 0;        if(lpsta->ifinit==ACHE_REGSIGNE)    {        unsigned freei;        CIRCLEACHE *circleache=lpsta->head;        CIRCLEACHE *next=NULL;        for(freei=0;freei<lpsta->pocount;freei++)        {            if(circleache!=NULL)            {                next=circleache->next;                free(circleache);                circleache=next;                if(circleache==lpsta->head)     break;            }            else                break;        }    }        lpsta->head=NULL;    lpsta->rid=NULL;    lpsta->wid=NULL;    lpsta->pocount=0;    lpsta->ifinit=0;    return 1;}void *GetCurCircleAche( CIRCLEACHESTATUS *lpsta){    void *ret=NULL;        if(lpsta EQ NULL)        return NULL;    if(lpsta->rid EQ 0)        return NULL;        if(lpsta->ifinit!=ACHE_REGSIGNE)    {        ResetCircleAche(lpsta);        return NULL;    }        ret = lpsta->rid->lppoint;    lpsta->rid=lpsta->rid->next;        return ret; }void *GetCircleAche( CIRCLEACHESTATUS *lpsta, unsigned long nWitch){    void *ret=NULL;    int freei,found=0;    CIRCLEACHE *circleache=NULL;    if(lpsta EQ NULL||lpsta->pocount<=0)        return NULL;        if(lpsta->ifinit!=ACHE_REGSIGNE)            {        ResetCircleAche(lpsta);        return NULL;    }        circleache=lpsta->head;    for(freei=0;freei<(int)(lpsta->pocount);freei++)    {        if(circleache&&circleache->id EQ nWitch)        {            ret=circleache->lppoint;            found=1;            break;        }        circleache=circleache->next;    }    return ret ;}unsigned long  GetCurCircleAcheIndex( CIRCLEACHESTATUS *lpsta){    unsigned long ret=0;    if(lpsta EQ NULL)        return 0;    if(lpsta->rid EQ NULL)        return 0;        if(lpsta->ifinit!=ACHE_REGSIGNE)    {                ResetCircleAche(lpsta);        return 0;    }        ret = lpsta->rid->id;    return ret; }unsigned long GetCircleAcheCount( CIRCLEACHESTATUS *lpsta){    unsigned long ret=0;    if(lpsta EQ NULL)        return 0;        if(lpsta->ifinit!=ACHE_REGSIGNE)    {        ResetCircleAche(lpsta);        return 0;    }    ret = lpsta->pocount;    return ret; }unsigned long AddCircleAche( CIRCLEACHESTATUS *lpsta, void *lpoint){    unsigned long ret=0;    if(lpsta EQ NULL)        return 0;        if(lpsta->ifinit!=ACHE_REGSIGNE)    {        ResetCircleAche(lpsta);    }        if(lpsta->pocount EQ 0)    {        CIRCLEACHE *circleache=(CIRCLEACHE*)malloc(sizeof(CIRCLEACHE));        if(circleache EQ NULL)            return 0;                        circleache->id=1;        circleache->lppoint=lpoint;        circleache->next=circleache;        circleache->prev=circleache;        lpsta->head=circleache;        lpsta->pocount=1;        lpsta->rid=lpsta->head;        lpsta->wid=lpsta->head;        ret=lpsta->pocount;    }    else    {        unsigned long freei;        CIRCLEACHE *circleache=lpsta->head;        for(freei=0;freei<lpsta->pocount;freei++)        {            if(circleache!=NULL&&circleache->lppoint EQ lpoint)                return circleache->id;            circleache=circleache->next;        }                circleache=(CIRCLEACHE*)malloc(sizeof(CIRCLEACHE));        if(circleache EQ NULL)            return 0;        lpsta->pocount++;        circleache->id=lpsta->pocount;        circleache->lppoint=lpoint;                circleache->next=lpsta->wid->next;        lpsta->wid->next->prev=circleache;                circleache->prev=lpsta->wid;        lpsta->wid->next=circleache;                lpsta->wid=circleache;        ret= circleache->id;            }        lpsta->ifinit=ACHE_REGSIGNE;    return ret ;}unsigned long  DelCircleAche( CIRCLEACHESTATUS *lpsta, void *lpoint){    unsigned long ret=0,freei=0;    int found=0;    CIRCLEACHE *circleache=NULL;    if(lpsta EQ NULL||lpsta->pocount<=0)        return 0;        if(lpsta->ifinit!=ACHE_REGSIGNE)    {        ResetCircleAche(lpsta);        return 0;    }        if(lpsta->pocount EQ 1&&lpsta->head->lppoint EQ lpoint)    {        ResetCircleAche(lpsta);        return 1;    }        circleache=lpsta->head;    for(freei=0;freei<lpsta->pocount;freei++)    {        if(circleache!=NULL&&circleache->lppoint EQ lpoint)        {            ret= circleache->id;            if(circleache EQ lpsta->head)            {                lpsta->head=lpsta->head->next;            }                        if(circleache EQ lpsta->rid)            {                lpsta->rid=lpsta->rid->next;            }                        circleache->prev->next=circleache->next;            circleache->next->prev=circleache->prev;                        free(circleache);                        lpsta->pocount--;            found=1;            break;        }        circleache=circleache->next;    }        if(found)    {        circleache=lpsta->head;        for(freei=0;freei<lpsta->pocount;freei++)        {            if(circleache!=NULL&&circleache->id>ret)                circleache->id--;            circleache=circleache->next;        }    }        return ret ;}/*----------------------------------------------------*/#define CIR_WINPRO_SIGN    ("CWP:")static CIRCLEACHESTATUS WinProc_CircleAche ;static int WinPro_Circle_Occupy=0;void *MallocWinProCircleAche(unsigned long wnd, unsigned long buflen){    int curcount=0;    int found=0,freei;    void *PDAThis=NULL;    unsigned char icx[9];    unsigned char *lpbuf=NULL;    while(WinPro_Circle_Occupy);    WinPro_Circle_Occupy=1;        memcpy((void*)icx,(void*)(CIR_WINPRO_SIGN),4);    SetBuf44((unsigned char*)(icx+4),wnd);        curcount=GetCircleAcheCount(&WinProc_CircleAche);    for(freei=0;freei<curcount;freei++)    {        lpbuf=(unsigned char*)GetCurCircleAche(&WinProc_CircleAche);        if(lpbuf!=NULL)        {            if(memcmp((void*)(lpbuf),(void*)icx,8) EQ 0)            {                found=1;                break;            }        }    }        if(!found)    {        lpbuf = (unsigned char*)malloc(buflen+16);        if(lpbuf EQ NULL)            return NULL;        memcpy((void*)(lpbuf),(void*)(icx),8);        AddCircleAche(&WinProc_CircleAche, (void *)lpbuf);    }        PDAThis = (void*)(lpbuf+8);        WinPro_Circle_Occupy=0;    return (void*)(PDAThis);}int FreeWinProCircleAche( unsigned long wnd){    int curcount=0;    int found=0,freei;    unsigned char icx[9];    unsigned char *lpbuf=NULL;    while(WinPro_Circle_Occupy);    WinPro_Circle_Occupy=1;        memcpy((void*)icx,(void*)(CIR_WINPRO_SIGN),4);    SetBuf44((unsigned char*)(icx+4),wnd);        curcount=GetCircleAcheCount(&WinProc_CircleAche);    for(freei=0;freei<curcount;freei++)    {        lpbuf=(unsigned char*)GetCurCircleAche(&WinProc_CircleAche);        if(lpbuf!=NULL)        {            if(memcmp((void*)(lpbuf),(void*)icx,8) EQ 0)            {                found=1;                DelCircleAche(&WinProc_CircleAche,(void*)lpbuf);                free(lpbuf);                break;            }        }    }        WinPro_Circle_Occupy=0;    return found;}void *FindWinProCircleAche(unsigned long wnd){    int curcount=0;    int found=0,freei;    void *PDAThis=NULL;    unsigned char icx[9];    unsigned char *lpbuf=NULL;        while(WinPro_Circle_Occupy);    WinPro_Circle_Occupy=1;        memcpy((void*)icx,(void*)(CIR_WINPRO_SIGN),4);    SetBuf44((unsigned char*)(icx+4),wnd);        curcount=GetCircleAcheCount(&WinProc_CircleAche);    for(freei=0;freei<curcount;freei++)    {        lpbuf=(unsigned char *)GetCurCircleAche(&WinProc_CircleAche);        if(lpbuf!=NULL)        {            if(memcmp((void*)(lpbuf),(void*)icx,8) EQ 0)            {                found=1;                PDAThis=(void*)(lpbuf+8);                break;            }        }    }        WinPro_Circle_Occupy=0;    return PDAThis;}

⌨️ 快捷键说明

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