|
1 | 1 | // This file is a part of the IncludeOS unikernel - www.includeos.org |
2 | 2 | // |
3 | | -// Copyright 2016 Oslo and Akershus University College of Applied Sciences |
| 3 | +// Copyright 2016-2017 Oslo and Akershus University College of Applied Sciences |
4 | 4 | // and Alfred Bratterud |
5 | 5 | // |
6 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
|
19 | 19 | #ifndef HTTP_CONNECTION_HPP |
20 | 20 | #define HTTP_CONNECTION_HPP |
21 | 21 |
|
22 | | -#include "common.hpp" |
| 22 | +// http |
23 | 23 | #include "request.hpp" |
24 | 24 | #include "response.hpp" |
25 | | -#include "error.hpp" |
| 25 | + |
26 | 26 | #include <net/tcp/connection.hpp> |
27 | | -#include <map> |
28 | | -#include <vector> |
29 | | -#include <delegate> |
30 | | -#include <util/timer.hpp> |
31 | 27 |
|
32 | 28 | namespace http { |
33 | 29 |
|
34 | 30 | class Connection { |
35 | 31 | public: |
36 | | - using TCP_conn_ptr = net::tcp::Connection_ptr; |
37 | | - using Peer = net::tcp::Socket; |
38 | | - using buffer_t = net::tcp::buffer_t; |
39 | | - using Close_handler = delegate<void(Connection&)>; |
40 | | - using timeout_duration = std::chrono::milliseconds; |
| 32 | + using TCP_conn = net::tcp::Connection_ptr; |
| 33 | + using Peer = net::tcp::Socket; |
| 34 | + using buffer_t = net::tcp::buffer_t; |
41 | 35 |
|
42 | 36 | public: |
43 | | - |
44 | | - explicit Connection(TCP_conn_ptr, Close_handler); |
| 37 | + inline explicit Connection(TCP_conn tcpconn, bool keep_alive = true); |
45 | 38 |
|
46 | 39 | template <typename TCP> |
47 | | - explicit Connection(TCP&, Peer, Close_handler); |
48 | | - |
49 | | - bool available() const |
50 | | - { return on_response_ == nullptr && keep_alive_; } |
51 | | - |
52 | | - bool occupied() const |
53 | | - { return !available(); } |
| 40 | + explicit Connection(TCP&, Peer); |
54 | 41 |
|
55 | | - void send(Request_ptr, Response_handler, const size_t bufsize, timeout_duration = timeout_duration::zero()); |
56 | | - |
57 | | - net::tcp::port_t local_port() const |
| 42 | + net::tcp::port_t local_port() const noexcept |
58 | 43 | { return (tcpconn_) ? tcpconn_->local_port() : 0; } |
59 | 44 |
|
60 | | - Peer peer() const |
| 45 | + Peer peer() const noexcept |
61 | 46 | { return (tcpconn_) ? tcpconn_->remote() : Peer(); } |
62 | | - //bool operator==(const Connection& other) |
63 | | - //{ return this == &other; } |
64 | | - //{ return tcpconn_->local_port() == other.tcpconn_->local_port(); } |
65 | 47 |
|
66 | | - private: |
67 | | - TCP_conn_ptr tcpconn_; |
| 48 | + void timeout() |
| 49 | + { tcpconn_->is_closing() ? tcpconn_->abort() : tcpconn_->close(); } |
| 50 | + |
| 51 | + protected: |
| 52 | + TCP_conn tcpconn_; |
68 | 53 | Request_ptr req_; |
69 | 54 | Response_ptr res_; |
70 | | - Close_handler on_close_; |
71 | | - Response_handler on_response_; |
72 | | - Timer timer_; |
73 | | - |
74 | | - timeout_duration timeout_dur_; |
75 | | - bool keep_alive_; |
76 | | - |
77 | | - void send_request(const size_t bufsize); |
78 | | - |
79 | | - void recv_response(buffer_t buf, size_t len); |
80 | | - |
81 | | - void end_response(Error err = Error::NONE); |
82 | | - |
83 | | - void timeout_request() |
84 | | - { end_response(Error::TIMEOUT); } |
85 | | - |
86 | | - void close(); |
| 55 | + bool keep_alive_; |
87 | 56 |
|
88 | 57 | }; // < class Connection |
89 | 58 |
|
| 59 | + inline Connection::Connection(TCP_conn tcpconn, bool keep_alive) |
| 60 | + : tcpconn_{std::move(tcpconn)}, |
| 61 | + req_{nullptr}, |
| 62 | + res_{nullptr}, |
| 63 | + keep_alive_{keep_alive} |
| 64 | + { |
| 65 | + Ensures(tcpconn_ != nullptr); |
| 66 | + debug("<http::Connection> Created %u -> %s %p\n", local_port(), peer().to_string().c_str(), this); |
| 67 | + } |
| 68 | + |
| 69 | + template <typename TCP> |
| 70 | + Connection::Connection(TCP& tcp, Peer addr) |
| 71 | + : Connection(tcp.connect(addr)) |
| 72 | + { |
| 73 | + } |
| 74 | + |
90 | 75 | } // < namespace http |
91 | 76 |
|
92 | 77 | #endif // < HTTP_CONNECTION_HPP |
0 commit comments