mirror of https://github.com/commaai/cereal.git
62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
# must be built with scons
|
|
from msgq.messaging.messaging_pyx import Context, Poller, SubSocket, PubSocket, SocketEventHandle, toggle_fake_events, \
|
|
set_fake_prefix, get_fake_prefix, delete_fake_prefix, wait_for_one_event
|
|
from msgq.messaging.messaging_pyx import MultiplePublishersError, MessagingError
|
|
|
|
from typing import Optional, List
|
|
|
|
assert MultiplePublishersError
|
|
assert MessagingError
|
|
assert toggle_fake_events
|
|
assert set_fake_prefix
|
|
assert get_fake_prefix
|
|
assert delete_fake_prefix
|
|
assert wait_for_one_event
|
|
|
|
NO_TRAVERSAL_LIMIT = 2**64-1
|
|
|
|
context = Context()
|
|
|
|
|
|
def fake_event_handle(endpoint: str, identifier: Optional[str] = None, override: bool = True, enable: bool = False) -> SocketEventHandle:
|
|
identifier = identifier or get_fake_prefix()
|
|
handle = SocketEventHandle(endpoint, identifier, override)
|
|
if override:
|
|
handle.enabled = enable
|
|
|
|
return handle
|
|
|
|
def pub_sock(endpoint: str) -> PubSocket:
|
|
sock = PubSocket()
|
|
sock.connect(context, endpoint)
|
|
return sock
|
|
|
|
|
|
def sub_sock(endpoint: str, poller: Optional[Poller] = None, addr: str = "127.0.0.1",
|
|
conflate: bool = False, timeout: Optional[int] = None) -> SubSocket:
|
|
sock = SubSocket()
|
|
sock.connect(context, endpoint, addr.encode('utf8'), conflate)
|
|
|
|
if timeout is not None:
|
|
sock.setTimeout(timeout)
|
|
|
|
if poller is not None:
|
|
poller.registerSocket(sock)
|
|
return sock
|
|
|
|
def drain_sock_raw(sock: SubSocket, wait_for_one: bool = False) -> List[bytes]:
|
|
"""Receive all message currently available on the queue"""
|
|
ret: List[bytes] = []
|
|
while 1:
|
|
if wait_for_one and len(ret) == 0:
|
|
dat = sock.receive()
|
|
else:
|
|
dat = sock.receive(non_blocking=True)
|
|
|
|
if dat is None:
|
|
break
|
|
|
|
ret.append(dat)
|
|
|
|
return ret
|