Stream.h 37 KB

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