socket库C++模型设计和实现--企业信息系统框架集成第三方产品案例

本文主要介绍C++面向对象编程的一个综合示例:socket库C++模型设计和实现--企业信息系统框架集成第三方产品案例。下面是示例的主要内容介绍。

内容介绍:

 案例背景:一般的企业信息系统都有成熟的框架,软件框架一般不发生变化,能自由的集成第三方厂商的产品
 案例需求:在企业信息系统框架中集成第三方厂商的Socket通信产品和第三方加密产品
        第三方厂商的socket通信产品:完成两点之间的通信
        第三方厂商加密产品:完成数据发送时加密;数据接收解密
 案例要求:1 能支持多个第三方厂商加密产品的入围
        2 能支持多个厂商的socket通信产品的入围
        3 企业信息系统框架不轻易发生框架
 需求实现:思考1:企业信息系统框架,第三方产品如何分层  --> 通过一个接口层进行分层
        思考2 :企业信息系统框架如何自由继承第三方产品(软件设计:模块要求松,接口要求紧)--》
        思考3 :软件分层以后,开发企业信息系统框架的程序员应该做什么?第三方产品入围应该做什么
 编码实现:分析有多少个类:CSocketProtocol,CSckFactoryImp1,CSckFactoryImp2
                  CEncDesProtocol(加密厂商) HwEncDes CiscoEncDes
        1 定义CSocketProtocol 抽象类
        2 编写框架函数
        3 编写框架测试函数
        4 厂商1CSckFactoryImp1实现CSocketProtocol,厂商2实现CSocketProtocol
        5 抽象加密接口,加密厂商1,加密厂商2,集成实现业务模型
        6 框架(c语言函数方式,框架函数:c++类方式,框架类)
其中主要用到了几个重要的面向对象思想:继承-组合(强弱),注入,控制反转IOC,面向对象思想扩展aop思想(aop思想是对继承编程思想的有力补充)。

下面是具体的代码实现。

一 首先是.h文件:

1.des.h文件

#pragma once

class des
{
public:
	//用户使用的函数 加密
	int DesEnc(unsigned char *pInData,
				int           nInDataLen,
				unsigned char *poutData,
				int           *poutDataLen);
	// 用户使用函数 des 解密
	int DesDec(unsigned char *pInData,
			   int           nInDataLen,
			   unsigned char *poutData,
			   int           *poutDataLen);
};

2.CEncDesProtocol.h文件

#pragma once

// 加密 解密 

class CEncDesProtocol
{
public:
	CEncDesProtocol()
	{

	}
	virtual~CEncDesProtocol()
	{

	}
	// 加密 明文--》密文
	virtual int EncData(unsigned char *plain,int plainlen,unsigned char *cryptdata,int *cryptdatalen) = 0;
	// 解密 密文-->>明文
	virtual int DecData(unsigned char *cryptdata,int cryptdatalen,unsigned char *plain,int *plainlen) = 0;
};

3.HwEncDes.h文件

#pragma once

#include "CEncDesProtocol.h"
#include "iostream"
#include "des.h"
using namespace std;

class HwEncDes : public CEncDesProtocol,public des
{
public:
	// 加密 明文--》密文
	virtual int EncData(unsigned char *plain,int plainlen,unsigned char *cryptdata,int *cryptdatalen);
	// 解密 密文-->>明文
	virtual int DecData(unsigned char *cryptdata,int cryptdatalen,unsigned char *plain,int *plainlen);
};

4.CSocketProtocol.h文件

#pragma once

#include "iostream"

using namespace std;

class CSocketProtocol
{
public:
	CSocketProtocol()
	{
		;
	}
	virtual ~CSocketProtocol()//虚析构函数
	{
		;
	}
	// 客户端初始化 
	virtual int cltSocketInit() = 0;
	// 客户端发报文
	virtual int cltSocketSend(unsigned char *buf,int buflen) = 0;
	// 客户端收报文
	virtual int cltSocketRev(unsigned char *buf,int *buflen) = 0;
	// 客户端释放资源
	virtual int cltSocketDestroy() = 0;
protected:

private:

};

