-
Notifications
You must be signed in to change notification settings - Fork 19
/
neat_swig.i
193 lines (162 loc) · 6.05 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
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
/* NEAT declarations for SWIG */
%module neat
%include "stdint.i" /* Convert uintXX_t correctly */
%include "typemaps.i"
%include "cpointer.i"
%include "carrays.i"
%{
#include "neat.h"
%}
// This input typemap declares that char** requires no input parameter.
// Instead, the address of a local char* is used to call the function.
%typemap(in,numinputs=0) char** (char* tmp) %{
$1 = &tmp;
%}
// After the function is called, the char** parameter contains a malloc'ed char* pointer.
// Construct a Python Unicode object (I'm using Python 3) and append it to
// any existing return value for the wrapper.
%typemap(argout) char** (PyObject* obj) %{
obj = PyUnicode_FromString(*$1);
$result = SWIG_Python_AppendOutput($result,obj);
%}
// The malloc'ed pointer is no longer needed, so make sure it is freed.
%typemap(freearg) char** %{
free(*$1);
%}
%apply int *OUTPUT { int *send, int *recv};
%inline %{
extern void neat_get_max_buffer_sizes(struct neat_flow *flow, int *send, int *recv);
%}
%clear int *send;
%clear int *recv;
%{
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_parameters;
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 neat_error_code disp_on_parameters(struct neat_flow_operations *ops) {
return dispatch_fx(ops, py_callbacks.on_parameters);
}
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);
}
//%typemap(in) (void *) {
// $1 = (void *) $input;
//};
%pointer_functions(uint32_t, uint32_tp);
%pointer_functions(size_t, size_tp);
%array_class(unsigned char, charArr);
%include "neat.h"