-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommonServer.h
More file actions
165 lines (143 loc) · 4.76 KB
/
CommonServer.h
File metadata and controls
165 lines (143 loc) · 4.76 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* CommonServer.h
*
* Created on: 2016年7月13日
* Author: wong
*/
#ifndef COMMONSERVER_H_
#define COMMONSERVER_H_
#include <queue>
#include <map>
#include "base/Thread.h"
#include "base/Condition.h"
#include "net/EventLoop.h"
#include "CCMS.h"
class Package;
class LoginPackage;
class LogoutPackage;
class ServerBase;
typedef int (MESSAGE_CALLBACK)(int sessionId, int msgId, const char* buf, int len, void* userdata);
typedef int (CONNECT_CALLBACK)(int sessionId, const char* sn, int status, void* userdata);
typedef int (DATAFLOW_CALLBACK)(int sessionId, const char* sn, int recvbytes, void* userdata);
class CommonServer
{
public:
CommonServer(const std::string& ip, uint16_t port, bool bServer,
uint32_t numThreads = 0, uint32_t maxConns = 0,
const std::string& sn = "", int devType = CCMS_DEVTYPE_UNKNOWN);
virtual ~CommonServer();
public:
int Start();
int SetMsgCallback(MESSAGE_CALLBACK* callback, void* pUserData);
int SetConnCallback(CONNECT_CALLBACK* callback, void* pUserData);
int SetDataFlowCallback(DATAFLOW_CALLBACK* callback, void* pUserData);
int SendData(uint32_t sessionId, uint32_t msgId, const char* buf, size_t len);
int SendData(const std::string& sn, uint32_t msgId, const char* buf, size_t len);
int SendData(emDevType devType, uint32_t msgId, const char* buf, size_t len);
int SendCommon(uint32_t sessionId, uint32_t msgId, uint32_t result);
int SendLogin(uint32_t sessionId, const std::string& username, const std::string& password);
int SendLoginResp(uint32_t sessionId, uint32_t result);
int SendLogout(uint32_t sessionId, const std::string& username);
int SendLogoutResp(uint32_t sessionId, uint32_t result);
int SendKeepAlive(uint32_t sessionId);
private:
void ProcessThreadFunc(); // work thread
void ProcessMsg(uint32_t sessionId, Package* package);
private:
static int RawConnCB(int sessionId, int status, const char* ip, unsigned short port, void* userdata)
{
CommonServer* that = reinterpret_cast<CommonServer*>(userdata);
return that->OnRawConn(sessionId, status, ip, port);
}
int OnRawConn(int sessionId, int status, const char* ip, unsigned short port);
static int RawDataFlowCB(int sessionId, int recvbytes, void* userdata)
{
CommonServer* that = reinterpret_cast<CommonServer*>(userdata);
return that->OnRawDataFlow(sessionId, recvbytes);
}
int OnRawDataFlow(int sessionId, int recvbytes);
static int RawMsgCB(int sessionId, const char* buf, int len, void* userdata)
{
CommonServer* that = reinterpret_cast<CommonServer*>(userdata);
return that->OnRawMsg(sessionId, buf, len);
}
int OnRawMsg(int sessionId, const char* buf, int len);
int ParsePackage(const char* buf, int len, Package** pPackage);
Package* CreatePackage(uint32_t msgId);
private:
void OnLogin(uint32_t sessionId, LoginPackage* package);
void OnLogout(uint32_t sessionId, LogoutPackage* package);
private:
void ResizeBuffer(size_t size);
void ReleaseBuffer();
int SendImpl(uint32_t sessionId, Package& package, size_t len);
struct PeerAttr;
PeerAttr* GetPeerAttr(uint32_t seesionId);
std::string RemovePeerAttr(uint32_t seesionId);
private:
struct PeerAttr
{
uint32_t seqno;
std::string sn;
int devType;
};
typedef std::map<uint32_t, PeerAttr> MapPeer;
class PeerAttrSNFinder
{
public:
PeerAttrSNFinder(const std::string& sn)
: m_sn(sn)
{
}
bool operator ()(const MapPeer::value_type& pair)
{
return pair.second.sn == m_sn;
}
private:
const std::string& m_sn;
};
class PeerAttrTypeFinder
{
public:
PeerAttrTypeFinder(int devType)
: m_devType(devType)
{
}
bool operator ()(const MapPeer::value_type& pair)
{
return pair.second.devType == m_devType;
}
private:
int m_devType;
};
private:
bool m_bServer;
std::string m_ip;
uint16_t m_port;
//uint32_t m_numThreads;
uint32_t m_maxConns;
std::string m_sn;
emDevType m_devType;
muduo::Thread m_threadProcess;
bool m_bProcessThreadRunning;
MapPeer m_mapPeer;
struct PackageAttr
{
Package* package;
uint32_t sessionId;
};
muduo::MutexLock m_mutexMsg;
muduo::Condition m_condMsg;
std::queue<PackageAttr> m_packgeQueue;
muduo::net::EventLoop* m_loop;
MESSAGE_CALLBACK* m_pfnOnRecv;
CONNECT_CALLBACK* m_pfnOnConn;
DATAFLOW_CALLBACK* m_pfnOnDataFlow;
void* m_pUserData;
char* m_bufferPool;
size_t m_bufferPoolSize;
ServerBase* m_netBase;
private:
CommonServer(const CommonServer& other);
};
#endif /* COMMONSERVER_H_ */