Stream.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/EnumBits.h>
  9. #include <AK/Function.h>
  10. #include <AK/IPv4Address.h>
  11. #include <AK/MemMem.h>
  12. #include <AK/Noncopyable.h>
  13. #include <AK/Result.h>
  14. #include <AK/Span.h>
  15. #include <AK/String.h>
  16. #include <AK/Time.h>
  17. #include <AK/Variant.h>
  18. #include <LibCore/Notifier.h>
  19. #include <LibCore/SocketAddress.h>
  20. #include <errno.h>
  21. #include <netdb.h>
  22. namespace Core::Stream {
  23. /// The base, abstract class for stream operations. This class defines the
  24. /// operations one can perform on every stream in LibCore.
  25. class Stream {
  26. public:
  27. virtual bool is_readable() const { return false; }
  28. /// Reads into a buffer, with the maximum size being the size of the buffer.
  29. /// The amount of bytes read can be smaller than the size of the buffer.
  30. /// Returns either the bytes that were read, or an errno in the case of
  31. /// failure.
  32. virtual ErrorOr<Bytes> read(Bytes) = 0;
  33. /// Tries to fill the entire buffer through reading. Returns whether the
  34. /// buffer was filled without an error.
  35. virtual bool read_or_error(Bytes);
  36. /// Reads the stream until EOF, storing the contents into a ByteBuffer which
  37. /// is returned once EOF is encountered. The block size determines the size
  38. /// of newly allocated chunks while reading.
  39. virtual ErrorOr<ByteBuffer> read_all(size_t block_size = 4096);
  40. virtual bool is_writable() const { return false; }
  41. /// Tries to write the entire contents of the buffer. It is possible for
  42. /// less than the full buffer to be written. Returns either the amount of
  43. /// bytes written into the stream, or an errno in the case of failure.
  44. virtual ErrorOr<size_t> write(ReadonlyBytes) = 0;
  45. /// Same as write, but does not return until either the entire buffer
  46. /// contents are written or an error occurs. Returns whether the entire
  47. /// contents were written without an error.
  48. virtual bool write_or_error(ReadonlyBytes);
  49. /// Returns whether the stream has reached the end of file. For sockets,
  50. /// this most likely means that the protocol has disconnected (in the case
  51. /// of TCP). For seekable streams, this means the end of the file. Note that
  52. /// is_eof will only return true _after_ a read with 0 length, so this
  53. /// method should be called after a read.
  54. virtual bool is_eof() const = 0;
  55. virtual bool is_open() const = 0;
  56. virtual void close() = 0;
  57. virtual ~Stream()
  58. {
  59. }
  60. protected:
  61. /// Provides a default implementation of read_all that works for streams
  62. /// that behave like POSIX file descriptors. expected_file_size can be
  63. /// passed as a heuristic for what the Stream subclass expects the file
  64. /// content size to be in order to reduce allocations (does not affect
  65. /// actual reading).
  66. ErrorOr<ByteBuffer> read_all_impl(size_t block_size, size_t expected_file_size = 0);
  67. };
  68. enum class SeekMode {
  69. SetPosition,
  70. FromCurrentPosition,
  71. FromEndPosition,
  72. };
  73. /// Adds seekability to Core::Stream. Classes inheriting from SeekableStream
  74. /// will be seekable to any point in the stream.
  75. class SeekableStream : public Stream {
  76. public:
  77. /// Seeks to the given position in the given mode. Returns either the
  78. /// current position of the file, or an errno in the case of an error.
  79. virtual ErrorOr<off_t> seek(i64 offset, SeekMode) = 0;
  80. /// Returns the current position of the file, or an errno in the case of
  81. /// an error.
  82. virtual ErrorOr<off_t> tell() const;
  83. /// Returns the total size of the stream, or an errno in the case of an
  84. /// error. May not preserve the original position on the stream on failure.
  85. virtual ErrorOr<off_t> size();
  86. /// Shrinks or extends the stream to the given size. Returns an errno in
  87. /// the case of an error.
  88. virtual ErrorOr<void> truncate(off_t length) = 0;
  89. };
  90. /// The Socket class is the base class for all concrete BSD-style socket
  91. /// classes. Sockets are non-seekable streams which can be read byte-wise.
  92. class Socket : public Stream {
  93. public:
  94. Socket(Socket&&) = default;
  95. Socket& operator=(Socket&&) = default;
  96. /// Checks how many bytes of data are currently available to read on the
  97. /// socket. For datagram-based socket, this is the size of the first
  98. /// datagram that can be read. Returns either the amount of bytes, or an
  99. /// errno in the case of failure.
  100. virtual ErrorOr<size_t> pending_bytes() const = 0;
  101. /// Returns whether there's any data that can be immediately read, or an
  102. /// errno on failure.
  103. virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const = 0;
  104. // Sets the blocking mode of the socket. If blocking mode is disabled, reads
  105. // will fail with EAGAIN when there's no data available to read, and writes
  106. // will fail with EAGAIN when the data cannot be written without blocking
  107. // (due to the send buffer being full, for example).
  108. virtual ErrorOr<void> set_blocking(bool enabled) = 0;
  109. // Sets the close-on-exec mode of the socket. If close-on-exec mode is
  110. // enabled, then the socket will be automatically closed by the kernel when
  111. // an exec call happens.
  112. virtual ErrorOr<void> set_close_on_exec(bool enabled) = 0;
  113. /// Disables any listening mechanisms that this socket uses.
  114. /// Can be called with 'false' when `on_ready_to_read` notifications are no longer needed.
  115. /// Conversely, set_notifications_enabled(true) will re-enable notifications.
  116. virtual void set_notifications_enabled(bool) { }
  117. Function<void()> on_ready_to_read;
  118. protected:
  119. enum class SocketDomain {
  120. Local,
  121. Inet,
  122. };
  123. enum class SocketType {
  124. Stream,
  125. Datagram,
  126. };
  127. Socket()
  128. {
  129. }
  130. static ErrorOr<int> create_fd(SocketDomain, SocketType);
  131. // FIXME: This will need to be updated when IPv6 socket arrives. Perhaps a
  132. // base class for all address types is appropriate.
  133. static ErrorOr<IPv4Address> resolve_host(String const&, SocketType);
  134. static ErrorOr<void> connect_local(int fd, String const& path);
  135. static ErrorOr<void> connect_inet(int fd, SocketAddress const&);
  136. };
  137. /// A reusable socket maintains state about being connected in addition to
  138. /// normal Socket capabilities, and can be reconnected once disconnected.
  139. class ReusableSocket : public Socket {
  140. public:
  141. /// Returns whether the socket is currently connected.
  142. virtual bool is_connected() = 0;
  143. /// Reconnects the socket to the given host and port. Returns EALREADY if
  144. /// is_connected() is true.
  145. virtual ErrorOr<void> reconnect(String const& host, u16 port) = 0;
  146. /// Connects the socket to the given socket address (IP address + port).
  147. /// Returns EALREADY is_connected() is true.
  148. virtual ErrorOr<void> reconnect(SocketAddress const&) = 0;
  149. };
  150. // Concrete classes.
  151. enum class OpenMode : unsigned {
  152. NotOpen = 0,
  153. Read = 1,
  154. Write = 2,
  155. ReadWrite = 3,
  156. Append = 4,
  157. Truncate = 8,
  158. MustBeNew = 16,
  159. KeepOnExec = 32,
  160. Nonblocking = 64,
  161. };
  162. AK_ENUM_BITWISE_OPERATORS(OpenMode)
  163. class File final : public SeekableStream {
  164. AK_MAKE_NONCOPYABLE(File);
  165. public:
  166. static ErrorOr<NonnullOwnPtr<File>> open(StringView filename, OpenMode, mode_t = 0644);
  167. static ErrorOr<NonnullOwnPtr<File>> adopt_fd(int fd, OpenMode);
  168. static bool exists(StringView filename);
  169. static ErrorOr<NonnullOwnPtr<File>> open_file_or_standard_stream(StringView filename, OpenMode mode);
  170. File(File&& other) { operator=(move(other)); }
  171. File& operator=(File&& other)
  172. {
  173. if (&other == this)
  174. return *this;
  175. m_mode = exchange(other.m_mode, OpenMode::NotOpen);
  176. m_fd = exchange(other.m_fd, -1);
  177. m_last_read_was_eof = exchange(other.m_last_read_was_eof, false);
  178. return *this;
  179. }
  180. virtual bool is_readable() const override;
  181. virtual ErrorOr<Bytes> read(Bytes) override;
  182. virtual ErrorOr<ByteBuffer> read_all(size_t block_size = 4096) override;
  183. virtual bool is_writable() const override;
  184. virtual ErrorOr<size_t> write(ReadonlyBytes) override;
  185. virtual bool is_eof() const override;
  186. virtual bool is_open() const override;
  187. virtual void close() override;
  188. virtual ErrorOr<off_t> seek(i64 offset, SeekMode) override;
  189. virtual ErrorOr<void> truncate(off_t length) override;
  190. virtual ~File() override { close(); }
  191. static int open_mode_to_options(OpenMode mode);
  192. private:
  193. File(OpenMode mode)
  194. : m_mode(mode)
  195. {
  196. }
  197. ErrorOr<void> open_path(StringView filename, mode_t);
  198. OpenMode m_mode { OpenMode::NotOpen };
  199. int m_fd { -1 };
  200. bool m_last_read_was_eof { false };
  201. };
  202. class PosixSocketHelper {
  203. AK_MAKE_NONCOPYABLE(PosixSocketHelper);
  204. public:
  205. template<typename T>
  206. PosixSocketHelper(Badge<T>) requires(IsBaseOf<Socket, T>) { }
  207. PosixSocketHelper(PosixSocketHelper&& other)
  208. {
  209. operator=(move(other));
  210. }
  211. PosixSocketHelper& operator=(PosixSocketHelper&& other)
  212. {
  213. m_fd = exchange(other.m_fd, -1);
  214. m_last_read_was_eof = exchange(other.m_last_read_was_eof, false);
  215. m_notifier = move(other.m_notifier);
  216. return *this;
  217. }
  218. int fd() const { return m_fd; }
  219. void set_fd(int fd) { m_fd = fd; }
  220. ErrorOr<Bytes> read(Bytes, int flags = 0);
  221. ErrorOr<size_t> write(ReadonlyBytes);
  222. bool is_eof() const { return !is_open() || m_last_read_was_eof; }
  223. bool is_open() const { return m_fd != -1; }
  224. void close();
  225. ErrorOr<size_t> pending_bytes() const;
  226. ErrorOr<bool> can_read_without_blocking(int timeout) const;
  227. ErrorOr<void> set_blocking(bool enabled);
  228. ErrorOr<void> set_close_on_exec(bool enabled);
  229. ErrorOr<void> set_receive_timeout(Time timeout);
  230. void setup_notifier();
  231. RefPtr<Core::Notifier> notifier() { return m_notifier; }
  232. private:
  233. int m_fd { -1 };
  234. bool m_last_read_was_eof { false };
  235. RefPtr<Core::Notifier> m_notifier;
  236. };
  237. class TCPSocket final : public Socket {
  238. public:
  239. static ErrorOr<NonnullOwnPtr<TCPSocket>> connect(String const& host, u16 port);
  240. static ErrorOr<NonnullOwnPtr<TCPSocket>> connect(SocketAddress const& address);
  241. static ErrorOr<NonnullOwnPtr<TCPSocket>> adopt_fd(int fd);
  242. TCPSocket(TCPSocket&& other)
  243. : Socket(static_cast<Socket&&>(other))
  244. , m_helper(move(other.m_helper))
  245. {
  246. if (is_open())
  247. setup_notifier();
  248. }
  249. TCPSocket& operator=(TCPSocket&& other)
  250. {
  251. Socket::operator=(static_cast<Socket&&>(other));
  252. m_helper = move(other.m_helper);
  253. if (is_open())
  254. setup_notifier();
  255. return *this;
  256. }
  257. virtual bool is_readable() const override { return is_open(); }
  258. virtual bool is_writable() const override { return is_open(); }
  259. virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer); }
  260. virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer); }
  261. virtual bool is_eof() const override { return m_helper.is_eof(); }
  262. virtual bool is_open() const override { return m_helper.is_open(); };
  263. virtual void close() override { m_helper.close(); };
  264. virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
  265. virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
  266. virtual void set_notifications_enabled(bool enabled) override
  267. {
  268. if (auto notifier = m_helper.notifier())
  269. notifier->set_enabled(enabled);
  270. }
  271. ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
  272. ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
  273. virtual ~TCPSocket() override { close(); }
  274. private:
  275. TCPSocket()
  276. {
  277. }
  278. void setup_notifier()
  279. {
  280. VERIFY(is_open());
  281. m_helper.setup_notifier();
  282. m_helper.notifier()->on_ready_to_read = [this] {
  283. if (on_ready_to_read)
  284. on_ready_to_read();
  285. };
  286. }
  287. PosixSocketHelper m_helper { Badge<TCPSocket> {} };
  288. };
  289. class UDPSocket final : public Socket {
  290. public:
  291. static ErrorOr<NonnullOwnPtr<UDPSocket>> connect(String const& host, u16 port, Optional<Time> timeout = {});
  292. static ErrorOr<NonnullOwnPtr<UDPSocket>> connect(SocketAddress const& address, Optional<Time> timeout = {});
  293. UDPSocket(UDPSocket&& other)
  294. : Socket(static_cast<Socket&&>(other))
  295. , m_helper(move(other.m_helper))
  296. {
  297. if (is_open())
  298. setup_notifier();
  299. }
  300. UDPSocket& operator=(UDPSocket&& other)
  301. {
  302. Socket::operator=(static_cast<Socket&&>(other));
  303. m_helper = move(other.m_helper);
  304. if (is_open())
  305. setup_notifier();
  306. return *this;
  307. }
  308. virtual ErrorOr<Bytes> read(Bytes buffer) override
  309. {
  310. auto pending_bytes = TRY(this->pending_bytes());
  311. if (pending_bytes > buffer.size()) {
  312. // With UDP datagrams, reading a datagram into a buffer that's
  313. // smaller than the datagram's size will cause the rest of the
  314. // datagram to be discarded. That's not very nice, so let's bail
  315. // early, telling the caller that he should allocate a bigger
  316. // buffer.
  317. return Error::from_errno(EMSGSIZE);
  318. }
  319. return m_helper.read(buffer);
  320. }
  321. virtual bool is_readable() const override { return is_open(); }
  322. virtual bool is_writable() const override { return is_open(); }
  323. virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer); }
  324. virtual bool is_eof() const override { return m_helper.is_eof(); }
  325. virtual bool is_open() const override { return m_helper.is_open(); }
  326. virtual void close() override { m_helper.close(); }
  327. virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
  328. virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
  329. virtual void set_notifications_enabled(bool enabled) override
  330. {
  331. if (auto notifier = m_helper.notifier())
  332. notifier->set_enabled(enabled);
  333. }
  334. ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
  335. ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
  336. virtual ~UDPSocket() override { close(); }
  337. private:
  338. UDPSocket() = default;
  339. void setup_notifier()
  340. {
  341. VERIFY(is_open());
  342. m_helper.setup_notifier();
  343. m_helper.notifier()->on_ready_to_read = [this] {
  344. if (on_ready_to_read)
  345. on_ready_to_read();
  346. };
  347. }
  348. PosixSocketHelper m_helper { Badge<UDPSocket> {} };
  349. };
  350. class LocalSocket final : public Socket {
  351. public:
  352. static ErrorOr<NonnullOwnPtr<LocalSocket>> connect(String const& path);
  353. static ErrorOr<NonnullOwnPtr<LocalSocket>> adopt_fd(int fd);
  354. LocalSocket(LocalSocket&& other)
  355. : Socket(static_cast<Socket&&>(other))
  356. , m_helper(move(other.m_helper))
  357. {
  358. if (is_open())
  359. setup_notifier();
  360. }
  361. LocalSocket& operator=(LocalSocket&& other)
  362. {
  363. Socket::operator=(static_cast<Socket&&>(other));
  364. m_helper = move(other.m_helper);
  365. if (is_open())
  366. setup_notifier();
  367. return *this;
  368. }
  369. virtual bool is_readable() const override { return is_open(); }
  370. virtual bool is_writable() const override { return is_open(); }
  371. virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(buffer); }
  372. virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.write(buffer); }
  373. virtual bool is_eof() const override { return m_helper.is_eof(); }
  374. virtual bool is_open() const override { return m_helper.is_open(); }
  375. virtual void close() override { m_helper.close(); }
  376. virtual ErrorOr<size_t> pending_bytes() const override { return m_helper.pending_bytes(); }
  377. virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.can_read_without_blocking(timeout); }
  378. virtual ErrorOr<void> set_blocking(bool enabled) override { return m_helper.set_blocking(enabled); }
  379. virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.set_close_on_exec(enabled); }
  380. virtual void set_notifications_enabled(bool enabled) override
  381. {
  382. if (auto notifier = m_helper.notifier())
  383. notifier->set_enabled(enabled);
  384. }
  385. ErrorOr<int> receive_fd(int flags);
  386. ErrorOr<void> send_fd(int fd);
  387. ErrorOr<pid_t> peer_pid() const;
  388. ErrorOr<Bytes> read_without_waiting(Bytes buffer);
  389. /// Release the fd associated with this LocalSocket. After the fd is
  390. /// released, the socket will be considered "closed" and all operations done
  391. /// on it will fail with ENOTCONN. Fails with ENOTCONN if the socket is
  392. /// already closed.
  393. ErrorOr<int> release_fd();
  394. virtual ~LocalSocket() { close(); }
  395. private:
  396. LocalSocket() = default;
  397. void setup_notifier()
  398. {
  399. VERIFY(is_open());
  400. m_helper.setup_notifier();
  401. m_helper.notifier()->on_ready_to_read = [this] {
  402. if (on_ready_to_read)
  403. on_ready_to_read();
  404. };
  405. }
  406. PosixSocketHelper m_helper { Badge<LocalSocket> {} };
  407. };
  408. // Buffered stream wrappers
  409. template<typename T>
  410. concept StreamLike = IsBaseOf<Stream, T>;
  411. template<typename T>
  412. concept SeekableStreamLike = IsBaseOf<SeekableStream, T>;
  413. template<typename T>
  414. concept SocketLike = IsBaseOf<Socket, T>;
  415. template<typename T>
  416. class BufferedHelper {
  417. AK_MAKE_NONCOPYABLE(BufferedHelper);
  418. public:
  419. template<StreamLike U>
  420. BufferedHelper(Badge<U>, NonnullOwnPtr<T> stream, ByteBuffer buffer)
  421. : m_stream(move(stream))
  422. , m_buffer(move(buffer))
  423. {
  424. }
  425. BufferedHelper(BufferedHelper&& other)
  426. : m_stream(move(other.m_stream))
  427. , m_buffer(move(other.m_buffer))
  428. , m_buffered_size(exchange(other.m_buffered_size, 0))
  429. {
  430. }
  431. BufferedHelper& operator=(BufferedHelper&& other)
  432. {
  433. m_stream = move(other.m_stream);
  434. m_buffer = move(other.m_buffer);
  435. m_buffered_size = exchange(other.m_buffered_size, 0);
  436. return *this;
  437. }
  438. template<template<typename> typename BufferedType>
  439. static ErrorOr<NonnullOwnPtr<BufferedType<T>>> create_buffered(NonnullOwnPtr<T> stream, size_t buffer_size)
  440. {
  441. if (!buffer_size)
  442. return Error::from_errno(EINVAL);
  443. if (!stream->is_open())
  444. return Error::from_errno(ENOTCONN);
  445. auto buffer = TRY(ByteBuffer::create_uninitialized(buffer_size));
  446. return adopt_nonnull_own_or_enomem(new BufferedType<T>(move(stream), move(buffer)));
  447. }
  448. T& stream() { return *m_stream; }
  449. T const& stream() const { return *m_stream; }
  450. ErrorOr<Bytes> read(Bytes buffer)
  451. {
  452. if (!stream().is_open())
  453. return Error::from_errno(ENOTCONN);
  454. if (!buffer.size())
  455. return Error::from_errno(ENOBUFS);
  456. // Fill the internal buffer if it has run dry.
  457. if (m_buffered_size == 0)
  458. TRY(populate_read_buffer());
  459. // Let's try to take all we can from the buffer first.
  460. size_t buffer_nread = 0;
  461. if (m_buffered_size > 0) {
  462. // FIXME: Use a circular buffer to avoid shifting the buffer
  463. // contents.
  464. size_t amount_to_take = min(buffer.size(), m_buffered_size);
  465. auto slice_to_take = m_buffer.span().slice(0, amount_to_take);
  466. auto slice_to_shift = m_buffer.span().slice(amount_to_take);
  467. slice_to_take.copy_to(buffer);
  468. buffer_nread += amount_to_take;
  469. if (amount_to_take < m_buffered_size) {
  470. m_buffer.overwrite(0, slice_to_shift.data(), m_buffered_size - amount_to_take);
  471. }
  472. m_buffered_size -= amount_to_take;
  473. }
  474. return Bytes { buffer.data(), buffer_nread };
  475. }
  476. // Reads into the buffer until \n is encountered.
  477. // The size of the Bytes object is the maximum amount of bytes that will be
  478. // read. Returns the bytes read as a StringView.
  479. ErrorOr<StringView> read_line(Bytes buffer)
  480. {
  481. return StringView { TRY(read_until(buffer, "\n"sv)) };
  482. }
  483. ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate)
  484. {
  485. return read_until_any_of(buffer, Array { candidate });
  486. }
  487. template<size_t N>
  488. ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> candidates)
  489. {
  490. if (!stream().is_open())
  491. return Error::from_errno(ENOTCONN);
  492. if (buffer.is_empty())
  493. return Error::from_errno(ENOBUFS);
  494. // We fill the buffer through can_read_line.
  495. if (!TRY(can_read_line()))
  496. return Bytes {};
  497. if (stream().is_eof()) {
  498. if (buffer.size() < m_buffered_size) {
  499. // Normally, reading from an EOFed stream and receiving bytes
  500. // would mean that the stream is no longer EOF. However, it's
  501. // possible with a buffered stream that the user is able to read
  502. // the buffer contents even when the underlying stream is EOF.
  503. // We already violate this invariant once by giving the user the
  504. // chance to read the remaining buffer contents, but if the user
  505. // doesn't give us a big enough buffer, then we would be
  506. // violating the invariant twice the next time the user attempts
  507. // to read, which is No Good. So let's give a descriptive error
  508. // to the caller about why it can't read.
  509. return Error::from_errno(EMSGSIZE);
  510. }
  511. }
  512. // The intention here is to try to match all of the possible
  513. // delimiter candidates and try to find the longest one we can
  514. // remove from the buffer after copying up to the delimiter to the
  515. // user buffer.
  516. Optional<size_t> longest_match;
  517. size_t match_size = 0;
  518. for (auto& candidate : candidates) {
  519. auto result = AK::memmem_optional(m_buffer.data(), m_buffered_size, candidate.bytes().data(), candidate.bytes().size());
  520. if (result.has_value()) {
  521. auto previous_match = longest_match.value_or(*result);
  522. if ((previous_match < *result) || (previous_match == *result && match_size < candidate.length())) {
  523. longest_match = result;
  524. match_size = candidate.length();
  525. }
  526. }
  527. }
  528. if (longest_match.has_value()) {
  529. auto size_written_to_user_buffer = *longest_match;
  530. auto buffer_to_take = m_buffer.span().slice(0, size_written_to_user_buffer);
  531. auto buffer_to_shift = m_buffer.span().slice(size_written_to_user_buffer + match_size);
  532. buffer_to_take.copy_to(buffer);
  533. m_buffer.overwrite(0, buffer_to_shift.data(), buffer_to_shift.size());
  534. m_buffered_size -= size_written_to_user_buffer + match_size;
  535. return buffer.slice(0, size_written_to_user_buffer);
  536. }
  537. // If we still haven't found anything, then it's most likely the case
  538. // that the delimiter ends beyond the length of the caller-passed
  539. // buffer. Let's just fill the caller's buffer up.
  540. auto readable_size = min(m_buffered_size, buffer.size());
  541. auto buffer_to_take = m_buffer.span().slice(0, readable_size);
  542. auto buffer_to_shift = m_buffer.span().slice(readable_size);
  543. buffer_to_take.copy_to(buffer);
  544. m_buffer.overwrite(0, buffer_to_shift.data(), buffer_to_shift.size());
  545. m_buffered_size -= readable_size;
  546. return buffer.slice(0, readable_size);
  547. }
  548. // Returns whether a line can be read, populating the buffer in the process.
  549. ErrorOr<bool> can_read_line()
  550. {
  551. if (stream().is_eof() && m_buffered_size > 0)
  552. return true;
  553. if (m_buffer.span().slice(0, m_buffered_size).contains_slow('\n'))
  554. return true;
  555. if (!stream().is_readable())
  556. return false;
  557. while (m_buffered_size < m_buffer.size()) {
  558. auto populated_slice = TRY(populate_read_buffer());
  559. if (stream().is_eof()) {
  560. // We give the user one last hurrah to read the remaining
  561. // contents as a "line".
  562. return m_buffered_size > 0;
  563. }
  564. if (populated_slice.contains_slow('\n'))
  565. return true;
  566. if (populated_slice.is_empty())
  567. break;
  568. }
  569. return false;
  570. }
  571. bool is_eof() const
  572. {
  573. if (m_buffered_size > 0) {
  574. return false;
  575. }
  576. return stream().is_eof();
  577. }
  578. size_t buffer_size() const
  579. {
  580. return m_buffer.size();
  581. }
  582. size_t buffered_data_size() const
  583. {
  584. return m_buffered_size;
  585. }
  586. void clear_buffer()
  587. {
  588. m_buffered_size = 0;
  589. }
  590. private:
  591. ErrorOr<ReadonlyBytes> populate_read_buffer()
  592. {
  593. if (m_buffered_size == m_buffer.size())
  594. return ReadonlyBytes {};
  595. auto fillable_slice = m_buffer.span().slice(m_buffered_size);
  596. size_t nread = 0;
  597. do {
  598. auto result = stream().read(fillable_slice);
  599. if (result.is_error()) {
  600. if (!result.error().is_errno())
  601. return result.error();
  602. if (result.error().code() == EINTR)
  603. continue;
  604. if (result.error().code() == EAGAIN)
  605. break;
  606. return result.error();
  607. }
  608. auto read_size = result.value().size();
  609. m_buffered_size += read_size;
  610. nread += read_size;
  611. break;
  612. } while (true);
  613. return fillable_slice.slice(0, nread);
  614. }
  615. NonnullOwnPtr<T> m_stream;
  616. // FIXME: Replacing this with a circular buffer would be really nice and
  617. // would avoid excessive copies; however, right now
  618. // AK::CircularDuplexBuffer inlines its entire contents, and that
  619. // would make for a very large object on the stack.
  620. //
  621. // The proper fix is to make a CircularQueue which uses a buffer on
  622. // the heap.
  623. ByteBuffer m_buffer;
  624. size_t m_buffered_size { 0 };
  625. };
  626. // NOTE: A Buffered which accepts any Stream could be added here, but it is not
  627. // needed at the moment.
  628. template<SeekableStreamLike T>
  629. class BufferedSeekable final : public SeekableStream {
  630. friend BufferedHelper<T>;
  631. public:
  632. static ErrorOr<NonnullOwnPtr<BufferedSeekable<T>>> create(NonnullOwnPtr<T> stream, size_t buffer_size = 16384)
  633. {
  634. return BufferedHelper<T>::template create_buffered<BufferedSeekable>(move(stream), buffer_size);
  635. }
  636. BufferedSeekable(BufferedSeekable&& other) = default;
  637. BufferedSeekable& operator=(BufferedSeekable&& other) = default;
  638. virtual bool is_readable() const override { return m_helper.stream().is_readable(); }
  639. virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(move(buffer)); }
  640. virtual bool is_writable() const override { return m_helper.stream().is_writable(); }
  641. virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.stream().write(buffer); }
  642. virtual bool is_eof() const override { return m_helper.is_eof(); }
  643. virtual bool is_open() const override { return m_helper.stream().is_open(); }
  644. virtual void close() override { m_helper.stream().close(); }
  645. virtual ErrorOr<off_t> seek(i64 offset, SeekMode mode) override
  646. {
  647. auto result = TRY(m_helper.stream().seek(offset, mode));
  648. m_helper.clear_buffer();
  649. return result;
  650. }
  651. virtual ErrorOr<void> truncate(off_t length) override
  652. {
  653. return m_helper.stream().truncate(length);
  654. }
  655. ErrorOr<StringView> read_line(Bytes buffer) { return m_helper.read_line(move(buffer)); }
  656. ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) { return m_helper.read_until(move(buffer), move(candidate)); }
  657. template<size_t N>
  658. ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }
  659. ErrorOr<bool> can_read_line() { return m_helper.can_read_line(); }
  660. size_t buffer_size() const { return m_helper.buffer_size(); }
  661. virtual ~BufferedSeekable() override = default;
  662. private:
  663. BufferedSeekable(NonnullOwnPtr<T> stream, ByteBuffer buffer)
  664. : m_helper(Badge<BufferedSeekable<T>> {}, move(stream), buffer)
  665. {
  666. }
  667. BufferedHelper<T> m_helper;
  668. };
  669. class BufferedSocketBase : public Socket {
  670. public:
  671. virtual ErrorOr<StringView> read_line(Bytes buffer) = 0;
  672. virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) = 0;
  673. virtual ErrorOr<bool> can_read_line() = 0;
  674. virtual size_t buffer_size() const = 0;
  675. };
  676. template<SocketLike T>
  677. class BufferedSocket final : public BufferedSocketBase {
  678. friend BufferedHelper<T>;
  679. public:
  680. static ErrorOr<NonnullOwnPtr<BufferedSocket<T>>> create(NonnullOwnPtr<T> stream, size_t buffer_size = 16384)
  681. {
  682. return BufferedHelper<T>::template create_buffered<BufferedSocket>(move(stream), buffer_size);
  683. }
  684. BufferedSocket(BufferedSocket&& other)
  685. : BufferedSocketBase(static_cast<BufferedSocketBase&&>(other))
  686. , m_helper(move(other.m_helper))
  687. {
  688. setup_notifier();
  689. }
  690. BufferedSocket& operator=(BufferedSocket&& other)
  691. {
  692. Socket::operator=(static_cast<Socket&&>(other));
  693. m_helper = move(other.m_helper);
  694. setup_notifier();
  695. return *this;
  696. }
  697. virtual bool is_readable() const override { return m_helper.stream().is_readable(); }
  698. virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_helper.read(move(buffer)); }
  699. virtual bool is_writable() const override { return m_helper.stream().is_writable(); }
  700. virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_helper.stream().write(buffer); }
  701. virtual bool is_eof() const override { return m_helper.is_eof(); }
  702. virtual bool is_open() const override { return m_helper.stream().is_open(); }
  703. virtual void close() override { m_helper.stream().close(); }
  704. virtual ErrorOr<size_t> pending_bytes() const override
  705. {
  706. return TRY(m_helper.stream().pending_bytes()) + m_helper.buffered_data_size();
  707. }
  708. virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_helper.buffered_data_size() > 0 || TRY(m_helper.stream().can_read_without_blocking(timeout)); }
  709. virtual ErrorOr<void> set_blocking(bool enabled) override { return m_helper.stream().set_blocking(enabled); }
  710. virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_helper.stream().set_close_on_exec(enabled); }
  711. virtual void set_notifications_enabled(bool enabled) override { m_helper.stream().set_notifications_enabled(enabled); }
  712. virtual ErrorOr<StringView> read_line(Bytes buffer) override { return m_helper.read_line(move(buffer)); }
  713. virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); }
  714. template<size_t N>
  715. ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }
  716. virtual ErrorOr<bool> can_read_line() override { return m_helper.can_read_line(); }
  717. virtual size_t buffer_size() const override { return m_helper.buffer_size(); }
  718. virtual ~BufferedSocket() override = default;
  719. private:
  720. BufferedSocket(NonnullOwnPtr<T> stream, ByteBuffer buffer)
  721. : m_helper(Badge<BufferedSocket<T>> {}, move(stream), buffer)
  722. {
  723. setup_notifier();
  724. }
  725. void setup_notifier()
  726. {
  727. m_helper.stream().on_ready_to_read = [this] {
  728. if (on_ready_to_read)
  729. on_ready_to_read();
  730. };
  731. }
  732. BufferedHelper<T> m_helper;
  733. };
  734. using BufferedFile = BufferedSeekable<File>;
  735. using BufferedTCPSocket = BufferedSocket<TCPSocket>;
  736. using BufferedUDPSocket = BufferedSocket<UDPSocket>;
  737. using BufferedLocalSocket = BufferedSocket<LocalSocket>;
  738. /// A BasicReusableSocket allows one to use one of the base Core::Stream classes
  739. /// as a ReusableSocket. It does not preserve any connection state or options,
  740. /// and instead just recreates the stream when reconnecting.
  741. template<SocketLike T>
  742. class BasicReusableSocket final : public ReusableSocket {
  743. public:
  744. static ErrorOr<NonnullOwnPtr<BasicReusableSocket<T>>> connect(String const& host, u16 port)
  745. {
  746. return make<BasicReusableSocket<T>>(TRY(T::connect(host, port)));
  747. }
  748. static ErrorOr<NonnullOwnPtr<BasicReusableSocket<T>>> connect(SocketAddress const& address)
  749. {
  750. return make<BasicReusableSocket<T>>(TRY(T::connect(address)));
  751. }
  752. virtual bool is_connected() override
  753. {
  754. return m_socket.is_open();
  755. }
  756. virtual ErrorOr<void> reconnect(String const& host, u16 port) override
  757. {
  758. if (is_connected())
  759. return Error::from_errno(EALREADY);
  760. m_socket = TRY(T::connect(host, port));
  761. return {};
  762. }
  763. virtual ErrorOr<void> reconnect(SocketAddress const& address) override
  764. {
  765. if (is_connected())
  766. return Error::from_errno(EALREADY);
  767. m_socket = TRY(T::connect(address));
  768. return {};
  769. }
  770. virtual bool is_readable() const override { return m_socket.is_readable(); }
  771. virtual ErrorOr<Bytes> read(Bytes buffer) override { return m_socket.read(move(buffer)); }
  772. virtual bool is_writable() const override { return m_socket.is_writable(); }
  773. virtual ErrorOr<size_t> write(ReadonlyBytes buffer) override { return m_socket.write(buffer); }
  774. virtual bool is_eof() const override { return m_socket.is_eof(); }
  775. virtual bool is_open() const override { return m_socket.is_open(); }
  776. virtual void close() override { m_socket.close(); }
  777. virtual ErrorOr<size_t> pending_bytes() const override { return m_socket.pending_bytes(); }
  778. virtual ErrorOr<bool> can_read_without_blocking(int timeout = 0) const override { return m_socket.can_read_without_blocking(timeout); }
  779. virtual ErrorOr<void> set_blocking(bool enabled) override { return m_socket.set_blocking(enabled); }
  780. virtual ErrorOr<void> set_close_on_exec(bool enabled) override { return m_socket.set_close_on_exec(enabled); }
  781. private:
  782. BasicReusableSocket(NonnullOwnPtr<T> socket)
  783. : m_socket(move(socket))
  784. {
  785. }
  786. NonnullOwnPtr<T> m_socket;
  787. };
  788. using ReusableTCPSocket = BasicReusableSocket<TCPSocket>;
  789. using ReusableUDPSocket = BasicReusableSocket<UDPSocket>;
  790. }