-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathudt_py.cxx
1418 lines (1208 loc) · 34 KB
/
udt_py.cxx
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Python.h>
#include <iostream>
#include <memory>
#include <arpa/inet.h>
#include <set>
#include "udt_py.h"
PyObject* pyudt_socket_accept(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject* pyudt_epoll_add_usock(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject* pyudt_epoll_remove_usock(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject* pyudt_epoll_add_ssock(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject* pyudt_epoll_remove_ssock(PyObject *self, PyObject *args, PyObject *kwargs);
static PyObject* pyudt_epoll_wait(PyObject *self, PyObject *args, PyObject *kwargs);
#define PY_TRY_CXX \
try \
{
#define PY_CATCH_CXX(ret_val) \
} \
catch(py_udt_error& e) \
{ \
try \
{ \
UDT::ERRORINFO& udt_err = UDT::getlasterror(); \
PyErr_SetString(PyExc_RuntimeError, udt_err.getErrorMessage()); \
UDT::getlasterror().clear(); \
return ret_val; \
} \
catch(...)\
{\
PyErr_SetString(PyExc_RuntimeError, "UDT error"); \
UDT::getlasterror().clear(); \
return ret_val; \
}\
} \
catch(std::exception& e) \
{ \
PyErr_SetString(PyExc_RuntimeError, e.what()); \
return ret_val; \
} \
catch(...) \
{\
PyErr_SetString(PyExc_RuntimeError, "C++ error"); \
return ret_val; \
}
AutoDecref::AutoDecref(PyObject *ptr)
{
this->ptr = ptr;
}
AutoDecref::~AutoDecref()
{
if(this->ptr != NULL)
{
Py_DECREF(ptr);
}
}
void AutoDecref::ok()
{
this->ptr = NULL;
}
AutoGILCallOut::AutoGILCallOut()
{
state = PyEval_SaveThread();
}
AutoGILCallOut::~AutoGILCallOut()
{
PyEval_RestoreThread(state);
}
AutoGILCallBack::AutoGILCallBack()
{
state = PyGILState_Ensure();
}
AutoGILCallBack::~AutoGILCallBack()
{
PyGILState_Release(state);
}
PyObject* pyudt_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
if(!PyArg_ParseTuple(args, ""))
{
return NULL;
}
if (UDT::cleanup() == UDT::ERROR)
{
throw py_udt_error();
}
Py_RETURN_NONE;
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_startup(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
if(!PyArg_ParseTuple(args, ""))
{
return NULL;
}
if (UDT::startup() == UDT::ERROR)
{
throw py_udt_error();
}
Py_RETURN_NONE;
PY_CATCH_CXX(NULL)
}
RecvBuffer::RecvBuffer(unsigned int len)
{
head = new char[len + 1];
buf_len = 0;
max_buf_len = len;
head[max_buf_len] = '\0';
}
RecvBuffer::~RecvBuffer()
{
delete[] head;
}
char *RecvBuffer::get_head()
{
return head;
}
unsigned int RecvBuffer::get_max_len()
{
return max_buf_len;
}
unsigned int RecvBuffer::get_buf_len()
{
return buf_len;
}
unsigned int RecvBuffer::set_buf_len(unsigned int new_len)
{
/* FIXME - overflow check required */
buf_len = new_len;
head[buf_len] = '\0';
return buf_len;
}
int pyudt_epoll_init(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
if(!PyArg_ParseTuple(args, ""))
{
return -1;
}
pyudt_epoll_object* py_epoll = ((pyudt_epoll_object*)self);
py_epoll->eid = UDT::epoll_create();
if(py_epoll->eid < 0)
{
throw py_udt_error();
}
return 0;
PY_CATCH_CXX(-1)
}
PyObject* pyudt_epoll_release(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
if(!PyArg_ParseTuple(args, ""))
{
return NULL;
}
if(UDT::epoll_release(((pyudt_epoll_object*)self)->eid))
{
throw py_udt_error();
}
Py_RETURN_NONE;
PY_CATCH_CXX(NULL)
}
static PyObject* pyudt_epoll_get_eid(PyObject *py_epoll)
{
return Py_BuildValue("i", ((pyudt_epoll_object*)py_epoll)->eid);
}
static PyGetSetDef pyudt_epoll_getset[] = {
{
(char*)"eid",
(getter)pyudt_epoll_get_eid,
NULL,
(char*)"get epoll id",
NULL
},
{NULL} /* Sentinel */
};
static PyMethodDef pyudt_epoll_methods[] = {
{
"release",
(PyCFunction)pyudt_epoll_release,
METH_VARARGS,
"epoll release"
},
{
"add_usock",
(PyCFunction)pyudt_epoll_add_usock,
METH_VARARGS,
"add udt socket to epoll"
},
{
"add_ssock",
(PyCFunction)pyudt_epoll_add_ssock,
METH_VARARGS,
"add system socket to epoll"
},
{
"remove_usock",
(PyCFunction)pyudt_epoll_remove_usock,
METH_VARARGS,
"remove udt socket from epoll"
},
{
"remove_ssock",
(PyCFunction)pyudt_epoll_remove_ssock,
METH_VARARGS,
"remove system socket from epoll"
},
{
"epoll_wait",
(PyCFunction)pyudt_epoll_wait,
METH_VARARGS,
"wait on a epoll events"
},
{NULL} /* Sentinel */
};
static PyTypeObject pyudt_epoll_type = {
PyObject_HEAD_INIT(NULL)
"_udt.socket", /* tp_name */
sizeof(pyudt_socket_object), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/* tp_flags */
"UDT epoll", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
pyudt_epoll_methods, /* tp_methods */
0, /* tp_members */
pyudt_epoll_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)pyudt_epoll_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
static PyObject* pyudt_socket_get_family(PyObject *py_socket)
{
return Py_BuildValue("i", ((pyudt_socket_object*)py_socket)->family);
}
static PyObject* pyudt_socket_get_type(PyObject *py_socket)
{
return Py_BuildValue("i", ((pyudt_socket_object*)py_socket)->type);
}
static PyObject* pyudt_socket_get_proto(PyObject *py_socket)
{
return Py_BuildValue("i", ((pyudt_socket_object*)py_socket)->proto);
}
PyObject* pyudt_socket_connect(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
char *address;
int port = 0;
sockaddr_in serv_addr;
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
if(!PyArg_ParseTuple(args, "(si)", &address, &port))
{
return NULL;
}
serv_addr.sin_family = py_socket->family;
serv_addr.sin_port = htons(port);
int res = inet_pton(py_socket->family, address, &serv_addr.sin_addr);
if(res == 0)
{
PyErr_SetString(PyExc_ValueError, "bad address");
return NULL;
}
else if(res == -1 && errno == EAFNOSUPPORT)
{
PyErr_SetString(PyExc_ValueError, "address family not supported");
return NULL;
}
memset(&(serv_addr.sin_zero), '\0', 8);
if (UDT::ERROR == UDT::connect(
py_socket->cc_socket,
(sockaddr*)&serv_addr, sizeof(serv_addr)))
{
throw py_udt_error();
}
Py_RETURN_NONE;
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_socket_bind(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
char *address;
int port;
sockaddr_in serv_addr;
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
if(!PyArg_ParseTuple(args, "(si)", &address, &port))
{
return NULL;
}
int res = inet_pton(py_socket->family, address, &serv_addr.sin_addr);
serv_addr.sin_family = py_socket->family;
serv_addr.sin_port = htons(port);
if(res == 0)
{
PyErr_SetString(PyExc_ValueError, "bad address");
return NULL;
}
else if(res == -1 && errno == EAFNOSUPPORT)
{
PyErr_SetString(PyExc_ValueError, "address family not supported");
return NULL;
}
memset(&(serv_addr.sin_zero), '\0', 8);
if (UDT::ERROR == UDT::bind(
py_socket->cc_socket,
(sockaddr*)&serv_addr, sizeof(serv_addr)))
{
throw py_udt_error();
}
Py_RETURN_NONE;
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_socket_close(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
if(UDT::close(((pyudt_socket_object*)self)->cc_socket))
{
throw py_udt_error();
}
Py_RETURN_NONE;
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_socket_listen(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
int backlog;
if(!PyArg_ParseTuple(args, "i", &backlog))
{
return NULL;
}
if(UDT::listen(((pyudt_socket_object*)self)->cc_socket, backlog))
{
throw py_udt_error();
}
Py_RETURN_NONE;
PY_CATCH_CXX(NULL)
}
int pyudt_socket_init(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
int family;
int type;
int proto;
if(!PyArg_ParseTuple(args, "iii", &family, &type, &proto))
{
return -1;
}
if(family != AF_INET)
{
PyErr_SetString(PyExc_ValueError, "UDT only supports AF_INET addresses");
return -1;
}
UDTSOCKET cc_socket = UDT::socket(family, type, proto);
if(cc_socket == -1)
{
PyErr_SetString(PyExc_RuntimeError, "socket creation returned -1");
return -1;
}
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
py_socket->cc_socket = cc_socket;
py_socket->family = family;
py_socket->type = type;
py_socket->proto = proto;
return 0;
PY_CATCH_CXX(-1)
}
PyObject* pyudt_socket_send(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
char* buf;
int buf_len;
int flags;
int send_len = 0;
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
if(!PyArg_ParseTuple(args, "y#i", &buf, &buf_len, &flags))
{
return NULL;
}
{
AutoGILCallOut g;
send_len = UDT::send(py_socket->cc_socket, buf, buf_len, flags);
}
if(send_len == UDT::ERROR)
{
throw py_udt_error();
}
return Py_BuildValue("i", send_len);
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_socket_sendmsg(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
char* buf;
int buf_len;
int ttl;
int send_len = 0;
int in_order = 0;
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
if(!PyArg_ParseTuple(args, "y#ii", &buf, &buf_len, &ttl, &in_order))
{
return NULL;
}
send_len = UDT::sendmsg(py_socket->cc_socket, buf, buf_len, ttl, in_order);
if(send_len == UDT::ERROR)
{
throw py_udt_error();
}
return Py_BuildValue("i", send_len);
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_socket_recv(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
int len;
int flags;
int recv_len = 0;
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
if(!PyArg_ParseTuple(args, "ii", &len, &flags))
{
return NULL;
}
RecvBuffer recv_buf(len);
recv_len = UDT::recv(py_socket->cc_socket, recv_buf.get_head(), len, flags);
if(recv_len == UDT::ERROR)
{
throw py_udt_error();
}
recv_buf.set_buf_len(recv_len);
return Py_BuildValue("y#", recv_buf.get_head(), recv_len);
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_socket_recvmsg(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
int len;
int recv_len = 0;
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
if(!PyArg_ParseTuple(args, "i", &len))
{
return NULL;
}
RecvBuffer recv_buf(len);
recv_len = UDT::recvmsg(py_socket->cc_socket, recv_buf.get_head(), len);
if(recv_len == UDT::ERROR)
{
throw py_udt_error();
}
recv_buf.set_buf_len(recv_len);
return Py_BuildValue("y#", recv_buf.get_head(), recv_len);
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_socket_perfmon(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
int clear = 0;
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
if(!PyArg_ParseTuple(args, "i", &clear))
{
return NULL;
}
UDT::TRACEINFO info;
if(UDT::perfmon(py_socket->cc_socket, &info, clear) == UDT::ERROR)
{
throw py_udt_error();
}
/* Slow */
return Py_BuildValue(
"{sLsLsLsisisisisisisisLsLsisisisisisisisdsdsdsisisisdsdsisi}",
/* Aggregate Values */
"msTimeStamp",
info.msTimeStamp,
"pktSentTotal",
info.pktSentTotal,
"pktRecvTotal",
info.pktRecvTotal,
"pktSndLossTotal",
info.pktSndLossTotal,
"pktRcvLossTotal",
info.pktRcvLossTotal,
"pktRetransTotal",
info.pktRetransTotal,
"pktSentACKTotal",
info.pktSentACKTotal,
"pktRecvACKTotal",
info.pktRecvACKTotal,
"pktSentNAKTotal",
info.pktSentNAKTotal,
"pktRecvNAKTotal",
info.pktRecvNAKTotal,
/* Local Values */
"pktSent",
info.pktSent,
"pktRecv",
info.pktRecv,
"pktSndLoss",
info.pktSndLoss,
"pktRcvLoss",
info.pktRcvLoss,
"pktRetrans",
info.pktRetrans,
"pktSentACK",
info.pktSentACK,
"pktRecvACK",
info.pktRecvACK,
"pktSentNAK",
info.pktSentNAK,
"pktRecvNAK",
info.pktRecvNAK,
"mbpsSendRate",
info.mbpsSendRate,
"mbpsRecvRate",
info.mbpsRecvRate,
/* Instant Values */
"usPktSndPeriod",
info.usPktSndPeriod,
"pktFlowWindow",
info.pktFlowWindow,
"pktCongestionWindow",
info.pktCongestionWindow,
"pktFlightSize",
info.pktFlightSize,
"msRTT",
info.msRTT,
"mbpsBandwidth",
info.mbpsBandwidth,
"byteAvailSndBuf",
info.byteAvailSndBuf,
"byteAvailRcvBuf",
info.byteAvailRcvBuf
);
PY_CATCH_CXX(NULL)
}
/*
It looks like UDT::getsockopt doesn't check the buffer length.
The big switch makes it easier to call from Python and prevents segfaults.
*/
PyObject* pyudt_socket_getsockopt(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
int level = 0;
int opt_name = 0;
int opt_len = 0;
if(!PyArg_ParseTuple(args, "ii", &level, &opt_name))
{
return NULL;
}
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
UDT::SOCKOPT switch_opt = (UDT::SOCKOPT)opt_name;
switch(switch_opt)
{
case UDT_MSS:
case UDT_FC:
case UDT_SNDBUF:
case UDT_RCVBUF:
case UDP_SNDBUF:
case UDP_RCVBUF:
case UDT_SNDTIMEO:
case UDT_RCVTIMEO:
{
int opt_val = 0;
if (UDT::getsockopt(
py_socket->cc_socket,
level, switch_opt, &opt_val, &opt_len) == UDT::ERROR)
{
throw py_udt_error();
}
return Py_BuildValue("i", opt_val);
break;
}
case UDT_SNDSYN:
case UDT_RCVSYN:
case UDT_RENDEZVOUS:
case UDT_REUSEADDR:
{
bool opt_val = true;
if (UDT::getsockopt(py_socket->cc_socket,
level, switch_opt, &opt_val, &opt_len) == UDT::ERROR)
{
throw py_udt_error();
}
if(opt_val)
{
Py_RETURN_TRUE;
}
else
{
Py_RETURN_FALSE;
}
break;
}
case UDT_CC:
{
PyErr_SetString(PyExc_NotImplementedError, "UTC_CC not implemented yet");
return NULL;
break;
}
case UDT_LINGER:
{
linger opt_val;
opt_val.l_onoff = 0;
opt_val.l_linger = 0;
opt_len = sizeof(linger);
if (UDT::getsockopt(py_socket->cc_socket,
level, switch_opt, &opt_val, &opt_len) == UDT::ERROR)
{
throw py_udt_error();
}
return Py_BuildValue("(ii)", opt_val.l_onoff, opt_val.l_linger);
break;
}
case UDT_MAXBW:
{
int64_t opt_val;
if (UDT::getsockopt(py_socket->cc_socket,
level, switch_opt, &opt_val, &opt_len) == UDT::ERROR)
{
throw py_udt_error();
}
return Py_BuildValue("l", opt_val);
break;
}
default:
{
PyErr_SetString(PyExc_ValueError, "unknown socket option");
return NULL;
}
}
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_socket_setsockopt(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
int level = 0;
int opt_name = 0;
PyObject *opt_obj = NULL;
if(!PyArg_ParseTuple(args, "iiO", &level, &opt_name, &opt_obj))
{
return NULL;
}
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
UDT::SOCKOPT switch_opt = (UDT::SOCKOPT)opt_name;
switch(switch_opt)
{
case UDT_MSS:
case UDT_FC:
case UDT_SNDBUF:
case UDT_RCVBUF:
case UDP_SNDBUF:
case UDP_RCVBUF:
case UDT_SNDTIMEO:
case UDT_RCVTIMEO:
{
int opt_val = 0;
if(!PyArg_ParseTuple(args, "iii", &level, &opt_name, &opt_val))
{
return NULL;
}
if (UDT::setsockopt(
py_socket->cc_socket,
level, switch_opt, &opt_val, sizeof(int)) == UDT::ERROR)
{
throw py_udt_error();
}
Py_RETURN_NONE;
break;
}
case UDT_SNDSYN:
case UDT_RCVSYN:
case UDT_RENDEZVOUS:
case UDT_REUSEADDR:
{
bool opt_val = true;
if(opt_obj == Py_True)
{
opt_val = true;
}
else if(opt_obj == Py_False)
{
opt_val = false;
}
else
{
PyErr_SetString(PyExc_TypeError, "option must be boolean");
return NULL;
}
if (UDT::setsockopt(py_socket->cc_socket,
level, switch_opt, &opt_val, sizeof(bool)) == UDT::ERROR)
{
throw py_udt_error();
}
Py_RETURN_NONE;
break;
}
case UDT_CC:
{
PyErr_SetString(PyExc_NotImplementedError, "UTC_CC not implemented yet");
return NULL;
break;
}
case UDT_LINGER:
{
linger opt_val;
int opt_onoff;
int opt_linger;
if(!PyArg_ParseTuple(args, "ii(ii)", &level, &opt_name, &opt_onoff, &opt_linger))
{
return NULL;
}
opt_val.l_onoff = opt_onoff;
opt_val.l_linger = opt_linger;
if (UDT::setsockopt(py_socket->cc_socket,
level, switch_opt, &opt_val, sizeof(linger)) == UDT::ERROR)
{
throw py_udt_error();
}
Py_RETURN_NONE;
break;
}
case UDT_MAXBW:
{
int64_t opt_val;
if(!PyArg_ParseTuple(args, "iil", &level, &opt_name, &opt_val))
{
return NULL;
}
if (UDT::setsockopt(py_socket->cc_socket,
level, switch_opt, &opt_val, sizeof(int64_t)) == UDT::ERROR)
{
throw py_udt_error();
}
return Py_BuildValue("l", opt_val);
break;
}
default:
{
PyErr_SetString(PyExc_ValueError, "unknown socket option");
return NULL;
}
}
PY_CATCH_CXX(NULL)
}
PyObject* pyudt_socket_fileno(PyObject *self, PyObject *args, PyObject *kwargs)
{
PY_TRY_CXX
pyudt_socket_object* py_socket = ((pyudt_socket_object*)self);
if(!PyArg_ParseTuple(args, ""))
{
return NULL;
}
return Py_BuildValue("l", py_socket->cc_socket);
PY_CATCH_CXX(NULL)
}
static PyMethodDef pyudt_socket_methods[] = {
{
"accept",
(PyCFunction)pyudt_socket_accept,
METH_VARARGS,
"socket accept"
},
{
"connect",
(PyCFunction)pyudt_socket_connect,
METH_VARARGS,
"socket connect"
},
{
"close",
(PyCFunction)pyudt_socket_close,
METH_VARARGS,
"close the socket"
},
{
"bind",
(PyCFunction)pyudt_socket_bind,
METH_VARARGS,
"socket bind"
},
{
"listen",
(PyCFunction)pyudt_socket_listen,
METH_VARARGS,
"listen with backlog"
},
{
"send",
(PyCFunction)pyudt_socket_send,
METH_VARARGS,
"send data"
},
{
"sendmsg",
(PyCFunction)pyudt_socket_sendmsg,
METH_VARARGS,
"send msg"
},
{
"recv",
(PyCFunction)pyudt_socket_recv,
METH_VARARGS,
"recv data"
},
{
"recvmsg",
(PyCFunction)pyudt_socket_recvmsg,
METH_VARARGS,
"recv msg"
},
{
"perfmon",
(PyCFunction)pyudt_socket_perfmon,
METH_VARARGS,
"get perfmon stats"
},
{
"getsockopt",
(PyCFunction)pyudt_socket_getsockopt,
METH_VARARGS,
"get socket options"
},
{
"setsockopt",
(PyCFunction)pyudt_socket_setsockopt,
METH_VARARGS,
"set socket options"
},
{
"fileno",
(PyCFunction)pyudt_socket_fileno,
METH_VARARGS,
"get socket file descriptor"
},
{NULL} /* Sentinel */
};
static PyGetSetDef pyudt_socket_getset[] = {
{
(char*)"family",
(getter)pyudt_socket_get_family,
NULL,
(char*)"address family",
NULL
},
{
(char*)"type",
(getter)pyudt_socket_get_type,