2013-10-18 04:40:50 +08:00
|
|
|
"""Stream-related things."""
|
|
|
|
|
2013-11-26 07:07:18 +08:00
|
|
|
__all__ = ['StreamReader', 'StreamWriter', 'StreamReaderProtocol',
|
2014-01-25 22:32:06 +08:00
|
|
|
'open_connection', 'start_server', 'IncompleteReadError',
|
2013-11-20 03:43:38 +08:00
|
|
|
]
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
from . import events
|
|
|
|
from . import futures
|
|
|
|
from . import protocols
|
|
|
|
from . import tasks
|
|
|
|
|
|
|
|
|
|
|
|
_DEFAULT_LIMIT = 2**16
|
|
|
|
|
2014-01-31 08:05:28 +08:00
|
|
|
|
2014-01-25 22:32:06 +08:00
|
|
|
class IncompleteReadError(EOFError):
|
|
|
|
"""
|
|
|
|
Incomplete read error. Attributes:
|
|
|
|
|
|
|
|
- partial: read bytes string before the end of stream was reached
|
|
|
|
- expected: total number of expected bytes
|
|
|
|
"""
|
|
|
|
def __init__(self, partial, expected):
|
|
|
|
EOFError.__init__(self, "%s bytes read on a total of %s expected bytes"
|
|
|
|
% (len(partial), expected))
|
|
|
|
self.partial = partial
|
|
|
|
self.expected = expected
|
|
|
|
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
@tasks.coroutine
|
|
|
|
def open_connection(host=None, port=None, *,
|
|
|
|
loop=None, limit=_DEFAULT_LIMIT, **kwds):
|
|
|
|
"""A wrapper for create_connection() returning a (reader, writer) pair.
|
|
|
|
|
|
|
|
The reader returned is a StreamReader instance; the writer is a
|
2014-01-24 00:40:03 +08:00
|
|
|
StreamWriter instance.
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
The arguments are all the usual arguments to create_connection()
|
|
|
|
except protocol_factory; most common are positional host and port,
|
|
|
|
with various optional keyword arguments following.
|
|
|
|
|
|
|
|
Additional optional keyword arguments are loop (to set the event loop
|
|
|
|
instance to use) and limit (to set the buffer limit passed to the
|
|
|
|
StreamReader).
|
|
|
|
|
|
|
|
(If you want to customize the StreamReader and/or
|
|
|
|
StreamReaderProtocol classes, just copy the code -- there's
|
|
|
|
really nothing special here except some convenience.)
|
|
|
|
"""
|
|
|
|
if loop is None:
|
|
|
|
loop = events.get_event_loop()
|
|
|
|
reader = StreamReader(limit=limit, loop=loop)
|
2014-01-11 05:26:38 +08:00
|
|
|
protocol = StreamReaderProtocol(reader, loop=loop)
|
2013-10-18 04:40:50 +08:00
|
|
|
transport, _ = yield from loop.create_connection(
|
|
|
|
lambda: protocol, host, port, **kwds)
|
2013-10-19 06:17:11 +08:00
|
|
|
writer = StreamWriter(transport, protocol, reader, loop)
|
|
|
|
return reader, writer
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
|
2013-11-20 03:43:38 +08:00
|
|
|
@tasks.coroutine
|
|
|
|
def start_server(client_connected_cb, host=None, port=None, *,
|
|
|
|
loop=None, limit=_DEFAULT_LIMIT, **kwds):
|
|
|
|
"""Start a socket server, call back for each client connected.
|
|
|
|
|
|
|
|
The first parameter, `client_connected_cb`, takes two parameters:
|
|
|
|
client_reader, client_writer. client_reader is a StreamReader
|
|
|
|
object, while client_writer is a StreamWriter object. This
|
|
|
|
parameter can either be a plain callback function or a coroutine;
|
|
|
|
if it is a coroutine, it will be automatically converted into a
|
|
|
|
Task.
|
|
|
|
|
|
|
|
The rest of the arguments are all the usual arguments to
|
|
|
|
loop.create_server() except protocol_factory; most common are
|
|
|
|
positional host and port, with various optional keyword arguments
|
|
|
|
following. The return value is the same as loop.create_server().
|
|
|
|
|
|
|
|
Additional optional keyword arguments are loop (to set the event loop
|
|
|
|
instance to use) and limit (to set the buffer limit passed to the
|
|
|
|
StreamReader).
|
|
|
|
|
|
|
|
The return value is the same as loop.create_server(), i.e. a
|
|
|
|
Server object which can be used to stop the service.
|
|
|
|
"""
|
|
|
|
if loop is None:
|
|
|
|
loop = events.get_event_loop()
|
|
|
|
|
|
|
|
def factory():
|
|
|
|
reader = StreamReader(limit=limit, loop=loop)
|
|
|
|
protocol = StreamReaderProtocol(reader, client_connected_cb,
|
|
|
|
loop=loop)
|
|
|
|
return protocol
|
|
|
|
|
|
|
|
return (yield from loop.create_server(factory, host, port, **kwds))
|
|
|
|
|
|
|
|
|
2014-01-30 06:24:45 +08:00
|
|
|
class FlowControlMixin(protocols.Protocol):
|
|
|
|
"""Reusable flow control logic for StreamWriter.drain().
|
|
|
|
|
|
|
|
This implements the protocol methods pause_writing(),
|
|
|
|
resume_reading() and connection_lost(). If the subclass overrides
|
|
|
|
these it must call the super methods.
|
|
|
|
|
|
|
|
StreamWriter.drain() must check for error conditions and then call
|
|
|
|
_make_drain_waiter(), which will return either () or a Future
|
|
|
|
depending on the paused state.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, loop=None):
|
|
|
|
self._loop = loop # May be None; we may never need it.
|
|
|
|
self._paused = False
|
|
|
|
self._drain_waiter = None
|
|
|
|
|
|
|
|
def pause_writing(self):
|
|
|
|
assert not self._paused
|
|
|
|
self._paused = True
|
|
|
|
|
|
|
|
def resume_writing(self):
|
|
|
|
assert self._paused
|
|
|
|
self._paused = False
|
|
|
|
waiter = self._drain_waiter
|
|
|
|
if waiter is not None:
|
|
|
|
self._drain_waiter = None
|
|
|
|
if not waiter.done():
|
|
|
|
waiter.set_result(None)
|
|
|
|
|
|
|
|
def connection_lost(self, exc):
|
|
|
|
# Wake up the writer if currently paused.
|
|
|
|
if not self._paused:
|
|
|
|
return
|
|
|
|
waiter = self._drain_waiter
|
|
|
|
if waiter is None:
|
|
|
|
return
|
|
|
|
self._drain_waiter = None
|
|
|
|
if waiter.done():
|
|
|
|
return
|
|
|
|
if exc is None:
|
|
|
|
waiter.set_result(None)
|
|
|
|
else:
|
|
|
|
waiter.set_exception(exc)
|
|
|
|
|
|
|
|
def _make_drain_waiter(self):
|
|
|
|
if not self._paused:
|
|
|
|
return ()
|
|
|
|
waiter = self._drain_waiter
|
|
|
|
assert waiter is None or waiter.cancelled()
|
|
|
|
waiter = futures.Future(loop=self._loop)
|
|
|
|
self._drain_waiter = waiter
|
|
|
|
return waiter
|
|
|
|
|
|
|
|
|
|
|
|
class StreamReaderProtocol(FlowControlMixin, protocols.Protocol):
|
|
|
|
"""Helper class to adapt between Protocol and StreamReader.
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
(This is a helper class instead of making StreamReader itself a
|
|
|
|
Protocol subclass, because the StreamReader has other potential
|
|
|
|
uses, and to prevent the user of the StreamReader to accidentally
|
|
|
|
call inappropriate methods of the protocol.)
|
|
|
|
"""
|
|
|
|
|
2013-11-20 03:43:38 +08:00
|
|
|
def __init__(self, stream_reader, client_connected_cb=None, loop=None):
|
2014-01-30 06:24:45 +08:00
|
|
|
super().__init__(loop=loop)
|
2013-10-19 06:17:11 +08:00
|
|
|
self._stream_reader = stream_reader
|
2013-11-20 03:43:38 +08:00
|
|
|
self._stream_writer = None
|
|
|
|
self._client_connected_cb = client_connected_cb
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
def connection_made(self, transport):
|
2013-10-19 06:17:11 +08:00
|
|
|
self._stream_reader.set_transport(transport)
|
2013-11-20 03:43:38 +08:00
|
|
|
if self._client_connected_cb is not None:
|
|
|
|
self._stream_writer = StreamWriter(transport, self,
|
|
|
|
self._stream_reader,
|
|
|
|
self._loop)
|
|
|
|
res = self._client_connected_cb(self._stream_reader,
|
|
|
|
self._stream_writer)
|
|
|
|
if tasks.iscoroutine(res):
|
|
|
|
tasks.Task(res, loop=self._loop)
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
def connection_lost(self, exc):
|
|
|
|
if exc is None:
|
2013-10-19 06:17:11 +08:00
|
|
|
self._stream_reader.feed_eof()
|
2013-10-18 04:40:50 +08:00
|
|
|
else:
|
2013-10-19 06:17:11 +08:00
|
|
|
self._stream_reader.set_exception(exc)
|
2014-01-30 06:24:45 +08:00
|
|
|
super().connection_lost(exc)
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
def data_received(self, data):
|
2013-10-19 06:17:11 +08:00
|
|
|
self._stream_reader.feed_data(data)
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
def eof_received(self):
|
2013-10-19 06:17:11 +08:00
|
|
|
self._stream_reader.feed_eof()
|
|
|
|
|
|
|
|
|
|
|
|
class StreamWriter:
|
|
|
|
"""Wraps a Transport.
|
|
|
|
|
|
|
|
This exposes write(), writelines(), [can_]write_eof(),
|
|
|
|
get_extra_info() and close(). It adds drain() which returns an
|
|
|
|
optional Future on which you can wait for flow control. It also
|
2014-01-11 05:26:38 +08:00
|
|
|
adds a transport property which references the Transport
|
2013-10-19 06:17:11 +08:00
|
|
|
directly.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, transport, protocol, reader, loop):
|
|
|
|
self._transport = transport
|
|
|
|
self._protocol = protocol
|
|
|
|
self._reader = reader
|
|
|
|
self._loop = loop
|
|
|
|
|
|
|
|
@property
|
|
|
|
def transport(self):
|
|
|
|
return self._transport
|
|
|
|
|
|
|
|
def write(self, data):
|
|
|
|
self._transport.write(data)
|
|
|
|
|
|
|
|
def writelines(self, data):
|
|
|
|
self._transport.writelines(data)
|
|
|
|
|
|
|
|
def write_eof(self):
|
|
|
|
return self._transport.write_eof()
|
|
|
|
|
|
|
|
def can_write_eof(self):
|
|
|
|
return self._transport.can_write_eof()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
return self._transport.close()
|
|
|
|
|
|
|
|
def get_extra_info(self, name, default=None):
|
|
|
|
return self._transport.get_extra_info(name, default)
|
|
|
|
|
|
|
|
def drain(self):
|
|
|
|
"""This method has an unusual return value.
|
|
|
|
|
|
|
|
The intended use is to write
|
|
|
|
|
|
|
|
w.write(data)
|
|
|
|
yield from w.drain()
|
|
|
|
|
|
|
|
When there's nothing to wait for, drain() returns (), and the
|
|
|
|
yield-from continues immediately. When the transport buffer
|
|
|
|
is full (the protocol is paused), drain() creates and returns
|
|
|
|
a Future and the yield-from will block until that Future is
|
|
|
|
completed, which will happen when the buffer is (partially)
|
|
|
|
drained and the protocol is resumed.
|
|
|
|
"""
|
2014-01-30 06:24:45 +08:00
|
|
|
if self._reader is not None and self._reader._exception is not None:
|
2014-01-08 09:03:26 +08:00
|
|
|
raise self._reader._exception
|
2013-10-19 06:17:11 +08:00
|
|
|
if self._transport._conn_lost: # Uses private variable.
|
|
|
|
raise ConnectionResetError('Connection lost')
|
2014-01-30 06:24:45 +08:00
|
|
|
return self._protocol._make_drain_waiter()
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
class StreamReader:
|
|
|
|
|
|
|
|
def __init__(self, limit=_DEFAULT_LIMIT, loop=None):
|
|
|
|
# The line length limit is a security feature;
|
|
|
|
# it also doubles as half the buffer limit.
|
2013-10-19 06:17:11 +08:00
|
|
|
self._limit = limit
|
2013-10-18 04:40:50 +08:00
|
|
|
if loop is None:
|
|
|
|
loop = events.get_event_loop()
|
2013-10-19 06:17:11 +08:00
|
|
|
self._loop = loop
|
2014-02-06 07:11:13 +08:00
|
|
|
self._buffer = bytearray()
|
2013-10-19 06:17:11 +08:00
|
|
|
self._eof = False # Whether we're done.
|
|
|
|
self._waiter = None # A future.
|
2013-10-18 04:40:50 +08:00
|
|
|
self._exception = None
|
|
|
|
self._transport = None
|
|
|
|
self._paused = False
|
|
|
|
|
|
|
|
def exception(self):
|
|
|
|
return self._exception
|
|
|
|
|
|
|
|
def set_exception(self, exc):
|
|
|
|
self._exception = exc
|
|
|
|
|
2013-10-19 06:17:11 +08:00
|
|
|
waiter = self._waiter
|
2013-10-18 04:40:50 +08:00
|
|
|
if waiter is not None:
|
2013-10-19 06:17:11 +08:00
|
|
|
self._waiter = None
|
2013-10-18 04:40:50 +08:00
|
|
|
if not waiter.cancelled():
|
|
|
|
waiter.set_exception(exc)
|
|
|
|
|
|
|
|
def set_transport(self, transport):
|
|
|
|
assert self._transport is None, 'Transport already set'
|
|
|
|
self._transport = transport
|
|
|
|
|
|
|
|
def _maybe_resume_transport(self):
|
2014-02-06 07:11:13 +08:00
|
|
|
if self._paused and len(self._buffer) <= self._limit:
|
2013-10-18 04:40:50 +08:00
|
|
|
self._paused = False
|
2013-10-18 22:58:20 +08:00
|
|
|
self._transport.resume_reading()
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
def feed_eof(self):
|
2013-10-19 06:17:11 +08:00
|
|
|
self._eof = True
|
|
|
|
waiter = self._waiter
|
2013-10-18 04:40:50 +08:00
|
|
|
if waiter is not None:
|
2013-10-19 06:17:11 +08:00
|
|
|
self._waiter = None
|
2013-10-18 04:40:50 +08:00
|
|
|
if not waiter.cancelled():
|
|
|
|
waiter.set_result(True)
|
|
|
|
|
2014-02-06 13:14:30 +08:00
|
|
|
def at_eof(self):
|
|
|
|
"""Return True if the buffer is empty and 'feed_eof' was called."""
|
|
|
|
return self._eof and not self._buffer
|
|
|
|
|
2013-10-18 04:40:50 +08:00
|
|
|
def feed_data(self, data):
|
2014-02-06 07:11:13 +08:00
|
|
|
assert not self._eof, 'feed_data after feed_eof'
|
|
|
|
|
2013-10-18 04:40:50 +08:00
|
|
|
if not data:
|
|
|
|
return
|
|
|
|
|
2014-02-06 07:11:13 +08:00
|
|
|
self._buffer.extend(data)
|
2013-10-18 04:40:50 +08:00
|
|
|
|
2013-10-19 06:17:11 +08:00
|
|
|
waiter = self._waiter
|
2013-10-18 04:40:50 +08:00
|
|
|
if waiter is not None:
|
2013-10-19 06:17:11 +08:00
|
|
|
self._waiter = None
|
2013-10-18 04:40:50 +08:00
|
|
|
if not waiter.cancelled():
|
|
|
|
waiter.set_result(False)
|
|
|
|
|
|
|
|
if (self._transport is not None and
|
|
|
|
not self._paused and
|
2014-02-06 07:11:13 +08:00
|
|
|
len(self._buffer) > 2*self._limit):
|
2013-10-18 04:40:50 +08:00
|
|
|
try:
|
2013-10-18 22:58:20 +08:00
|
|
|
self._transport.pause_reading()
|
2013-10-18 04:40:50 +08:00
|
|
|
except NotImplementedError:
|
|
|
|
# The transport can't be paused.
|
|
|
|
# We'll just have to buffer all data.
|
|
|
|
# Forget the transport so we don't keep trying.
|
|
|
|
self._transport = None
|
|
|
|
else:
|
|
|
|
self._paused = True
|
|
|
|
|
2014-01-24 00:40:03 +08:00
|
|
|
def _create_waiter(self, func_name):
|
|
|
|
# StreamReader uses a future to link the protocol feed_data() method
|
|
|
|
# to a read coroutine. Running two read coroutines at the same time
|
|
|
|
# would have an unexpected behaviour. It would not possible to know
|
|
|
|
# which coroutine would get the next data.
|
|
|
|
if self._waiter is not None:
|
|
|
|
raise RuntimeError('%s() called while another coroutine is '
|
|
|
|
'already waiting for incoming data' % func_name)
|
|
|
|
return futures.Future(loop=self._loop)
|
|
|
|
|
2013-10-18 04:40:50 +08:00
|
|
|
@tasks.coroutine
|
|
|
|
def readline(self):
|
|
|
|
if self._exception is not None:
|
|
|
|
raise self._exception
|
|
|
|
|
2014-02-06 07:11:13 +08:00
|
|
|
line = bytearray()
|
2013-10-18 04:40:50 +08:00
|
|
|
not_enough = True
|
|
|
|
|
|
|
|
while not_enough:
|
2013-10-19 06:17:11 +08:00
|
|
|
while self._buffer and not_enough:
|
2014-02-06 07:11:13 +08:00
|
|
|
ichar = self._buffer.find(b'\n')
|
2013-10-18 04:40:50 +08:00
|
|
|
if ichar < 0:
|
2014-02-06 07:11:13 +08:00
|
|
|
line.extend(self._buffer)
|
|
|
|
self._buffer.clear()
|
2013-10-18 04:40:50 +08:00
|
|
|
else:
|
|
|
|
ichar += 1
|
2014-02-06 07:11:13 +08:00
|
|
|
line.extend(self._buffer[:ichar])
|
|
|
|
del self._buffer[:ichar]
|
2013-10-18 04:40:50 +08:00
|
|
|
not_enough = False
|
|
|
|
|
2014-02-06 07:11:13 +08:00
|
|
|
if len(line) > self._limit:
|
2013-10-18 04:40:50 +08:00
|
|
|
self._maybe_resume_transport()
|
|
|
|
raise ValueError('Line is too long')
|
|
|
|
|
2013-10-19 06:17:11 +08:00
|
|
|
if self._eof:
|
2013-10-18 04:40:50 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
if not_enough:
|
2014-01-24 00:40:03 +08:00
|
|
|
self._waiter = self._create_waiter('readline')
|
2013-10-18 04:40:50 +08:00
|
|
|
try:
|
2013-10-19 06:17:11 +08:00
|
|
|
yield from self._waiter
|
2013-10-18 04:40:50 +08:00
|
|
|
finally:
|
2013-10-19 06:17:11 +08:00
|
|
|
self._waiter = None
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
self._maybe_resume_transport()
|
2014-02-06 07:11:13 +08:00
|
|
|
return bytes(line)
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
@tasks.coroutine
|
|
|
|
def read(self, n=-1):
|
|
|
|
if self._exception is not None:
|
|
|
|
raise self._exception
|
|
|
|
|
|
|
|
if not n:
|
|
|
|
return b''
|
|
|
|
|
|
|
|
if n < 0:
|
2013-10-19 06:17:11 +08:00
|
|
|
while not self._eof:
|
2014-01-24 00:40:03 +08:00
|
|
|
self._waiter = self._create_waiter('read')
|
2013-10-18 04:40:50 +08:00
|
|
|
try:
|
2013-10-19 06:17:11 +08:00
|
|
|
yield from self._waiter
|
2013-10-18 04:40:50 +08:00
|
|
|
finally:
|
2013-10-19 06:17:11 +08:00
|
|
|
self._waiter = None
|
2013-10-18 04:40:50 +08:00
|
|
|
else:
|
2014-02-06 07:11:13 +08:00
|
|
|
if not self._buffer and not self._eof:
|
2014-01-24 00:40:03 +08:00
|
|
|
self._waiter = self._create_waiter('read')
|
2013-10-18 04:40:50 +08:00
|
|
|
try:
|
2013-10-19 06:17:11 +08:00
|
|
|
yield from self._waiter
|
2013-10-18 04:40:50 +08:00
|
|
|
finally:
|
2013-10-19 06:17:11 +08:00
|
|
|
self._waiter = None
|
2013-10-18 04:40:50 +08:00
|
|
|
|
2014-02-06 07:11:13 +08:00
|
|
|
if n < 0 or len(self._buffer) <= n:
|
|
|
|
data = bytes(self._buffer)
|
2013-10-19 06:17:11 +08:00
|
|
|
self._buffer.clear()
|
2014-02-06 07:11:13 +08:00
|
|
|
else:
|
|
|
|
# n > 0 and len(self._buffer) > n
|
|
|
|
data = bytes(self._buffer[:n])
|
|
|
|
del self._buffer[:n]
|
|
|
|
|
|
|
|
self._maybe_resume_transport()
|
|
|
|
return data
|
2013-10-18 04:40:50 +08:00
|
|
|
|
|
|
|
@tasks.coroutine
|
|
|
|
def readexactly(self, n):
|
|
|
|
if self._exception is not None:
|
|
|
|
raise self._exception
|
|
|
|
|
2014-01-07 08:09:18 +08:00
|
|
|
# There used to be "optimized" code here. It created its own
|
|
|
|
# Future and waited until self._buffer had at least the n
|
|
|
|
# bytes, then called read(n). Unfortunately, this could pause
|
|
|
|
# the transport if the argument was larger than the pause
|
|
|
|
# limit (which is twice self._limit). So now we just read()
|
|
|
|
# into a local buffer.
|
|
|
|
|
|
|
|
blocks = []
|
|
|
|
while n > 0:
|
|
|
|
block = yield from self.read(n)
|
|
|
|
if not block:
|
2014-01-25 22:32:06 +08:00
|
|
|
partial = b''.join(blocks)
|
|
|
|
raise IncompleteReadError(partial, len(partial) + n)
|
2014-01-07 08:09:18 +08:00
|
|
|
blocks.append(block)
|
|
|
|
n -= len(block)
|
2013-10-18 04:40:50 +08:00
|
|
|
|
2014-01-07 08:09:18 +08:00
|
|
|
return b''.join(blocks)
|