Skip to content

Commit

Permalink
Update session.py (#576)
Browse files Browse the repository at this point in the history
With the original ordering of send of messages and polling for incoming messages, it was possible for certain platforms (e.g. Huawei) to trigger the change from BASE_10 encoding to BASE_11 for the `<hello/>` message.

---------

Co-authored-by: Einar Nilsen-Nygaard <einarnn@gmail.com>
  • Loading branch information
iLaus and einarnn committed Dec 26, 2023
1 parent f8207de commit 9e947eb
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions ncclient/transport/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ def start_delim(data_len):
self._transport_register(s, selectors.EVENT_READ)
self.logger.debug('selector type = %s', s.__class__.__name__)
while True:

if not q.empty() and self._send_ready():
self.logger.debug("Sending message")
data = q.get().encode()
if self._base == NetconfBase.BASE_11:
data = b"%s%s%s" % (start_delim(len(data)), data, END_DELIM)
else:
data = b"%s%s" % (data, MSG_DELIM)
self.logger.info("Sending:\n%s", data)
while data:
n = self._transport_write(data)
if n <= 0:
raise SessionCloseError(self._buffer.getvalue(), data)
data = data[n:]

events = s.select(timeout=TICK)
if events:
data = self._transport_read()
Expand All @@ -234,19 +249,6 @@ def start_delim(data_len):
else:
# End of session, unexpected
raise SessionCloseError(self._buffer.getvalue())
if not q.empty() and self._send_ready():
self.logger.debug("Sending message")
data = q.get().encode()
if self._base == NetconfBase.BASE_11:
data = b"%s%s%s" % (start_delim(len(data)), data, END_DELIM)
else:
data = b"%s%s" % (data, MSG_DELIM)
self.logger.info("Sending:\n%s", data)
while data:
n = self._transport_write(data)
if n <= 0:
raise SessionCloseError(self._buffer.getvalue(), data)
data = data[n:]
except Exception as e:
self.logger.debug("Broke out of main loop, error=%r", e)
self._dispatch_error(e)
Expand Down

0 comments on commit 9e947eb

Please sign in to comment.