2016-08-28 14:43:00 +03:00
|
|
|
# Python rough implementation of a C# TCP client
|
2016-08-26 13:58:53 +03:00
|
|
|
import socket
|
2016-09-09 12:47:37 +03:00
|
|
|
import time
|
2016-09-10 11:17:15 +03:00
|
|
|
from threading import Lock
|
2016-09-09 12:47:37 +03:00
|
|
|
|
|
|
|
from errors import ReadCancelledError
|
2016-09-07 20:48:49 +03:00
|
|
|
from utils import BinaryWriter
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TcpClient:
|
|
|
|
def __init__(self):
|
|
|
|
self.connected = False
|
|
|
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
2016-09-10 11:17:15 +03:00
|
|
|
|
|
|
|
# Support for multi-threading advantages and safety
|
2016-09-09 12:47:37 +03:00
|
|
|
self.cancelled = False # Has the read operation been cancelled?
|
|
|
|
self.delay = 0.1 # Read delay when there was no data available
|
2016-09-10 11:17:15 +03:00
|
|
|
self.lock = Lock()
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def connect(self, ip, port):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Connects to the specified IP and port number"""
|
2016-08-26 13:58:53 +03:00
|
|
|
self.socket.connect((ip, port))
|
2016-09-03 11:54:58 +03:00
|
|
|
self.connected = True
|
2016-09-09 12:47:37 +03:00
|
|
|
self.socket.setblocking(False)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def close(self):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Closes the connection"""
|
2016-08-26 13:58:53 +03:00
|
|
|
self.socket.close()
|
2016-09-03 11:54:58 +03:00
|
|
|
self.connected = False
|
2016-09-09 12:47:37 +03:00
|
|
|
self.socket.setblocking(True)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def write(self, data):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Writes (sends) the specified bytes to the connected peer"""
|
2016-09-10 11:17:15 +03:00
|
|
|
|
|
|
|
# Ensure that only one thread can send data at once
|
|
|
|
with self.lock:
|
2016-09-12 20:32:16 +03:00
|
|
|
# Do not use .sendall:
|
|
|
|
# "on error, an exception is raised, and there is no way to
|
|
|
|
# determine how much data, if any, was successfully sent."
|
|
|
|
while data:
|
|
|
|
try:
|
|
|
|
sent = self.socket.send(data)
|
|
|
|
data = data[sent:]
|
|
|
|
except BlockingIOError as e:
|
|
|
|
if 'Errno 11' in str(e): # Error #11: Resource temporary unavailable
|
|
|
|
time.sleep(0.1) # Sleep a bit waiting for the resource to be available
|
|
|
|
else:
|
|
|
|
raise e
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def read(self, buffer_size):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Reads (receives) the specified bytes from the connected peer"""
|
2016-09-09 12:47:37 +03:00
|
|
|
|
2016-09-10 11:17:15 +03:00
|
|
|
# Ensure that only one thread can receive data at once
|
|
|
|
with self.lock:
|
|
|
|
# Ensure it is not cancelled at first, so we can enter the loop
|
|
|
|
self.cancelled = False
|
|
|
|
|
|
|
|
with BinaryWriter() as writer:
|
2016-09-11 12:50:38 +03:00
|
|
|
while writer.written_count < buffer_size:
|
|
|
|
# Only do cancel if no data was read yet
|
|
|
|
# Otherwise, carry on reading and finish
|
|
|
|
if self.cancelled and writer.written_count == 0:
|
|
|
|
raise ReadCancelledError()
|
|
|
|
|
2016-09-10 11:17:15 +03:00
|
|
|
try:
|
|
|
|
# When receiving from the socket, we may not receive all the data at once
|
|
|
|
# This is why we need to keep checking to make sure that we receive it all
|
|
|
|
left_count = buffer_size - writer.written_count
|
|
|
|
partial = self.socket.recv(left_count)
|
|
|
|
writer.write(partial)
|
2016-09-09 12:47:37 +03:00
|
|
|
|
2016-09-10 11:17:15 +03:00
|
|
|
except BlockingIOError:
|
|
|
|
# There was no data available for us to read. Sleep a bit
|
|
|
|
time.sleep(self.delay)
|
2016-09-09 12:47:37 +03:00
|
|
|
|
2016-09-10 11:17:15 +03:00
|
|
|
# If everything went fine, return the read bytes
|
|
|
|
return writer.get_bytes()
|
2016-09-09 12:47:37 +03:00
|
|
|
|
2016-09-12 20:32:16 +03:00
|
|
|
|
2016-09-09 12:47:37 +03:00
|
|
|
def cancel_read(self):
|
2016-09-11 12:50:38 +03:00
|
|
|
"""Cancels the read operation IF it hasn't yet
|
|
|
|
started, raising a ReadCancelledError"""
|
2016-09-09 12:47:37 +03:00
|
|
|
self.cancelled = True
|