forked from huoyu820125/Micro-Development-Kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestServer.cpp
More file actions
90 lines (81 loc) · 1.92 KB
/
TestServer.cpp
File metadata and controls
90 lines (81 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// TestServer.cpp: implementation of the TestServer class.
//
//////////////////////////////////////////////////////////////////////
#include "TestServer.h"
#include <stdio.h>
#include <string.h>
#ifndef WIN32
#include <unistd.h>
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
TestServer::TestServer()
{
SetIOThreadCount(4);//设置网络IO线程数量
SetWorkThreadCount(4);//设置工作线程数
Listen(8888);
// Listen(6666);
// Listen(9999);
// Connect("127.0.0.1", 10086, 5);//连接自身,未测试,不建议这么做
}
TestServer::~TestServer()
{
}
/*
* 服务器主线程
*/
void* TestServer::Main(void* pParam)
{
while ( IsOk() )
{
//执行业务
#ifndef WIN32
usleep( 1000 * 1000 );
#else
Sleep( 1000 );
#endif
}
return 0;
}
/**
* 新连接事件回调方法
*
* 派生类实现具体连接业务处理
*
*/
void TestServer::OnConnect(mdk::NetHost& host)
{
printf( "new client(%d) connect in\n", host.ID() );
}
/**
* 连接关闭事件,回调方法
*
* 派生类实现具体断开连接业务处理
*
*/
void TestServer::OnCloseConnect(mdk::NetHost &host)
{
printf( "client(%d) close connect\n", host.ID() );
}
void TestServer::OnMsg(mdk::NetHost &host)
{
//假设报文结构为:2byte表示数据长度+报文数据
unsigned char c[256];
/*
读取数据长度,长度不足2byte直接返回,等待下次数据到达时再读取
只读取2byte,false表示,不将读取到的数据从缓冲中删除,下次还是可以读到
*/
if ( !host.Recv( c, 2, false ) ) return;
unsigned short len = 0;
memcpy( &len, c, 2 );//得到数据长度
len += 2;//报文长度 = 报文头长度+数据长度
if ( len > 256 )
{
printf( "close client:invaild fromat msg\n" );
host.Close();
return;
}
if ( !host.Recv(c, len) ) return;//将报文读出,并从接收缓冲中删除,绝对不可能长度不够,即使连接已经断开,也可以读到数据
host.Send( c, len );//收到消息原样回复
}