forked from NEAT-project/neat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathneat_swig.i
148 lines (128 loc) · 4.85 KB
/
neat_swig.i
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
/* NEAT declarations for SWIG */
%module neat
%include "stdint.i" /* Convert uintXX_t correctly */
%include "typemaps.i"
%{
#include "neat.h"
%}
%{
static struct {
PyObject *on_connected;
PyObject *on_error;
PyObject *on_readable;
PyObject *on_writable;
PyObject *on_all_written;
PyObject *on_network_status_changed;
PyObject *on_aborted;
PyObject *on_timeout;
PyObject *on_close;
PyObject *on_send_failure;
PyObject *on_slowdown;
PyObject *on_rate_hint;
} py_callbacks ;
static neat_error_code dispatch_fx(struct neat_flow_operations *ops, PyObject *pyfunc) {
PyObject *pyops = SWIG_NewPointerObj(SWIG_as_voidptr(ops), SWIGTYPE_p_neat_flow_operations, 0 | 0 );
PyObject *res = PyObject_CallFunctionObjArgs(pyfunc, pyops, NULL);
unsigned long val = PyLong_AsUnsignedLong(res);
return (neat_error_code)(val);
}
static neat_error_code disp_on_connected(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_connected);
}
static neat_error_code disp_on_error(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_error);
}
static neat_error_code disp_on_readable(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_readable);
}
static neat_error_code disp_on_writable(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_writable);
}
static neat_error_code disp_on_all_written(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_all_written);
}
static neat_error_code disp_on_network_status_changed(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_network_status_changed);
}
static neat_error_code disp_on_aborted(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_aborted);
}
static neat_error_code disp_on_timeout(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_timeout);
}
static neat_error_code disp_on_close(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_close);
}
static void dispatch_send_failure(struct neat_flow_operations *ops, int context, const unsigned char *unsent) {
PyObject *pyfunc = py_callbacks.on_send_failure;
PyObject *pyops = SWIG_NewPointerObj(SWIG_as_voidptr(ops), SWIGTYPE_p_neat_flow_operations, 0 | 0 );
PyObject *pyctx = PyInt_FromLong(context);
PyObject *pymsg = PyString_FromString((const char *) unsent);
PyObject_CallFunctionObjArgs(pyfunc, pyops, pyctx, pymsg, NULL);
}
static void dispatch_slowdown(struct neat_flow_operations *ops, int ecn, uint32_t rate) {
PyObject *pyfunc = py_callbacks.on_slowdown;
PyObject *pyops = SWIG_NewPointerObj(SWIG_as_voidptr(ops), SWIGTYPE_p_neat_flow_operations, 0 | 0 );
PyObject *pyecn = PyInt_FromLong(ecn);
PyObject *pyrate = PyInt_FromLong(rate);
PyObject_CallFunctionObjArgs(pyfunc, pyops, pyecn, pyrate, NULL);
}
static void dispatch_rate_hint(struct neat_flow_operations *ops, uint32_t rate) {
PyObject *pyfunc = py_callbacks.on_slowdown;
PyObject *pyops = SWIG_NewPointerObj(SWIG_as_voidptr(ops), SWIGTYPE_p_neat_flow_operations, 0 | 0 );
PyObject *pyrate = PyInt_FromLong(rate);
PyObject_CallFunctionObjArgs(pyfunc, pyops, pyrate, NULL);
}
%}
%typemap(in) neat_flow_operations_fx {
if ($input == Py_None) { /* Unset a callback function */
$1 = NULL;
py_callbacks.$1_name = NULL;
} else if (!PyCallable_Check($input)) {
PyErr_SetString(PyExc_TypeError, "Need a callable object!");
return NULL;
} else {
$1 = disp_$1_name;
py_callbacks.$1_name = $input;
}
}
%typemap(in) neat_cb_send_failure_t {
if ($input == Py_None) { /* Unset a callback function */
$1 = NULL;
py_callbacks.$1_name = NULL;
} else if (!PyCallable_Check($input)) {
PyErr_SetString(PyExc_TypeError, "Need a callable object!");
return NULL;
} else {
$1 = dispatch_send_failure;
py_callbacks.$1_name = $input;
}
}
%typemap(in) neat_cb_flow_slowdown_t {
if ($input == Py_None) { /* Unset a callback function */
$1 = NULL;
py_callbacks.$1_name = NULL;
} else if (!PyCallable_Check($input)) {
PyErr_SetString(PyExc_TypeError, "Need a callable object!");
return NULL;
} else {
$1 = dispatch_slowdown;
py_callbacks.$1_name = $input;
}
}
%typemap(in) neat_cb_flow_rate_hint_t {
if ($input == Py_None) { /* Unset a callback function */
$1 = NULL;
py_callbacks.$1_name = NULL;
} else if (!PyCallable_Check($input)) {
PyErr_SetString(PyExc_TypeError, "Need a callable object!");
return NULL;
} else {
$1 = dispatch_rate_hint;
py_callbacks.$1_name = $input;
}
}
%typemap(in) const unsigned char *buffer {
$1 = (unsigned char*) PyString_AsString($input);
}
%include "neat.h"