-
Notifications
You must be signed in to change notification settings - Fork 1
/
PyConnectNetComm.h
228 lines (190 loc) · 6.26 KB
/
PyConnectNetComm.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* PyConnectNetComm.h
*
* Copyright 2006, 2007 Xun Wang.
* This file is part of PyConnect.
*
* PyConnect is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* PyConnect 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PyConnectNetComm_h_DEFINED
#define PyConnectNetComm_h_DEFINED
#include <sys/types.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif
#include "PyConnectObjComm.h"
// critical section/mutex
#ifdef MULTI_THREAD
#ifdef WIN32
#include <winbase.h>
extern CRITICAL_SECTION g_criticalSection;
#else
#include <pthread.h>
extern pthread_mutex_t g_mutex;
#endif
#endif
#ifndef WIN32
#define PYCONNECT_DOMAINSOCKET_PATH "/tmp"
#ifdef PYTHON_SERVER
#define PYCONNECT_SVRSOCKET_PREFIX "pysvr"
#define PYCONNECT_CLTSOCKET_PREFIX "pyclt"
#else
#define PYCONNECT_SVRSOCKET_PREFIX "pyclt"
#define PYCONNECT_CLTSOCKET_PREFIX "pysvr"
#endif
#endif //!WIN32
#define PYCONNECT_NETCOMM_PORT 37251
#ifndef PYCONNECT_BROADCAST_IP
#ifdef USE_MULTICAST
#define PYCONNECT_BROADCAST_IP "230.15.20.20"
#else
#define PYCONNECT_BROADCAST_IP "255.255.255.255" // need to be modified
#endif
#endif
#define PYCONNECT_UDP_BUFFER_SIZE 2048
#define PYCONNECT_TCP_BUFFER_SIZE 4096
#define PYCONNECT_MAX_TCP_SESSION 50
#define PYCONNECT_COMMPORT_RANGE 100 // this basically limits number of pythonised objects running on same machine/interface
#ifdef HAS_OWN_MAIN_LOOP
#define PYCONNECT_NETCOMM_INIT \
PyConnectNetComm::instance()->init( this->getMP(), this )
#define PYCONNECT_NETCOMM_DECLARE \
virtual void setFD( const SOCKET_T & fd ); \
virtual void clearFD( const SOCKET_T & fd )
#define PYCONNECT_NETCOMM_PROCESS_DATA( READYFDSET ) \
PyConnectNetComm::instance()->processIncomingData( READYFDSET )
#else
#define PYCONNECT_NETCOMM_INIT \
PyConnectNetComm::instance()->init( this->getMP() )
#define PYCONNECT_NETCOMM_DECLARE
#define PYCONNECT_NETCOMM_PROCESS_DATA \
PyConnectNetComm::instance()->continuousProcessing()
#endif
#define PYCONNECT_NETCOMM_FINI \
PyConnectNetComm::instance()->fini()
#ifndef PYTHON_SERVER
#define PYCONNECT_NETCOMM_ENABLE_NET \
PyConnectNetComm::instance()->enableNetComm()
#define PYCONNECT_NETCOMM_DISABLE_NET \
PyConnectNetComm::instance()->disableNetComm()
#ifdef WIN32
#define PYCONNECT_NETCOMM_ENABLE_IPC \
static_assert( 0, "PYCONNECT_NETCOMM_ENABLE_IPC is not supported under Windows." )
#define PYCONNECT_NETCOMM_DISABLE_IPC \
static_assert( 0,"PYCONNECT_NETCOMM_DISABLE_IPC is not supported under Windows." )
#else
#define PYCONNECT_NETCOMM_ENABLE_IPC \
PyConnectNetComm::instance()->enableIPCComm()
#define PYCONNECT_NETCOMM_DISABLE_IPC \
PyConnectNetComm::instance()->disableIPCComm()
#endif
#endif // HAS_OWN_MAIN_LOOP
namespace pyconnect {
class FDSetOwner {
public:
virtual void setFD( const SOCKET_T & fd ) = 0;
virtual void clearFD( const SOCKET_T & fd ) = 0;
virtual ~FDSetOwner() {}
};
class PyConnectNetComm : public ObjectComm
{
public:
static PyConnectNetComm * instance();
virtual ~PyConnectNetComm();
void processIncomingData( fd_set * readyFDSet );
void init( MessageProcessor * pMP, FDSetOwner * fdOwner = NULL );
void continuousProcessing();
void dataPacketSender( const unsigned char * data, int size, bool broadcast = false );
CommObjectStat createTalker( char * host, int port = 0 );
CommObjectStat setBroadcastAddr( char * addr );
void fini();
void enableNetComm();
void disableNetComm( bool onExit = false );
#ifndef WIN32
void enableIPCComm();
void disableIPCComm( bool onExit = false );
#endif //!WIN32
private:
enum FDDomain {
UNKNOWN,
NETWORK,
LOCALIPC
};
struct SocketDataBufferInfo {
unsigned char * bufferedData;
int expectedDataLength;
int bufferedDataLength;
};
typedef struct sClientFD {
SOCKET_T fd;
FDDomain domain;
struct sockaddr_in cAddr; // client address, NETWORK only
int localProcID; // server process id, IPC only
struct SocketDataBufferInfo dataInfo;
sClientFD * pNext;
} ClientFD;
PyConnectNetComm();
bool initUDPListener();
bool initTCPListener();
void clientDataSend( const unsigned char * data, int size );
void netBroadcastSend( const unsigned char * data, int size );
void localBroadcastSend( const unsigned char * data, int size );
void processUDPInput( unsigned char * recBuffer, int recBytes, struct sockaddr_in & cAddr );
bool createTCPTalker( struct sockaddr_in & cAddr );
#ifndef WIN32
SOCKET_T findOrCreateIPCTalker( int procID );
SOCKET_T findFdFromClientListByProcID( int procID );
void consolidateIPCSockets();
#endif // !WIN32
void addFdToClientList( const SOCKET_T & fd, FDDomain domain,
struct sockaddr_in * cAddr, int procID = 0 );
void destroyCurrentClient( SOCKET_T fd );
void destroyCurrentClient( ClientFD * & FDPtr, ClientFD * & prevFDPtr );
void updateMPID();
bool getIDFromIP( int & addr );
static PyConnectNetComm * s_pPyConnectNetComm;
MessageProcessor * pMP_;
struct sockaddr_in sAddr_;
struct sockaddr_in bcAddr_;
SOCKET_T udpSocket_;
SOCKET_T tcpSocket_;
SOCKET_T domainSocket_;
unsigned char * dgramBuffer_;
unsigned char * clientDataBuffer_;
unsigned char * dispatchDataBuffer_;
ClientFD * clientFDList_;
// only used in own main loop
FDSetOwner * pFDOwner_;
int maxFD_;
fd_set masterFDSet_;
bool netCommEnabled_;
bool IPCCommEnabled_;
bool invalidUDPSock_;
bool keepRunning_;
unsigned short portInUse_;
#ifdef USE_MULTICAST
struct ip_mreq multiCastReq_;
#endif
typedef std::vector<int> ClientSocketList; // server process id
ClientSocketList liveServerSocketList_;
};
} // namespace pyconnect
#endif // PyConnectNetComm_h_DEFINED