-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTcp.h
147 lines (135 loc) · 3.77 KB
/
Tcp.h
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
#ifndef ___NICE_TCPSERVER____
#define ___NICE_TCPSERVER____
#include <string>
#include "Type.h"
#include <memory>
#include "Server.h"
#include <unordered_map>
#include <array>
#include <list>
#include <atomic>
#include "Message.h"
#include "NoCopy.h"
#include <functional>
#include "CopyablePtr.hpp"
#include "Task.hpp"
namespace nicehero
{
class TcpSession;
using tcpuid = std::string;
using TcpSessionPtr = std::shared_ptr<TcpSession>;
using MessagePtr = CopyablePtr<Message>;
using TcpTask = Task<bool,TO_MAIN>;
using tcpcommand = TcpTask(*)(TcpSessionPtr,MessagePtr);
//typedef std::function<bool(TcpSession&, Message&)> tcpcommand;
class TcpServer;
class TcpSessionImpl;
class TcpServerImpl;
class TcpMessageParser
{
public:
tcpcommand m_commands[65536] = {nullptr};
};
class TcpSessionCommand
{
public:
TcpSessionCommand(const std::type_info & info,
ui16 command,
tcpcommand fucnc
);
};
class TcpSession
:public NoCopy,public std::enable_shared_from_this<TcpSession>
{
friend class TcpSessionImpl;
friend class TcpServer;
friend class TcpServerImpl;
public:
TcpSession();
virtual void init();
virtual void init(TcpServer& server);
virtual void init2(TcpServer& server);
virtual void close();
virtual void setMessageParser(TcpMessageParser* messageParser);
virtual void sendMessage(Message& msg);
virtual void sendMessage(const Serializable& msg);
TcpMessageParser* m_MessageParser = nullptr;
tcpuid& getUid();
protected:
std::unique_ptr<TcpSessionImpl> m_impl;
std::string m_hash;
tcpuid m_uid;
ui64 m_serialID;
Message m_PreMsg;
bool parseMsg(unsigned char* data, ui32 len);
virtual void removeSelf();
virtual void removeSelfImpl();
void doRead();
void doSend(Message& msg);
void doSend();
std::atomic_bool m_IsSending;
std::list<Message> m_SendList;
virtual void handleMessage(MessagePtr msg);
};
class TcpSessionS
:public TcpSession
{
friend class TcpServer;
friend class TcpSessionImpl;
friend class TcpServerImpl;
public:
TcpSessionS();
virtual ~TcpSessionS();
void init(TcpServer& server);
void init2(TcpServer& server);
protected:
TcpServer* m_TcpServer = nullptr;
void removeSelf();
void removeSelfImpl();
};
class TcpSessionC
:public TcpSession,public Server
{
public:
TcpSessionC();
virtual ~TcpSessionC();
bool connect(const std::string& ip, ui16 port);
void init(bool isAsync = false);
void startRead();
std::atomic<bool> m_isInit;
protected:
void removeSelf();
private:
int checkServerSign(ui8* data_);//return 0 ok 1 error 2 warning
};
class TcpServer
:public Server
{
friend class TcpSession;
friend class TcpSessionS;
public:
TcpServer(const std::string& ip,ui16 port );
virtual ~TcpServer();
std::unique_ptr<TcpServerImpl> m_impl;
bool m_Started = false;
virtual TcpSessionS* createSession();
virtual void addSession(const tcpuid& uid, TcpSessionPtr session);
virtual void removeSession(const tcpuid& uid,ui64 serialID);
virtual void accept();
std::unordered_map<tcpuid, TcpSessionPtr > m_sessions;
};
TcpMessageParser& getTcpMessagerParse(const std::type_info& typeInfo);
inline TcpSessionCommand::TcpSessionCommand(const std::type_info & info, ui16 command, tcpcommand func )
{
TcpMessageParser& msgParser = getTcpMessagerParse(info);
msgParser.m_commands[command] = func;
}
}
#define TCP_SESSION_COMMAND(CLASS,COMMAND) \
static nicehero::TcpTask _##CLASS##_##COMMAND##FUNC(nicehero::TcpSessionPtr session,nicehero::MessagePtr msg);\
static nicehero::TcpSessionCommand _##CLASS##_##COMMAND(typeid(CLASS), COMMAND, _##CLASS##_##COMMAND##FUNC);\
static nicehero::TcpTask _##CLASS##_##COMMAND##FUNC(nicehero::TcpSessionPtr session,nicehero::MessagePtr msg)
#ifndef SESSION_COMMAND
#define SESSION_COMMAND TCP_SESSION_COMMAND
#endif
#endif