protocols.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. """Abstract Protocol base classes."""
  2. __all__ = (
  3. 'BaseProtocol', 'Protocol', 'DatagramProtocol',
  4. 'SubprocessProtocol', 'BufferedProtocol',
  5. )
  6. class BaseProtocol:
  7. """Common base class for protocol interfaces.
  8. Usually user implements protocols that derived from BaseProtocol
  9. like Protocol or ProcessProtocol.
  10. The only case when BaseProtocol should be implemented directly is
  11. write-only transport like write pipe
  12. """
  13. __slots__ = ()
  14. def connection_made(self, transport):
  15. """Called when a connection is made.
  16. The argument is the transport representing the pipe connection.
  17. To receive data, wait for data_received() calls.
  18. When the connection is closed, connection_lost() is called.
  19. """
  20. def connection_lost(self, exc):
  21. """Called when the connection is lost or closed.
  22. The argument is an exception object or None (the latter
  23. meaning a regular EOF is received or the connection was
  24. aborted or closed).
  25. """
  26. def pause_writing(self):
  27. """Called when the transport's buffer goes over the high-water mark.
  28. Pause and resume calls are paired -- pause_writing() is called
  29. once when the buffer goes strictly over the high-water mark
  30. (even if subsequent writes increases the buffer size even
  31. more), and eventually resume_writing() is called once when the
  32. buffer size reaches the low-water mark.
  33. Note that if the buffer size equals the high-water mark,
  34. pause_writing() is not called -- it must go strictly over.
  35. Conversely, resume_writing() is called when the buffer size is
  36. equal or lower than the low-water mark. These end conditions
  37. are important to ensure that things go as expected when either
  38. mark is zero.
  39. NOTE: This is the only Protocol callback that is not called
  40. through EventLoop.call_soon() -- if it were, it would have no
  41. effect when it's most needed (when the app keeps writing
  42. without yielding until pause_writing() is called).
  43. """
  44. def resume_writing(self):
  45. """Called when the transport's buffer drains below the low-water mark.
  46. See pause_writing() for details.
  47. """
  48. class Protocol(BaseProtocol):
  49. """Interface for stream protocol.
  50. The user should implement this interface. They can inherit from
  51. this class but don't need to. The implementations here do
  52. nothing (they don't raise exceptions).
  53. When the user wants to requests a transport, they pass a protocol
  54. factory to a utility function (e.g., EventLoop.create_connection()).
  55. When the connection is made successfully, connection_made() is
  56. called with a suitable transport object. Then data_received()
  57. will be called 0 or more times with data (bytes) received from the
  58. transport; finally, connection_lost() will be called exactly once
  59. with either an exception object or None as an argument.
  60. State machine of calls:
  61. start -> CM [-> DR*] [-> ER?] -> CL -> end
  62. * CM: connection_made()
  63. * DR: data_received()
  64. * ER: eof_received()
  65. * CL: connection_lost()
  66. """
  67. __slots__ = ()
  68. def data_received(self, data):
  69. """Called when some data is received.
  70. The argument is a bytes object.
  71. """
  72. def eof_received(self):
  73. """Called when the other end calls write_eof() or equivalent.
  74. If this returns a false value (including None), the transport
  75. will close itself. If it returns a true value, closing the
  76. transport is up to the protocol.
  77. """
  78. class BufferedProtocol(BaseProtocol):
  79. """Interface for stream protocol with manual buffer control.
  80. Event methods, such as `create_server` and `create_connection`,
  81. accept factories that return protocols that implement this interface.
  82. The idea of BufferedProtocol is that it allows to manually allocate
  83. and control the receive buffer. Event loops can then use the buffer
  84. provided by the protocol to avoid unnecessary data copies. This
  85. can result in noticeable performance improvement for protocols that
  86. receive big amounts of data. Sophisticated protocols can allocate
  87. the buffer only once at creation time.
  88. State machine of calls:
  89. start -> CM [-> GB [-> BU?]]* [-> ER?] -> CL -> end
  90. * CM: connection_made()
  91. * GB: get_buffer()
  92. * BU: buffer_updated()
  93. * ER: eof_received()
  94. * CL: connection_lost()
  95. """
  96. __slots__ = ()
  97. def get_buffer(self, sizehint):
  98. """Called to allocate a new receive buffer.
  99. *sizehint* is a recommended minimal size for the returned
  100. buffer. When set to -1, the buffer size can be arbitrary.
  101. Must return an object that implements the
  102. :ref:`buffer protocol <bufferobjects>`.
  103. It is an error to return a zero-sized buffer.
  104. """
  105. def buffer_updated(self, nbytes):
  106. """Called when the buffer was updated with the received data.
  107. *nbytes* is the total number of bytes that were written to
  108. the buffer.
  109. """
  110. def eof_received(self):
  111. """Called when the other end calls write_eof() or equivalent.
  112. If this returns a false value (including None), the transport
  113. will close itself. If it returns a true value, closing the
  114. transport is up to the protocol.
  115. """
  116. class DatagramProtocol(BaseProtocol):
  117. """Interface for datagram protocol."""
  118. __slots__ = ()
  119. def datagram_received(self, data, addr):
  120. """Called when some datagram is received."""
  121. def error_received(self, exc):
  122. """Called when a send or receive operation raises an OSError.
  123. (Other than BlockingIOError or InterruptedError.)
  124. """
  125. class SubprocessProtocol(BaseProtocol):
  126. """Interface for protocol for subprocess calls."""
  127. __slots__ = ()
  128. def pipe_data_received(self, fd, data):
  129. """Called when the subprocess writes data into stdout/stderr pipe.
  130. fd is int file descriptor.
  131. data is bytes object.
  132. """
  133. def pipe_connection_lost(self, fd, exc):
  134. """Called when a file descriptor associated with the child process is
  135. closed.
  136. fd is the int file descriptor that was closed.
  137. """
  138. def process_exited(self):
  139. """Called when subprocess has exited."""
  140. def _feed_data_to_buffered_proto(proto, data):
  141. data_len = len(data)
  142. while data_len:
  143. buf = proto.get_buffer(data_len)
  144. buf_len = len(buf)
  145. if not buf_len:
  146. raise RuntimeError('get_buffer() returned an empty buffer')
  147. if buf_len >= data_len:
  148. buf[:data_len] = data
  149. proto.buffer_updated(data_len)
  150. return
  151. else:
  152. buf[:buf_len] = data[:buf_len]
  153. proto.buffer_updated(buf_len)
  154. data = data[buf_len:]
  155. data_len = len(data)