Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sendmsg.py 2.6KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- test-case-name: twisted.test.test_sendmsg -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. sendmsg(2) and recvmsg(2) support for Python.
  6. """
  7. from collections import namedtuple
  8. from socket import CMSG_SPACE, SCM_RIGHTS, socket as Socket
  9. from typing import List, Tuple
  10. __all__ = ["sendmsg", "recvmsg", "getSocketFamily", "SCM_RIGHTS"]
  11. ReceivedMessage = namedtuple("ReceivedMessage", ["data", "ancillary", "flags"])
  12. def sendmsg(
  13. socket: Socket,
  14. data: bytes,
  15. ancillary: List[Tuple[int, int, bytes]] = [],
  16. flags: int = 0,
  17. ) -> int:
  18. """
  19. Send a message on a socket.
  20. @param socket: The socket to send the message on.
  21. @param data: Bytes to write to the socket.
  22. @param ancillary: Extra data to send over the socket outside of the normal
  23. datagram or stream mechanism. By default no ancillary data is sent.
  24. @param flags: Flags to affect how the message is sent. See the C{MSG_}
  25. constants in the sendmsg(2) manual page. By default no flags are set.
  26. @return: The return value of the underlying syscall, if it succeeds.
  27. """
  28. return socket.sendmsg([data], ancillary, flags)
  29. def recvmsg(
  30. socket: Socket, maxSize: int = 8192, cmsgSize: int = 4096, flags: int = 0
  31. ) -> ReceivedMessage:
  32. """
  33. Receive a message on a socket.
  34. @param socket: The socket to receive the message on.
  35. @param maxSize: The maximum number of bytes to receive from the socket using
  36. the datagram or stream mechanism. The default maximum is 8192.
  37. @param cmsgSize: The maximum number of bytes to receive from the socket
  38. outside of the normal datagram or stream mechanism. The default maximum
  39. is 4096.
  40. @param flags: Flags to affect how the message is sent. See the C{MSG_}
  41. constants in the sendmsg(2) manual page. By default no flags are set.
  42. @return: A named 3-tuple of the bytes received using the datagram/stream
  43. mechanism, a L{list} of L{tuple}s giving ancillary received data, and
  44. flags as an L{int} describing the data received.
  45. """
  46. # In Twisted's _sendmsg.c, the csmg_space was defined as:
  47. # int cmsg_size = 4096;
  48. # cmsg_space = CMSG_SPACE(cmsg_size);
  49. # Since the default in Python 3's socket is 0, we need to define our
  50. # own default of 4096. -hawkie
  51. data, ancillary, flags = socket.recvmsg(maxSize, CMSG_SPACE(cmsgSize), flags)[0:3]
  52. return ReceivedMessage(data=data, ancillary=ancillary, flags=flags)
  53. def getSocketFamily(socket: Socket) -> int:
  54. """
  55. Return the family of the given socket.
  56. @param socket: The socket to get the family of.
  57. """
  58. return socket.family