@@ -56,6 +56,9 @@ class Connection : public std::enable_shared_from_this<Connection> {
5656 struct Disconnect ;
5757 /* * Reason for packet being dropped */
5858 enum class Drop_reason ;
59+ /* * A Connection stream */
60+ class Stream ;
61+
5962 using Byte = uint8_t ;
6063
6164 using WriteBuffer = Write_queue::WriteBuffer;
@@ -214,6 +217,205 @@ class Connection : public std::enable_shared_from_this<Connection> {
214217 */
215218 inline void abort ();
216219
220+ /* *
221+ * @brief Exposes a TCP Connection as a Stream with only the most necessary features.
222+ * May be overrided by extensions like TLS etc for additional functionality.
223+ */
224+ class Stream {
225+ public:
226+ /* *
227+ * @brief Construct a Stream for a Connection ptr
228+ *
229+ * @param[in] conn The connection
230+ */
231+ Stream (Connection_ptr conn)
232+ : ptr{std::move (conn)}
233+ {}
234+
235+ /* * Called when the stream is ready to use. */
236+ using ConnectCallback = delegate<void (Stream& self)>;
237+ /* *
238+ * @brief Event when the stream is connected/established/ready to use.
239+ *
240+ * @param[in] cb The connect callback
241+ */
242+ virtual void on_connect (ConnectCallback cb)
243+ {
244+ ptr->on_connect (Connection::ConnectCallback::make_packed (
245+ [this , cb] (Connection_ptr)
246+ { cb (*this ); }));
247+ }
248+
249+ /* * Called with a shared buffer and the length of the data when received. */
250+ using ReadCallback = delegate<void (buffer_t , size_t )>;
251+ /* *
252+ * @brief Event when data is received.
253+ *
254+ * @param[in] n The size of the receive buffer
255+ * @param[in] cb The read callback
256+ */
257+ virtual void on_read (size_t n, ReadCallback cb)
258+ { ptr->on_read (n, cb); }
259+
260+ /* * Called with nothing ¯\_(ツ)_/¯ */
261+ using CloseCallback = delegate<void ()>;
262+ /* *
263+ * @brief Event for when the Stream is being closed.
264+ *
265+ * @param[in] cb The close callback
266+ */
267+ virtual void on_close (CloseCallback cb)
268+ { ptr->on_close (cb); }
269+
270+ /* * Called with the number of bytes written. */
271+ using WriteCallback = delegate<void (size_t )>;
272+ /* *
273+ * @brief Event for when data has been written.
274+ *
275+ * @param[in] cb The write callback
276+ */
277+ virtual void on_write (WriteCallback cb)
278+ { ptr->on_write (cb); }
279+
280+ /* *
281+ * @brief Async write of a data with a length.
282+ *
283+ * @param[in] buf data
284+ * @param[in] n length
285+ */
286+ virtual void write (const void * buf, size_t n)
287+ { ptr->write (buf, n); }
288+
289+ /* *
290+ * @brief Async write of a chunk.
291+ *
292+ * @param[in] c A chunk
293+ */
294+ virtual void write (Chunk c)
295+ { ptr->write (c); }
296+
297+ /* *
298+ * @brief Async write of a shared buffer with a length.
299+ * Calls write(Chunk c).
300+ *
301+ * @param[in] buffer shared buffer
302+ * @param[in] n length
303+ */
304+ virtual void write (buffer_t buf, size_t n)
305+ { ptr->write (buf, n); }
306+
307+ /* *
308+ * @brief Async write of a string.
309+ * Calls write(const void* buf, size_t n)
310+ *
311+ * @param[in] str The string
312+ */
313+ void write (const std::string& str)
314+ { write (str.data (), str.size ()); }
315+
316+ /* *
317+ * @brief Closes the stream.
318+ */
319+ virtual void close ()
320+ { ptr->close (); }
321+
322+ /* *
323+ * @brief Aborts (terminates) the stream.
324+ */
325+ virtual void abort ()
326+ { ptr->abort (); }
327+
328+ /* *
329+ * @brief Resets all callbacks.
330+ */
331+ virtual void reset_callbacks ()
332+ { ptr->reset_callbacks (); }
333+
334+ /* *
335+ * @brief Returns the streams local socket.
336+ *
337+ * @return A TCP Socket
338+ */
339+ tcp::Socket local () const
340+ { return ptr->local (); }
341+
342+ /* *
343+ * @brief Returns the streams remote socket.
344+ *
345+ * @return A TCP Socket
346+ */
347+ tcp::Socket remote () const
348+ { return ptr->remote (); }
349+
350+ /* *
351+ * @brief Returns the local port.
352+ *
353+ * @return A TCP port
354+ */
355+ uint16_t local_port () const
356+ { return ptr->local_port (); }
357+
358+ /* *
359+ * @brief Returns a string representation of the stream.
360+ *
361+ * @return String representation of the stream.
362+ */
363+ virtual std::string to_string () const noexcept
364+ { return ptr->to_string (); }
365+
366+ /* *
367+ * @brief Determines if connected (established).
368+ *
369+ * @return True if connected, False otherwise.
370+ */
371+ virtual bool is_connected () const noexcept
372+ { return ptr->is_connected (); }
373+
374+ /* *
375+ * @brief Determines if writable. (write is allowed)
376+ *
377+ * @return True if writable, False otherwise.
378+ */
379+ virtual bool is_writable () const noexcept
380+ { return ptr->is_writable (); }
381+
382+ /* *
383+ * @brief Determines if readable. (data can be received)
384+ *
385+ * @return True if readable, False otherwise.
386+ */
387+ virtual bool is_readable () const noexcept
388+ { return ptr->is_readable (); }
389+
390+ /* *
391+ * @brief Determines if closing.
392+ *
393+ * @return True if closing, False otherwise.
394+ */
395+ virtual bool is_closing () const noexcept
396+ { return ptr->is_closing (); }
397+
398+ /* *
399+ * @brief Determines if closed.
400+ *
401+ * @return True if closed, False otherwise.
402+ */
403+ virtual bool is_closed () const noexcept
404+ { return ptr->is_closed (); };
405+
406+ protected:
407+ /* *
408+ * @brief Returns the underlying TCP connection.
409+ *
410+ * @return A TCP Connection ptr
411+ */
412+ Connection_ptr tcp ()
413+ { return ptr; };
414+
415+ private:
416+ Connection_ptr ptr;
417+
418+ }; // < class Connection::Stream
217419
218420 /* *
219421 * @brief Reason for disconnect event.
0 commit comments