📄 netqueue.c
字号:
/**********************************************************************
chengjy@felab, copyright 2002-2004
netQueue.c
网络缓冲队列操作。队列的个数由宏定义QUEUE_NUM
规定。
函数:
void queueInit();
char queueAdd(int index, unsigned char* pBuff, int pri);
char queueDelHead(int index);
char queueDelAll();
调用: 无
被调用: 网络通讯程序及用户程序的初始化部分
**********************************************************************/
#include "vxWorks.h"
#include "taskLib.h"
#include "board.h"
/*定义队头和对尾的指针*/
struct cmdSingle *queueHead[QUEUE_NUM],*queueRear[QUEUE_NUM];
/*定义队列长度*/
int queueLen[QUEUE_NUM];
void queueInit();
char queueAdd(int index, unsigned char* pBuff, int pri);
char queueDelHead(int index);
char queueDelAll();
/**********************************************************************
void queueInit()
函数说明: 队列初始化。
参数: 无
返回: 无
调用: 无
被调用:
netSvr.c
void netInit(int mode)
**********************************************************************/
void queueInit()
{
int i;
/*设置头尾指针为空,队列长度为0*/
for(i=0;i<QUEUE_NUM;i++)
{
queueHead[i] = NULL;
queueRear[i] = NULL;
queueLen[i] = 0;
}
}
/**********************************************************************
char queueAdd(int index, unsigned char* pBuff, int pri);
函数说明: 队列元素添加,根据pri决定添加到队头还是队尾
参数:
index, 需要添加到的队列号,必须大于或等于0
且小于QUEUE_NUM
pBuff, 从网络接收数据或者待发送数据的首地址指针
pri, 添加的优先级
返回: 成功添加返回STATUS_NORMAL,否则返回STATUS_ERROR
调用: 无
被调用:
netSvr.c
void netCMDRecv()
char netCMDAdd(unsigned char *pBuff, int buffLen, int cmdNum, unsigned char priority)
**********************************************************************/
char queueAdd(int index, unsigned char* pBuff, int pri)
{
struct cmdSingle *queueHeadTmpt,*queueRearTmpt;
/*判断参数是否合法*/
if( (index<0) || (index>=QUEUE_NUM) )
{
logMsg("queueAdd: unable to local queue index %d\n", index,0,0,0,0,0);
logMsg("queueAdd: index must be greater than or equal to 0 and smaller than %d\n",QUEUE_NUM,0,0,0,0,0);
return(STATUS_ERROR);
}
taskLock(); /*禁止任务调度,保证当前任务执行过程中不会被打断*/
/*如果队列中还没有元素,当前添加的元素同时位于对头和队尾*/
if(queueLen[index]==0)
{
queueHead[index] = malloc(sizeof(struct cmdSingle));
if(queueHead[index] == NULL)
{
/*内存分配失败*/
logMsg("queueAdd: can't malloc enough memory\n",0,0,0,0,0,0);
taskUnlock();
return(STATUS_ERROR);
}
queueRear[index] = queueHead[index];
queueRear[index]->pNext = NULL;
queueRear[index]->pBuff = pBuff;
queueLen[index]++;
}
else if(pri == QUEUE_PRI_HIGH) /*高优先级,添加到队头*/
{
queueHeadTmpt = malloc(sizeof(struct cmdSingle));
if(queueHeadTmpt == NULL)
{
/*内存分配失败*/
logMsg("queueAdd: can't malloc enough memory\n",0,0,0,0,0,0);
taskUnlock();
return(STATUS_ERROR);
}
queueHeadTmpt->pNext = queueHead[index]->pNext;
queueHeadTmpt->pBuff = pBuff;
queueHead[index]->pNext = queueHeadTmpt;
queueLen[index]++;
}
else if(pri == QUEUE_PRI_LOW) /*低优先级,添加到队尾*/
{
queueRearTmpt = malloc(sizeof(struct cmdSingle));
if(queueRearTmpt == NULL)
{
/*内存分配失败*/
logMsg("queueAdd: can't malloc enough memory\n",0,0,0,0,0,0);
taskUnlock();
return(STATUS_ERROR);
}
queueRearTmpt->pNext = NULL;
queueRearTmpt->pBuff = pBuff;
queueRear[index]->pNext = queueRearTmpt;
queueRear[index] = queueRearTmpt;
queueLen[index]++;
}
else /*优先级参数非法*/
{
logMsg("queueAdd: priority be %d, should only be %d or %d\n",
pri,QUEUE_PRI_HIGH,QUEUE_PRI_LOW,0,0,0);
}
taskUnlock(); /*允许任务重新调度*/
return(STATUS_NORMAL);
}
/**********************************************************************
char queueDelHead(int index)
函数说明:队列队头元素删除
参数:
index, 需要删除元素的队列号,必须大于或等于0
且小于QUEUE_NUM
返回: STATUS_NORMAL
调用: 无
被调用: char queueDelAll()
netSvr.c
void netCMDExplain()
void netCMDSend()
**********************************************************************/
char queueDelHead(int index)
{
struct cmdSingle *queueHeadTmpt;
/*判断参数是否合法*/
if( (index<0) || (index>=QUEUE_NUM) )
{
logMsg("queueDelHead: unable to local queue index %d\n", index,0,0,0,0,0);
logMsg("queueDelHead: index must be greater than or equal to 0 and smaller than %d\n",QUEUE_NUM,0,0,0,0,0);
return(STATUS_ERROR);
}
taskLock(); /*禁止任务调度,保证当前任务执行过程中不会被打断*/
queueHeadTmpt = queueHead[index]->pNext;
free(queueHead[index]);
queueHead[index] = queueHeadTmpt;
queueLen[index]--;
if(queueLen[index]==0) /*如果连最后一个元素也被删除,则队列尾指针需要设定为空*/
{
queueRear[index]=NULL;
}
taskUnlock(); /*允许任务调度*/
return(STATUS_NORMAL);
}
/**********************************************************************
char queueDelAll()
函数说明: 队列元素全部删除
参数: 无
返回: STATUS_NORMAL
调用:
char queueDelHead(int index)
被调用:
netSvr.c
void netCloseAll(int mode)
**********************************************************************/
char queueDelAll()
{
char *pBegin;
int i;
/*将所有队列中的所有元素从队头开始全部删除*/
for(i=0;i<QUEUE_NUM;i++)
{
while(queueLen[i]>0)
{
pBegin = queueHead[i]->pBuff;
free(pBegin);
queueDelHead(i);
}
}
return(STATUS_NORMAL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -