protocols.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """Abstract Protocol class."""
  2. __all__ = ['BaseProtocol', 'Protocol', 'DatagramProtocol',
  3. 'SubprocessProtocol']
  4. class BaseProtocol:
  5. """Common base class for protocol interfaces.
  6. Usually user implements protocols that derived from BaseProtocol
  7. like Protocol or ProcessProtocol.
  8. The only case when BaseProtocol should be implemented directly is
  9. write-only transport like write pipe
  10. """
  11. def connection_made(self, transport):
  12. """Called when a connection is made.
  13. The argument is the transport representing the pipe connection.
  14. To receive data, wait for data_received() calls.
  15. When the connection is closed, connection_lost() is called.
  16. """
  17. def connection_lost(self, exc):
  18. """Called when the connection is lost or closed.
  19. The argument is an exception object or None (the latter
  20. meaning a regular EOF is received or the connection was
  21. aborted or closed).
  22. """
  23. def pause_writing(self):
  24. """Called when the transport's buffer goes over the high-water mark.
  25. Pause and resume calls are paired -- pause_writing() is called
  26. once when the buffer goes strictly over the high-water mark
  27. (even if subsequent writes increases the buffer size even
  28. more), and eventually resume_writing() is called once when the
  29. buffer size reaches the low-water mark.
  30. Note that if the buffer size equals the high-water mark,
  31. pause_writing() is not called -- it must go strictly over.
  32. Conversely, resume_writing() is called when the buffer size is
  33. equal or lower than the low-water mark. These end conditions
  34. are important to ensure that things go as expected when either
  35. mark is zero.
  36. NOTE: This is the only Protocol callback that is not called
  37. through EventLoop.call_soon() -- if it were, it would have no
  38. effect when it's most needed (when the app keeps writing
  39. without yielding until pause_writing() is called).
  40. """
  41. def resume_writing(self):
  42. """Called when the transport's buffer drains below the low-water mark.
  43. See pause_writing() for details.
  44. """
  45. class Protocol(BaseProtocol):
  46. """Interface for stream protocol.
  47. The user should implement this interface. They can inherit from
  48. this class but don't need to. The implementations here do
  49. nothing (they don't raise exceptions).
  50. When the user wants to requests a transport, they pass a protocol
  51. factory to a utility function (e.g., EventLoop.create_connection()).
  52. When the connection is made successfully, connection_made() is
  53. called with a suitable transport object. Then data_received()
  54. will be called 0 or more times with data (bytes) received from the
  55. transport; finally, connection_lost() will be called exactly once
  56. with either an exception object or None as an argument.
  57. State machine of calls:
  58. start -> CM [-> DR*] [-> ER?] -> CL -> end
  59. * CM: connection_made()
  60. * DR: data_received()
  61. * ER: eof_received()
  62. * CL: connection_lost()
  63. """
  64. def data_received(self, data):
  65. """Called when some data is received.
  66. The argument is a bytes object.
  67. """
  68. def eof_received(self):
  69. """Called when the other end calls write_eof() or equivalent.
  70. If this returns a false value (including None), the transport
  71. will close itself. If it returns a true value, closing the
  72. transport is up to the protocol.
  73. """
  74. class DatagramProtocol(BaseProtocol):
  75. """Interface for datagram protocol."""
  76. def datagram_received(self, data, addr):
  77. """Called when some datagram is received."""
  78. def error_received(self, exc):
  79. """Called when a send or receive operation raises an OSError.
  80. (Other than BlockingIOError or InterruptedError.)
  81. """
  82. class SubprocessProtocol(BaseProtocol):
  83. """Interface for protocol for subprocess calls."""
  84. def pipe_data_received(self, fd, data):
  85. """Called when the subprocess writes data into stdout/stderr pipe.
  86. fd is int file descriptor.
  87. data is bytes object.
  88. """
  89. def pipe_connection_lost(self, fd, exc):
  90. """Called when a file descriptor associated with the child process is
  91. closed.
  92. fd is the int file descriptor that was closed.
  93. """
  94. def process_exited(self):
  95. """Called when subprocess has exited."""