Skip to content

Commit

Permalink
Ensure single thread send data at a time.
Browse files Browse the repository at this point in the history
  • Loading branch information
farhanmustar committed Nov 6, 2024
1 parent c6f804a commit d3693cf
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions secsgem/hsms/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(self, active, address, port, session_id=0, delegate=None):

# connection socket
self.sock = None
self.lock = threading.Lock()

# buffer for received data
self.receiveBuffer = b""
Expand Down Expand Up @@ -178,32 +179,34 @@ def send_packet(self, packet):
:param packet: encoded data to be transmitted
:type packet: string / byte array
"""
# encode the packet
data = packet.encode()

for i in range(0, len(data), self.send_block_size):
block = data[i: i + self.send_block_size]
retry = True
with self.lock:
# encode the packet
data = packet.encode()

# not sent yet, retry
while retry:
# wait until socket is writable
while not select.select([], [self.sock], [], self.select_timeout)[1]:
pass
for i in range(0, len(data), self.send_block_size):
block = data[i: i + self.send_block_size]
retry = True

try:
# send packet
self.sock.send(block)
# not sent yet, retry
while retry:
# wait until socket is writable
while not select.select([], [self.sock], [], self.select_timeout)[1]:
pass

# retry will be cleared if send succeeded
retry = False
except OSError as exc:
if not secsgem.common.is_errorcode_ewouldblock(exc.errno):
# raise if not EWOULDBLOCK
return False
# it is EWOULDBLOCK, so retry sending
try:
# send packet
self.sock.send(block)

# retry will be cleared if send succeeded
retry = False
except OSError as exc:
if not secsgem.common.is_errorcode_ewouldblock(exc.errno):
# raise if not EWOULDBLOCK
return False
# it is EWOULDBLOCK, so retry sending

return True
return True

def _process_receive_buffer(self):
"""
Expand Down

0 comments on commit d3693cf

Please sign in to comment.