0% found this document useful (0 votes)
155 views12 pages

SIGINT Handler for Engine.IO Client

This document defines a Client class for connecting to an Engine.IO server using websocket or long-polling transports. The class handles connecting to the server, sending and receiving data, and disconnecting. Key methods include connect(), on() to register event handlers, send() to send data, and disconnect() to disconnect from the server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views12 pages

SIGINT Handler for Engine.IO Client

This document defines a Client class for connecting to an Engine.IO server using websocket or long-polling transports. The class handles connecting to the server, sending and receiving data, and disconnecting. Key methods include connect(), on() to register event handlers, send() to send data, and disconnect() to disconnect from the server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import logging

try:
import queue
except ImportError: # pragma: no cover
import Queue as queue
import signal
import ssl
import threading
import time

import six
from [Link] import urllib
try:
import requests
except ImportError: # pragma: no cover
requests = None
try:
import websocket
except ImportError: # pragma: no cover
websocket = None
from . import exceptions
from . import packet
from . import payload

default_logger = [Link]('[Link]')
connected_clients = []

if six.PY2: # pragma: no cover


ConnectionError = OSError

def signal_handler(sig, frame):


"""SIGINT handler.

Disconnect all active clients and then invoke the original signal handler.
"""
for client in connected_clients[:]:
if client.is_asyncio_based():
client.start_background_task([Link], abort=True)
else:
[Link](abort=True)
if callable(original_signal_handler):
return original_signal_handler(sig, frame)
else: # pragma: no cover
# Handle case where no original SIGINT handler was present.
return signal.default_int_handler(sig, frame)

original_signal_handler = None

class Client(object):
"""An [Link] client.

This class implements a fully compliant [Link] web client with support
for websocket and long-polling transports.

:param logger: To enable logging set to ``True`` or pass a logger object to


use. To disable logging set to ``False``. The default is
``False``.
:param json: An alternative json module to use for encoding and decoding
packets. Custom json modules must have ``dumps`` and ``loads``
functions that are compatible with the standard library
versions.
:param request_timeout: A timeout in seconds for requests. The default is
5 seconds.
:param ssl_verify: ``True`` to verify SSL certificates, or ``False`` to
skip SSL certificate verification, allowing
connections to servers with self signed certificates.
The default is ``True``.
"""
event_names = ['connect', 'disconnect', 'message']

def __init__(self,
logger=False,
json=None,
request_timeout=5,
ssl_verify=True):
global original_signal_handler
if original_signal_handler is None:
original_signal_handler = [Link]([Link],
signal_handler)
[Link] = {}
self.base_url = None
[Link] = None
self.current_transport = None
[Link] = None
[Link] = None
self.ping_interval = None
self.ping_timeout = None
self.pong_received = True
[Link] = None
[Link] = None
self.read_loop_task = None
self.write_loop_task = None
self.ping_loop_task = None
self.ping_loop_event = None
[Link] = None
[Link] = 'disconnected'
self.ssl_verify = ssl_verify

if json is not None:


[Link] = json
if not isinstance(logger, bool):
[Link] = logger
else:
[Link] = default_logger
if not [Link] and \
[Link] == [Link]:
if logger:
[Link]([Link])
else:
[Link]([Link])
[Link]([Link]())

self.request_timeout = request_timeout

def is_asyncio_based(self):
return False

def on(self, event, handler=None):


"""Register an event handler.

:param event: The event name. Can be ``'connect'``, ``'message'`` or


``'disconnect'``.
:param handler: The function that should be invoked to handle the
event. When this parameter is not given, the method
acts as a decorator for the handler function.

Example usage::

# as a decorator:
@[Link]('connect')
def connect_handler():
print('Connection request')

# as a method:
def message_handler(msg):
print('Received message: ', msg)
[Link]('response')
[Link]('message', message_handler)
"""
if event not in self.event_names:
raise ValueError('Invalid event')

def set_handler(handler):
[Link][event] = handler
return handler