5.CSckFactoryImp1.h文件

#pragma once

#include "iostream"

using namespace std;

#include "CSocketProtocol.h"

class CSckFactoryImp1 : public CSocketProtocol
{
public:
	// 客户端初始化 
	virtual int cltSocketInit();
	// 客户端发报文
	virtual int cltSocketSend(unsigned char *buf,int buflen);
	// 客户端收报文
	virtual int cltSocketRev(unsigned char *buf,int *buflen);
	// 客户端释放资源
	virtual int cltSocketDestroy();
protected:

private:
	unsigned char *p;
	int len;
};

6.CSckFactoryImp2.h文件

#pragma once

#include "iostream"

using namespace std;

#include "CSocketProtocol.h"

class CSckFactoryImp2 : public CSocketProtocol
{
public:
	// 客户端初始化 
	virtual int cltSocketInit();
	// 客户端发报文
	virtual int cltSocketSend(unsigned char *buf,int buflen);
	// 客户端收报文
	virtual int cltSocketRev(unsigned char *buf,int *buflen);
	// 客户端释放资源
	virtual int cltSocketDestroy();
protected:

private:
	unsigned char *p;
	int len;
};

二 .cpp文件:

1.des.cpp文件

#include "des.h"


//用户使用的函数 加密
int des::DesEnc(unsigned char *pInData,int nInDataLen,unsigned char *poutData,int*poutDataLen)
{
	return 0;
}
// 用户使用函数 des 解密
int des::DesDec(unsigned char *pInData,int nInDataLen,unsigned char *poutData,int*poutDataLen)
{
	return 0;
}

2.HwEncDes.cpp文件

#include "HwEncDes.h"
#include "des.h"


// 加密 明文--》密文
 int HwEncDes::EncData(unsigned char *plain,int plainlen,unsigned char *cryptdata,int *cryptdatalen)
 {
	 int ret = 0;
	 //用户使用的函数 加密
	 ret =  des::DesEnc(plain,plainlen,cryptdata,cryptdatalen);
	 if (ret != 0)
	 {
		 printf("func DesEnc() err:%d\n",ret);
	 }
	 return ret;
 }
// 解密 密文-->>明文
 int  HwEncDes::DecData(unsigned char *cryptdata,int cryptdatalen,unsigned char *plain,int *plainlen)
 {
	 int ret = 0;
	 // 用户使用函数 des 解密
	 ret = des::DesDec(cryptdata,cryptdatalen,plain,plainlen);
	 if (ret != 0)
	 {
		 printf("func DesDec() err:%d\n",ret);
	 }
	 return ret;
 }

3.CSckFactoryImp1.cpp文件

#include "CSckFactoryImp1.h"

#include "iostream"

using namespace std;

// 客户端初始化 
 int CSckFactoryImp1::cltSocketInit()
 {
	 p = NULL;
	 len = 0;
	 return 0;
 }
// 客户端发报文
 int CSckFactoryImp1::cltSocketSend(unsigned char *buf,int buflen)
 {
	 p = (unsigned char *)malloc(sizeof(unsigned char) * buflen);
	 if ( p == NULL)
	 {
		 return -1;
	 }
	 memcpy(p,buf,buflen);
	 len = buflen;
	 return 0;
 }
// 客户端收报文
 int CSckFactoryImp1::cltSocketRev(unsigned char *buf,int *buflen)
 {
	 if (buf == NULL || buflen == NULL)
	 {
		 return -1;
	 }
	 *buflen = this->len;
	 memcpy(buf,this->p,this->len);
	 return 0;
 }
// 客户端释放资源
 int CSckFactoryImp1::cltSocketDestroy()
 {
	 if ( p != NULL)
	 {
		 free(p);
		 p = NULL;
		 len = 0;
	 }
	 return 0;
 }

4.CSckFactoryImp2.cpp文件

#include "CSckFactoryImp2.h"

