-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_client.py
70 lines (62 loc) · 1.74 KB
/
test_client.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
67
68
69
70
import random
import socket
import struct, binascii
import messages_pb2
HOST = "localhost"
PORT = 15538
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error as msg:
s = None
continue
try:
s.connect(sa)
except socket.error as msg:
s.close()
s = None
continue
break
def sendMessage(msgType, msg):
body = msg.SerializeToString()
msg = struct.pack("!HH", len(body), msgType)
msg += body
s.sendall(msg)
pass
hello_msg = messages_pb2.Hello()
hello_msg.version = 123
sendMessage(0x01, hello_msg)
msg = messages_pb2.MsgSubscribe()
msg.channel = "test_msgs"
sendMessage(0x0102, msg)
msg = messages_pb2.MsgBroadcast()
msg.channel = "test_msgs"
msg.json = "Message from a client!"
sendMessage(0x0101, msg)
# Spawn an entity to play with
msg = messages_pb2.SpawnEntity()
msg.cfg_filename = "test2.json"
#msg.start_position = messages_pb2.Vec3()
msg.start_position.x = random.random()*20.0-10.0
msg.start_position.y = random.random()*20.0-10.0
msg.start_position.z = random.random()*20.0-10.0
sendMessage(0x0201, msg)
# Now receive a message
while 1:
hdr = ""
while len(hdr) < 4:
hdr += s.recv(4-len(hdr))
print binascii.hexlify(hdr)
(msg_size, msg_type) = struct.unpack("!HH", hdr)
print "%X %X"%(msg_size,msg_type)
body = ""
while len(body) < msg_size:
print "receiving %i..."%(msg_size-len(body))
body += s.recv(msg_size-len(body))
if msg_type == 0x0101:
msg = messages_pb2.MsgBroadcast()
msg.ParseFromString(body)
print "Got message: %s"%msg.json
s.close()