if handler is None:
return set_handler
set_handler(handler)

def connect(self, url, headers={}, transports=None,


engineio_path='[Link]'):
"""Connect to an [Link] server.

:param url: The URL of the [Link] server. It can include custom
query string parameters if required by the server.
:param headers: A dictionary with custom headers to send with the
connection request.
:param transports: The list of allowed transports. Valid transports
are ``'polling'`` and ``'websocket'``. If not
given, the polling transport is connected first,
then an upgrade to websocket is attempted.
:param engineio_path: The endpoint where the [Link] server is
installed. The default value is appropriate for
most cases.

Example usage::

eio = [Link]()
[Link]('[Link]
"""
if [Link] != 'disconnected':
raise ValueError('Client is not in a disconnected state')
valid_transports = ['polling', 'websocket']
if transports is not None:
if isinstance(transports, six.string_types):
transports = [transports]
transports = [transport for transport in transports
if transport in valid_transports]
if not transports:
raise ValueError('No valid transports provided')
[Link] = transports or valid_transports
[Link] = self.create_queue()
return getattr(self, '_connect_' + [Link][0])(
url, headers, engineio_path)

def wait(self):
"""Wait until the connection with the server ends.

Client applications can use this function to block the main thread
during the life of the connection.
"""
if self.read_loop_task:
self.read_loop_task.join()

def send(self, data, binary=None):


"""Send a message to a client.

:param data: The data to send to the client. Data can be of type
``str``, ``bytes``, ``list`` or ``dict``. If a ``list``
or ``dict``, the data will be serialized as JSON.
:param binary: ``True`` to send packet as binary, ``False`` to send
as text. If not given, unicode (Python 2) and str
(Python 3) are sent as text, and str (Python 2) and
bytes (Python 3) are sent as binary.
"""
self._send_packet([Link]([Link], data=data,
binary=binary))

def disconnect(self, abort=False):


"""Disconnect from the server.

:param abort: If set to ``True``, do not wait for background tasks


associated with the connection to end.
"""
if [Link] == 'connected':
self._send_packet([Link]([Link]))
[Link](None)
[Link] = 'disconnecting'
self._trigger_event('disconnect', run_async=False)
if self.current_transport == 'websocket':
[Link]()
if not abort:
self.read_loop_task.join()
[Link] = 'disconnected'
try:
connected_clients.remove(self)
except ValueError: # pragma: no cover
pass
self._reset()

def transport(self):
"""Return the name of the transport currently in use.
The possible values returned by this function are ``'polling'`` and
``'websocket'``.
"""
return self.current_transport

def start_background_task(self, target, *args, **kwargs):


"""Start a background task.

This is a utility function that applications can use to start a


background task.

:param target: the target function to execute.


:param args: arguments to pass to the function.
:param kwargs: keyword arguments to pass to the function.

This function returns an object compatible with the `Thread` class in


the Python standard library. The `start()` method on this object is
already called by this function.
"""
th = [Link](target=target, args=args, kwargs=kwargs)
[Link]()
return th

def sleep(self, seconds=0):


"""Sleep for the requested amount of time."""
return [Link](seconds)

def create_queue(self, *args, **kwargs):


"""Create a queue object."""
q = [Link](*args, **kwargs)
[Link] = [Link]
return q

def create_event(self, *args, **kwargs):


"""Create an event object."""
return [Link](*args, **kwargs)

def _reset(self):
[Link] = 'disconnected'
[Link] = None

def _connect_polling(self, url, headers, engineio_path):