// 客户端初始化 
int CSckFactoryImp2::cltSocketInit()
{
	p = NULL;
	len = 0;
	return 0;
}
// 客户端发报文
int CSckFactoryImp2::cltSocketSend(unsigned char *buf,int buflen)
{
	p = (unsigned char *)malloc(sizeof(unsigned char) * buflen);
	if ( p == NULL)
	{
		return -1;
	}
	memcpy(p,buf,buflen);
	len = buflen;
	return 0;
}
// 客户端收报文
int CSckFactoryImp2::cltSocketRev(unsigned char *buf,int *buflen)
{
	if (buf == NULL || buflen == NULL)
	{
		return -1;
	}
	*buflen = this->len;
	memcpy(buf,this->p,this->len);
	return 0;
}
// 客户端释放资源
int CSckFactoryImp2::cltSocketDestroy()
{
	if ( p != NULL)
	{
		free(p);
		p = NULL;
		len = 0;
	}
	return 0;
}

三 main()函数测试示例:

1.main01():

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"

using namespace std;

#include "CSocketProtocol.h"
#include "CSckFactoryImp1.h"
#include "CSckFactoryImp2.h"

// 
int SckSendAndRec01(CSocketProtocol *sp,unsigned char *in,int inlen,unsigned char *out,int *outlen)
{ 
	int ret = 0;
	ret = sp->cltSocketInit();
	if ( ret != 0)
	{
		goto End;
	}
	ret = sp->cltSocketSend(in,inlen);
	if ( ret != 0)
	{
		goto End;
	}
	ret = sp->cltSocketRev(out,outlen);
	if ( ret != 0)
	{
		goto End;
	}
End:
	ret = sp->cltSocketDestroy();
	return 0;
}

//写一个框架
int main01()
{
	int ret = 0;
	unsigned char in[4096];
	int inlen;
	unsigned char out[4096];
	int outlen = 0;

	strcpy((char *)in,"aaaaaabbbbbbbdddd");
	inlen = 9;
	// 第一个厂商
	CSocketProtocol *sp = NULL;
	sp = new CSckFactoryImp1;

	ret = SckSendAndRec01(sp,in,inlen,out,&outlen);

	if ( ret != 0)
	{
		cout << "func SckSendAndRec() err:" << ret << endl;
		return ret;
	}

	delete sp;//想通过父类指针,释放所有的子类对象的资源,应使用虚析构函数

	// 第二个厂商
	CSckFactoryImp2 *sp2 = NULL;
	sp2 = new CSckFactoryImp2;

	ret = SckSendAndRec01(sp2,in,inlen,out,&outlen);
	if ( ret != 0)
	{
		cout << "func SckSendAndRec() err:" << ret << endl;
		return ret;
	}

	cout << "hello..."<< endl;

	system("pause");

}

2.main02():

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"

using namespace std;

#include "CSocketProtocol.h"
#include "CSckFactoryImp1.h"
#include "CSckFactoryImp2.h"
#include "CEncDesProtocol.h"
#include "des.h"
#include "HwEncDes.h"
// 
int SckSendAndRec2(CSocketProtocol *sp,unsigned char *in,int inlen,unsigned char *out,int *outlen)
{ 
	int ret = 0;
	ret = sp->cltSocketInit();
	if ( ret != 0)
	{
		goto End;
	}
	ret = sp->cltSocketSend(in,inlen);
	if ( ret != 0)
	{
		goto End;
	}
	ret = sp->cltSocketRev(out,outlen);
	if ( ret != 0)
	{
		goto End;
	}
End:
	ret = sp->cltSocketDestroy();
	return 0;
}

