Use msgpack instead of pbjson as it is a more widely-adopted library.

This commit is contained in:
Yuxin Wang 2020-10-14 14:19:42 -04:00
parent 4b8064fb60
commit 8ddc2ae848
2 changed files with 4 additions and 3 deletions

View file

@ -3,6 +3,7 @@ import logging
import asyncio import asyncio
import struct import struct
import pbjson import pbjson
import msgpack
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -35,7 +36,7 @@ async def read_message(reader):
msglen = struct.unpack('>I', raw_msg_len)[0] msglen = struct.unpack('>I', raw_msg_len)[0]
raw_msg = await reader.readexactly(msglen) raw_msg = await reader.readexactly(msglen)
msg = pbjson.loads(raw_msg) msg = msgpack.loads(raw_msg)
logger.debug('Message received {}'.format(_message_log(msg))) logger.debug('Message received {}'.format(_message_log(msg)))
return msg return msg
@ -47,7 +48,7 @@ async def write_message(writer, message):
if isinstance(message['type'], MessageType): if isinstance(message['type'], MessageType):
message['type'] = message['type'].value message['type'] = message['type'].value
# pbjson string (bytes) -> add length header (bytes) # pbjson string (bytes) -> add length header (bytes)
raw_msg = pbjson.dumps(message) raw_msg = msgpack.dumps(message)
raw_msg = struct.pack('>I', len(raw_msg)) + raw_msg raw_msg = struct.pack('>I', len(raw_msg)) + raw_msg
writer.write(raw_msg) writer.write(raw_msg)
await writer.drain() await writer.drain()

View file

@ -25,7 +25,7 @@ setup(
], ],
keywords='P2P, Networking', keywords='P2P, Networking',
packages=find_packages(exclude=['tests']), packages=find_packages(exclude=['tests']),
install_requires=['coloredlogs', 'beautifultable', 'tqdm', 'aioconsole', 'pbjson', install_requires=['coloredlogs', 'beautifultable', 'tqdm', 'aioconsole', 'msgpack',
'uvloop ; platform_system != "Windows"'], 'uvloop ; platform_system != "Windows"'],
extras_require={ extras_require={
'test': ['pytest', 'pytest-asyncio', 'pytest-cov', 'coverage'], 'test': ['pytest', 'pytest-asyncio', 'pytest-cov', 'coverage'],