"""Establish a long-polling connection to the [Link] server."""
if requests is None: # pragma: no cover
# not installed
[Link]('requests package is not installed -- cannot '
'send HTTP requests!')
return
self.base_url = self._get_engineio_url(url, engineio_path, 'polling')
[Link]('Attempting polling connection to ' + self.base_url)
r = self._send_request(
'GET', self.base_url + self._get_url_timestamp(), headers=headers,
timeout=self.request_timeout)
if r is None:
self._reset()
raise [Link](
'Connection refused by the server')
if r.status_code < 200 or r.status_code >= 300:
raise [Link](
'Unexpected status code {} in server response'.format(
r.status_code))
try:
p = [Link](encoded_payload=[Link])
except ValueError:
six.raise_from([Link](
'Unexpected response from server'), None)
open_packet = [Link][0]
if open_packet.packet_type != [Link]:
raise [Link](
'OPEN packet not returned by server')
[Link](
'Polling connection accepted with ' + str(open_packet.data))
[Link] = open_packet.data['sid']
[Link] = open_packet.data['upgrades']
self.ping_interval = open_packet.data['pingInterval'] / 1000.0
self.ping_timeout = open_packet.data['pingTimeout'] / 1000.0
self.current_transport = 'polling'
self.base_url += '&sid=' + [Link]

[Link] = 'connected'
connected_clients.append(self)
self._trigger_event('connect', run_async=False)

for pkt in [Link][1:]:


self._receive_packet(pkt)

if 'websocket' in [Link] and 'websocket' in [Link]:


# attempt to upgrade to websocket
if self._connect_websocket(url, headers, engineio_path):
# upgrade to websocket succeeded, we're done here
return

# start background tasks associated with this client


self.ping_loop_task = self.start_background_task(self._ping_loop)
self.write_loop_task = self.start_background_task(self._write_loop)
self.read_loop_task = self.start_background_task(
self._read_loop_polling)

def _connect_websocket(self, url, headers, engineio_path):


"""Establish or upgrade to a WebSocket connection with the server."""
if websocket is None: # pragma: no cover
# not installed
[Link]('websocket-client package not installed, only '
'polling transport is available')
return False
websocket_url = self._get_engineio_url(url, engineio_path, 'websocket')
if [Link]:
[Link](
'Attempting WebSocket upgrade to ' + websocket_url)
upgrade = True
websocket_url += '&sid=' + [Link]
else:
upgrade = False
self.base_url = websocket_url
[Link](
'Attempting WebSocket connection to ' + websocket_url)
# get the cookies from the long-polling connection so that they can
# also be sent the the WebSocket route
cookies = None
if [Link]:
cookies = '; '.join(["{}={}".format([Link], [Link])
for cookie in [Link]])

try:
if not self.ssl_verify:
ws = websocket.create_connection(
websocket_url + self._get_url_timestamp(), header=headers,
cookie=cookies, sslopt={"cert_reqs": ssl.CERT_NONE})
else:
ws = websocket.create_connection(
websocket_url + self._get_url_timestamp(), header=headers,
cookie=cookies)
except (ConnectionError, IOError, [Link]):
if upgrade:
[Link](
'WebSocket upgrade failed: connection error')
return False
else:
raise [Link]('Connection error')
if upgrade:
p = [Link]([Link],
data=six.text_type('probe')).encode()
try:
[Link](p)
except Exception as e: # pragma: no cover
[Link](
'WebSocket upgrade failed: unexpected send exception: %s',
str(e))
return False
try:
p = [Link]()
except Exception as e: # pragma: no cover
[Link](
'WebSocket upgrade failed: unexpected recv exception: %s',
str(e))
return False
pkt = [Link](encoded_packet=p)
if pkt.packet_type != [Link] or [Link] != 'probe':
[Link](
'WebSocket upgrade failed: no PONG packet')
return False
p = [Link]([Link]).encode()
try:
[Link](p)
except Exception as e: # pragma: no cover
[Link](
'WebSocket upgrade failed: unexpected send exception: %s',
str(e))
return False
self.current_transport = 'websocket'
[Link]('WebSocket upgrade was successful')
else:
try:
p = [Link]()
except Exception as e: # pragma: no cover
raise [Link](
'Unexpected recv exception: ' + str(e))
open_packet = [Link](encoded_packet=p)
if open_packet.packet_type != [Link]:
raise [Link]('no OPEN packet')
[Link](
'WebSocket connection accepted with ' + str(open_packet.data))
[Link] = open_packet.data['sid']
[Link] = open_packet.data['upgrades']
self.ping_interval = open_packet.data['pingInterval'] / 1000.0
self.ping_timeout = open_packet.data['pingTimeout'] / 1000.0
self.current_transport = 'websocket'

