c++ 双线程协议解析 链表 sleep pthread_join g++编译

主类
testlinklist.cpp
#include <stdio.h>
#include "LinkList.h"
#include <pthread.h>
#include <stdlib.h> 
#include <sys/time.h>
#include   <cstring>
#include <string.h>

#include <unistd.h>

using   namespace   std;
//delete data before 0x5a 0xa5,返回数据5a a5的首地址
int slipToHeader(unsigned char *data,int dataLength,unsigned char **ptail){
int i;
int noflag = 1;
for(i=0;i<dataLength;i++){
if(data[i]==0x5a){
noflag = 0;
if(dataLength-(i+1)>0){
if(*(data+i+1)==0xa5){
if(i!=0){
int j;
for(j=i;j<dataLength;j++){
data[j-i]=data[j];
}
dataLength=dataLength-i;
*ptail = (*ptail)-i;
}else{
break;
}
}else{
continue;
}
}else{
dataLength = 1;
data[0] = 0x5a;
*ptail = data+1;
}
}
}
if(noflag==1){
*ptail = data;
dataLength = 0;
}


//char *str1 = "Borland International", *str2 = "nation", *ptr;
//ptr = strstr(str1, str2);
   //printf("The substring is: %s\n", ptr);
return dataLength;

}

//delete data before 0x5a 0xa5,返回数据5a a5的首地址
int cutHandledData(unsigned char *data,int dataLength,int beCutDataLen,unsigned char **ptail){
int i;
for(i=0;i<dataLength;i++){
data[i]=data[i+beCutDataLen];
}
*ptail = (*ptail)-beCutDataLen;
return dataLength - beCutDataLen;
}
void handleProtocol(unsigned char *protocolData,int len){
//spidev_debug_print_msg((char *)__func__, (unsigned char *)protocolData, len);
switch(protocolData[3]){
case 0x80:
//env->CallVoidMethod(obj, method_reportDeviceStatusDisc, 3);
printf ("handleProtocol get 0x80!\n");
break;

case 0xa2:
printf ("handleProtocol get 0xa2!\n");
break;
case 0x83:
printf ("handleProtocol get 0x83!\n");
break;
case 0x99:
printf ("handleProtocol get 0x99!\n");
break;

}
}


void *insertMcuData(void *arg){
printf ("pthread insertData be start!\n");
unsigned char recv_buf[LEN] ={0x5a,0xa5,0x5,0x80,0x00,0x11,0x0,0x6a,0x5a,0xa5,0x12,0xa2,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x4c};
unsigned char recv_buf1[LEN] ={0x5a,0xa5,0x7,0x83,0x00,0x00,0x00,0x11,0x0,0x7d};
unsigned char recv_buf2[LEN] ={0x5a,0xa5,0x6,0x99,0x00,0x11,0x0,0x01,0x8c};
LinkList *tempList = NULL;
tempList = (LinkList *)arg;

//memset(recv_buf, 0x00, sizeof(recv_buf));

//while(1){
printf ("pthread insertData1!\n");
//int realLen = android_spidev_read_data(fd,LEN,0,recv_buf);

int realLen = 29;
if(recv_buf!=NULL&&realLen>0){
//LOGE("pthread insertData data_buf len=%d"+strlen(recv_buf));//strlen 计算字符串的长度,end with '\0'.
//sizeof 计算的则是分配的数组 str[11] 所占的内存空间的大小
tempList->EnQueue(recv_buf,realLen);
}
sleep(2);
realLen = 10;
if(recv_buf1!=NULL&&realLen>0){
//LOGE("pthread insertData data_buf len=%d"+strlen(recv_buf));//strlen 计算字符串的长度,end with '\0'.
//sizeof 计算的则是分配的数组 str[11] 所占的内存空间的大小
tempList->EnQueue(recv_buf1,realLen);
}
sleep(2);
realLen = 9;
if(recv_buf1!=NULL&&realLen>0){
//LOGE("pthread insertData data_buf len=%d"+strlen(recv_buf));//strlen 计算字符串的长度,end with '\0'.
//sizeof 计算的则是分配的数组 str[11] 所占的内存空间的大小
tempList->EnQueue(recv_buf2,realLen);
}

sleep(2);
//system("sleep 1");

//}
  return ((void *)0);
}

