mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-29 09:20:34 +00:00
Defensive programming inc
This commit is contained in:
@ -413,8 +413,8 @@ class Client:
|
||||
def __init__(self, host='127.0.0.1', port=4877):
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.connection = socket.socket()
|
||||
self.connection.connect((self.host, self.port))
|
||||
self.socket = socket.socket()
|
||||
self.socket.connect((self.host, self.port))
|
||||
|
||||
def ctrl_connect(self, username, password):
|
||||
# INPUT: (STRING username, STRING password)
|
||||
@ -862,24 +862,24 @@ class Client:
|
||||
|
||||
def __recv_DATA_BYTE(self):
|
||||
while True:
|
||||
if (len(self.connection.recv(1, socket.MSG_PEEK)) == 1):
|
||||
byte_data = self.connection.recv(1)
|
||||
if (len(self.socket.recv(1, socket.MSG_PEEK)) == 1):
|
||||
byte_data = self.socket.recv(1)
|
||||
break
|
||||
sleep(0.1)
|
||||
return struct.unpack('!B', byte_data)[0]
|
||||
|
||||
def __recv_DATA_BOOL(self):
|
||||
while True:
|
||||
if (len(self.connection.recv(1, socket.MSG_PEEK)) == 1):
|
||||
byte_data = self.connection.recv(1)
|
||||
if (len(self.socket.recv(1, socket.MSG_PEEK)) == 1):
|
||||
byte_data = self.socket.recv(1)
|
||||
break
|
||||
sleep(0.1)
|
||||
return struct.unpack('!?', byte_data)[0]
|
||||
|
||||
def __recv_DATA_INT(self):
|
||||
while True:
|
||||
if (len(self.connection.recv(4, socket.MSG_PEEK)) == 4):
|
||||
byte_data = self.connection.recv(4)
|
||||
if (len(self.socket.recv(4, socket.MSG_PEEK)) == 4):
|
||||
byte_data = self.socket.recv(4)
|
||||
break
|
||||
sleep(0.1)
|
||||
return struct.unpack('!i', byte_data)[0]
|
||||
@ -887,8 +887,8 @@ class Client:
|
||||
def __recv_DATA_STRING(self):
|
||||
n = self.__recv_WORD()
|
||||
while True:
|
||||
if (len(self.connection.recv(n, socket.MSG_PEEK)) == n):
|
||||
byte_data = self.connection.recv(n)
|
||||
if (len(self.socket.recv(n, socket.MSG_PEEK)) == n):
|
||||
byte_data = self.socket.recv(n)
|
||||
break
|
||||
sleep(0.1)
|
||||
return struct.unpack('!{}s'.format(n), byte_data)[0].decode()
|
||||
@ -947,29 +947,29 @@ class Client:
|
||||
|
||||
def __recv_WORD(self):
|
||||
while True:
|
||||
if (len(self.connection.recv(2, socket.MSG_PEEK)) == 2):
|
||||
byte_data = self.connection.recv(2)
|
||||
if (len(self.socket.recv(2, socket.MSG_PEEK)) == 2):
|
||||
byte_data = self.socket.recv(2)
|
||||
break
|
||||
sleep(0.1)
|
||||
return struct.unpack('!H', byte_data)[0]
|
||||
|
||||
def __send_BYTE(self, byte_data):
|
||||
data = struct.pack('!B', byte_data)
|
||||
self.connection.sendall(data)
|
||||
self.socket.sendall(data)
|
||||
|
||||
def __send_BOOL(self, bool_data):
|
||||
data = struct.pack('!?', bool_data)
|
||||
self.connection.sendall(data)
|
||||
self.socket.sendall(data)
|
||||
|
||||
def __send_INT(self, int_data):
|
||||
data = struct.pack('!i', int_data)
|
||||
self.connection.sendall(data)
|
||||
self.socket.sendall(data)
|
||||
|
||||
def __send_STRING(self, string_data):
|
||||
encoded_string_data = string_data.encode('utf-8')
|
||||
n = len(encoded_string_data)
|
||||
data = struct.pack('!H{}s'.format(n), n, encoded_string_data)
|
||||
self.connection.sendall(data)
|
||||
self.socket.sendall(data)
|
||||
|
||||
def __send_INT_LIST(self, int_list_data):
|
||||
n = len(int_list_data)
|
||||
@ -985,4 +985,4 @@ class Client:
|
||||
|
||||
def __send_WORD(self, word_data):
|
||||
data = struct.pack('!H', word_data)
|
||||
self.connection.sendall(data)
|
||||
self.socket.sendall(data)
|
||||
|
Reference in New Issue
Block a user