|
Posted on 2009-01-31 01:52 S.l.e!ep.¢% 阅读(1368) 评论(0) 编辑 收藏 引用 所属分类: test
//
网络通信协议
/**/
//////////////////////////////////////////////////////////////////////////////
//
struct
cmd

{
int
nCmd;
}
;

#pragma pack(
1
)
struct
tagClientLogin

{
cmd header;
char
username[
20
];
char
userpwd[
20
];
}
;

struct
tagRepClientLogin

{
cmd header;
bool
bLoginSuccess;
}
;
#pragma pack(
1
)

#define
NETWORK_CMD_LOGIN 1
#define
NETWORK_CMD_REP_LOGIN 2
/**/
//////////////////////////////////////////////////////////////////////////////
//
//
接口定义
/**/
//////////////////////////////////////////////////////////////////////////////
//
class
ClientObserver

{
public
:
 ClientObserver()
{}
~
ClientObserver()
{}
virtual
void
onRepLogin(
bool
bLoginSuccess)
=
0
;
}
;

class
INetWorkable

{
public
:
 INetWorkable()
{}
~
INetWorkable()
{}
//
pvoid : 欲发送的缓冲区; nSize : 缓冲区的大小
virtual
bool
send(
const
void
*
pvoid,
int
nSize)
=
0
;

//
由网络层调用,pvoid: 接收到的数据的缓冲区, nSize : 缓冲区的大小; 返回已经处理的数据长度
virtual
int
onreceive(
const
void
*
pvoid,
int
nSize)
=
0
;
}
;

class
ILogable

{
public
:
 ILogable()
{}
~
ILogable()
{}
virtual
void
log(
const
char
*
plog)
=
0
;
}
;

/**/
//////////////////////////////////////////////////////////////////////////////
//
//
业务逻辑类
/**/
//////////////////////////////////////////////////////////////////////////////
//
class
Client :
public
INetWorkable

{
public
:
Client();
virtual
~
Client();


void
registerObserver(ClientObserver
*
p)
{ m_pClientObserver
=
p; }
void
removeObserver()
{ m_pClientObserver
=
NULL; }
void
setLoger(ILogable
*
p)
{ m_plog
=
p; }
void
removeLoger()
{ m_plog
=
NULL; }
bool
SendLogin(
const
char
*
name,
const
char
*
pwd)

{
tagClientLogin login;
memset(
&
login,
0
,
sizeof
(login));
login.header.nCmd
=
NETWORK_CMD_LOGIN;
strncpy(login.username, name,
sizeof
(login.username));
strncpy(login.userpwd, pwd,
sizeof
(login.userpwd));
return
send(
&
login,
sizeof
(login));
}
protected
:
virtual
bool
send(
const
void
*
pvoid,
int
nSize)

{
cout
<<
"
Client socket send size =
"
<<
nSize
<<
endl;
return
true
;
}
virtual
int
onreceive(
const
void
*
pvoid,
int
nSize)

{
if
( nSize
<
sizeof
(cmd) )
return
0
;
cmd
*
pheader
=
(cmd
*
)pvoid;
if
( pheader
->
nCmd
==
NETWORK_CMD_REP_LOGIN )

{
if
( nSize
<
sizeof
(tagRepClientLogin) )
return
0
;
tagRepClientLogin
*
ptagRepClientLogin
=
(tagRepClientLogin
*
)pvoid;
if
( m_pClientObserver
!=
NULL )
m_pClientObserver
->
onRepLogin(ptagRepClientLogin
->
bLoginSuccess);
return
sizeof
(tagRepClientLogin);
}
return
0
;
}
private
:
ClientObserver
*
m_pClientObserver;
ILogable
*
m_plog;
}
;
下面是测试的代码 #include "client.h"
#include <string.h>

class testClient : public Client
  {
public:
 testClient() {}
 ~testClient() {}

virtual bool send(const void* pvoid, int nSize)
 {
memcpy(m_buf, pvoid, nSize);
return true;
}

int NetWorkReceive(const void* pvoid, int nSize)
 {
return onreceive(pvoid, nSize);
}

bool cmpMemory(const void* pvoid, int nSize)
 {
return ( 0 == memcmp(m_buf, pvoid, nSize) );
}
private:
char m_buf[1024];
};

int main()
  {
testClient test;
test.SendLogin("test_username", "test_pwd");

tagClientLogin clientlogin;
memset(&clientlogin, 0, sizeof(clientlogin));
clientlogin.header.nCmd = NETWORK_CMD_LOGIN;
strcpy(clientlogin.username, "test_username");
strcpy(clientlogin.userpwd, "test_pwd");
if( !test.cmpMemory(&clientlogin, sizeof(clientlogin)) )
cout << "test failed" << endl;
 char szBuf[1024] = {0};

// 摸拟服务器发送的包长度不足
if( 0 != test.NetWorkReceive(szBuf, sizeof(tagRepClientLogin) - 10) )
cout << "test failed" << endl;

// 摸拟服务器发送的包内容非法
if( 0 != test.NetWorkReceive(szBuf, sizeof(tagRepClientLogin)) )
cout << "test failed" << endl;

tagRepClientLogin repLogin;
repLogin.header.nCmd = NETWORK_CMD_REP_LOGIN;
memcpy(szBuf, &repLogin, sizeof(repLogin));

// 摸拟服务器发送了正确的包
if( sizeof(tagRepClientLogin) != test.NetWorkReceive(szBuf, sizeof(tagRepClientLogin)) )
cout << "test failed" << endl;
return 0;
}
|