void *parseMcuProtocol(void *arg){
printf ("pthread parseProtocol be start!\n");
LinkList *tempList = NULL;
tempList = (LinkList *)arg;

if(tempList!=NULL){
unsigned char data[LEN*3];//分配空间
unsigned char tempData[LEN];
int dataLength;
//int a[100];
unsigned char *pa = tempData;
unsigned char *phead=data,*ptail=data;

while(1){
printf ("pthread parseProtocol1!\n");
int tempLength = tempList->DeQueue(&pa);
memcpy(ptail,pa,tempLength);
ptail = ptail+tempLength;
dataLength+=tempLength;
printf("DeQueue ptail tempLength=%d\n",tempLength);
printf("DeQueue ptail dataLength=%d\n",dataLength);
//LOGE("DeQueue ptail point to =0x%x",**ptail);

//for (int i=0; i<dataLength; i++){
// LOGE("DeQueue data before slipToHeader [%d]=0x%x",i,data[i]);
//}

dataLength = slipToHeader(data,dataLength,&ptail);
printf("DeQueue data after slipToHeader dataLength=%d\n",dataLength);
for (int i=0; i<dataLength; i++){
if(i==0)
printf("DeQueue data after slipToHeader \n");
printf("0x%x ",data[i]);
if(i==(dataLength-1))
printf("\n");
}
if(dataLength<4){
//continue;
}else{
//spidev_debug_print_msg((char *)__func__, (unsigned char *)data, dataLength);

int protocolLength = data[2];
int oneProtocolLen = protocolLength+3;
printf("DeQueue data before cutHandledData oneProtocolLen=%d\n",oneProtocolLen);

if(dataLength<oneProtocolLen){
//continue;
}else{
printf("cutHandledData \n");
unsigned char *oneProtocol = new unsigned char[oneProtocolLen];
memset(oneProtocol,0x00,oneProtocolLen);
memcpy(oneProtocol,data,oneProtocolLen);
//when the field copy from data to oneProtocol,then delete field from data
dataLength = cutHandledData(data,dataLength,oneProtocolLen,&ptail);
printf("DeQueue data after cutHandledData dataLength=%d\n",dataLength);
for (int i=0; i<dataLength; i++){
if(i==0)
printf("DeQueue data after cutHandledData \n");
printf("0x%x ",data[i]);
if(i==(dataLength-1))
printf("\n");
}
handleProtocol(oneProtocol,oneProtocolLen);
delete []oneProtocol;
}
}
sleep(2);
}
}

return ((void *)0);
}


