Repository navigation
Expand file tree
/
Copy pathnetwork_interface.hpp
More file actions
162 lines (122 loc) · 5.14 KB
/
Copy pathnetwork_interface.hpp
File metadata and controls
162 lines (122 loc) · 5.14 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
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2012 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __NETWORK_INTERFACE__
#define __NETWORK_INTERFACE__
#include "cstdkbe/memorystream.hpp"
#include "network/common.hpp"
#include "cstdkbe/cstdkbe.hpp"
#include "cstdkbe/timer.hpp"
#include "helper/debug_helper.hpp"
#include "network/endpoint.hpp"
namespace KBEngine {
namespace Mercury
{
class Address;
class Bundle;
class Channel;
class ChannelTimeOutHandler;
class ChannelDeregisterHandler;
class DelayedChannels;
class ListenerReceiver;
class Packet;
class EventDispatcher;
class MessageHandlers;
class NetworkInterface : public TimerHandler
{
public:
static const int RECV_BUFFER_SIZE;
static const char * USE_KBEMACHINED;
typedef std::map<Address, Channel *> ChannelMap;
NetworkInterface(EventDispatcher * pMainDispatcher,
int32 extlisteningPort_min = -1, int32 extlisteningPort_max = -1, const char * extlisteningInterface = "",
uint32 extrbuffer = 0, uint32 extwbuffer = 0,
int32 intlisteningPort = 0, const char * intlisteningInterface = "",
uint32 intrbuffer = 0, uint32 intwbuffer = 0);
~NetworkInterface();
void attach(EventDispatcher & mainDispatcher);
void detach();
INLINE const Address & extaddr() const;
INLINE const Address & intaddr() const;
bool recreateListeningSocket(const char* pEndPointName, uint16 listeningPort_min, uint16 listeningPort_max,
const char * listeningInterface, EndPoint* pEP, ListenerReceiver* pLR, uint32 rbuffer = 0, uint32 wbuffer = 0);
bool registerChannel(Channel* pChannel);
bool deregisterChannel(Channel* pChannel);
bool deregisterChannel(const Address & addr);
bool deregisterAllChannels();
Channel * findChannel(const Address & addr);
Channel * findChannel(int fd);
ChannelTimeOutHandler * pChannelTimeOutHandler() const
{ return pChannelTimeOutHandler_; }
void pChannelTimeOutHandler(ChannelTimeOutHandler * pHandler)
{ pChannelTimeOutHandler_ = pHandler; }
ChannelDeregisterHandler * pChannelDeregisterHandler() const
{ return pChannelDeregisterHandler_; }
void pChannelDeregisterHandler(ChannelDeregisterHandler * pHandler)
{ pChannelDeregisterHandler_ = pHandler; }
EventDispatcher & dispatcher() { return *pDispatcher_; }
EventDispatcher & mainDispatcher() { return *pMainDispatcher_; }
/* 外部网点和内部网点 */
EndPoint & extEndpoint() { return extEndpoint_; }
EndPoint & intEndpoint() { return intEndpoint_; }
bool isExternal() const { return isExternal_; }
const char * c_str() const { return extEndpoint_.c_str(); }
void * pExtensionData() const { return pExtensionData_; }
void pExtensionData(void * pData) { pExtensionData_ = pData; }
const ChannelMap& channels(void) { return channelMap_; }
/** 发送相关 */
Reason send(Bundle & bundle, Channel * pChannel = NULL);
Reason sendPacket(Packet * pPacket, Channel * pChannel = NULL);
void sendIfDelayed(Channel & channel);
void delayedSend(Channel & channel);
Reason basicSendSingleTry(Channel * pChannel, Packet * pPacket);
Reason basicSendWithRetries(Channel * pChannel, Packet * pPacket);
bool good() const{ return (!isExternal() || extEndpoint_.good()) && (intEndpoint_.good()); }
void onPacketIn(const Packet & packet);
void onPacketOut(const Packet & packet);
void onChannelGone(Channel * pChannel);
void onChannelTimeOut(Channel * pChannel);
/*
处理所有消息包
*/
void processAllChannelPackets(KBEngine::Mercury::MessageHandlers* pMsgHandlers);
/*
获取一次send或者sendto操作产生错误的原因
*/
static Reason getSendErrorReason(const EndPoint * endpoint, int retSendSize, int packetTotalSize);
INLINE int32 numExtChannels() const;
private:
virtual void handleTimeout(TimerHandle handle, void * arg);
void closeSocket();
private:
EndPoint extEndpoint_, intEndpoint_;
ChannelMap channelMap_;
EventDispatcher * pDispatcher_;
EventDispatcher * pMainDispatcher_;
void * pExtensionData_;
ListenerReceiver * pExtListenerReceiver_;
ListenerReceiver * pIntListenerReceiver_;
DelayedChannels * pDelayedChannels_;
ChannelTimeOutHandler * pChannelTimeOutHandler_; // 超时的通道可被这个句柄捕捉, 例如告知上层client断开
ChannelDeregisterHandler * pChannelDeregisterHandler_;
const bool isExternal_;
int32 numExtChannels_;
};
}
}
#ifdef CODE_INLINE
#include "network_interface.ipp"
#endif
#endif // __NETWORK_INTERFACE__