//该框架式一个函数 要改为类的形式 见mainclass03_框架类.cpp
int SckSendAndRecDecEnc2(CSocketProtocol *sp,CEncDesProtocol *ed,unsigned char *in,int inlen,unsigned char *out,int *outlen)
{ 
	int ret = 0;
	unsigned char data[4096];
	int datalen = 0;
	ret = sp->cltSocketInit();
	if ( ret != 0)
	{
		goto End;
	}

	ret = ed->EncData(in,inlen,data,&datalen);// 数据发送之前要加密,发送的是密文
	if ( ret != 0)
	{
		goto End;
	}

	ret = sp->cltSocketSend(data,datalen);// 发送密文
	if ( ret != 0)
	{
		goto End;
	}

	ret = sp->cltSocketRev(data,&datalen);// 收到的是密文,要进行解密
	if ( ret != 0)
	{
		goto End;
	}

	ret = ed->DecData(data,datalen,out,outlen);//解密
	if ( ret != 0)
	{
		goto End;
	}
End:
	ret = sp->cltSocketDestroy();
	return 0;
}
//写一个框架
int main02()
{
	int ret = 0;
	unsigned char in[4096];
	int inlen;
	unsigned char out[4096];
	int outlen = 0;

	strcpy((char *)in,"aaaaaabbbbbbbdddd");
	inlen = 9;
	// 第一个厂商
	CSocketProtocol *sp = NULL;
	sp = new CSckFactoryImp1;

	ret = SckSendAndRec2(sp,in,inlen,out,&outlen);

	if ( ret != 0)
	{
		cout << "func SckSendAndRec() err:" << ret << endl;
		return ret;
	}

	delete sp;//想通过父类指针,释放所有的子类对象的资源,应使用虚析构函数

	// 第二个厂商
	CSckFactoryImp2 *sp2 = NULL;
	sp2 = new CSckFactoryImp2;

	ret = SckSendAndRec2(sp2,in,inlen,out,&outlen);
	if ( ret != 0)
	{
		cout << "func SckSendAndRec() err:" << ret << endl;
		return ret;
	}

	// 加解密

	CEncDesProtocol *ed = NULL;
	ed = new HwEncDes;
	ret = SckSendAndRecDecEnc2(sp,ed,in,inlen,out,&outlen);
	if ( ret != 0)
	{
		cout << "func SckSendAndRecDecEnc() err:" << ret << endl;
		return ret;
	}
	delete ed;

	cout << "hello..."<< endl;

	system("pause");

}

3.main03():

#define _CRT_SECURE_NO_WARNINGS
#include "iostream"

using namespace std;

#include "CSocketProtocol.h"
#include "CSckFactoryImp1.h"
#include "CSckFactoryImp2.h"
#include "CEncDesProtocol.h"
#include "des.h"
#include "HwEncDes.h"

// 抽象类在多继承中的应用
//class MainOp : public CSocketProtocol,public CEncDesProtocol
//{
//public:
//
//protected:
//
//private:
//
//};

class MainOp
{
public:
	MainOp()
	{
		this->ed = NULL;
		this->sp = NULL;
	}
	MainOp(CSocketProtocol *sp,CEncDesProtocol *ed)
	{
		this->ed = ed;
		this->sp = sp;
	}
	void setSp(CSocketProtocol *sp)
	{
		this->sp = sp;
	}
	void setEd(CEncDesProtocol *ed)
	{
		this->ed = ed;
	}

public:
	int SckSendAndRec(unsigned char *in,int inlen,unsigned char *out,int *outlen)
	{ 
		int ret = 0;
		ret = this->sp->cltSocketInit();
		if ( ret != 0)
		{
			goto End;
		}
		ret = sp->cltSocketSend(in,inlen);
		if ( ret != 0)
		{
			goto End;
		}
		ret = sp->cltSocketRev(out,outlen);
		if ( ret != 0)
		{
			goto End;
		}
	End:
		ret = this->sp->cltSocketDestroy();
		return 0;
	}