[Link] = 'connected'
connected_clients.append(self)
self._trigger_event('connect', run_async=False)
[Link] = ws

# start background tasks associated with this client


self.ping_loop_task = self.start_background_task(self._ping_loop)
self.write_loop_task = self.start_background_task(self._write_loop)
self.read_loop_task = self.start_background_task(
self._read_loop_websocket)
return True

def _receive_packet(self, pkt):


"""Handle incoming packets from the server."""
packet_name = packet.packet_names[pkt.packet_type] \
if pkt.packet_type < len(packet.packet_names) else 'UNKNOWN'
[Link](
'Received packet %s data %s', packet_name,
[Link] if not isinstance([Link], bytes) else '<binary>')
if pkt.packet_type == [Link]:
self._trigger_event('message', [Link], run_async=True)
elif pkt.packet_type == [Link]:
self.pong_received = True
elif pkt.packet_type == [Link]:
[Link](abort=True)
elif pkt.packet_type == [Link]:
pass
else:
[Link]('Received unexpected packet of type %s',
pkt.packet_type)

def _send_packet(self, pkt):


"""Queue a packet to be sent to the server."""
if [Link] != 'connected':
return
[Link](pkt)
[Link](
'Sending packet %s data %s',
packet.packet_names[pkt.packet_type],
[Link] if not isinstance([Link], bytes) else '<binary>')

def _send_request(
self, method, url, headers=None, body=None,
timeout=None): # pragma: no cover
if [Link] is None:
[Link] = [Link]()
try:
return [Link](method, url, headers=headers, data=body,
timeout=timeout, verify=self.ssl_verify)
except [Link] as exc:
[Link]('HTTP %s request to %s failed with error %s.',
method, url, exc)

def _trigger_event(self, event, *args, **kwargs):


"""Invoke an event handler."""
run_async = [Link]('run_async', False)
if event in [Link]:
if run_async:
return self.start_background_task([Link][event], *args)
else:
try:
return [Link][event](*args)
except:
[Link](event + ' handler error')

def _get_engineio_url(self, url, engineio_path, transport):


"""Generate the [Link] connection URL."""
engineio_path = engineio_path.strip('/')
parsed_url = [Link](url)

if transport == 'polling':
scheme = 'http'
elif transport == 'websocket':
scheme = 'ws'
else: # pragma: no cover
raise ValueError('invalid transport')
if parsed_url.scheme in ['https', 'wss']:
scheme += 's'

return ('{scheme}://{netloc}/{path}/?{query}'
'{sep}transport={transport}&EIO=3').format(
scheme=scheme, netloc=parsed_url.netloc,
path=engineio_path, query=parsed_url.query,
sep='&' if parsed_url.query else '',
transport=transport)

def _get_url_timestamp(self):
"""Generate the [Link] query string timestamp."""
return '&t=' + str([Link]())

def _ping_loop(self):
"""This background task sends a PING to the server at the requested
interval.
"""
self.pong_received = True
if self.ping_loop_event is None:
self.ping_loop_event = self.create_event()
else:
self.ping_loop_event.clear()
while [Link] == 'connected':
if not self.pong_received:
[Link](
'PONG response has not been received, aborting')
if [Link]:
[Link](timeout=0)
[Link](None)
break
self.pong_received = False
self._send_packet([Link]([Link]))
self.ping_loop_event.wait(timeout=self.ping_interval)
[Link]('Exiting ping task')

