Skip to content

Commit 04f8b2f

Browse files
authored
Merge pull request #1231 from AndreasAakesson/dev
TCP Connection Stream interface
2 parents 3daaa6a + 7def409 commit 04f8b2f

16 files changed

Lines changed: 511 additions & 211 deletions

File tree

api/net/http/client_connection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ namespace http {
3636
using timeout_duration = std::chrono::milliseconds;
3737

3838
public:
39-
explicit Client_connection(Client&, TCP_conn);
39+
explicit Client_connection(Client&, Stream_ptr);
4040

4141
bool available() const
4242
{ return on_response_ == nullptr && keep_alive_; }

api/net/http/connection.hpp

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,30 @@ namespace http {
2929

3030
class Connection {
3131
public:
32-
using TCP_conn = net::tcp::Connection_ptr;
32+
using Stream = net::tcp::Connection::Stream;
33+
using Stream_ptr = std::unique_ptr<Stream>;
3334
using Peer = net::tcp::Socket;
3435
using buffer_t = net::tcp::buffer_t;
3536

3637
public:
37-
inline explicit Connection(TCP_conn tcpconn, bool keep_alive = true);
38+
inline explicit Connection(Stream_ptr stream, bool keep_alive = true);
3839

3940
template <typename TCP>
4041
explicit Connection(TCP&, Peer);
4142

4243
inline explicit Connection() noexcept;
4344

4445
net::tcp::port_t local_port() const noexcept
45-
{ return (tcpconn_) ? tcpconn_->local_port() : 0; }
46+
{ return (stream_) ? stream_->local_port() : 0; }
4647

4748
Peer peer() const noexcept
4849
{ return peer_; }
4950

5051
void timeout()
51-
{ tcpconn_->is_closing() ? tcpconn_->abort() : tcpconn_->close(); }
52+
{ stream_->is_closing() ? stream_->abort() : stream_->close(); }
5253

53-
auto&& tcp() const
54-
{ return tcpconn_; }
54+
auto& stream() const
55+
{ return stream_; }
5556

5657
/**
5758
* @brief Shutdown the underlying TCP connection
@@ -64,15 +65,15 @@ namespace http {
6465
*
6566
* @return The underlying TCP connection
6667
*/
67-
inline TCP_conn release();
68+
inline Stream_ptr release();
6869

6970
/**
7071
* @brief Whether the underlying TCP connection has been released or not
7172
*
7273
* @return true if the underlying TCP connection is released
7374
*/
7475
bool released() const
75-
{ return tcpconn_ == nullptr; }
76+
{ return stream_ == nullptr; }
7677

7778
static Connection& empty() noexcept
7879
{
@@ -101,52 +102,50 @@ namespace http {
101102
virtual ~Connection() {}
102103

103104
protected:
104-
TCP_conn tcpconn_;
105+
Stream_ptr stream_;
105106
bool keep_alive_;
106107
Peer peer_;
107108

108109
virtual void close() {}
109110

110111
}; // < class Connection
111112

112-
inline Connection::Connection(TCP_conn tcpconn, bool keep_alive)
113-
: tcpconn_{std::move(tcpconn)},
113+
inline Connection::Connection(Stream_ptr stream, bool keep_alive)
114+
: stream_{std::move(stream)},
114115
keep_alive_{keep_alive},
115-
peer_{tcpconn_->remote()}
116+
peer_{stream_->remote()}
116117
{
117-
Ensures(tcpconn_ != nullptr);
118+
Ensures(stream_ != nullptr);
118119
debug("<http::Connection> Created %u -> %s %p\n", local_port(), peer().to_string().c_str(), this);
119120
}
120121

121122
template <typename TCP>
122123
Connection::Connection(TCP& tcp, Peer addr)
123-
: Connection(tcp.connect(addr))
124+
: Connection(std::make_unique<Stream>(tcp.connect(addr)))
124125
{
125126
}
126127

127128
inline Connection::Connection() noexcept
128-
: tcpconn_(nullptr),
129+
: stream_(nullptr),
129130
keep_alive_(false),
130131
peer_{}
131132
{
132133
}
133134

134135
inline void Connection::shutdown()
135136
{
136-
if(not released() and not tcpconn_->is_closing())
137-
tcpconn_->close();
137+
if(not released() and not stream_->is_closing())
138+
stream_->close();
138139
}
139140

140-
inline Connection::TCP_conn Connection::release()
141+
inline Connection::Stream_ptr Connection::release()
141142
{
142-
auto copy = tcpconn_;
143+
auto copy = std::move(stream_);
143144

144145
// this is expensive and may be unecessary,
145146
// but just to be safe for now
146147
copy->reset_callbacks();
147148

148-
tcpconn_ = nullptr;
149-
150149
return copy;
151150
}
152151

api/net/http/server.hpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace http {
6363
*
6464
* @param[in] port The port to listen on
6565
*/
66-
void listen(uint16_t port);
66+
virtual void listen(uint16_t port);
6767

6868
/**
6969
* @brief Setup handler for when a Request is received
@@ -90,21 +90,6 @@ namespace http {
9090
*/
9191
Response_ptr create_response(status_t code = http::OK) const;
9292

93-
/**
94-
* @brief Returns a vector of all TCP connections currently connected to the server
95-
*
96-
* @return A vector of TCP connections
97-
*/
98-
std::vector<TCP_conn> active_tcp_connections() const;
99-
100-
/**
101-
* @brief Reconnects a TCP connection by creating a Server connection
102-
*
103-
* @param[in] conn The TCP connection
104-
*/
105-
void reconnect(TCP_conn conn)
106-
{ if (conn != nullptr) connect(conn); }
107-
10893
~Server();
10994

11095
private:
@@ -124,7 +109,10 @@ namespace http {
124109
Stat& stat_req_bad_;
125110
Stat& stat_timeouts_;
126111

127-
void connect(TCP_conn conn);
112+
void connect(TCP_conn conn)
113+
{ connect(std::make_unique<Connection::Stream>(conn)); }
114+
115+
void connect(Connection::Stream_ptr stream);
128116

129117
void close(Server_connection&);
130118

api/net/http/server_connection.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace http {
3333
static constexpr size_t DEFAULT_BUFSIZE = 1460;
3434

3535
public:
36-
explicit Server_connection(Server&, TCP_conn, size_t idx, const size_t bufsize = DEFAULT_BUFSIZE);
36+
explicit Server_connection(Server&, Stream_ptr, size_t idx, const size_t bufsize = DEFAULT_BUFSIZE);
3737

3838
void send(Response_ptr res);
3939

0 commit comments

Comments
 (0)