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