blob: c2feb93d0ac513e646c4df66c0516aa0160b6e75 [file] [log] [blame]
Guido van Rossum27b7c7e2013-10-17 13:40:50 -07001"""Abstract Transport class."""
2
Guido van Rossumf10345e2013-12-02 18:36:30 -08003import sys
4
5PY34 = sys.version_info >= (3, 4)
6
Guido van Rossum27b7c7e2013-10-17 13:40:50 -07007__all__ = ['ReadTransport', 'WriteTransport', 'Transport']
8
9
10class BaseTransport:
Guido van Rossum9204af42013-11-30 15:35:42 -080011 """Base class for transports."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070012
13 def __init__(self, extra=None):
14 if extra is None:
15 extra = {}
16 self._extra = extra
17
18 def get_extra_info(self, name, default=None):
19 """Get optional transport information."""
20 return self._extra.get(name, default)
21
22 def close(self):
Antoine Pitroudec43382013-11-23 12:30:00 +010023 """Close the transport.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070024
25 Buffered data will be flushed asynchronously. No more data
26 will be received. After all buffered data is flushed, the
27 protocol's connection_lost() method will (eventually) called
28 with None as its argument.
29 """
30 raise NotImplementedError
31
32
33class ReadTransport(BaseTransport):
Guido van Rossum9204af42013-11-30 15:35:42 -080034 """Interface for read-only transports."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070035
Guido van Rossum57497ad2013-10-18 07:58:20 -070036 def pause_reading(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070037 """Pause the receiving end.
38
39 No data will be passed to the protocol's data_received()
Guido van Rossum57497ad2013-10-18 07:58:20 -070040 method until resume_reading() is called.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070041 """
42 raise NotImplementedError
43
Guido van Rossum57497ad2013-10-18 07:58:20 -070044 def resume_reading(self):
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070045 """Resume the receiving end.
46
47 Data received will once again be passed to the protocol's
48 data_received() method.
49 """
50 raise NotImplementedError
51
52
53class WriteTransport(BaseTransport):
Guido van Rossum9204af42013-11-30 15:35:42 -080054 """Interface for write-only transports."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070055
Guido van Rossum355491d2013-10-18 15:17:11 -070056 def set_write_buffer_limits(self, high=None, low=None):
57 """Set the high- and low-water limits for write flow control.
58
59 These two values control when to call the protocol's
60 pause_writing() and resume_writing() methods. If specified,
61 the low-water limit must be less than or equal to the
62 high-water limit. Neither value can be negative.
63
64 The defaults are implementation-specific. If only the
65 high-water limit is given, the low-water limit defaults to a
66 implementation-specific value less than or equal to the
67 high-water limit. Setting high to zero forces low to zero as
68 well, and causes pause_writing() to be called whenever the
69 buffer becomes non-empty. Setting low to zero causes
70 resume_writing() to be called only once the buffer is empty.
71 Use of zero for either limit is generally sub-optimal as it
72 reduces opportunities for doing I/O and computation
73 concurrently.
74 """
75 raise NotImplementedError
76
77 def get_write_buffer_size(self):
78 """Return the current size of the write buffer."""
79 raise NotImplementedError
80
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070081 def write(self, data):
82 """Write some data bytes to the transport.
83
84 This does not block; it buffers the data and arranges for it
85 to be sent out asynchronously.
86 """
87 raise NotImplementedError
88
89 def writelines(self, list_of_data):
90 """Write a list (or any iterable) of data bytes to the transport.
91
Guido van Rossumf10345e2013-12-02 18:36:30 -080092 The default implementation concatenates the arguments and
93 calls write() on the result.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -070094 """
Guido van Rossumf10345e2013-12-02 18:36:30 -080095 if not PY34:
96 # In Python 3.3, bytes.join() doesn't handle memoryview.
97 list_of_data = (
98 bytes(data) if isinstance(data, memoryview) else data
99 for data in list_of_data)
100 self.write(b''.join(list_of_data))
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700101
102 def write_eof(self):
Antoine Pitroudec43382013-11-23 12:30:00 +0100103 """Close the write end after flushing buffered data.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700104
105 (This is like typing ^D into a UNIX program reading from stdin.)
106
107 Data may still be received.
108 """
109 raise NotImplementedError
110
111 def can_write_eof(self):
Antoine Pitroudec43382013-11-23 12:30:00 +0100112 """Return True if this transport supports write_eof(), False if not."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700113 raise NotImplementedError
114
115 def abort(self):
Guido van Rossum488b0da2013-11-23 11:51:09 -0800116 """Close the transport immediately.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700117
118 Buffered data will be lost. No more data will be received.
119 The protocol's connection_lost() method will (eventually) be
120 called with None as its argument.
121 """
122 raise NotImplementedError
123
124
125class Transport(ReadTransport, WriteTransport):
Guido van Rossum9204af42013-11-30 15:35:42 -0800126 """Interface representing a bidirectional transport.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700127
128 There may be several implementations, but typically, the user does
129 not implement new transports; rather, the platform provides some
130 useful transports that are implemented using the platform's best
131 practices.
132
133 The user never instantiates a transport directly; they call a
134 utility function, passing it a protocol factory and other
135 information necessary to create the transport and protocol. (E.g.
136 EventLoop.create_connection() or EventLoop.create_server().)
137
138 The utility function will asynchronously create a transport and a
139 protocol and hook them up by calling the protocol's
140 connection_made() method, passing it the transport.
141
142 The implementation here raises NotImplemented for every method
143 except writelines(), which calls write() in a loop.
144 """
145
146
147class DatagramTransport(BaseTransport):
Guido van Rossum9204af42013-11-30 15:35:42 -0800148 """Interface for datagram (UDP) transports."""
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700149
150 def sendto(self, data, addr=None):
151 """Send data to the transport.
152
153 This does not block; it buffers the data and arranges for it
154 to be sent out asynchronously.
155 addr is target socket address.
156 If addr is None use target address pointed on transport creation.
157 """
158 raise NotImplementedError
159
160 def abort(self):
Antoine Pitroudec43382013-11-23 12:30:00 +0100161 """Close the transport immediately.
Guido van Rossum27b7c7e2013-10-17 13:40:50 -0700162
163 Buffered data will be lost. No more data will be received.
164 The protocol's connection_lost() method will (eventually) be
165 called with None as its argument.
166 """
167 raise NotImplementedError
168
169
170class SubprocessTransport(BaseTransport):
171
172 def get_pid(self):
173 """Get subprocess id."""
174 raise NotImplementedError
175
176 def get_returncode(self):
177 """Get subprocess returncode.
178
179 See also
180 http://docs.python.org/3/library/subprocess#subprocess.Popen.returncode
181 """
182 raise NotImplementedError
183
184 def get_pipe_transport(self, fd):
185 """Get transport for pipe with number fd."""
186 raise NotImplementedError
187
188 def send_signal(self, signal):
189 """Send signal to subprocess.
190
191 See also:
192 docs.python.org/3/library/subprocess#subprocess.Popen.send_signal
193 """
194 raise NotImplementedError
195
196 def terminate(self):
197 """Stop the subprocess.
198
199 Alias for close() method.
200
201 On Posix OSs the method sends SIGTERM to the subprocess.
202 On Windows the Win32 API function TerminateProcess()
203 is called to stop the subprocess.
204
205 See also:
206 http://docs.python.org/3/library/subprocess#subprocess.Popen.terminate
207 """
208 raise NotImplementedError
209
210 def kill(self):
211 """Kill the subprocess.
212
213 On Posix OSs the function sends SIGKILL to the subprocess.
214 On Windows kill() is an alias for terminate().
215
216 See also:
217 http://docs.python.org/3/library/subprocess#subprocess.Popen.kill
218 """
219 raise NotImplementedError