-
Notifications
You must be signed in to change notification settings - Fork 2
/
session.py
64 lines (49 loc) · 1.7 KB
/
session.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
# This module wraps communications in a signaling protocol. The purpose is to
# overlay a connection-based protocol with explicit message signaling.
#
# The protocol is to send the size of the message of length lengthbytes followed by the
# message itself. A size of -1 indicates that this side of the connection
# should be considered closed.
class SessionEOF(Exception):
pass
# messages are at most 32 bit = 4 bytes long
lengthbytes = 4
# get the next message off of the socket...
def recvmessage(socketobj):
# receive length of next message
msglen = socketobj.recv(lengthbytes)
messagesize = int.from_bytes(msglen, byteorder = 'big', signed=True)
#print("rcv", messagesize, end="")
# nothing to read...
if messagesize == 0:
return b''
# end of messages
if messagesize == -1:
raise SessionEOF("Connection Closed")
if messagesize < 0:
raise ValueError("Bad message size")
data = b''
while len(data) < messagesize:
chunk = socketobj.recv(messagesize-len(data))
if chunk == b'':
raise SessionEOF("Connection Closed")
data = data + chunk
#print(":", data[:8])
return data
# a private helper function
def _sendhelper(socketobj, data):
#print("send", len(data), ":", str(data[0:8]), "...")
sentlength = 0
# if I'm still missing some, continue to send (I could have used sendall
# instead but this isn't supported in reply currently)
while sentlength < len(data):
thissent = socketobj.send(data[sentlength:])
sentlength = sentlength + thissent
# send the message
def sendmessage(socketobj, data):
# the length
_sendhelper(socketobj, len(data).to_bytes(lengthbytes, byteorder = 'big', signed=True))
if type(data) == str:
data = str.encode(data)
#the data
_sendhelper(socketobj, data)