Skip to content

Commit 823061d

Browse files
MsgPackPacket.configure method
1 parent 6c899d7 commit 823061d

File tree

12 files changed

+73
-141
lines changed

12 files changed

+73
-141
lines changed

src/socketio/async_client.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ class AsyncClient(base_client.BaseClient):
4545
leave interrupt handling to the calling application.
4646
Interrupt handling can only be enabled when the
4747
client instance is created in the main thread.
48-
:param serializer_args: A mapping of additional parameters to pass to
49-
the serializer. The content of this dictionary
50-
depends on the selected serialization method.
5148
5249
The Engine.IO configuration supports the following settings:
5350
@@ -246,7 +243,7 @@ async def emit(self, event, data=None, namespace=None, callback=None):
246243
data = [data]
247244
else:
248245
data = []
249-
await self._send_packet(self._create_packet(
246+
await self._send_packet(self.packet_class(
250247
packet.EVENT, namespace=namespace, data=[event] + data, id=id))
251248

252249
async def send(self, data, namespace=None, callback=None):
@@ -328,7 +325,7 @@ async def disconnect(self):
328325
# here we just request the disconnection
329326
# later in _handle_eio_disconnect we invoke the disconnect handler
330327
for n in self.namespaces:
331-
await self._send_packet(self._create_packet(packet.DISCONNECT,
328+
await self._send_packet(self.packet_class(packet.DISCONNECT,
332329
namespace=n))
333330
await self.eio.disconnect()
334331

@@ -425,7 +422,7 @@ async def _handle_event(self, namespace, id, data):
425422
data = list(r)
426423
else:
427424
data = [r]
428-
await self._send_packet(self._create_packet(
425+
await self._send_packet(self.packet_class(
429426
packet.ACK, namespace=namespace, id=id, data=data))
430427

431428
async def _handle_ack(self, namespace, id, data):
@@ -558,7 +555,7 @@ async def _handle_eio_connect(self):
558555
self.sid = self.eio.sid
559556
real_auth = await self._get_real_value(self.connection_auth) or {}
560557
for n in self.connection_namespaces:
561-
await self._send_packet(self._create_packet(
558+
await self._send_packet(self.packet_class(
562559
packet.CONNECT, data=real_auth, namespace=n))
563560

564561
async def _handle_eio_message(self, data):
@@ -572,7 +569,7 @@ async def _handle_eio_message(self, data):
572569
else:
573570
await self._handle_ack(pkt.namespace, pkt.id, pkt.data)
574571
else:
575-
pkt = self._create_packet(encoded_packet=data)
572+
pkt = self.packet_class(encoded_packet=data)
576573
if pkt.packet_type == packet.CONNECT:
577574
await self._handle_connect(pkt.namespace, pkt.data)
578575
elif pkt.packet_type == packet.DISCONNECT:

src/socketio/async_server.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ class AsyncServer(base_server.BaseServer):
5050
default is `['/']`, which always accepts connections to
5151
the default namespace. Set to `'*'` to accept all
5252
namespaces.
53-
:param serializer_args: A mapping of additional parameters to pass to
54-
the serializer. The content of this dictionary
55-
depends on the selected serialization method.
5653
:param kwargs: Connection parameters for the underlying Engine.IO server.
5754
5855
The Engine.IO configuration supports the following settings:
@@ -428,7 +425,7 @@ async def disconnect(self, sid, namespace=None, ignore_queue=False):
428425
if delete_it:
429426
self.logger.info('Disconnecting %s [%s]', sid, namespace)
430427
eio_sid = self.manager.pre_disconnect(sid, namespace=namespace)
431-
await self._send_packet(eio_sid, self._create_packet(
428+
await self._send_packet(eio_sid, self.packet_class(
432429
packet.DISCONNECT, namespace=namespace))
433430
await self._trigger_event('disconnect', namespace, sid,
434431
self.reason.SERVER_DISCONNECT)
@@ -541,13 +538,13 @@ async def _handle_connect(self, eio_sid, namespace, data):
541538
or self.namespaces == '*' or namespace in self.namespaces:
542539
sid = await self.manager.connect(eio_sid, namespace)
543540
if sid is None:
544-
await self._send_packet(eio_sid, self._create_packet(
541+
await self._send_packet(eio_sid, self.packet_class(
545542
packet.CONNECT_ERROR, data='Unable to connect',
546543
namespace=namespace))
547544
return
548545

549546
if self.always_connect:
550-
await self._send_packet(eio_sid, self._create_packet(
547+
await self._send_packet(eio_sid, self.packet_class(
551548
packet.CONNECT, {'sid': sid}, namespace=namespace))
552549
fail_reason = exceptions.ConnectionRefusedError().error_args
553550
try:
@@ -571,15 +568,15 @@ async def _handle_connect(self, eio_sid, namespace, data):
571568
if success is False:
572569
if self.always_connect:
573570
self.manager.pre_disconnect(sid, namespace)
574-
await self._send_packet(eio_sid, self._create_packet(
571+
await self._send_packet(eio_sid, self.packet_class(
575572
packet.DISCONNECT, data=fail_reason, namespace=namespace))
576573
else:
577-
await self._send_packet(eio_sid, self._create_packet(
574+
await self._send_packet(eio_sid, self.packet_class(
578575
packet.CONNECT_ERROR, data=fail_reason,
579576
namespace=namespace))
580577
await self.manager.disconnect(sid, namespace, ignore_queue=True)
581578
elif not self.always_connect:
582-
await self._send_packet(eio_sid, self._create_packet(
579+
await self._send_packet(eio_sid, self.packet_class(
583580
packet.CONNECT, {'sid': sid}, namespace=namespace))
584581

585582
async def _handle_disconnect(self, eio_sid, namespace, reason=None):
@@ -625,7 +622,7 @@ async def _handle_event_internal(self, server, sid, eio_sid, data,
625622
data = list(r)
626623
else:
627624
data = [r]
628-
await server._send_packet(eio_sid, self._create_packet(
625+
await server._send_packet(eio_sid, self.packet_class(
629626
packet.ACK, namespace=namespace, id=id, data=data))
630627

631628
async def _handle_ack(self, eio_sid, namespace, id, data):
@@ -689,7 +686,7 @@ async def _handle_eio_message(self, eio_sid, data):
689686
await self._handle_ack(eio_sid, pkt.namespace, pkt.id,
690687
pkt.data)
691688
else:
692-
pkt = self._create_packet(encoded_packet=data)
689+
pkt = self.packet_class(encoded_packet=data)
693690
if pkt.packet_type == packet.CONNECT:
694691
await self._handle_connect(eio_sid, pkt.namespace, pkt.data)
695692
elif pkt.packet_type == packet.DISCONNECT:

src/socketio/base_client.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class BaseClient:
3838
def __init__(self, reconnection=True, reconnection_attempts=0,
3939
reconnection_delay=1, reconnection_delay_max=5,
4040
randomization_factor=0.5, logger=False, serializer='default',
41-
json=None, handle_sigint=True, serializer_args=None,
42-
**kwargs):
41+
json=None, handle_sigint=True, **kwargs):
4342
global original_signal_handler
4443
if handle_sigint and original_signal_handler is None and \
4544
threading.current_thread() == threading.main_thread():
@@ -64,7 +63,6 @@ def __init__(self, reconnection=True, reconnection_attempts=0,
6463
self.packet_class = msgpack_packet.MsgPackPacket
6564
else:
6665
self.packet_class = serializer
67-
self.packet_class_args = serializer_args or {}
6866
if json is not None:
6967
self.packet_class.json = json
7068
engineio_options['json'] = json
@@ -285,9 +283,6 @@ def _generate_ack_id(self, namespace, callback):
285283
self.callbacks[namespace][id] = callback
286284
return id
287285

288-
def _create_packet(self, *args, **kwargs):
289-
return self.packet_class(*args, **kwargs, **self.packet_class_args)
290-
291286
def _handle_eio_connect(self): # pragma: no cover
292287
raise NotImplementedError()
293288

src/socketio/base_server.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class BaseServer:
1515

1616
def __init__(self, client_manager=None, logger=False, serializer='default',
1717
json=None, async_handlers=True, always_connect=False,
18-
namespaces=None, serializer_args=None, **kwargs):
18+
namespaces=None, **kwargs):
1919
engineio_options = kwargs
2020
engineio_logger = engineio_options.pop('engineio_logger', None)
2121
if engineio_logger is not None:
@@ -27,7 +27,6 @@ def __init__(self, client_manager=None, logger=False, serializer='default',
2727
self.packet_class = msgpack_packet.MsgPackPacket
2828
else:
2929
self.packet_class = serializer
30-
self.packet_class_args = serializer_args or {}
3130
if json is not None:
3231
self.packet_class.json = json
3332
engineio_options['json'] = json
@@ -254,10 +253,6 @@ def _get_namespace_handler(self, namespace, args):
254253
args = (namespace, *args)
255254
return handler, args
256255

257-
def _create_packet(self, *args, **kwargs):
258-
return self.packet_class(*args, **kwargs,
259-
**self.packet_class_args)
260-
261256
def _handle_eio_connect(self): # pragma: no cover
262257
raise NotImplementedError()
263258

src/socketio/client.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ class Client(base_client.BaseClient):
4848
leave interrupt handling to the calling application.
4949
Interrupt handling can only be enabled when the
5050
client instance is created in the main thread.
51-
:param serializer_args: A mapping of additional parameters to pass to
52-
the serializer. The content of this dictionary
53-
depends on the selected serialization method.
5451
5552
The Engine.IO configuration supports the following settings:
5653
@@ -238,8 +235,8 @@ def emit(self, event, data=None, namespace=None, callback=None):
238235
else:
239236
data = []
240237
self._send_packet(
241-
self._create_packet(packet.EVENT, namespace=namespace,
242-
data=[event] + data, id=id))
238+
self.packet_class(packet.EVENT, namespace=namespace,
239+
data=[event] + data, id=id))
243240

244241
def send(self, data, namespace=None, callback=None):
245242
"""Send a message to the server.
@@ -311,7 +308,7 @@ def disconnect(self):
311308
# here we just request the disconnection
312309
# later in _handle_eio_disconnect we invoke the disconnect handler
313310
for n in self.namespaces:
314-
self._send_packet(self._create_packet(
311+
self._send_packet(self.packet_class(
315312
packet.DISCONNECT, namespace=n))
316313
self.eio.disconnect()
317314

@@ -406,7 +403,7 @@ def _handle_event(self, namespace, id, data):
406403
data = list(r)
407404
else:
408405
data = [r]
409-
self._send_packet(self._create_packet(
406+
self._send_packet(self.packet_class(
410407
packet.ACK, namespace=namespace, id=id, data=data))
411408

412409
def _handle_ack(self, namespace, id, data):
@@ -510,7 +507,7 @@ def _handle_eio_connect(self):
510507
self.sid = self.eio.sid
511508
real_auth = self._get_real_value(self.connection_auth) or {}
512509
for n in self.connection_namespaces:
513-
self._send_packet(self._create_packet(
510+
self._send_packet(self.packet_class(
514511
packet.CONNECT, data=real_auth, namespace=n))
515512

516513
def _handle_eio_message(self, data):
@@ -524,7 +521,7 @@ def _handle_eio_message(self, data):
524521
else:
525522
self._handle_ack(pkt.namespace, pkt.id, pkt.data)
526523
else:
527-
pkt = self._create_packet(encoded_packet=data)
524+
pkt = self.packet_class(encoded_packet=data)
528525
if pkt.packet_type == packet.CONNECT:
529526
self._handle_connect(pkt.namespace, pkt.data)
530527
elif pkt.packet_type == packet.DISCONNECT:

src/socketio/msgpack_packet.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,28 @@
44

55
class MsgPackPacket(packet.Packet):
66
uses_binary_events = False
7+
dumps_default = None
8+
ext_hook = None
79

8-
def __init__(
9-
self,
10-
packet_type=packet.EVENT,
11-
data=None,
12-
namespace=None,
13-
id=None,
14-
binary=None,
15-
encoded_packet=None,
16-
dumps_default=None,
17-
ext_hook=None,
18-
):
19-
self.dumps_default = dumps_default
20-
self.ext_hook = ext_hook
21-
super().__init__(
22-
packet_type, data, namespace, id, binary, encoded_packet
23-
)
10+
@classmethod
11+
def configure(cls, dumps_default=None, ext_hook=None):
12+
class CustomMsgPackPacket(MsgPackPacket):
13+
dumps_default = None
14+
ext_hook = None
15+
16+
CustomMsgPackPacket.dumps_default = dumps_default
17+
CustomMsgPackPacket.ext_hook = ext_hook
18+
return CustomMsgPackPacket
2419

2520
def encode(self):
2621
"""Encode the packet for transmission."""
27-
return msgpack.dumps(self._to_dict(), default=self.dumps_default)
22+
return msgpack.dumps(self._to_dict(),
23+
default=self.__class__.dumps_default)
2824

2925
def decode(self, encoded_packet):
3026
"""Decode a transmitted package."""
31-
if self.ext_hook is None:
32-
decoded = msgpack.loads(encoded_packet)
33-
else:
34-
decoded = msgpack.loads(encoded_packet, ext_hook=self.ext_hook)
27+
decoded = msgpack.loads(encoded_packet,
28+
ext_hook=self.__class__.ext_hook)
3529
self.packet_type = decoded['type']
3630
self.data = decoded.get('data')
3731
self.id = decoded.get('id')

src/socketio/server.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ class Server(base_server.BaseServer):
5353
default is `['/']`, which always accepts connections to
5454
the default namespace. Set to `'*'` to accept all
5555
namespaces.
56-
:param serializer_args: A mapping of additional parameters to pass to
57-
the serializer. The content of this dictionary
58-
depends on the selected serialization method.
5956
:param kwargs: Connection parameters for the underlying Engine.IO server.
6057
6158
The Engine.IO configuration supports the following settings:
@@ -404,7 +401,7 @@ def disconnect(self, sid, namespace=None, ignore_queue=False):
404401
if delete_it:
405402
self.logger.info('Disconnecting %s [%s]', sid, namespace)
406403
eio_sid = self.manager.pre_disconnect(sid, namespace=namespace)
407-
self._send_packet(eio_sid, self._create_packet(
404+
self._send_packet(eio_sid, self.packet_class(
408405
packet.DISCONNECT, namespace=namespace))
409406
self._trigger_event('disconnect', namespace, sid,
410407
self.reason.SERVER_DISCONNECT)
@@ -523,13 +520,13 @@ def _handle_connect(self, eio_sid, namespace, data):
523520
or self.namespaces == '*' or namespace in self.namespaces:
524521
sid = self.manager.connect(eio_sid, namespace)
525522
if sid is None:
526-
self._send_packet(eio_sid, self._create_packet(
523+
self._send_packet(eio_sid, self.packet_class(
527524
packet.CONNECT_ERROR, data='Unable to connect',
528525
namespace=namespace))
529526
return
530527

531528
if self.always_connect:
532-
self._send_packet(eio_sid, self._create_packet(
529+
self._send_packet(eio_sid, self.packet_class(
533530
packet.CONNECT, {'sid': sid}, namespace=namespace))
534531
fail_reason = exceptions.ConnectionRefusedError().error_args
535532
try:
@@ -553,15 +550,15 @@ def _handle_connect(self, eio_sid, namespace, data):
553550
if success is False:
554551
if self.always_connect:
555552
self.manager.pre_disconnect(sid, namespace)
556-
self._send_packet(eio_sid, self._create_packet(
553+
self._send_packet(eio_sid, self.packet_class(
557554
packet.DISCONNECT, data=fail_reason, namespace=namespace))
558555
else:
559-
self._send_packet(eio_sid, self._create_packet(
556+
self._send_packet(eio_sid, self.packet_class(
560557
packet.CONNECT_ERROR, data=fail_reason,
561558
namespace=namespace))
562559
self.manager.disconnect(sid, namespace, ignore_queue=True)
563560
elif not self.always_connect:
564-
self._send_packet(eio_sid, self._create_packet(
561+
self._send_packet(eio_sid, self.packet_class(
565562
packet.CONNECT, {'sid': sid}, namespace=namespace))
566563

567564
def _handle_disconnect(self, eio_sid, namespace, reason=None):
@@ -604,7 +601,7 @@ def _handle_event_internal(self, server, sid, eio_sid, data, namespace,
604601
data = list(r)
605602
else:
606603
data = [r]
607-
server._send_packet(eio_sid, self._create_packet(
604+
server._send_packet(eio_sid, self.packet_class(
608605
packet.ACK, namespace=namespace, id=id, data=data))
609606

610607
def _handle_ack(self, eio_sid, namespace, id, data):
@@ -653,7 +650,7 @@ def _handle_eio_message(self, eio_sid, data):
653650
else:
654651
self._handle_ack(eio_sid, pkt.namespace, pkt.id, pkt.data)
655652
else:
656-
pkt = self._create_packet(encoded_packet=data)
653+
pkt = self.packet_class(encoded_packet=data)
657654
if pkt.packet_type == packet.CONNECT:
658655
self._handle_connect(eio_sid, pkt.namespace, pkt.data)
659656
elif pkt.packet_type == packet.DISCONNECT:

0 commit comments

Comments
 (0)