| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -19,12 +19,14 @@ | |
| """SMPP client module""" | ||
|
|
||
| import binascii | ||
| import collections | ||
| import logging | ||
| import select | ||
| import socket | ||
| import struct | ||
| import warnings | ||
|
|
||
| from monotonic import monotonic | ||
| from smpplib import consts, exceptions, smpp | ||
|
|
||
|
|
||
| Expand Down Expand Up | @@ -91,7 +93,6 @@ def __init__( | |
| else: | ||
| self.allow_unknown_opt_params = allow_unknown_opt_params | ||
|
|
||
|
|
||
| self._socket = self._create_socket() | ||
|
|
||
| def __enter__(self): | ||
| Expand Down Expand Up | @@ -301,7 +302,7 @@ def set_message_received_handler(self, func): | |
| def set_message_sent_handler(self, func): | ||
| """Set new function to handle message sent event""" | ||
| self.message_sent_handler = func | ||
|
|
||
| def set_query_resp_handler(self, func): | ||
| """Set new function to handle query resp event""" | ||
| self.query_resp_handler = func | ||
| Expand Down Expand Up | @@ -421,3 +422,73 @@ def query_message(self, **kwargs): | |
| qsm = smpp.make_pdu('query_sm', client=self, **kwargs) | ||
| self.send_pdu(qsm) | ||
| return qsm | ||
|
|
||
|
|
||
| class ThreadSafeClient(Client): | ||
| should_stop = False | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| # Socket polling period | ||
| select_timeout = kwargs.get('select_timeout', 1.0) | ||
|
|
||
| super(ThreadSafeClient, self).__init__(*args, **kwargs) | ||
|
|
||
| self._select_timeout = select_timeout | ||
|
|
||
| self._send_queue = collections.deque() | ||
| self._read_sock, self._send_sock = socket.socketpair() | ||
|
|
||
| # It will help not to spam the server | ||
| self._last_active_time = 0.0 | ||
|
|
||
| def accept(self, obj): | ||
| """Accept an object""" | ||
| raise NotImplementedError('not implemented') | ||
|
|
||
| def send_pdu(self, pdu, send_later=False): | ||
| if send_later: | ||
| self._send_queue.append(pdu) | ||
| self._send_sock.send(b'\x00') | ||
| return True | ||
| else: | ||
| pdu_sent = super(ThreadSafeClient, self).send_pdu(pdu) | ||
| self._last_active_time = monotonic() | ||
| return pdu_sent | ||
|
|
||
| def send_message(self, send_later=True, **kwargs): | ||
| submit_sm_pdu = smpp.make_pdu('submit_sm', client=self, **kwargs) | ||
| self.send_pdu(submit_sm_pdu, send_later=send_later) | ||
| return submit_sm_pdu | ||
|
|
||
| def _should_prolong_session(self): | ||
| # We need some time to send enquire_link before the next `select` call comes | ||
| passed_from_last_message = monotonic() - self._last_active_time | ||
|
|
||
| return self.timeout - self._select_timeout <= passed_from_last_message | ||
|
|
||
| def observe(self, ignore_error_codes=None, auto_send_enquire_link=True): | ||
| while not self.should_stop: | ||
| rlist, _, _ = select.select( | ||
| [self._socket, self._read_sock], [], [], self._select_timeout, | ||
| ) | ||
|
|
||
| if self.should_stop: | ||
| break | ||
|
|
||
| if not rlist: | ||
| if self._should_prolong_session(): | ||
| if not auto_send_enquire_link: | ||
|
Comment thread
Copy link
Copy Markdown
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityTODO: check auto_send_enquire_link before self._should_prolong_session() call
Sorry, something went wrong.
All reactions
|
||
| raise exceptions.SessionProlongationDisabled() | ||
|
|
||
| self.logger.debug('Sending enquire_link') | ||
| pdu = smpp.make_pdu('enquire_link', client=self) | ||
| self.send_pdu(pdu) | ||
| else: | ||
| for ready_socket in rlist: | ||
| if ready_socket is self._socket: | ||
| self.read_once(ignore_error_codes, auto_send_enquire_link) | ||
| else: | ||
| self._read_sock.recv(1) | ||
| self.send_pdu(self._send_queue.pop()) | ||
|
|
||
| self.logger.info('Finished observing...') | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWhy are we sending a 00? Is it a no-op? If so, why are we sending it?
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIt is some sort of notification frame. We put it into the send socket to wake up our read socket
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.