|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2015-2017 Oslo and Akershus University College of Applied Sciences |
| 4 | +// and Alfred Bratterud |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#pragma once |
| 19 | +#ifndef NET_STREAM_HPP |
| 20 | +#define NET_STREAM_HPP |
| 21 | + |
| 22 | +#include <cstdint> |
| 23 | +#include <cstddef> |
| 24 | +#include <delegate> |
| 25 | +#include <util/chunk.hpp> |
| 26 | +#include "tcp/socket.hpp" |
| 27 | + |
| 28 | +namespace net { |
| 29 | + class Stream; |
| 30 | + using Stream_ptr = std::unique_ptr<Stream>; |
| 31 | + /** |
| 32 | + * @brief An abstract network Stream interface based on TCP. |
| 33 | + */ |
| 34 | + class Stream { |
| 35 | + public: |
| 36 | + using buffer_t = std::shared_ptr<uint8_t>; |
| 37 | + using ptr = Stream_ptr; |
| 38 | + |
| 39 | + /** Called when the stream is ready to be used. */ |
| 40 | + using ConnectCallback = delegate<void(Stream& self)>; |
| 41 | + /** |
| 42 | + * @brief Event when the stream is connected/established/ready to be used. |
| 43 | + * |
| 44 | + * @param[in] cb The connect callback |
| 45 | + */ |
| 46 | + virtual void on_connect(ConnectCallback cb) = 0; |
| 47 | + |
| 48 | + /** Called with a shared buffer and the length of the data when received. */ |
| 49 | + using ReadCallback = delegate<void(buffer_t, size_t)>; |
| 50 | + /** |
| 51 | + * @brief Event when data is received. |
| 52 | + * |
| 53 | + * @param[in] n The size of the receive buffer |
| 54 | + * @param[in] cb The read callback |
| 55 | + */ |
| 56 | + virtual void on_read(size_t n, ReadCallback cb) = 0; |
| 57 | + |
| 58 | + /** Called with nothing ¯\_(ツ)_/¯ */ |
| 59 | + using CloseCallback = delegate<void()>; |
| 60 | + /** |
| 61 | + * @brief Event for when the Stream is being closed. |
| 62 | + * |
| 63 | + * @param[in] cb The close callback |
| 64 | + */ |
| 65 | + virtual void on_close(CloseCallback cb) = 0; |
| 66 | + |
| 67 | + /** Called with the number of bytes written. */ |
| 68 | + using WriteCallback = delegate<void(size_t)>; |
| 69 | + /** |
| 70 | + * @brief Event for when data has been written. |
| 71 | + * |
| 72 | + * @param[in] cb The write callback |
| 73 | + */ |
| 74 | + virtual void on_write(WriteCallback cb) = 0; |
| 75 | + |
| 76 | + /** |
| 77 | + * @brief Async write of a data with a length. |
| 78 | + * |
| 79 | + * @param[in] buf data |
| 80 | + * @param[in] n length |
| 81 | + */ |
| 82 | + virtual void write(const void* buf, size_t n) = 0; |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Async write of a chunk. |
| 86 | + * |
| 87 | + * @param[in] c A chunk |
| 88 | + */ |
| 89 | + virtual void write(Chunk c) = 0; |
| 90 | + |
| 91 | + /** |
| 92 | + * @brief Async write of a shared buffer with a length. |
| 93 | + * |
| 94 | + * @param[in] buffer shared buffer |
| 95 | + * @param[in] n length |
| 96 | + */ |
| 97 | + virtual void write(buffer_t buf, size_t n) = 0; |
| 98 | + |
| 99 | + /** |
| 100 | + * @brief Async write of a string. |
| 101 | + * |
| 102 | + * @param[in] str The string |
| 103 | + */ |
| 104 | + virtual void write(const std::string& str) = 0; |
| 105 | + |
| 106 | + /** |
| 107 | + * @brief Closes the stream. |
| 108 | + */ |
| 109 | + virtual void close() = 0; |
| 110 | + |
| 111 | + /** |
| 112 | + * @brief Aborts (terminates) the stream. |
| 113 | + */ |
| 114 | + virtual void abort() = 0; |
| 115 | + |
| 116 | + /** |
| 117 | + * @brief Resets all callbacks. |
| 118 | + */ |
| 119 | + virtual void reset_callbacks() = 0; |
| 120 | + |
| 121 | + /** |
| 122 | + * @brief Returns the streams local socket. |
| 123 | + * |
| 124 | + * @return A TCP Socket |
| 125 | + */ |
| 126 | + virtual tcp::Socket local() const = 0; |
| 127 | + |
| 128 | + /** |
| 129 | + * @brief Returns the streams remote socket. |
| 130 | + * |
| 131 | + * @return A TCP Socket |
| 132 | + */ |
| 133 | + virtual tcp::Socket remote() const = 0; |
| 134 | + |
| 135 | + /** |
| 136 | + * @brief Returns the local port. |
| 137 | + * |
| 138 | + * @return A TCP port |
| 139 | + */ |
| 140 | + virtual uint16_t local_port() const = 0; |
| 141 | + |
| 142 | + /** |
| 143 | + * @brief Returns a string representation of the stream. |
| 144 | + * |
| 145 | + * @return String representation of the stream. |
| 146 | + */ |
| 147 | + virtual std::string to_string() const = 0; |
| 148 | + |
| 149 | + /** |
| 150 | + * @brief Determines if connected (established). |
| 151 | + * |
| 152 | + * @return True if connected, False otherwise. |
| 153 | + */ |
| 154 | + virtual bool is_connected() const noexcept = 0; |
| 155 | + |
| 156 | + /** |
| 157 | + * @brief Determines if writable. (write is allowed) |
| 158 | + * |
| 159 | + * @return True if writable, False otherwise. |
| 160 | + */ |
| 161 | + virtual bool is_writable() const noexcept = 0; |
| 162 | + |
| 163 | + /** |
| 164 | + * @brief Determines if readable. (data can be received) |
| 165 | + * |
| 166 | + * @return True if readable, False otherwise. |
| 167 | + */ |
| 168 | + virtual bool is_readable() const noexcept = 0; |
| 169 | + |
| 170 | + /** |
| 171 | + * @brief Determines if closing. |
| 172 | + * |
| 173 | + * @return True if closing, False otherwise. |
| 174 | + */ |
| 175 | + virtual bool is_closing() const noexcept = 0; |
| 176 | + |
| 177 | + /** |
| 178 | + * @brief Determines if closed. |
| 179 | + * |
| 180 | + * @return True if closed, False otherwise. |
| 181 | + */ |
| 182 | + virtual bool is_closed() const noexcept = 0; |
| 183 | + |
| 184 | + Stream() = default; |
| 185 | + virtual ~Stream() {} |
| 186 | + |
| 187 | + }; // < class Stream |
| 188 | + |
| 189 | +} // < namespace net |
| 190 | + |
| 191 | +#endif // < NET_STREAM_HPP |
0 commit comments