-
Notifications
You must be signed in to change notification settings - Fork 30
/
enet.pyx
1094 lines (857 loc) · 33.3 KB
/
enet.pyx
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
import atexit
from cpython cimport bool
from libc.stdint cimport uintptr_t
cdef extern from "enet/types.h":
ctypedef unsigned char enet_uint8
ctypedef unsigned short enet_uint16
ctypedef unsigned int enet_uint32
ctypedef unsigned int size_t
cdef extern from "enet/enet.h":
ctypedef enet_uint32 ENetVersion
# forward declaration
ctypedef struct ENetPeer
ctypedef struct ENetHost
cdef enum:
ENET_HOST_ANY = 0
ENET_HOST_BROADCAST = 0xFFFFFFFF
ENET_PORT_ANY = 0
ctypedef int ENetSocket
ctypedef struct ENetBuffer:
void *data
size_t dataLength
ctypedef struct ENetEvent
ctypedef enet_uint32 (__cdecl *ENetChecksumCallback) (const ENetBuffer * buffers, size_t bufferCount)
ctypedef int (__cdecl *ENetInterceptCallback) (ENetHost *host, ENetEvent *event) except -1 # __cdecl is standard on unix and overwriten for win32
ctypedef struct ENetAddress:
enet_uint32 host
enet_uint16 port
ctypedef enum ENetPacketFlag:
ENET_PACKET_FLAG_RELIABLE = (1 << 0)
ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1)
ENET_PACKET_FLAG_NO_ALLOCATE = (1 << 2)
ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT = (1 << 3)
ctypedef struct ENetPacket:
size_t referenceCount
enet_uint32 flags
enet_uint8 *data
size_t dataLength
ctypedef enum ENetPeerState:
ENET_PEER_STATE_DISCONNECTED = 0
ENET_PEER_STATE_CONNECTING = 1
ENET_PEER_STATE_ACKNOWLEDGING_CONNECT = 2
ENET_PEER_STATE_CONNECTION_PENDING = 3
ENET_PEER_STATE_CONNECTION_SUCCEEDED = 4
ENET_PEER_STATE_CONNECTED = 5
ENET_PEER_STATE_DISCONNECT_LATER = 6
ENET_PEER_STATE_DISCONNECTING = 7
ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT = 8
ENET_PEER_STATE_ZOMBIE = 9
ctypedef struct ENetPeer:
ENetHost *host
enet_uint16 outgoingPeerID
enet_uint16 incomingPeerID
enet_uint32 connectID
enet_uint8 outgoingSessionID
enet_uint8 incomingSessionID
ENetAddress address
char *data
ENetPeerState state
size_t channelCount
enet_uint32 incomingBandwidth
enet_uint32 outgoingBandwidth
enet_uint32 incomingBandwidthThrottleEpoch
enet_uint32 outgoingBandwidthThrottleEpoch
enet_uint32 incomingDataTotal
enet_uint32 outgoingDataTotal
enet_uint32 lastSendTime
enet_uint32 lastReceiveTime
enet_uint32 nextTimeout
enet_uint32 earliestTimeout
enet_uint32 packetLossEpoch
enet_uint32 packetsSent
enet_uint32 packetsLost
enet_uint32 packetLoss
enet_uint32 packetLossVariance
enet_uint32 packetThrottle
enet_uint32 packetThrottleLimit
enet_uint32 packetThrottleCounter
enet_uint32 packetThrottleEpoch
enet_uint32 packetThrottleAcceleration
enet_uint32 packetThrottleDeceleration
enet_uint32 packetThrottleInterval
enet_uint32 lastRoundTripTime
enet_uint32 lowestRoundTripTime
enet_uint32 lastRoundTripTimeVariance
enet_uint32 highestRoundTripTimeVariance
enet_uint32 roundTripTime
enet_uint32 roundTripTimeVariance
enet_uint32 mtu
enet_uint32 windowSize
enet_uint32 reliableDataInTransit
enet_uint16 outgoingReliableSequenceNumber
enet_uint16 flags
enet_uint16 incomingUnsequencedGroup
enet_uint16 outgoingUnsequencedGroup
enet_uint32 unsequencedWindow
enet_uint32 eventData
ctypedef struct ENetHost:
ENetSocket socket
ENetAddress address
enet_uint32 incomingBandwidth
enet_uint32 outgoingBandwidth
ENetPeer *peers
size_t peerCount
size_t channelLimit
enet_uint8 *receivedData
size_t receivedDataLength
ENetAddress receivedAddress
enet_uint32 totalSentData
enet_uint32 totalSentPackets
enet_uint32 totalReceivedData
enet_uint32 totalReceivedPackets
ENetInterceptCallback intercept
ENetChecksumCallback checksum
ctypedef enum ENetEventType:
ENET_EVENT_TYPE_NONE = 0
ENET_EVENT_TYPE_CONNECT = 1
ENET_EVENT_TYPE_DISCONNECT = 2
ENET_EVENT_TYPE_RECEIVE = 3
ctypedef struct ENetEvent:
ENetEventType type
ENetPeer *peer
enet_uint8 channelID
enet_uint32 data
ENetPacket *packet
# Global functions
int enet_initialize()
void enet_deinitialize()
#checksum callback
enet_uint32 enet_crc32(const ENetBuffer *, size_t)
# Address functions
int enet_address_set_host(ENetAddress *address, char *hostName)
int enet_address_get_host_ip(ENetAddress *address, char *hostName, size_t nameLength)
int enet_address_get_host(ENetAddress *address, char *hostName, size_t nameLength)
# Packet functions
ENetPacket* enet_packet_create(char *dataContents, size_t dataLength, enet_uint32 flags)
void enet_packet_destroy(ENetPacket *packet)
int enet_packet_resize(ENetPacket *packet, size_t dataLength)
# Host functions
int enet_host_compress_with_range_coder(ENetHost *host)
ENetHost* enet_host_create(ENetAddress *address, size_t peerCount, size_t channelLimit, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
void enet_host_destroy(ENetHost *host)
ENetPeer* enet_host_connect(ENetHost *host, ENetAddress *address, size_t channelCount, enet_uint32 data)
void enet_host_broadcast(ENetHost *host, enet_uint8 channelID, ENetPacket *packet)
void enet_host_channel_limit(ENetHost *host, size_t channelLimit)
void enet_host_bandwidth_limit(ENetHost *host, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth)
void enet_host_flush(ENetHost *host)
int enet_host_check_events(ENetHost *host, ENetEvent *event)
int enet_host_service(ENetHost *host, ENetEvent *event, enet_uint32 timeout)
# Peer functions
void enet_peer_throttle_configure(ENetPeer *peer, enet_uint32 interval, enet_uint32 acceleration, enet_uint32 deacceleration)
int enet_peer_send(ENetPeer *peer, enet_uint8 channelID, ENetPacket *packet)
ENetPacket* enet_peer_receive(ENetPeer *peer, enet_uint8 *channelID)
void enet_peer_ping(ENetPeer *peer)
void enet_peer_reset(ENetPeer *peer)
void enet_peer_disconnect(ENetPeer *peer, enet_uint32 data)
void enet_peer_disconnect_now(ENetPeer *peer, enet_uint32 data)
void enet_peer_disconnect_later(ENetPeer *peer, enet_uint32 data)
# Socket functions
int enet_socket_send(ENetSocket socket, ENetAddress *address, ENetBuffer *buffer, size_t size)
cdef enum:
MAXHOSTNAME = 257
PACKET_FLAG_RELIABLE = ENET_PACKET_FLAG_RELIABLE
PACKET_FLAG_UNSEQUENCED = ENET_PACKET_FLAG_UNSEQUENCED
PACKET_FLAG_NO_ALLOCATE = ENET_PACKET_FLAG_NO_ALLOCATE
PACKET_FLAG_UNRELIABLE_FRAGMENT = ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT
EVENT_TYPE_NONE = ENET_EVENT_TYPE_NONE
EVENT_TYPE_CONNECT = ENET_EVENT_TYPE_CONNECT
EVENT_TYPE_DISCONNECT = ENET_EVENT_TYPE_DISCONNECT
EVENT_TYPE_RECEIVE = ENET_EVENT_TYPE_RECEIVE
PEER_STATE_DISCONNECTED = ENET_PEER_STATE_DISCONNECTED
PEER_STATE_CONNECTING = ENET_PEER_STATE_CONNECTING
PEER_STATE_ACKNOWLEDGING_CONNECT = ENET_PEER_STATE_ACKNOWLEDGING_CONNECT
PEER_STATE_CONNECTION_PENDING = ENET_PEER_STATE_CONNECTION_PENDING
PEER_STATE_CONNECTION_SUCCEEDED = ENET_PEER_STATE_CONNECTION_SUCCEEDED
PEER_STATE_CONNECTED = ENET_PEER_STATE_CONNECTED
PEER_STATE_DISCONNECT_LATER = ENET_PEER_STATE_DISCONNECT_LATER
PEER_STATE_DISCONNECTING = ENET_PEER_STATE_DISCONNECTING
PEER_STATE_ACKNOWLEDGING_DISCONNECT = ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT
PEER_STATE_ZOMBIE = ENET_PEER_STATE_ZOMBIE
ENET_CRC32 = 1
cdef class Address
cdef class Socket:
"""
Socket (int socket)
DESCRIPTION
An ENet socket.
Can be used with select and poll.
"""
cdef ENetSocket _enet_socket
def send(self, Address address, data):
cdef ENetBuffer buffer
data = data if isinstance(data, bytes) else data.encode()
buffer.data = <char*>data
buffer.dataLength = len(data)
cdef int result = enet_socket_send(self._enet_socket,
&address._enet_address, &buffer, 1)
return result
def fileno(self):
return self._enet_socket
cdef class Address:
"""
Address (str address, int port)
ATTRIBUTES
str host Hostname referred to by the Address.
int port Port referred to by the Address.
DESCRIPTION
An ENet address and port pair.
When instantiated, performs a resolution upon 'address'. However, if
'address' is None, enet.HOST_ANY is assumed.
"""
cdef ENetAddress _enet_address
def __init__(self, host, port):
if host is not None:
# Convert the hostname to a byte string if needed
self.host = host if isinstance(host, bytes) else bytes(host, "ascii")
else:
self.host = None
self.port = port
def __str__(self):
return "{0}:{1}".format(self.host, self.port)
def __richcmp__(self, obj, op):
if isinstance(obj, Address):
if op == 2:
# This is a '==' operation
return (obj.host == self.host) and (obj.port == self.port)
elif op == 3:
# This is a '!=' operation
return (obj.host != self.host) or (obj.port != self.port)
raise NotImplementedError
property host:
def __get__(self):
cdef char host[MAXHOSTNAME]
if self._enet_address.host == ENET_HOST_ANY:
return "*"
elif self._enet_address.host:
if enet_address_get_host_ip(&self._enet_address, host, MAXHOSTNAME):
raise IOError("Resolution failure!")
return unicode(host, "ascii")
def __set__(self, value):
if not value or value == "*":
self._enet_address.host = ENET_HOST_ANY
else:
if enet_address_set_host(&self._enet_address, value):
raise IOError("Resolution failure!")
property hostname:
def __get__(self):
cdef char host[MAXHOSTNAME]
if self._enet_address.host == ENET_HOST_ANY:
return "*"
elif self._enet_address.host:
if enet_address_get_host(&self._enet_address, host, MAXHOSTNAME):
raise IOError("Resolution failure!")
return unicode(host, "ascii")
property port:
def __get__(self):
return self._enet_address.port
def __set__(self, value):
self._enet_address.port = value
cdef class Packet:
"""
Packet (str dataContents, int flags)
ATTRIBUTES
str data Contains the data for the packet.
int flags Flags modifying delivery of the Packet:
enet.PACKET_FLAG_RELIABLE Packet must be received by the target peer
and resend attempts should be made until
the packet is delivered.
enet.PACKET_FLAG_UNSEQUENCED Packet will not be sequenced with other
packets not supported for reliable
packets.
enet.PACKET_FLAG_NO_ALLOCATE Packet will not allocate data and user
must supply it instead.
enet.PACKET_FLAG_UNRELIABLE_FRAGMENT Packet will be fragmented using
unreliable (instead of reliable)
sends if it exceeds the MTU...
DESCRIPTION
An ENet data packet that may be sent to or received from a peer.
"""
cdef ENetPacket *_enet_packet
cdef bool sent
def __init__(self, data=None, flags=0):
if data is not None:
self._enet_packet = enet_packet_create(data, len(data), flags)
# This will get set to True when a peer.send() is called with the Packet
# to ensure we don't try to destroy this packet as ENET will handle that
# for us.
self.sent = False
def __dealloc__(self):
if self.is_valid() and not self.sent:
enet_packet_destroy(self._enet_packet)
def is_valid(self):
if self._enet_packet:
return True
else:
return False
property data:
def __get__(self):
if self.is_valid():
return (<char *>self._enet_packet.data)[:self._enet_packet.dataLength]
else:
raise MemoryError("Packet has not been initialized properly!")
property dataLength:
def __get__(self):
if self.is_valid():
return self._enet_packet.dataLength
else:
raise MemoryError("Packet has not been initialized properly!")
property flags:
def __get__(self):
if self.is_valid():
return self._enet_packet.flags
else:
raise MemoryError("Packet has not been initialized properly!")
property sent:
def __get__(self):
return self.sent
def __set__(self, value):
self.sent = value
cdef class Peer:
"""
Peer ()
ATTRIBUTES
Address address
int state The peer's current state which is one of
enet.PEER_STATE_*
int packetLoss Mean packet loss of reliable packets as a ratio with
respect to the constant enet.PEER_PACKET_LOSS_SCALE.
int packetThrottleAcceleration
int packetThrottleDeceleration
int packetThrottleInterval
int roundTripTime Mean round trip time (RTT), in milliseconds,
between sending a reliable packet and receiving
its acknowledgement.
int incomingPeerID
DESCRIPTION
An ENet peer which data packets may be sent or received from.
This class should never be instantiated directly, but rather via
enet.Host.connect or enet.Event.Peer. If you try to access any members
of a Peer without being properly instantiated from a Host or Event
object then a MemoryError will be raised.
"""
cdef ENetPeer *_enet_peer
def __richcmp__(self, obj, op):
if isinstance(obj, Peer):
if op == 2:
return self.address == obj.address
elif op == 3:
return self.address != obj.address
raise NotImplementedError
def __hash__(self):
return <uintptr_t>self._enet_peer
def send(self, channelID, Packet packet):
"""
send (int channelID, Packet packet)
Queues a packet to be sent.
returns 0 on success, < 0 on failure
"""
if self.check_valid() and packet.is_valid():
packet.sent = True
return enet_peer_send(self._enet_peer, channelID, packet._enet_packet)
def receive(self, unsigned char channelID):
"""
receive (int channelID)
Attempts to dequeue any incoming queued packet.
"""
if self.check_valid():
packet = Packet()
(<Packet> packet)._enet_packet = enet_peer_receive(self._enet_peer, &channelID)
if packet._enet_packet:
return packet
else:
return None
def reset(self):
"""
reset ()
Forcefully disconnects a peer.
"""
if self.check_valid():
enet_peer_reset(self._enet_peer)
def ping(self):
"""
ping ()
Sends a ping request to a peer.
"""
if self.check_valid():
enet_peer_ping(self._enet_peer)
def disconnect(self, data=0):
"""
disconnect ()
Request a disconnection from a peer.
"""
if self.check_valid():
enet_peer_disconnect(self._enet_peer, data)
def disconnect_later(self, data=0):
"""
disconnect_later ()
Request a disconnection from a peer, but only after all queued outgoing
packets are sent.
"""
if self.check_valid():
enet_peer_disconnect_later(self._enet_peer, data)
def disconnect_now(self, data=0):
"""
disconnect_now ()
Force an immediate disconnection from a peer.
"""
if self.check_valid():
enet_peer_disconnect_now(self._enet_peer, data)
def check_valid(self):
"""
check_valid ()
Returns True if there is a valid enet_peer set
Raises a Memory error if not
"""
if self._enet_peer:
return True
else:
raise MemoryError("Empty Peer object accessed!")
property host:
def __get__(self):
if self.check_valid():
# be a bit like pickle here. otherwise Host
# would create a new enet host inside __init__
h = Host.__new__(Host)
(<Host> h)._enet_host = self._enet_peer.host
return h
property outgoingPeerID:
def __get__(self):
if self.check_valid():
return self._enet_peer.outgoingPeerID
property incomingPeerID:
def __get__(self):
if self.check_valid():
return self._enet_peer.incomingPeerID
property connectID:
def __get__(self):
if self.check_valid():
return self._enet_peer.connectID
property outgoingSessionID:
def __get__(self):
if self.check_valid():
return self._enet_peer.outgoingSessionID
property incomingSessionID:
def __get__(self):
if self.check_valid():
return self._enet_peer.incomingSessionID
property address:
def __get__(self):
if self.check_valid():
a = Address(None, 0)
(<Address> a)._enet_address = self._enet_peer.address
return a
property data:
def __get__(self):
if self.check_valid():
return self._enet_peer.data
def __set__(self, value):
if self.check_valid():
self._enet_peer.data = value
property state:
def __get__(self):
if self.check_valid():
return self._enet_peer.state
property channelCount:
def __get__(self):
if self.check_valid():
return self._enet_peer.channelCount
property incomingBandwidth:
def __get__(self):
if self.check_valid():
return self._enet_peer.incomingBandwidth
property outgoingBandwidth:
def __get__(self):
if self.check_valid():
return self._enet_peer.outgoingBandwidth
property incomingBandwidthThrottleEpoch:
def __get__(self):
if self.check_valid():
return self._enet_peer.incomingBandwidthThrottleEpoch
property outgoingBandwidthThrottleEpoch:
def __get__(self):
if self.check_valid():
return self._enet_peer.outgoingBandwidthThrottleEpoch
property incomingDataTotal:
def __get__(self):
if self.check_valid():
return self._enet_peer.incomingDataTotal
property outgoingDataTotal:
def __get__(self):
if self.check_valid():
return self._enet_peer.outgoingDataTotal
property lastSendTime:
def __get__(self):
if self.check_valid():
return self._enet_peer.lastSendTime
property lastReceiveTime:
def __get__(self):
if self.check_valid():
return self._enet_peer.lastReceiveTime
property nextTimeout:
def __get__(self):
if self.check_valid():
return self._enet_peer.nextTimeout
property earliestTimeout:
def __get__(self):
if self.check_valid():
return self._enet_peer.earliestTimeout
property packetLossEpoch:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetLossEpoch
property packetsSent:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetsSent
property packetsLost:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetsLost
property packetLoss:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetLoss
property packetLossVariance:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetLossVariance
property packetThrottle:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetThrottle
property packetThrottleLimit:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetThrottleLimit
property packetThrottleCounter:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetThrottleCounter
property packetThrottleEpoch:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetThrottleEpoch
property packetThrottleAcceleration:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetThrottleAcceleration
def __set__(self, value):
if self.check_valid():
enet_peer_throttle_configure(self._enet_peer,
self.packetThrottleInterval, value,
self.packetThrottleDeceleration)
property packetThrottleDeceleration:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetThrottleDeceleration
def __set__(self, value):
if self.check_valid():
enet_peer_throttle_configure(self._enet_peer,
self.packetThrottleInterval,
self.packetThrottleAcceleration, value)
property packetThrottleInterval:
def __get__(self):
if self.check_valid():
return self._enet_peer.packetThrottleInterval
def __set__(self, value):
if self.check_valid():
enet_peer_throttle_configure(
self._enet_peer, value, self.packetThrottleAcceleration,
self.packetThrottleDeceleration)
property lastRoundTripTime:
def __get__(self):
if self.check_valid():
return self._enet_peer.lastRoundTripTime
property lowestRoundTripTime:
def __get__(self):
if self.check_valid():
return self._enet_peer.lowestRoundTripTime
property lastRoundTripTimeVariance:
def __get__(self):
if self.check_valid():
return self._enet_peer.lastRoundTripTimeVariance
property highestRoundTripTimeVariance:
def __get__(self):
if self.check_valid():
return self._enet_peer.highestRoundTripTimeVariance
property roundTripTime:
def __get__(self):
if self.check_valid():
return self._enet_peer.roundTripTime
property roundTripTimeVariance:
def __get__(self):
if self.check_valid():
return self._enet_peer.roundTripTimeVariance
property mtu:
def __get__(self):
if self.check_valid():
return self._enet_peer.mtu
property windowSize:
def __get__(self):
if self.check_valid():
return self._enet_peer.windowSize
property reliableDataInTransit:
def __get__(self):
if self.check_valid():
return self._enet_peer.reliableDataInTransit
property outgoingReliableSequenceNumber:
def __get__(self):
if self.check_valid():
return self._enet_peer.outgoingReliableSequenceNumber
property flags:
def __get__(self):
if self.check_valid():
return self._enet_peer.flags
property incomingUnsequencedGroup:
def __get__(self):
if self.check_valid():
return self._enet_peer.incomingUnsequencedGroup
property outgoingUnsequencedGroup:
def __get__(self):
if self.check_valid():
return self._enet_peer.outgoingUnsequencedGroup
property eventData:
def __get__(self):
if self.check_valid():
return self._enet_peer.eventData
cdef class Event:
"""
Event ()
ATTRIBUTES
int type Type of the event. Will be enet.EVENT_TYPE_*.
Peer peer Peer that generated the event.
int channelID
Packet packet
DESCRIPTION
An ENet event as returned by enet.Host.service.
This class should never be instantiated directly.
"""
cdef ENetEvent _enet_event
cdef Packet _packet
def __init__(self):
self._packet = None
property type:
def __get__(self):
return self._enet_event.type
property peer:
def __get__(self):
peer = Peer()
(<Peer> peer)._enet_peer = self._enet_event.peer
return peer
property channelID:
def __get__(self):
return self._enet_event.channelID
property data:
def __get__(self):
return self._enet_event.data
property packet:
def __get__(self):
if not self._packet:
self._packet = Packet()
(<Packet> self._packet)._enet_packet = self._enet_event.packet
return self._packet
from weakref import WeakValueDictionary
cdef host_static_instances = WeakValueDictionary()
cdef class Host:
"""
Host (Address address, int peerCount, int channelLimit,
int incomingBandwidth, int outgoingBandwidth)
ATTRIBUTES
Address address Internet address of the host.
Socket socket The socket the host services.
int incomingBandwidth Downstream bandwidth of the host.
int outgoingBandwidth Upstream bandwidth of the host.
DESCRIPTION
An ENet host for communicating with peers.
If 'address' is None, then the Host will be client only.
"""
cdef ENetHost *_enet_host
cdef bool dealloc
cdef object _interceptCallback
cdef object __weakref__
cdef int _checksum
def __init__ (self, Address address=None, peerCount=0, channelLimit=0,
incomingBandwidth=0, outgoingBandwidth=0):
if address:
self._enet_host = enet_host_create(&address._enet_address, peerCount, channelLimit, incomingBandwidth, outgoingBandwidth)
else:
self._enet_host = enet_host_create(NULL, peerCount, channelLimit, incomingBandwidth, outgoingBandwidth)
if not self._enet_host:
raise MemoryError("Unable to create host structure!")
self.dealloc = True
self._checksum = 0
global host_static_instances
host_static_instances[<uintptr_t>self._enet_host] = self
def __hash__(self):
return <uintptr_t>self._enet_host
def __cinit__(self):
self.dealloc = False
self._enet_host = NULL
def __dealloc__(self):
if self.dealloc:
enet_host_destroy(self._enet_host)
def connect(self, Address address, channelCount, data=0):
"""
Peer connect (Address address, int channelCount, int data)
Initiates a connection to a foreign host and returns a Peer.
"""
if self._enet_host:
peer = Peer()
(<Peer> peer)._enet_peer = enet_host_connect(
self._enet_host, &address._enet_address, channelCount, data)
if not (<Peer> peer)._enet_peer:
raise IOError("Connection failure!")
return peer
def check_events(self):
"""
Checks for any queued events on the host and dispatches one if available
"""
if self._enet_host:
event = Event()
result = enet_host_check_events(
self._enet_host, &(<Event> event)._enet_event)
if result < 0:
raise IOError("Servicing error - probably disconnected.")
elif result == 0:
return None
else:
return event
def service(self, timeout, fast_drop=False):
"""
Event service (int timeout)
Waits for events on the host specified and shuttles packets between
the host and its peers. The timeout is in milliseconds.
if fast_drop is set, None can be returned instead
"""
if self._enet_host:
event = Event()
result = enet_host_service(
self._enet_host, &(<Event> event)._enet_event, timeout)
if result < 0:
raise IOError("Servicing error - probably disconnected.")
if result == 0 and fast_drop:
return None
else:
return event
def flush(self):
"""
flush ()
Sends any queued packets on the host specified to its designated peers.
"""
if self._enet_host:
enet_host_flush(self._enet_host)
def broadcast(self, channelID, Packet packet):
"""
broadcast (int channelID, Packet packet)
Queues a packet to be sent to all peers associated with the host.
"""
if self._enet_host:
if packet.is_valid():
packet.sent = True
enet_host_broadcast(self._enet_host, channelID, packet._enet_packet)
def compress_with_range_coder(self):
"""
Sets the packet compressor the host should use to the default range coder
"""
if self._enet_host:
return enet_host_compress_with_range_coder(self._enet_host)
property socket:
def __get__(self):
socket = Socket()
(<Socket> socket)._enet_socket = self._enet_host.socket
return socket
property address:
def __get__(self):
if self._enet_host:
a = Address(None, 0)
(<Address> a)._enet_address = self._enet_host.address
return a
property incomingBandwidth:
def __get__(self):
return self._enet_host.incomingBandwidth
def __set__(self, value):
enet_host_bandwidth_limit(self._enet_host,
value, self.outgoingBandwidth)