	//该框架式一个函数 改为类的形式 
	int SckSendAndRecDecEnc(unsigned char *in,int inlen,unsigned char *out,int *outlen)
	{ 
		int ret = 0;
		unsigned char data[4096];
		int datalen = 0;
		ret = this->sp->cltSocketInit();
		if ( ret != 0)
		{
			goto End;
		}

		ret = this->ed->EncData(in,inlen,data,&datalen);// 数据发送之前要加密,发送的是密文
		if ( ret != 0)
		{
			goto End;
		}

		ret = this->sp->cltSocketSend(data,datalen);// 发送密文
		if ( ret != 0)
		{
			goto End;
		}

		ret = sp->cltSocketRev(data,&datalen);// 收到的是密文,要进行解密
		if ( ret != 0)
		{
			goto End;
		}

		ret = ed->DecData(data,datalen,out,outlen);//解密
		if ( ret != 0)
		{
			goto End;
		}
	End:
		ret = sp->cltSocketDestroy();
		return 0;
	}
protected:

private:
	CSocketProtocol *sp;
	CEncDesProtocol *ed;
};

//写一个框架
int main()
{
	int ret = 0;
	unsigned char in[4096];
	int inlen;
	unsigned char out[4096];
	int outlen = 0;

	strcpy((char *)in,"aaaaaabbbbbbbdddd");
	inlen = 9;

	CSocketProtocol *sp = NULL;
	CSocketProtocol *sp2 = NULL;
	sp = new CSckFactoryImp1;
	sp2 = new CSckFactoryImp2;

	CEncDesProtocol *ed = NULL;
	ed = new HwEncDes;
	
	MainOp *myMainOp = new MainOp;//此时调用无参构造函数MainOp(),需要设置,如下
	myMainOp->setSp(sp);
	myMainOp->setEd(ed);

	// 第二个厂商
	myMainOp->SckSendAndRec(in,inlen,out,&outlen);
	if ( ret != 0)
	{
		cout << "func SckSendAndRec() err:" << ret << endl;
		return ret;
	}

	//加解密
	ret = myMainOp->SckSendAndRecDecEnc(in,inlen,out,&outlen);
	if ( ret != 0)
	{
		cout << "func SckSendAndRecDecEnc() err:" << ret << endl;
		return ret;
	}

	MainOp *m2 = new MainOp(sp,ed);//此时调用有参构造函数,MainOp(CSocketProtocol *sp,CEncDesProtocol *ed)不需要设置
	m2->SckSendAndRecDecEnc(in,inlen,out,&outlen);
	if ( ret != 0)
	{
		cout << "func SckSendAndRecDecEnc() err:" << ret << endl;
		return ret;
	}

	delete sp;
	delete ed;
	delete myMainOp;
	delete sp2;
	delete m2;

	cout << "hello..."<< endl;

	system("pause");

}

下面链接是上面的的所有代码,不想复制的同学可以直接下载,只需两个积分:socket库C++模型设计和实现--企业信息系统框架集成第三方产品案例


这个工程是基于TCP长连接的包模式的网络通讯框架。 在TCP连接中,按照一个一个的包方式进行数据传输, 框架实现了可以同时侦听多个端口, 每个数据包既可以不压缩传输,也能支持zlib压缩和blowfish加密传输。 服务端提供三种线程池来进行tcp连接处理, 一类是接收线程池,接收线程池获取每个socket传输来的数据包, 同时保证每个socket的包按照到来的顺序进行处理, 二类是工作线程池,由接收线程池把接收到的数据包投递到工作线程池, 工作线程池专门处理这些接收到的数据包。 三类是发送线程池,当工作线程池处理完这些数据包,确定需要发送处理结果数据包到客户端, 或者其他线程需要发送数据包到客户端,他们首先把数据包投递到发送线程池, 发送线程池专门负责数据包的发送。 框架同时提供了每个客户端的定时器功能,在服务端内部各个socket之间数据通信等。 框架来源于一个没做完的手游服务端,至于没有使用现成的游戏通讯框架而自己实现, 主要是因为习惯了自己造轮子。 因为项目没做完,所有没进行严格的测试,无法保证代码无BUG。 您若要使用到自己的项目中,请完全熟悉了之后再用,这样出现BUG也好自己修改。 框架支持 Linux和windows平台。 相关BLOG请看如下连接: http://blog.csdn.net/fanxiushu/article/details/50631626
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bixiwen_liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值