Python socket.MSG_TRUNC Examples
The following are 8
code examples of socket.MSG_TRUNC().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
socket
, or try the search function
.
Example #1
Source File: _socketcan.py From pyuavcan with MIT License | 6 votes |
def _read_frame(self, ts_mono_ns: int) -> _media.TimestampedDataFrame: while True: data, ancdata, msg_flags, _addr = self._sock.recvmsg(self._native_frame_size, self._ancillary_data_buffer_size) assert msg_flags & socket.MSG_TRUNC == 0, 'The data buffer is not large enough' assert msg_flags & socket.MSG_CTRUNC == 0, 'The ancillary data buffer is not large enough' loopback = bool(msg_flags & socket.MSG_CONFIRM) ts_system_ns = 0 for cmsg_level, cmsg_type, cmsg_data in ancdata: if cmsg_level == socket.SOL_SOCKET and cmsg_type == _SO_TIMESTAMP: sec, usec = _TIMEVAL_STRUCT.unpack(cmsg_data) ts_system_ns = (sec * 1_000_000 + usec) * 1000 else: assert False, f'Unexpected ancillary data: {cmsg_level}, {cmsg_type}, {cmsg_data!r}' assert ts_system_ns > 0, 'Missing the timestamp; does the driver support timestamping?' timestamp = pyuavcan.transport.Timestamp(system_ns=ts_system_ns, monotonic_ns=ts_mono_ns) out = SocketCANMedia._parse_native_frame(data, loopback=loopback, timestamp=timestamp) if out is not None: return out
Example #2
Source File: test_socket.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def _testRecvmsgShorter(self): self.sendToServer(MSG) # FreeBSD < 8 doesn't always set the MSG_TRUNC flag when a truncated # datagram is received (issue #13001).
Example #3
Source File: test_socket.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def testRecvmsgPeek(self): # Check that MSG_PEEK in flags enables examination of pending # data without consuming it. # Receive part of data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3, 0, socket.MSG_PEEK) self.assertEqual(msg, MSG[:-3]) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) # Ignoring MSG_TRUNC here (so this test is the same for stream # and datagram sockets). Some wording in POSIX seems to # suggest that it needn't be set when peeking, but that may # just be a slip. self.checkFlags(flags, eor=False, ignore=getattr(socket, "MSG_TRUNC", 0)) # Receive all data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 0, socket.MSG_PEEK) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) # Check that the same data can still be received normally. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True)
Example #4
Source File: test_socket.py From ironpython3 with Apache License 2.0 | 5 votes |
def _testRecvmsgShorter(self): self.sendToServer(MSG) # FreeBSD < 8 doesn't always set the MSG_TRUNC flag when a truncated # datagram is received (issue #13001).
Example #5
Source File: test_socket.py From ironpython3 with Apache License 2.0 | 5 votes |
def testRecvmsgPeek(self): # Check that MSG_PEEK in flags enables examination of pending # data without consuming it. # Receive part of data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3, 0, socket.MSG_PEEK) self.assertEqual(msg, MSG[:-3]) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) # Ignoring MSG_TRUNC here (so this test is the same for stream # and datagram sockets). Some wording in POSIX seems to # suggest that it needn't be set when peeking, but that may # just be a slip. self.checkFlags(flags, eor=False, ignore=getattr(socket, "MSG_TRUNC", 0)) # Receive all data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 0, socket.MSG_PEEK) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) # Check that the same data can still be received normally. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True)
Example #6
Source File: test_socket.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def _testRecvmsgShorter(self): self.sendToServer(MSG) # FreeBSD < 8 doesn't always set the MSG_TRUNC flag when a truncated # datagram is received (issue #13001).
Example #7
Source File: test_socket.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def testRecvmsgPeek(self): # Check that MSG_PEEK in flags enables examination of pending # data without consuming it. # Receive part of data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG) - 3, 0, socket.MSG_PEEK) self.assertEqual(msg, MSG[:-3]) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) # Ignoring MSG_TRUNC here (so this test is the same for stream # and datagram sockets). Some wording in POSIX seems to # suggest that it needn't be set when peeking, but that may # just be a slip. self.checkFlags(flags, eor=False, ignore=getattr(socket, "MSG_TRUNC", 0)) # Receive all data with MSG_PEEK. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG), 0, socket.MSG_PEEK) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True) # Check that the same data can still be received normally. msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock, len(MSG)) self.assertEqual(msg, MSG) self.checkRecvmsgAddress(addr, self.cli_addr) self.assertEqual(ancdata, []) self.checkFlags(flags, eor=True)
Example #8
Source File: jsoncomm.py From osbuild with Apache License 2.0 | 4 votes |
def recv(self): """Receive a Message This receives the next pending message from the socket. This operation is synchronous. A tuple consisting of the deserialized message payload, the auxiliary file-descriptor set, and the socket-address of the sender is returned. """ # On `SOCK_DGRAM`, packets might be arbitrarily sized. There is no # hard-coded upper limit, since it is only restricted by the size of # the kernel write buffer on sockets (which itself can be modified via # sysctl). The only real maximum is probably something like 2^31-1, # since that is the maximum of that sysctl datatype. # Anyway, `MSG_TRUNC+MSG_PEEK` usually allows us to easily peek at the # incoming buffer. Unfortunately, the python `recvmsg()` wrapper # discards the return code and we cannot use that. Instead, we simply # loop until we know the size. This is slightly awkward, but seems fine # as long as you do not put this into a hot-path. size = 4096 while True: peek = self._socket.recvmsg(size, 0, socket.MSG_PEEK) if not (peek[2] & socket.MSG_TRUNC): break size *= 2 # Fetch a packet from the socket. On linux, the maximum SCM_RIGHTS array # size is hard-coded to 253. This allows us to size the ancillary buffer # big enough to receive any possible message. fds = array.array("i") msg = self._socket.recvmsg(size, socket.CMSG_LEN(253 * fds.itemsize)) # First thing we do is always to fetch the CMSG FDs into an FdSet. This # guarantees that we do not leak FDs in case the message handling fails # for other reasons. for level, ty, data in msg[1]: if level == socket.SOL_SOCKET and ty == socket.SCM_RIGHTS: assert len(data) % fds.itemsize == 0 fds.frombytes(data) fdset = FdSet(rawfds=fds) # Check the returned message flags. If the message was truncated, we # have to discard it. This shouldn't happen, but there is no harm in # handling it. However, `CTRUNC` can happen, since it is also triggered # when LSMs reject FD transmission. Treat it the same as a parser error. flags = msg[2] if flags & (socket.MSG_TRUNC | socket.MSG_CTRUNC): raise BufferError try: payload = json.loads(msg[0]) except json.JSONDecodeError: raise BufferError return (payload, fdset, msg[3])