InterProcessCommunicationClientModelV1.0

本文介绍了一个客户端通信模块的设计与实现,包括与服务器的双向通信、数据读写与解析等功能。通过初始化通信模型、发送和接收消息等接口,实现了高效稳定的数据交换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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();
标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
资源下载链接为: https://pan.quark.cn/s/9648a1f24758 Java中将Word文档转换为PDF是一种常见的技术需求,尤其在跨平台共享、保持格式一致性和便于在线预览等场景中非常实用。通常,开发者会借助专门的库来实现这一功能,其中Aspose.Words是一个非常强大的选择。Aspose.Words是由Aspose公司开发的文档处理组件,支持多种文件格式,包括Word和PDF。它提供了丰富的API,方便开发者在Java应用程序中进行文件转换、编辑和格式化操作,尤其在Word转PDF方面表现卓越。 使用Aspose.Words进行Word转PDF的步骤如下: 添加依赖:通过Maven或Gradle等工具将Aspose.Words的Java库引入项目。 加载Word文档:使用Document类加载Word文件,例如: 配置输出选项:创建PdfSaveOptions对象,用于设置PDF保存时的选项,如图像质量、安全性等。 执行转换:调用Document的save方法,传入输出路径和PdfSaveOptions对象,例如: 支持多种输出格式:Aspose.Words不仅支持将Word转换为PDF,还能转换为HTML、EPUB、XPS等多种格式,只需更换SaveOptions的子类即可。 保持格式与样式:在转换过程中,Aspose.Words能够最大程度地保留源文档的格式和样式,包括文本样式、图像位置、表格布局等。 优化性能:Aspose.Words支持并行处理和多线程技术,可以显著提高大量文档转换的速度。 处理复杂文档:它能够处理包含宏、复杂公式、图表、脚注等元素的Word文档,确保转换后的PDF内容完整且可读。 安全性与版权:在转换过程中,可以设置PDF的访问权限,例如禁止打印或复制文本,从而保护文档内容。 在实际开发中,还需要注意错误和异常的处理,以
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值