关于网络发送的数据缓冲例子(读数据和发送采用双线程)主要用于视频的实时传输

此为测试代码,


#include <stdio.h>
#include <windows.h>
#include <string.h> 
#include <time.h>


#define DVR_CIF_BUFFER_MAX_NUM  2
#define DVR_CIF_BUFFER_LENGTH   1024*1024
typedef struct _ST_SAVE_CIF_NODE_
{
    unsigned char szCifDataBuf[DVR_CIF_BUFFER_MAX_NUM][DVR_CIF_BUFFER_LENGTH];
    unsigned int unCurSaveCifBufNum;
    unsigned int unSaveCifBufPosition;
    unsigned int unCurGetCifBufNum;
    unsigned int unGetCifBufPosition;
}ST_SAVE_CIF_NODE, PST_SAVE_CIF_NODE;


ST_SAVE_CIF_NODE stSaveCifNode[4];


int GetCifDataFromSaveBuf(int nVchn, unsigned char * pucPacketBuf, unsigned int unDataLen)
{
    unsigned char ucBlockReadNum, ucBlockWriteNum;
    unsigned int  unBlockReadPosition, unBlockWritePosition;


    ucBlockReadNum = stSaveCifNode[nVchn].unCurGetCifBufNum;
    ucBlockWriteNum = stSaveCifNode[nVchn].unCurSaveCifBufNum;
    unBlockReadPosition = stSaveCifNode[nVchn].unGetCifBufPosition;
    unBlockWritePosition = stSaveCifNode[nVchn].unSaveCifBufPosition;


    if(ucBlockWriteNum == ucBlockReadNum && (unBlockWritePosition - unBlockReadPosition >= unDataLen)
        ||ucBlockWriteNum != ucBlockReadNum)
    {
        if(unBlockReadPosition < DVR_CIF_BUFFER_LENGTH)
        {
            memcpy(pucPacketBuf, &stSaveCifNode[nVchn].szCifDataBuf[ucBlockReadNum][unBlockReadPosition], unDataLen);
            unBlockReadPosition += unDataLen;
        }
        else
        {
            ucBlockReadNum++;
            if(ucBlockReadNum >= DVR_CIF_BUFFER_MAX_NUM)
                ucBlockReadNum = 0;
            unBlockReadPosition = 0;
        }
        stSaveCifNode[nVchn].unCurGetCifBufNum = ucBlockReadNum ;
        stSaveCifNode[nVchn].unGetCifBufPosition = unBlockReadPosition;
        return 1;
    }
    else
        return 0;
}


__inline void SaveCifDataToSaveBuf(int nVchn, unsigned char * pucPacketBuf, unsigned int unDataLen)
{
    unsigned char ucBlockReadNum, ucBlockWriteNum;
    unsigned int  unBlockReadPosition, unBlockWritePosition;
    unsigned int  unLeavingDataLen;


    ucBlockReadNum = stSaveCifNode[nVchn].unCurGetCifBufNum;
    ucBlockWriteNum = stSaveCifNode[nVchn].unCurSaveCifBufNum;
    unBlockReadPosition = stSaveCifNode[nVchn].unGetCifBufPosition;
    unBlockWritePosition = stSaveCifNode[nVchn].unSaveCifBufPosition;


    if(ucBlockReadNum != ucBlockWriteNum && unBlockWritePosition >= DVR_CIF_BUFFER_LENGTH)
    {
        //* 读和写分别操作不同的内存块,但是当前写内存块已满,则应切换读内存块后切换写内存块


        ucBlockReadNum ++;
        if(ucBlockReadNum >= DVR_CIF_BUFFER_MAX_NUM)
            ucBlockReadNum = 0;
        else;
        unBlockReadPosition = 0;


        stSaveCifNode[nVchn].unCurGetCifBufNum = ucBlockReadNum;
     stSaveCifNode[nVchn].unGetCifBufPosition = unBlockReadPosition;


        ucBlockWriteNum ++;
        if(ucBlockWriteNum >= DVR_CIF_BUFFER_MAX_NUM)
            ucBlockWriteNum = 0;
        else;
        unBlockWritePosition = 0;
    }
    else;


    if(unDataLen < (DVR_CIF_BUFFER_LENGTH - unBlockWritePosition))
    {
        memcpy(&stSaveCifNode[nVchn].szCifDataBuf[ucBlockWriteNum][unBlockWritePosition], pucPacketBuf, unDataLen);
        unBlockWritePosition += unDataLen;
    }
    else
    {
        memcpy(&stSaveCifNode[nVchn].szCifDataBuf[ucBlockWriteNum][unBlockWritePosition], pucPacketBuf, DVR_CIF_BUFFER_LENGTH - unBlockWritePosition);
        if(ucBlockReadNum != ucBlockWriteNum)
        {
            //* 读和写分别操作不同的内存块,但是当前写内存块已满,则应切换读内存块后切换写内存块
            ucBlockReadNum ++;
            if(ucBlockReadNum >= DVR_CIF_BUFFER_MAX_NUM)
                ucBlockReadNum = 0;
            else;
            unBlockReadPosition = 0;


         stSaveCifNode[nVchn].unCurGetCifBufNum = ucBlockReadNum;
         stSaveCifNode[nVchn].unGetCifBufPosition = unBlockReadPosition;
        }
        else;
        unLeavingDataLen = unDataLen - (DVR_CIF_BUFFER_LENGTH - unBlockWritePosition);
        
        ucBlockWriteNum ++;
        if(ucBlockWriteNum >= DVR_CIF_BUFFER_MAX_NUM)
            ucBlockWriteNum = 0;
        else;
        unBlockWritePosition = 0;


        memcpy(&stSaveCifNode[nVchn].szCifDataBuf[ucBlockWriteNum][unBlockWritePosition], pucPacketBuf, unLeavingDataLen);
        unBlockWritePosition = unLeavingDataLen;
    }


    stSaveCifNode[nVchn].unCurSaveCifBufNum = ucBlockWriteNum;
    stSaveCifNode[nVchn].unSaveCifBufPosition = unBlockWritePosition;
}
//pthread_mutex_unlock(&hCifDataSaveMutex[]);
int main(void)
{
 unsigned char szDataBuf[1024*6];
 unsigned int unRunNum = 0;
 
 memset(szDataBuf, 0xff, sizeof(szDataBuf));
 memset(&stSaveCifNode, 0x00, 4*sizeof(ST_SAVE_CIF_NODE));
 while(1)
 {
        unRunNum++;
        printf("Run Times : %d \n", unRunNum);
        SaveCifDataToSaveBuf(1, szDataBuf, 4300);
        GetCifDataFromSaveBuf(1, szDataBuf, 1024);
        GetCifDataFromSaveBuf(1, szDataBuf, 1024);
        GetCifDataFromSaveBuf(1, szDataBuf, 1024);
        GetCifDataFromSaveBuf(1, szDataBuf, 1024);
        GetCifDataFromSaveBuf(1, szDataBuf, 1024);
        Sleep(10);
 }
 
}

你可能感兴趣的:(关于网络发送的数据缓冲例子(读数据和发送采用双线程)主要用于视频的实时传输)