-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_send.py
78 lines (62 loc) · 2.21 KB
/
client_send.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
71
72
73
74
75
76
77
78
from struct import pack
from utils import debug
version = b'\x01'
MAX_LENGTH = 5
def padded(string):
"""Pads a string with whitespace to be MAX_LENGTH characters.
Args:
string: string to pad, should be at most MAX_LENGTH characters.
Returns:
string, exactly MAX_LENGTH long.
"""
len_to_pad = MAX_LENGTH - len(string)
return string + ' ' * len_to_pad
def initialize_player(username, conn):
"""Send a request to the server to initialize a player.
Args:
username: string, representing username of the new player.
Should be at most MAX_LEN characters.
conn: connection object representing server to which to send
request.
"""
send_to_server(b'\x01', padded(username).encode('utf-8'), conn)
def make_move(move_char, conn):
"""Send a request to the server to change a player's direction.
Args:
move_char: Character, represents desired direction to face
(unencoded).
conn: connection object representing server to which to send
request.
"""
send_to_server(b'\x02', move_char.encode('utf-8'), conn)
def restart_player(username, conn):
"""Send a request to the server to restart a player.
Args:
username: Username of player to restart (unencoded).
conn: connection object representing server to which to send
request.
"""
send_to_server(b'\x03', username.encode('utf-8'), conn)
def send_to_server(opcode, body, conn):
"""Sends a message to server.
The message sent will have the following structure:
- Header:
- Version number (byte)
- Length of payload (unsigned int)
- Opcode (byte)
- Body: optional payload
Args:
opcode: single byte representing function opcode
body: body of the message to send, bytes object
conn: connection object representing server to which to send
request.
"""
header = version + pack('!I', len(body)) + opcode
msg = header + body
try:
conn.sendall(msg)
except:
# close the client if the connection is down
print('ERROR: connection down')
os.system('stty echo')
os.kill(os.getpid(), signal.SIGINT)