Browse Source

Added comments and a small formatting change.

Steven Engler 5 years ago
parent
commit
9885c13d3f
1 changed files with 19 additions and 2 deletions
  1. 19 2
      src/basic_protocols.py

+ 19 - 2
src/basic_protocols.py

@@ -18,12 +18,21 @@ class ProtocolHelper():
 		self._buffer = b''
 		self._buffer = b''
 	#
 	#
 	def set_buffer(self, data):
 	def set_buffer(self, data):
+		"""
+		Set the buffer contents to the data that you wish to send.
+		"""
+		#
 		self._buffer = data
 		self._buffer = data
 	#
 	#
 	def get_buffer(self):
 	def get_buffer(self):
 		return self._buffer
 		return self._buffer
 	#
 	#
 	def recv(self, socket, num_bytes):
 	def recv(self, socket, num_bytes):
+		"""
+		Try to fill up the buffer to a max of 'num_bytes'. If the buffer is filled,
+		return True, otherwise return False.
+		"""
+		#
 		data = socket.recv(num_bytes-len(self._buffer))
 		data = socket.recv(num_bytes-len(self._buffer))
 		self._buffer += data
 		self._buffer += data
 		if len(self._buffer) == num_bytes:
 		if len(self._buffer) == num_bytes:
@@ -32,6 +41,11 @@ class ProtocolHelper():
 		return False
 		return False
 	#
 	#
 	def send(self, socket):
 	def send(self, socket):
+		"""
+		Try to send the remainder of the buffer. If the entire buffer has been sent,
+		return True, otherwise return False.
+		"""
+		#
 		n = socket.send(self._buffer)
 		n = socket.send(self._buffer)
 		self._buffer = self._buffer[n:]
 		self._buffer = self._buffer[n:]
 		if len(self._buffer) == 0:
 		if len(self._buffer) == 0:
@@ -42,6 +56,10 @@ class ProtocolHelper():
 #
 #
 class Protocol():
 class Protocol():
 	def _run_iteration(self, block=True):
 	def _run_iteration(self, block=True):
+		"""
+		This function should be overridden. It runs a single iteration of the protocol.
+		"""
+		#
 		pass
 		pass
 	#
 	#
 	def run(self, block=True):
 	def run(self, block=True):
@@ -401,7 +419,6 @@ class ReceiveDataProtocol(Protocol):
 	#
 	#
 #
 #
 class ServerListener():
 class ServerListener():
-	"A TCP listener, binding, listening and accepting new connections."
 	def __init__(self, endpoint, accept_callback):
 	def __init__(self, endpoint, accept_callback):
 		self.callback = accept_callback
 		self.callback = accept_callback
 		#
 		#
@@ -425,7 +442,7 @@ class SimpleClientConnectionProtocol(Protocol):
 		self.proxy = proxy
 		self.proxy = proxy
 		self.username = username
 		self.username = username
 		#
 		#
-		self.states = enum.Enum('CLIENT_CONN_STATES', 'READY_TO_BEGIN CONNECT_TO_PROXY  PUSH_DATA DONE')
+		self.states = enum.Enum('CLIENT_CONN_STATES', 'READY_TO_BEGIN CONNECT_TO_PROXY PUSH_DATA DONE')
 		self.state = self.states.READY_TO_BEGIN
 		self.state = self.states.READY_TO_BEGIN
 		#
 		#
 		self.socket = socket.socket()
 		self.socket = socket.socket()