def _read_loop_polling(self):
"""Read packets by polling the [Link] server."""
while [Link] == 'connected':
[Link](
'Sending polling GET request to ' + self.base_url)
r = self._send_request(
'GET', self.base_url + self._get_url_timestamp(),
timeout=max(self.ping_interval, self.ping_timeout) + 5)
if r is None:
[Link](
'Connection refused by the server, aborting')
[Link](None)
break
if r.status_code < 200 or r.status_code >= 300:
[Link]('Unexpected status code %s in server '
'response, aborting', r.status_code)
[Link](None)
break
try:
p = [Link](encoded_payload=[Link])
except ValueError:
[Link](
'Unexpected packet from server, aborting')
[Link](None)
break
for pkt in [Link]:
self._receive_packet(pkt)

[Link]('Waiting for write loop task to end')


self.write_loop_task.join()
[Link]('Waiting for ping loop task to end')
if self.ping_loop_event: # pragma: no cover
self.ping_loop_event.set()
self.ping_loop_task.join()
if [Link] == 'connected':
self._trigger_event('disconnect', run_async=False)
try:
connected_clients.remove(self)
except ValueError: # pragma: no cover
pass
self._reset()
[Link]('Exiting read loop task')

def _read_loop_websocket(self):
"""Read packets from the [Link] WebSocket connection."""
while [Link] == 'connected':
p = None
try:
p = [Link]()
except [Link]:
[Link](
'WebSocket connection was closed, aborting')
[Link](None)
break
except Exception as e:
[Link](
'Unexpected error "%s", aborting', str(e))
[Link](None)
break
if isinstance(p, six.text_type): # pragma: no cover
p = [Link]('utf-8')
pkt = [Link](encoded_packet=p)
self._receive_packet(pkt)

[Link]('Waiting for write loop task to end')


self.write_loop_task.join()
[Link]('Waiting for ping loop task to end')
if self.ping_loop_event: # pragma: no cover
self.ping_loop_event.set()
self.ping_loop_task.join()
if [Link] == 'connected':
self._trigger_event('disconnect', run_async=False)
try:
connected_clients.remove(self)
except ValueError: # pragma: no cover
pass
self._reset()
[Link]('Exiting read loop task')

def _write_loop(self):
"""This background task sends packages to the server as they are
pushed to the send queue.
"""
while [Link] == 'connected':
# to simplify the timeout handling, use the maximum of the
# ping interval and ping timeout as timeout, with an extra 5
# seconds grace period
timeout = max(self.ping_interval, self.ping_timeout) + 5
packets = None
try:
packets = [[Link](timeout=timeout)]
except [Link]:
[Link]('packet queue is empty, aborting')
break
if packets == [None]:
[Link].task_done()
packets = []
else:
while True:
try:
[Link]([Link](block=False))
except [Link]:
break
if packets[-1] is None:
packets = packets[:-1]
[Link].task_done()
break
if not packets:
# empty packet list returned -> connection closed
break
if self.current_transport == 'polling':
p = [Link](packets=packets)
r = self._send_request(
'POST', self.base_url, body=[Link](),
headers={'Content-Type': 'application/octet-stream'},
timeout=self.request_timeout)
for pkt in packets:
[Link].task_done()
if r is None:
[Link](
'Connection refused by the server, aborting')
break
if r.status_code < 200 or r.status_code >= 300:
[Link]('Unexpected status code %s in server '
'response, aborting', r.status_code)
self._reset()
break
else:
# websocket
try:
for pkt in packets:
encoded_packet = [Link](always_bytes=False)
if [Link]:
[Link].send_binary(encoded_packet)
else:
[Link](encoded_packet)
[Link].task_done()
except [Link]:
[Link](
'WebSocket connection was closed, aborting')
break
[Link]('Exiting write loop task')

Common questions

Powered by AI

Key factors influencing the transition from polling to WebSocket include availability of WebSocket in the `transports` list, support indicated by the server via the `upgrades` field in the OPEN packet, and successful receipt of a PONG packet during the upgrade attempt . The transition is managed by attempting a WebSocket connection if WebSockets are supported. If successful, the transport protocol is switched, background tasks are adapted to the new transport mode, and the client starts using WebSocket exclusively, improving the efficiency and performance of data transmission .

