-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyConnectCommon.h
More file actions
434 lines (377 loc) · 12.7 KB
/
PyConnectCommon.h
File metadata and controls
434 lines (377 loc) · 12.7 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* PyConnectCommon.h
*
* Copyright 2006, 2007, 2013, 2014 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 PyConnectCommon_h_DEFINED
#define PyConnectCommon_h_DEFINED
#ifndef _MSC_VER
#include <cxxabi.h>
#endif
#include <iostream>
#ifdef PYTHON_SERVER
#ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif
#ifdef _XOPEN_SOURCE
#undef _XOPEN_SOURCE
#endif
#include "Python.h"
#else
#include <tuple>
#include <functional>
#include <vector>
#endif
#include <string>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define PYCONNECT_MSG_BUFFER_SIZE 10240
#ifdef RELEASE
#define PYCONNECT_LOGGING_INIT
#define PYCONNECT_LOGGING_DECLARE( LOGNAME )
#if defined( WIN32 ) || defined( SUN_COMPILER )
#define DEBUG_MSG( ... )
#define ERROR_MSG( ... )
#define WARNING_MSG( ... )
#define INFO_MSG( ... )
#else
#define DEBUG_MSG( MSG... )
#define ERROR_MSG( MSG... )
#define WARNING_MSG( MSG... )
#define INFO_MSG( MSG... )
#endif
#define PYCONNECT_LOGGING_FINI
#else
#ifdef OPENR_LOG
#define PYCONNECT_LOGGING_INIT
#define DEBUG_MSG( MSG... ) OSYSDEBUG(( MSG ))
#define ERROR_MSG( MSG... ) OSYSLOG1(( osyslogERROR, MSG ))
#define WARNING_MSG( MSG... ) OSYSLOG1(( osyslogWARNING, MSG ))
#define INFO_MSG( MSG... ) OSYSLOG1(( osyslogINFO, MSG ))
#else
#define PYCONNECT_LOGGING_DECLARE( LOGNAME ) \
FILE * s_pyconnectlog = NULL; \
char * logFileName = (char *)LOGNAME
extern FILE * s_pyconnectlog;
#define PYCONNECT_LOGGING_INIT \
s_pyconnectlog = fopen( logFileName, "a" )
//#define s_pyconnectlog stdout
#if defined( WIN32 ) || defined( SUN_COMPILER )
#define DEBUG_MSG(...) \
fprintf( s_pyconnectlog, "DEBUG: " ); \
fprintf( s_pyconnectlog, __VA_ARGS__ ); \
fflush( s_pyconnectlog )
#define INFO_MSG(...) \
fprintf( s_pyconnectlog, "INFO: " ); \
fprintf( s_pyconnectlog, __VA_ARGS__ ); \
fflush( s_pyconnectlog )
#define WARNING_MSG(...) \
fprintf( s_pyconnectlog, "WARNING: " ); \
fprintf( s_pyconnectlog, __VA_ARGS__ ); \
fflush( s_pyconnectlog )
#define ERROR_MSG(...) \
fprintf( s_pyconnectlog, "ERROR: " ); \
fprintf( s_pyconnectlog, __VA_ARGS__ ); \
fflush( s_pyconnectlog )
#else // !WIN32
#define DEBUG_MSG( MSG... ) \
fprintf( s_pyconnectlog, "DEBUG: " ); \
fprintf( s_pyconnectlog, MSG ); \
fflush( s_pyconnectlog )
#define INFO_MSG( MSG... ) \
fprintf( s_pyconnectlog, "INFO: " ); \
fprintf( s_pyconnectlog, MSG ); \
fflush( s_pyconnectlog )
#define WARNING_MSG( MSG... ) \
fprintf( s_pyconnectlog, "WARNING: " ); \
fprintf( s_pyconnectlog, MSG ); \
fflush( s_pyconnectlog )
#define ERROR_MSG( MSG... ) \
fprintf( s_pyconnectlog, "ERROR: " ); \
fprintf( s_pyconnectlog, MSG ); \
fflush( s_pyconnectlog )
#endif
#define PYCONNECT_LOGGING_FINI fclose( s_pyconnectlog )
#endif
#endif // define RELEASE
#ifndef DEFINED_ENCRYPTION_KEY
#pragma message ( "Make sure you change the encryption key for network communication. \
To disable this warning, set DEFINED_ENCRYPTION_KEY macro to true." )
#endif
namespace pyconnect {
const int MAX_STR_LENGTH = 32767;
const char PYCONNECT_MSG_INIT = '#';
const char PYCONNECT_MSG_END = '@';
typedef enum {
MODULE_DISCOVERY = 0x1,
MODULE_DECLARE = 0x2,
MODULE_ASSIGN_ID = 0x3,
ATTR_METD_EXPOSE = 0x4,
CALL_ATTR_METD = 0x5,
CALL_ATTR_METD_NOCB = 0x6,
ATTR_METD_RESP = 0x7,
ATTR_VALUE_UPDATE = 0x8,
GET_ATTR_METD_DESC = 0x9,
ATTR_METD_DESC = 0xa,
MODULE_SHUTDOWN = 0xb,
SERVER_SHUTDOWN = 0xc,
PEER_SERVER_DISCOVERY = 0xd, // TODO: to be implemented.
PEER_SERVER_MSG = 0xe
} PyConnectMsg;
typedef enum {
NO_ERRORS = 0x0,
NO_ATTR_METD = 0x1,
STR_TOO_LONG = 0x2,
INDEX_MISMATCH = 0x4,
NO_PYCONNECT_OBJECT = 0x8,
METD_EXCEPTION = 0xa,
MSG_CORRUPTED = 0xf
} PyConnectMsgStatus;
struct PyConnectType {
typedef enum {
INT = 0,
FLOAT = 1,
DOUBLE = 2, // same as float for Python in fact.
STRING = 3,
BOOL = 4,
COMPOSITE = 5,
PyVOID = 6 // silly window compile breaks if defined as VOID
} Type;
static const std::string typeName( const Type type );
static const Type typeName( const char * type );
#ifdef PYTHON_SERVER
static int validateTypeAndSize( PyObject * obj, Type type );
static PyObject * defaultValue( Type type );
static void packToStr( PyObject * arg, Type type, unsigned char * & dataPtr );
static PyObject * unpackStr( Type type, unsigned char * & dataPtr, int & remainingLength );
#endif
};
#ifndef PYTHON_SERVER
template <typename T>
struct is_string
{
static constexpr bool value = false;
};
template <class T, class Traits, class Alloc>
struct is_string<std::basic_string<T, Traits, Alloc>>
{
static constexpr bool value = true;
};
template <typename T>
struct is_supported : std::false_type {};
// Specialisations for supported types:
template <> struct is_supported<int> : std::true_type {};
template <> struct is_supported<float> : std::true_type {};
template <> struct is_supported<double> : std::true_type {};
template <> struct is_supported<bool> : std::true_type {};
template <> struct is_supported<void> : std::true_type {};
template <> struct is_supported<std::string> : std::true_type {};
template <typename T, typename std::enable_if<is_supported<T>{}, int>::type = 0>
struct pyconnect_type
{
static constexpr PyConnectType::Type value = PyConnectType::COMPOSITE;
};
template <> struct pyconnect_type<void> { static constexpr PyConnectType::Type value = PyConnectType::PyVOID; };
template <> struct pyconnect_type<bool> { static constexpr PyConnectType::Type value = PyConnectType::BOOL; };
template <> struct pyconnect_type<int> { static constexpr PyConnectType::Type value = PyConnectType::INT; };
template <> struct pyconnect_type<float> { static constexpr PyConnectType::Type value = PyConnectType::FLOAT; };
template <> struct pyconnect_type<double> { static constexpr PyConnectType::Type value = PyConnectType::DOUBLE; };
template <> struct pyconnect_type<std::string> { static constexpr PyConnectType::Type value = PyConnectType::STRING; };
template <typename T> static const PyConnectType::Type getVarType( const T& val )
{
#ifndef _MSC_VER
int status = 0;
char * demangled = abi::__cxa_demangle( typeid(val).name(), 0, 0, &status );
#else
char * demangled = typeid(val).name();
#endif
ERROR_MSG( "PyConnect::getVarType: c++ data type %s is not supported", demangled );
return PyConnectType::COMPOSITE;
}
template <typename T> static const char * getVarTypeName( const T& val )
{
#ifndef _MSC_VER
int status = 0;
char * demangled = abi::__cxa_demangle( typeid(val).name(), 0, 0, &status );
#else
char * demangled = typeid(val).name();
#endif
ERROR_MSG( "PyConnect::getVarTypeName: c++ data type %s is not supported", demangled );
return "";
}
template <> const PyConnectType::Type getVarType( const int& )
{
return PyConnectType::INT;
}
template <> const PyConnectType::Type getVarType( const float& )
{
return PyConnectType::FLOAT;
}
template <> const PyConnectType::Type getVarType( const double& )
{
return PyConnectType::DOUBLE;
}
template <> const PyConnectType::Type getVarType( const bool& )
{
return PyConnectType::BOOL;
}
template <> const PyConnectType::Type getVarType( const std::string & )
{
return PyConnectType::STRING;
}
template <> const char * getVarTypeName( const int& )
{
return "integer";
}
template <> const char * getVarTypeName( const float& )
{
return "float";
}
template <> const char * getVarTypeName( const double& )
{
return "double";
}
template <> const char * getVarTypeName( const bool& )
{
return "boolean";
}
template <> const char * getVarTypeName( const std::string& )
{
return "string";
}
template<class F>
struct function_traits;
// function pointer
template<class R, typename... Args>
struct function_traits<std::function<R(*)(Args...)>> : public function_traits<std::function<R(Args...)>>
{};
template<class R, class... Args>
struct function_traits<std::function<R(Args...)> >
{
using return_type = R;
static constexpr std::size_t arity = sizeof...(Args);
template <std::size_t N>
struct argument
{
static_assert(N < arity, "error: invalid parameter index.");
using type = typename std::tuple_element<N,std::tuple<Args...>>::type;
};
};
// member function pointer
template<class C, class R, class... Args>
struct function_traits<std::function<R(C::*)(Args...)>> : public function_traits<R(C&,Args...)>
{};
// const member function pointer
template<class C, class R, class... Args>
struct function_traits<std::function<R(C::*)(Args...)> const> : public function_traits<R(C&,Args...)>
{};
template<class C, class R, class... Args>
struct function_traits<R(C&,Args...)>
{
using return_type = R;
using class_type = C;
static constexpr std::size_t arity = sizeof...(Args);
template <std::size_t N>
struct argument
{
static_assert(N < arity, "error: invalid parameter index.");
using type = typename std::tuple_element<N,std::tuple<Args...>>::type;
};
};
template <size_t ArgIdx, typename R, typename... Args>
auto get_arg_types( std::vector<int> & atlist )
-> typename std::enable_if<ArgIdx == 1>::type
{
using fun = function_traits<std::function<R(Args...)>>;
atlist.push_back(pyconnect::pyconnect_type<typename std::decay<typename fun::template argument<0>::type>::type>::value);
}
template <size_t ArgIdx, typename R, typename... Args>
auto get_arg_types( std::vector<int> & atlist )
-> typename std::enable_if<(ArgIdx != 1)>::type
{
using fun = function_traits<std::function<R(Args...)>>;
atlist.push_back(pyconnect::pyconnect_type<typename std::decay<typename fun::template argument<ArgIdx-1>::type>::type>::value);
get_arg_types<ArgIdx - 1, R, Args...>(atlist);
}
template <typename F, typename std::enable_if<(F::arity > 0), int>::type = 0, std::size_t... Is>
void get_args_type_list(std::index_sequence<Is...>, std::vector<int> & rtl)
{
get_arg_types<F::arity, typename F::return_type, typename F::template argument<Is>::type...>(rtl);
}
template <typename F, typename std::enable_if<(F::arity == 0), int>::type = 0, std::size_t... Is>
void get_args_type_list(std::index_sequence<Is...>, std::vector<int> & rtl)
{
}
#endif
template <typename DataType> static int packToLENumber( const DataType & v, unsigned char * & dataPtr )
{
// TODO: optimise this operation
int dataLength = sizeof( DataType );
#ifdef WITH_BIG_ENDIAN
char * valPtr = (char *)&v;
for (int i = dataLength-1;i >= 0;i--) {
// for (int i = 0;i < dataLength;i++) {
// *dataPtr = (char)( (v >> (8*(dataLength - i - 1))) & 0xff );
*dataPtr = (*(valPtr+i) & 0xFF);
dataPtr++;
}
#else
memcpy( dataPtr, (char *)&v, dataLength );
dataPtr += dataLength;
#endif
return dataLength;
}
template <typename DataType> static void unpackLENumber( DataType & val, unsigned char * & dataPtr, int & remainingLength )
{
// TODO: optimise this operation
int dataLength = sizeof( DataType );
#ifdef WITH_BIG_ENDIAN
// convert to Big Endian
char * valPtr = (char *)&val;
for (int i = dataLength-1;i >= 0;i--) {
*(valPtr+i) = (*dataPtr & 0xFF);
dataPtr++;
}
#else
memcpy( (char *)&val, dataPtr, dataLength );
dataPtr += dataLength;
#endif
remainingLength -= dataLength;
}
void packString( unsigned char * str, int length, unsigned char * & dataBufPtr,
bool extendSize = false );
std::string unpackString( unsigned char * & dataBufPtr, int & remainingBytes,
bool extendSize = false );
int unpackStrToInt( unsigned char * & str, int & remainingBytes );
int packIntToStr( int num, unsigned char * & str );
int packedIntLen( int num );
#ifdef __cplusplus
extern "C" {
#endif
unsigned char * decodeBase64( char * input, size_t * outLen );
char * encodeBase64( const unsigned char * input, size_t length );
void endecryptInit();
void endecryptFini();
int decryptMessage( const unsigned char * origMesg, int origMesgLength, unsigned char ** decryptedMesg, int * decryptedMesgLength );
int encryptMessage( const unsigned char * origMesg, int origMesgLength, unsigned char ** encryptedMesg, int * encryptedMesgLength );
int secureSHA256Hash( const unsigned char * password, const int pwlen, unsigned char * code );
#ifdef __cplusplus
}
#endif
} // namespace pyconnect
#endif // PyConnectCommon_h_DEFINED