-
Notifications
You must be signed in to change notification settings - Fork 30
/
test_server.py
66 lines (56 loc) · 2.26 KB
/
test_server.py
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
import enet
SHUTDOWN_MSG = "SHUTDOWN"
host = enet.Host(enet.Address(b"localhost", 54301), 10, 0, 0, 0)
host.checksum = enet.ENET_CRC32
connect_count = 0
run = True
shutdown_recv = False
while run:
# Wait 1 second for an event
event = host.service(1000)
if event.type == enet.EVENT_TYPE_CONNECT:
print("%s: CONNECT" % event.peer.address)
connect_count += 1
elif event.type == enet.EVENT_TYPE_DISCONNECT:
print("%s: DISCONNECT" % event.peer.address)
connect_count -= 1
if connect_count <= 0 and shutdown_recv:
run = False
elif event.type == enet.EVENT_TYPE_RECEIVE:
print("%s: IN: %r" % (event.peer.address, event.packet.data))
msg = event.packet.data
if event.peer.send(0, enet.Packet(msg)) < 0:
print("%s: Error sending echo packet!" % event.peer.address)
else:
print("%s: OUT: %r" % (event.peer.address, msg))
if event.packet.data == b"SHUTDOWN":
shutdown_recv = True
# Part of the test to do with intercept callback and socket.send
connect_count = 0
run = True
shutdown_recv = False
def receive_callback(address, data):
if data and data == b"\xff\xff\xff\xffgetstatus\x00":
host.socket.send(address, b"\xff\xff\xff\xffstatusResponse\n")
host.intercept = receive_callback
while run:
# Wait 1 second for an event
event = host.service(1000)
if event.type == enet.EVENT_TYPE_CONNECT:
print("%s: CONNECT" % event.peer.address)
connect_count += 1
elif event.type == enet.EVENT_TYPE_DISCONNECT:
print("%s: DISCONNECT" % event.peer.address)
connect_count -= 1
if connect_count <= 0 and shutdown_recv:
run = False
elif event.type == enet.EVENT_TYPE_RECEIVE:
print("%s: IN: %r" % (event.peer.address, event.packet.data))
# This packet echo is used to mimick the usual use case when packets are going back&forth while the intercept callback is used
msg = event.packet.data
if event.peer.send(0, enet.Packet(msg)) < 0:
print("%s: Error sending echo packet!" % event.peer.address)
else:
print("%s: OUT: %r" % (event.peer.address, msg))
if event.packet.data == b"SHUTDOWN":
shutdown_recv = True