本项目总工包括四个部分:
1、抽象接口层
2、抽象接口层的实现类层
3、业务框架层
4、测试函数
1、抽象接口层:
class SocketProtocol
{
public:
SocketProtocol(void)
{
}
virtual ~SocketProtocol(void)
{
}
//客户端初始化 获取handle上下
virtual int cltSocketInit( /*out*/) = 0;
//客户端发报文
virtual int cltSocketSend( unsigned char *buf /*in*/, int buflen /*in*/) = 0;
//客户端收报文
virtual int cltSocketRev( unsigned char *buf /*in*/, int *buflen /*in out*/) = 0;
//客户端释放资源
virtual int cltSocketDestory() = 0;
};
2、抽象接口层的实现类层:
定义文件:
class CMySocketImp :public SocketProtocol
{
public:
CMySocketImp(void);
~CMySocketImp(void);
//客户端初始化 获取handle上下
virtual int cltSocketInit();
//客户端发报文
virtual int cltSocketSend( unsigned char *buf /*in*/, int buflen /*in*/);
//客户端收报文
virtual int cltSocketRev( unsigned char *buf /*in*/, int *buflen /*in out*/);
//客户端释放资源
virtual int cltSocketDestory();
private:
char *pbuf;
int len;
};
实现文件:
CMySocketImp::CMySocketImp(void)
{
pbuf = NULL;
len = 0;
}
CMySocketImp::~CMySocketImp(void)
{
}
//客户端初始化 获取handle上下
int CMySocketImp::cltSocketInit()
{
pbuf = NULL;
len = 0;
return 0;
}
//客户端发报文
int CMySocketImp::cltSocketSend( unsigned char *buf /*in*/, int buflen /*in*/)
{
if (buf == NULL || buflen<0 )
{
return -1;
}
this->pbuf = (char *)malloc(buflen *sizeof(char ));
if (this->pbuf == NULL)
{
return -2;
}
memcpy(this->pbuf, buf, buflen);
this->len = buflen;
return 0;
}
//客户端收报文
int CMySocketImp::cltSocketRev( unsigned char *buf /*in*/, int *buflen /*in out*/)
{
if (buf == NULL ||buflen== NULL)
{
return -1;
}
memcpy(buf, this->pbuf, this->len);
*buflen = this->len;
return 0;
}
//客户端释放资源
int CMySocketImp::cltSocketDestory()
{
if (this->pbuf != NULL)
{
free(this->pbuf);
this->pbuf= NULL;
this->len = 0;
}
return 0;
}
3、业务框架层:
class MainOPClass
{
public:
MainOPClass()
{
sp = NULL;
}
static int MainOP2(SocketProtocol *sp, unsigned char *buf, int buflen, unsigned char *out, int *outlen)
{
int ret = 0;
ret = sp->cltSocketInit();
ret = sp->cltSocketSend(buf, buflen);
ret = sp->cltSocketRev(out, outlen);
ret = sp->cltSocketDestory();
return ret;
}
int MainOP1( unsigned char *buf, int buflen, unsigned char *out, int *outlen)
{
int ret = 0;
ret = sp->cltSocketInit();
ret = sp->cltSocketSend(buf, buflen);
ret = sp->cltSocketRev(out, outlen);
ret = sp->cltSocketDestory();
return ret;
}
public:
SocketProtocol * getSp() const
{
return sp;
}
void setSp(SocketProtocol * val)
{
sp = val;
}
protected:
private:
//在框架类中集成了一个协议。。。。
SocketProtocol *sp;
};
void Test()
{
int ret = 0;
char buf[1024] = {0};
int buflen = 10;
char out[1024] = {0};
int outlen = 10;
SocketProtocol *sp = NULL;
<span style="white-space:pre"> </span>strcpy(buf, "dddddddddddddssssssssssssssssss");
sp = new CMySocketImp3();
MainOPClass mainOpClass;
mainOpClass.setSp(sp); //注入
//在MainOPClass中包含了一个协议类sp,这种关系强
mainOpClass.MainOP1((unsigned char *)buf, buflen, (unsigned char *)out, &outlen);
//对sp 做函数参数。。。。
//sp只是做函数参数,关系弱。。。
mainOpClass.MainOP2(sp, (unsigned char *)buf, buflen, (unsigned char *)out, &outlen);
MainOPClass::MainOP2(sp, (unsigned char *)buf, buflen, (unsigned char *)out, &outlen);
printf("out:%s \n", out);
delete sp;
}