smid+shmid



#define MAX_SHAREMEM_SIZE        (512*1024)


#define BASE_SHM_KEY             (11001100)


#define BASE_MUX_KEY             (11002200)
#define BASE_FUL_KEY             (11003300)
#define BASE_EMP_KEY             (11004400)


struct FrameData{
    int offset;
    int total;
    char data[0];
};


/* 定义信号量数据结构 */
struct sembuf P, V;


/* 初始化P V操作  */
P.sem_num = 0;
P.sem_op = -1;
P.sem_flg = SEM_UNDO;


V.sem_num = 0;
V.sem_op = 1;
V.sem_flg = SEM_UNDO;


static int shmIn(NetLiveSession *pSession, const void *data, size_t len)
{
    assert(pSession != NULL);
    assert(data != NULL);
    
    struct FrameData *pfhead = NULL;


    semop(pSession->emptyid, &P, 1);         //对 emptyid执行P操作
    semop(pSession->mutexid, &P, 1);         //对 mutexid执行P操作
    pfhead = (struct FrameData *)pSession->ptr;


    assert(pfhead->offset+len <= MAX_SHAREMEM_SIZE);
    memcpy(((char *)pfhead+pfhead->offset), data, len);
    pfhead->offset +=len;


    semop(pSession->mutexid, &V, 1);         //对mutexid执行 V 操作
    semop(pSession->fullid, &V, 1);          //对fullid执行 V 操作    
}




static int shmDel(NetLiveSession *pSession)
{
    shmdt(pSession->ptr);
    shmctl(pSession->shmid, IPC_RMID, 0);


    semctl(pSession->fullid, IPC_RMID, 0);
    semctl(pSession->emptyid, IPC_RMID, 0);
    semctl(pSession->mutexid, IPC_RMID, 0);
}


static int shmOut(NetLiveSession *pSession, void *data, size_t len)
{
    assert(pSession != NULL);
    assert(data != NULL);
    
    struct FrameData *pfhead = NULL;
    int datalen = 0;


    semop(pSession->fullid, &P, 1);         //对 emptyid执行P操作
    semop(pSession->mutexid, &P, 1);         //对 mutexid执行P操作


    pfhead = (struct FrameData *)pSession->ptr;
    datalen = pfhead->offset - sizeof(struct FrameData);
    assert(len >= datalen);
    memcpy(data, (char *)pfhead+sizeof(struct FrameData), datalen);


    semop(pSession->mutexid, &V, 1);         //对mutexid执行 V 操作
    semop(pSession->emptyid, &V, 1);          //对fullid执行 V 操作   
}


static int shmInit(NetLiveSession *pSession, int index)
{
    int ret = APP_OK;
    FrameData *pfhead = NULL;


    assert(pSession != NULL);


    key_t shmbase = BASE_SHM_KEY;
    shmbase = shmbase + (key_t)index;
    pSession->shmid = shmget(shmbase, MAX_SHAREMEM_SIZE, IPC_CREAT | 0666);
    if(pSession->shmid < 0)
    {
        printf("shmget err\n");
        ret = APP_FAIL;
        goto END;
    }
    printf("shmid = %d\n", pSession->shmid);


    key_t fullidbase = BASE_FUL_KEY;
    fullidbase = fullidbase + (key_t)index;
    pSession->fullid = semget(fullidbase, 1, IPC_CREAT | 0666);
    if(pSession->fullid < 0)
    {
        printf("semget err\n");
        ret = APP_FAIL;
        goto END;
    }
    printf("fullid = %d\n", semid);


    key_t emptybase = BASE_EMP_KEY;
    emptybase = emptybase + (key_t)index;
    pSession->emptyid = semget(emptybase, 1, IPC_CREAT | 0666);
    if(pSession->emptyid < 0)
    {
        printf("semget err\n");
        ret = APP_FAIL;
        goto END;
    }
    printf("emptyid = %d\n", pSession->emptyid);


    key_t mutexbase = BASE_MUX_KEY;
    mutexbase = mutexbase + (key_t)index;
    pSession->mutexid = semget(mutexbase, 1, IPC_CREAT | 0666);
    if(pSession->mutexid < 0)
    {
        printf("semget err\n");
        ret = APP_FAIL;
        goto END;
    }
    printf("mutexid = %d\n", pSession->mutexid);


    union semun{
        int val;
        struct semid_ds *buf;
        unsigned short *array;
    } arg;
    
    /*初始化信号量 */
    arg.val = 0;            //初始时缓冲区中无数据
    if(semctl(pSession->fullid, 0, SETVAL, arg) == -1)
    {
        ret = APP_FAIL;
        perror("semctl error !");
        goto END;
    }


    arg.val = 1;            //初始时缓冲区中有1个
    if(semctl(pSession->emptyid, 0, SETVAL, arg) == -1)
    {
        ret = APP_FAIL;
        perror("semctl error !");
        goto END;
    }


    arg.val = 1;            //初始时互斥信号为1,允许一个进程进入
    if(semctl(pSession->mutexid, 0, SETVAL, arg) == -1)
    {
        ret = APP_FAIL;
        perror("semctl error !");
        goto END;
    }


    pSession->ptr = (char *)shmat(pSession->shmid, 0, 0);
    pfhead = (struct FrameData *)pSession->ptr;


    pfhead->offset = sizeof(struct FrameData);
    pfhead->total = MAX_SHAREMEM_SIZE;


END:


    return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值