|
@@ -59,23 +59,20 @@ class ProtocolHelper():
|
|
|
#
|
|
|
#
|
|
|
class Protocol():
|
|
|
- def _run_iteration(self, block=True):
|
|
|
+ def _run_iteration(self):
|
|
|
"""
|
|
|
This function should be overridden. It runs a single iteration of the protocol.
|
|
|
"""
|
|
|
#
|
|
|
pass
|
|
|
#
|
|
|
- def run(self, block=True):
|
|
|
+ def run(self):
|
|
|
while True:
|
|
|
- finished = self._run_iteration(block=block)
|
|
|
+ finished = self._run_iteration()
|
|
|
#
|
|
|
if finished:
|
|
|
# protocol is done
|
|
|
return True
|
|
|
- elif not block:
|
|
|
- # not done the protocol yet, but don't block
|
|
|
- return False
|
|
|
#
|
|
|
#
|
|
|
#
|
|
@@ -88,12 +85,12 @@ class ChainedProtocol(Protocol):
|
|
|
self.states = enum.Enum('CHAIN_STATES', 'READY_TO_BEGIN RUNNING DONE')
|
|
|
self.state = self.states.READY_TO_BEGIN
|
|
|
#
|
|
|
- def _run_iteration(self, block=True):
|
|
|
+ def _run_iteration(self):
|
|
|
if self.state is self.states.READY_TO_BEGIN:
|
|
|
self.state = self.states.RUNNING
|
|
|
#
|
|
|
if self.state is self.states.RUNNING:
|
|
|
- if self.protocols[self.current_protocol] is None or self.protocols[self.current_protocol].run(block=block):
|
|
|
+ if self.protocols[self.current_protocol] is None or self.protocols[self.current_protocol].run():
|
|
|
self.current_protocol += 1
|
|
|
#
|
|
|
if self.current_protocol >= len(self.protocols):
|
|
@@ -117,7 +114,7 @@ class Socks4Protocol(Protocol):
|
|
|
#
|
|
|
self.protocol_helper = None
|
|
|
#
|
|
|
- def _run_iteration(self, block=True):
|
|
|
+ def _run_iteration(self):
|
|
|
if self.state is self.states.READY_TO_BEGIN:
|
|
|
self.protocol_helper = ProtocolHelper()
|
|
|
self.protocol_helper.set_buffer(self.socks_cmd(self.addr_port, self.username))
|
|
@@ -191,7 +188,7 @@ class PushDataProtocol(Protocol):
|
|
|
self.bytes_written = 0
|
|
|
self.protocol_helper = None
|
|
|
#
|
|
|
- def _run_iteration(self, block=True):
|
|
|
+ def _run_iteration(self):
|
|
|
if self.state is self.states.READY_TO_BEGIN:
|
|
|
info = self.total_bytes.to_bytes(8, byteorder='big', signed=False)
|
|
|
info += len(self.byte_buffer).to_bytes(8, byteorder='big', signed=False)
|
|
@@ -206,9 +203,6 @@ class PushDataProtocol(Protocol):
|
|
|
#
|
|
|
if self.state is self.states.PUSH_DATA:
|
|
|
if self.use_acceleration:
|
|
|
- if not block:
|
|
|
- logging.warning('Protocol set to non-blocking, but using the blocking accelerated function.')
|
|
|
- #
|
|
|
ret_val = accelerated_functions.push_data(self.socket.fileno(), self.total_bytes, self.byte_buffer)
|
|
|
if ret_val < 0:
|
|
|
raise ProtocolException('Error while pushing data.')
|
|
@@ -267,7 +261,7 @@ class PullDataProtocol(Protocol):
|
|
|
self._time_of_first_byte = None
|
|
|
self.elapsed_time = None
|
|
|
#
|
|
|
- def _run_iteration(self, block=True):
|
|
|
+ def _run_iteration(self):
|
|
|
if self.state is self.states.READY_TO_BEGIN:
|
|
|
self.protocol_helper = ProtocolHelper()
|
|
|
self.state = self.states.RECV_INFO
|
|
@@ -283,9 +277,6 @@ class PullDataProtocol(Protocol):
|
|
|
#
|
|
|
if self.state is self.states.PULL_DATA:
|
|
|
if self.use_acceleration:
|
|
|
- if not block:
|
|
|
- logging.warning('Protocol set to non-blocking, but using the blocking accelerated function.')
|
|
|
- #
|
|
|
(ret_val, elapsed_time) = accelerated_functions.pull_data(self.socket.fileno(), self.data_size, self.recv_buffer_len)
|
|
|
if ret_val < 0:
|
|
|
raise ProtocolException('Error while pulling data.')
|
|
@@ -344,7 +335,7 @@ class SendDataProtocol(Protocol):
|
|
|
#
|
|
|
self.protocol_helper = None
|
|
|
#
|
|
|
- def _run_iteration(self, block=True):
|
|
|
+ def _run_iteration(self):
|
|
|
if self.state is self.states.READY_TO_BEGIN:
|
|
|
info_size = 20
|
|
|
info = len(self.send_data).to_bytes(info_size, byteorder='big', signed=False)
|
|
@@ -392,7 +383,7 @@ class ReceiveDataProtocol(Protocol):
|
|
|
self.data_size = None
|
|
|
self.received_data = None
|
|
|
#
|
|
|
- def _run_iteration(self, block=True):
|
|
|
+ def _run_iteration(self):
|
|
|
if self.state is self.states.READY_TO_BEGIN:
|
|
|
self.protocol_helper = ProtocolHelper()
|
|
|
self.state = self.states.RECV_INFO
|
|
@@ -464,7 +455,7 @@ class SimpleClientConnectionProtocol(Protocol):
|
|
|
self.socket.connect(self.proxy)
|
|
|
#
|
|
|
#
|
|
|
- def _run_iteration(self, block=True):
|
|
|
+ def _run_iteration(self):
|
|
|
if self.state is self.states.READY_TO_BEGIN:
|
|
|
if self.proxy is None:
|
|
|
self.sub_protocol = PushDataProtocol(self.socket, self.total_bytes, self.data_generator)
|
|
@@ -475,13 +466,13 @@ class SimpleClientConnectionProtocol(Protocol):
|
|
|
#
|
|
|
#
|
|
|
if self.state is self.states.CONNECT_TO_PROXY:
|
|
|
- if self.sub_protocol.run(block=block):
|
|
|
+ if self.sub_protocol.run():
|
|
|
self.sub_protocol = PushDataProtocol(self.socket, self.total_bytes, self.data_generator)
|
|
|
self.state = self.states.PUSH_DATA
|
|
|
#
|
|
|
#
|
|
|
if self.state is self.states.PUSH_DATA:
|
|
|
- if self.sub_protocol.run(block=block):
|
|
|
+ if self.sub_protocol.run():
|
|
|
self.state = self.states.DONE
|
|
|
#
|
|
|
#
|
|
@@ -502,13 +493,13 @@ class SimpleServerConnectionProtocol(Protocol):
|
|
|
#
|
|
|
self.sub_protocol = None
|
|
|
#
|
|
|
- def _run_iteration(self, block=True):
|
|
|
+ def _run_iteration(self):
|
|
|
if self.state is self.states.READY_TO_BEGIN:
|
|
|
self.sub_protocol = PullDataProtocol(self.socket)
|
|
|
self.state = self.states.PULL_DATA
|
|
|
#
|
|
|
if self.state is self.states.PULL_DATA:
|
|
|
- if self.sub_protocol.run(block=block):
|
|
|
+ if self.sub_protocol.run():
|
|
|
if self.bandwidth_callback:
|
|
|
self.bandwidth_callback(self.conn_id, self.sub_protocol.data_size, self.sub_protocol.calc_transfer_rate())
|
|
|
#
|