int main(void){
pthread_t id1,id2;
void *a1,*a2;
int i,err;
LinkList *dataList = new LinkList;

err=pthread_create(&id1,NULL,insertMcuData,dataList);
if(err!=0){
        printf ("Create pthread error1!\n");
        //exit (1);
    }

err=pthread_create(&id2,NULL,parseMcuProtocol,dataList);

if(err!=0){
       printf ("Create pthread error2!\n");
        //exit (1);
    }
//起第二个线程
//Create monday;
  
//printf("At the first God made the heaven and the earth.\n");
//char* str = "Naming the light, Day, and the dark, Night, the first day.";
//monday.start(str);
//monday.wait();

pthread_join(id1,NULL);
pthread_join(id2,&a2);
//pthread_exit(NULL);



//unsigned char data[1024]={0x5a,0xa5,0x5,0x80,0x00,0x11,0x0,0x6a,0x5a,0xa5,0x12,0xa2,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x4c};
//unsigned char *ptail = data;
//int dataLength = 29;
//dataLength = slipToHeader(data,dataLength,&ptail);
//int i;
//for (i=0; i<dataLength; i++){
// printf("DeQueue data after slipToHeader [%d]=0x%x",i,data[i]);
//}
delete dataList;
return 0;
}
链表实现类
LinkList.cpp
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include "LinkList.h"
#include   <cstring>
using namespace std;
LinkList::LinkList()
{
pList = NULL;
front = NULL;
rear = NULL;
listLength = 0;
InitList();
}
LinkList::~LinkList()
{
if (!DestroyList())
{
  DestroyList();
}
}
//初始化,分配一个头节点。
bool LinkList::InitList()
{
if (!(pList = new LNode))
{
  return false;
}
front = rear = pList;
pList->data = NULL;
pList->dataLength = 0;
pList->next = NULL;
return true;
}
//销毁链表。
bool LinkList::DestroyList()
{
if (!ClearList()) {
  return false;
}

delete pList;
pList = front = rear = NULL;
return true;
}
//判断链表是否为空。若为空,返回true,否则返回false。
bool LinkList::IsEmpty() {
if (pList->next == NULL)
{
  return true;
}
return false;
}
//返回链表的中当前节点数。
int LinkList::GetLength()
{
return listLength;
}
//将链表清空,释放当前所有节点。
bool LinkList::ClearList()
{
if (pList == NULL)
{
  return false;
}
LNode *pTemp = NULL;
while (pList->next != NULL)
{
  pTemp = pList->next;
  pList->next = pTemp->next;
  delete []pTemp->data;
  delete pTemp;
  if(listLength>0){
listLength--;
   }

return true;
}

//新增一个节点到对尾,数据设置为data。
bool LinkList::EnQueue(unsigned char data[],int len)
{
LNode *pTemp = NULL;
if(listLength>MAXLENGTH){
return false;
}
pTemp = rear;
LNode *newNode = new LNode;
newNode->data = new unsigned char[len];
newNode->dataLength = len;
for (int i=0; i<len; i++){
newNode->data[i]=data[i];
//printf("EnQueue data [%d]=0x%x",i,data[i]);
if(i==0)
printf("----------LinkList.cpp EnQueue data \n");
printf("0x%x ",data[i]);
if(i==(len-1))
printf("\n");
//LOGE("EnQueue data [%d]=0x%x",i,data[i]);
}
newNode->next = NULL;
pTemp->next = newNode;
rear = newNode;
listLength++;
printf("----------LinkList.cpp EnQueue data pList->next %p \n",pList->next);
return true;
}

//从队头删除节点
int LinkList::DeQueue(unsigned char **data)
{
printf("----------LinkList.cpp DeQueue data pList->next111 %p \n",pList->next);
if(pList->next==NULL)
return 0;
LNode *pTemp = NULL;
pTemp = pList->next;
//*data = pTemp->data;

//LOGE("DeQueue size of tempLength=%d",sizeof(pTemp->data));
printf("---------LinkList.cpp DeQueue dataLength=%d\n",pTemp->dataLength);
memset(*data,0x00,pTemp->dataLength);
memcpy(*data, pTemp->data, pTemp->dataLength);

if(pTemp->next==NULL){
pList->next = NULL;
rear = pList;
}else{
pList->next = pTemp->next;
}


int tempLength = pTemp->dataLength;

for (int i=0; i<tempLength; i++){
if(i==0)
printf("----------LinkList.cpp DeQueue data \n");
printf("0x%x ",*(*data+i));
if(i==(tempLength-1))
printf("\n");
//printf("DeQueue data [%d]=0x%x",i,data[i]);
//LOGE("DeQueue *data [%d]=0x%x",i,*(*data+i));
}

//data = pTemp->data;
delete []pTemp->data;
delete pTemp;
printf("----------LinkList.cpp DeQueue data pList->next222 %p \n",pList->next);
listLength--;
return tempLength;
}



链表头文件类
#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED
const int  LEN=512;
const int  MAXLENGTH = 20;
typedef struct LNode
{
unsigned char* data;
int dataLength;
struct LNode *next;
} LNode, *pLinkList;

typedef struct{
pLinkList front;
pLinkList rear;
}LinkQueue;

class LinkList
{
private:
pLinkList pList;
pLinkList front;
pLinkList rear;
int listLength;
public:
LinkList();
~LinkList();
bool InitList();
bool DestroyList();
bool ClearList();
bool IsEmpty();
int GetLength();

bool EnQueue(unsigned char data[],int len);
int DeQueue(unsigned char **data);

};
#endif // LINKLIST_H_INCLUDED

ubuntu下编译
编译
g++ -g LinkList.cpp testlinklist.cpp -o testlinklist -lpthread
./testlinklist

你可能感兴趣的:(pthread,cpp,linklist,testlinklist)