#include "ClientCommunicationInterface.h"
/*************************************************************
************************************************************
包含功能:
1.维护整个通信模块的正常工作;
2.维护与服务器的通信功能;
3.维护与本地其他模板的通信功能;
4.维护与服务器端套接字的创建,回收等工作;
5.维护与服务器端通信的读写及信息解析;
6.维护与本地通信的数据读写及信息解析;
7.维护本地临界数据的一致性及同步问题等等。
对外接口主要功能函数:InitCommuncationModel(),GetMsg(),SendMsg()
GetMsg:
主要负责本地模块从套接字中读出数据到本地
SendMsg:
主要负责本地模块向套接字中写入数据
InitCommunicationModel:
主要负责初始化通信模块
************************************************************
************************************************************
*/
struct CommunicationManagerType
{
struct SlaverTransDataType *header;//未完成任务链表
struct SlaverTransDataType *freeHeader;//任务完成释放链表
int freeCondition;
int condition;
int sockfd;
int msgLength;
pthread_cond_t cond;
pthread_mutex_t mutex;
};
//套接字读管道维护数据结构
static struct CommunicationManagerType *readPool;
//套接字写管道数据维护结构
static struct CommunicationManagerType *writePool;
static void WritePool_Init(struct CommunicationManagerType *writePool,const int sockfd)
{
writePool->msgLength=sizeof(struct ExchangeDataType);
writePool->sockfd=sockfd;
writePool->header=NULL;
writePool->freeHeader=NULL;
writePool->freeCondition=0;
writePool->condition=0;
pthread_mutexattr_t mutexattr;
pthread_condattr_t condattr;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&writePool->mutex,&mutexattr);
pthread_condattr_init(&condattr);
pthread_condattr_setpshared(&condattr,PTHREAD_PROCESS_SHARED);
pthread_cond_init(&writePool->cond,&condattr);
}
static void ReadPool_Init(struct CommunicationManagerType *readPool,const int sockfd)
{
readPool->msgLength=sizeof(struct ExchangeDataType);
readPool->sockfd=sockfd;
readPool->header=NULL;
readPool->freeHeader=NULL;
readPool->freeCondition=0;
readPool->condition=0;
pthread_mutexattr_t mutexattr;
pthread_condattr_t condattr;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_setpshared(&mutexattr,PTHREAD_PROCESS_SHARED);
pthread_mutex_init(&readPool->mutex,&mutexattr);
pthread_condattr_init(&condattr);
pthread_condattr_setpshared(&condattr,PTHREAD_PROCESS_SHARED);
pthread_cond_init(&readPool->cond,&condattr);
}
/*
static struct SlaverTransDataType *WriteMallocResource(struct CommunicationManagerType *writePool)
{
if(writePool->freeCondition<=0)
{
return ((struct SlaverTransDataType *)malloc(sizeof(struct SlaverTransDataType)));
}
else
{
struct SlaverTransDataType *result=writePool->freeHeader;
writePool->freeHeader=writePool->freeHeader->next;
writePool->freeCondition--;
return result;
}
}
*/
//线程不安全 ReadThreadRun调用
static struct SlaverTransDataType *ReadMallocResource(struct CommunicationManagerType *readPool)
{
if(readPool->freeCondition==0||readPool->freeHeader==NULL)
{
return ((struct SlaverTransDataType *)malloc(sizeof(struct SlaverTransDataType)));
}
else
{
struct SlaverTransDataType *result=readPool->freeHeader;
readPool->freeHeader=readPool->freeHeader->next;
readPool->freeCondition--;
return result;
}
}
//线程不安全 WriteThreadRun调用
static void WriteFreeResource(struct CommunicationManagerType *writePool,struct SlaverTransDataType *tempData)
{
if(writePool->freeCondition>MAX_FREEDATANUM)
{
free(tempData);
}
else
{
writePool->freeCondition++;
tempData->next=writePool->freeHeader;
writePool->freeHeader=tempData;
}
}
/*
static void ReadFreeResource(struct CommunicationManagerType *readPool,struct SlaverTransDataType *tempData)
{
if(readPool->freeCondition>MAX_FREEDATANUM)
{
free(tempData);
}
else
{
readPool->freeCondition++;
tempData->next=readPool->freeHeader;
readPool->freeHeader=tempData;
}
}
*/
static void *WriteThreadRun(void *WritePool)
{
int i;
struct SlaverTransDataType *writePtr;
//struct CommunicationManagerType *writePool=(struct CommunicationManagerType *)WritePool;
for(;;)
{
pthread_mutex_lock(&(writePool->mutex));
while(writePool->condition==0)
{
pthread_cond_wait(&(writePool->cond),&(writePool->mutex));
}
//获取所有待传输数据
for(i=writePool->condition;i>0;i--)
{
writePtr=writePool->header;
writePool->header=writePool->header->next;
if(writePtr!=NULL)
{
send(writePool->sockfd,&writePtr->data,sizeof(struct ExchangeDataType),0);
WriteFreeResource(writePool,writePtr);
}
}
writePool->condition=0;
pthread_mutex_unlock(&(writePool->mutex));
}
}
static void *ReadThreadRun(void *ReadPool)
{
int readNum=-1;
struct SlaverTransDataType *readPtr;
struct ExchangeDataType *tempData=(struct ExchangeDataType *)malloc(sizeof(struct ExchangeDataType));
printf("read thread starting!\n");
for(;;)
{
//tempdata=(struct SlaverTransDataType *)malloc(sizeof(struct SlaverTransDataType));
readNum=recv(readPool->sockfd,tempData,readPool->msgLength,0);
if(readNum>0)
{
pthread_mutex_lock(&readPool->mutex);
readPtr=ReadMallocResource(readPool);
if(readPtr==NULL)
{
readPtr=((struct SlaverTransDataType *)malloc(sizeof(struct SlaverTransDataType)));
}
memset(readPtr,0,sizeof(struct SlaverTransDataType));
if(memcpy(&readPtr->data,tempData,sizeof(struct ExchangeDataType))!=NULL)
{
readPool->condition++;
readPtr->next=readPool->header;
readPool->header=readPtr;
memset(&tempData,0,sizeof(struct ExchangeDataType));
}
pthread_mutex_unlock(&readPool->mutex);
pthread_cond_signal(&readPool->cond);
}
}
free(tempData);
pthread_exit(0);
}
static int CreateClientSockFD(const char *addr,const int port)
{
struct sockaddr_in sockAddr;
int sockfd=-1;
bzero(&(sockAddr->sin_zero),0);
sockAddr->sin_family=AF_INET;
sockAddr->sin_port=htons(port);
sockAddr->sin_addr.s_addr=inet_addr(addr);
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
return -1;
}
if(connect(sockfd,(struct sockaddr *)&sockAddr,sizeof(struct sockaddr))==-1)
{
close(sockfd);
return -1;
}
return sockfd;
}
///线程不安全函数,需在互斥锁中执行,主要调用方法SetMsg,
static struct SlaverTransDataType *WriteMallocResource()
{
if(writePool->freeCondition==0||writePool->freeHeader==NULL)
{
writePool->freeCondition=0;
writePool->header=NULL;
return ((struct SlaverTransDataType *)malloc(sizeof(struct SlaverTransDataType)));
}
else
{
struct SlaverTransDataType *result=writePool->freeHeader;
writePool->freeHeader=writePool->freeHeader->next;
writePool->freeCondition--;
return result;
}
}
///传入参数为释放的传输资源完成任务后需要释放的链表
///tempData 传入参数为释放的传输资源完成任务后需要释放的链表,结尾以NULL表示
///主要实现回收malloc动态分配的资源 ,便于下次使用,减少malloc和free动态分配内存对整体性能带来的影响
void ReadFreeResource(struct SlaverTransDataType *tempData)
{
struct SlaverTransDataType *ptr;
pthread_mutex_lock(&readPool->mutex);
while(tempData!=NULL)
{
ptr=tempData;
if(readPool->freeCondition>MAX_FREEDATANUM)
{
tempData=tempData->next;
free(ptr);
}
else
{
memset(ptr,0,sizeof(struct SlaverTransDataType));
readPool->freeCondition++;
ptr->next=readPool->freeHeader;
readPool->freeHeader=ptr;
tempData=tempData->next;
}
}
pthread_mutex_unlock(&readPool->mutex);
}
//初始化通信模块,
int InitCommunicationModel(const char *serverAddr,const int port)
{
//创建两个线程分别用于:1.套接字读管道操作维护2.套接字写管道操作维护
int i;
int sockfd;
pthread_t *tid;
struct sockaddr_in sockAddr;
i=0;
sockfd=-1;
readPool=(struct CommunicationManagerType *)malloc(sizeof(struct CommunicationManagerType));
writePool=(struct CommunicationManagerType *)malloc(sizeof(struct CommunicationManagerType));
tid=(pthread_t *)malloc(sizeof(pthread_t)*2);
memset(readPool,0,sizeof(struct CommunicationManagerType));
memset(writePool,0,sizeof(struct CommunicationManagerType));
sockfd=CreateClientSockFD(serverAddr,port);
if(sockfd==-1)
{
printf("create client socket file descripter is failed! error reason :%s[error code=%d]",strerror(errno),errno);
return -1;
}
WritePool_Init(writePool,sockfd);
ReadPool_Init(readPool,sockfd);
if(pthread_create(tid,NULL,ReadThreadRun,readPool)!=0)
{
printf("create read Thread failed! error reason:%s[error code=%d]\n",strerror(errno),errno);
}
if(pthread_create(tid+1,NULL,WriteThreadRun,writePool)!=0)
{
printf("create write Thread failed! error reason:%s[error code=%d]\n",strerror(errno),errno);
}
for(i=0;i<2;i++)
{
pthread_join(tid[i],NULL);
}
free(tid);
close(readPool->sockfd);
free(writePool);
free(readPool);
return 0;
}
//用于各模块发送消息到服务器端,主要针对writePool
//客户端各模块向服务器端发送消息
int SetMsg(const struct ExchangeDataType sendMsg)
{
int result=-1;
struct SlaverTransDataType *writeData;
pthread_mutex_lock(&writePool->mutex);
writeData=WriteMallocResource();
if(writeData==NULL)
{
writeData=(struct SlaverTransDataType *)malloc(sizeof(struct SlaverTransDataType));
}
memset(writeData,0,sizeof(struct SlaverTransDataType));
if(memcpy(&writeData->data,&sendMsg,sizeof(struct ExchangeDataType))!=NULL)
{
writePool->condition++;
writeData->next=writePool->header;
writePool->header=writeData;
result=0;
}
else
{
result=-1;
}
pthread_mutex_unlock(&writePool->mutex);
pthread_cond_signal(&writePool->cond);
return result;
}
//主模块实现对服务器端传入信息进行解析,主要针对readPool
///返回值为当前需要处理的服务器端的任务请求链表包含所有的任务,
///任务处理完成后需要调用函数需要调用ReadFreeResource方法对处理完成的每个任务的资源进行回收
struct SlaverTransDataType *GetMsg()
{
struct SlaverTransDataType *result=NULL;
pthread_mutex_lock(&(readPool->mutex));
while(readPool->condition==0)
{
pthread_cond_wait(&readPool->cond,&readPool->mutex);
}
result=readPool->header;
//ReadFreeResource(readPtr);
readPool->condition=0;
readPool->header=NULL;
pthread_mutex_unlock(&readPool->mutex);
return result;
}
//////////////////////头文件////////////////////////////////////////
#include <string.h>
#include <netinet/in.h>#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#define MAX_FREEDATANUM 50
//客户端与服务器端传输的数据通用类型
//服务器、客户端、节点服务器之间通信的基本数据类型
struct ExchangeDataType
{
int Type;//传输数据类型
char baseData[1020];//范数据类型
};
//数据链表存储
struct SlaverTransDataType
{
struct SlaverTransDataType *next;
struct ExchangeDataType data;
};
void ReadFreeResource(struct ExchangeDataType *tempData);
int InitCommunicationModel(const char *serverAddr,const int port);
int SetMsg(const struct ExchangeDataType sendMsg);
struct ExchangeDataType *GetMsg();