Connections with an Engine.IO server can be established using either a long-polling method or a WebSocket method. Long-polling involves repeatedly sending HTTP requests to retrieve data, while WebSocket establishes a persistent connection allowing bi-directional communication. The long-polling connection sends a GET request to the server, anticipates an OPEN packet to confirm the connection, and maintains the connection by periodic polling . In contrast, a WebSocket connection either upgrades from a polling connection or connects directly, requiring an OPEN packet for confirmation .

The primary configurations available during the initialization of an Engine.IO client include logging options, custom JSON modules, request timeout durations, and SSL verification settings. Logging enables monitoring of client activities, which aids in debugging and performance tuning . Custom JSON modules allow flexibility in data serialization . Request timeout configures the duration before a connection attempt is aborted, impacting the responsiveness of the client in handling slow networks . SSL verification ensures secure connections, critical for preventing man-in-the-middle attacks, though it may be disabled for self-signed certificates .

The `start_background_task` method is integral in the Engine.IO client architecture, enabling asynchronous operations by allowing client components to run simultaneously without blocking the main application thread. By abstracting threading complexities, it provides a standard interface for executing separate tasks like ping, write, and read loops. This method aligns with the design principle of modularity, allowing key client functions to operate independently, enhancing efficiency, responsiveness, and scalability of the client .

The `signal_handler` function intercepts SIGINT signals, usually generated by interrupting the client process. Its role is to systematically disconnect all active clients by invoking their disconnect method. This function ensures a structured shutdown of client instances, thereby preventing issues like incomplete data handling or corruption. By restoring the previous signal handler (if any), it also integrates smoothly with existing signal handling systems without disrupting overall application control flow .

Not handling `queue.Empty` exceptions could lead to the write loop attempting to access a non-existent packet, causing crashes or undefined behavior. The current implementation mitigates this by catching `queue.Empty` exceptions, logging an error, and breaking the loop to terminate the task if the packet queue is found empty beyond a timeout period. This ensures that connection closure does not happen prematurely and resource allocation is handled correctly, capturing and addressing potential packet sending issues gracefully .

The client identifies incoming packets by parsing the packet type, which is encoded in the payload. Based on the packet type, specific actions are performed: MESSAGE packets trigger a 'message' event, PONG packets confirm a response to a prior PING packet, CLOSE packets initiate a disconnect process, and NOOP packets are ignored as they signify no operation. This packet differentiation enables the client to manage connection states, maintain communication integrity, and execute protocol-specific tasks efficiently .

During the WebSocket upgrade process, error handling is implemented through try-except blocks, which catch exceptions like ConnectionError, IOError, and WebSocketException. Additionally, specific warnings are logged for unexpected exceptions during packet send and receive operations. This error handling prevents issues such as abrupt termination of connection attempts, improper packet reception, and handling failures. It ensures the client gracefully handles failures during a WebSocket upgrade, possibly falling back to polling, avoiding abrupt application crashes .

The `disconnect` method first sends a CLOSE packet to the server to inform about the disconnection. It transitions the client state to 'disconnecting', handles the closure of WebSocket connections if open, and waits for related background tasks (ping, write, and read loops) to gracefully stop unless 'abort' is set to True, which forces an immediate disconnection. This workflow ensures orderly termination of tasks and resources, crucial for maintaining application stability, preventing resource leaks, and avoiding abrupt communication terminations .

The Engine.IO client manages background tasks through the creation and execution of threads for ping, write, and read loops. The ping loop sends PING packets to ensure the server's connection is active. The write loop handles packet queue transmission to the server, and the read loop processes incoming packets. These tasks are critical because they maintain connection health, handle real-time communication, and manage packet flow. Without these, the client might fail to keep the connection alive, potentially causing connection drops or miscommunications .

You might also like