2018-01-13 03:57:58 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2018-01-14 03:03:04 +01:00
|
|
|
#include <ircd/server/server.h>
|
2017-10-25 18:39:58 +02:00
|
|
|
#include <ircd/asio.h>
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
namespace ircd::server
|
|
|
|
{
|
|
|
|
std::shared_ptr<node> create(const net::hostport &);
|
|
|
|
}
|
|
|
|
|
|
|
|
decltype(ircd::server::log)
|
|
|
|
ircd::server::log
|
|
|
|
{
|
|
|
|
"server", 'S'
|
|
|
|
};
|
|
|
|
|
|
|
|
decltype(ircd::server::nodes)
|
|
|
|
ircd::server::nodes
|
|
|
|
{};
|
|
|
|
|
|
|
|
ircd::server::node &
|
|
|
|
ircd::server::get(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
auto it(nodes.lower_bound(host(hostport)));
|
|
|
|
if(it == nodes.end() || it->first != host(hostport))
|
|
|
|
{
|
|
|
|
auto node{create(hostport)};
|
|
|
|
const string_view key{node->remote.hostname};
|
|
|
|
it = nodes.emplace_hint(it, key, std::move(node));
|
|
|
|
}
|
|
|
|
|
|
|
|
return *it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ircd::server::node>
|
|
|
|
ircd::server::create(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
auto node(std::make_shared<node>());
|
|
|
|
node->remote.hostname = std::string{host(hostport)};
|
|
|
|
node->resolve(hostport);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::node &
|
|
|
|
ircd::server::find(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
return *nodes.at(host(hostport));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::exists(const net::hostport &hostport)
|
|
|
|
{
|
|
|
|
return nodes.find(host(hostport)) != end(nodes);
|
|
|
|
}
|
|
|
|
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
|
|
|
// init
|
|
|
|
//
|
|
|
|
|
|
|
|
ircd::server::init::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::init::~init()
|
|
|
|
noexcept
|
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
nodes.clear();
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2017-11-30 19:27:02 +01:00
|
|
|
void
|
|
|
|
ircd::server::init::interrupt()
|
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
nodes.clear();
|
2017-11-30 19:27:02 +01:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
///
|
|
|
|
// request
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
2018-01-13 03:57:58 +01:00
|
|
|
|
|
|
|
ircd::server::request::request(const net::hostport &hostport,
|
|
|
|
server::out out,
|
|
|
|
server::in in)
|
|
|
|
:tag{nullptr}
|
|
|
|
,out{std::move(out)}
|
|
|
|
,in{std::move(in)}
|
|
|
|
{
|
|
|
|
auto &node(server::get(hostport));
|
|
|
|
node.submit(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::request::request(request &&o)
|
|
|
|
noexcept
|
|
|
|
:ctx::future<http::code>{std::move(o)}
|
|
|
|
,tag{std::move(o.tag)}
|
|
|
|
,out{std::move(o.out)}
|
|
|
|
,in{std::move(o.in)}
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::request &
|
|
|
|
ircd::server::request::operator=(request &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
ctx::future<http::code>::operator=(std::move(o));
|
|
|
|
tag = std::move(o.tag);
|
|
|
|
out = std::move(o.out);
|
|
|
|
in = std::move(o.in);
|
|
|
|
|
|
|
|
assert(tag->request == &o);
|
|
|
|
tag->request = this;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::request::~request()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// request::tag
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::request::tag::tag(server::request &request)
|
|
|
|
:request{&request}
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
static_cast<ctx::future<http::code> &>(request) = p;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::request::tag::tag(tag &&o)
|
|
|
|
noexcept
|
|
|
|
:request{std::move(o.request)}
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
assert(o.request->tag == &o);
|
|
|
|
o.request = nullptr;
|
|
|
|
request->tag = this;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
struct ircd::server::request::tag &
|
|
|
|
ircd::server::request::tag::operator=(tag &&o)
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
request = std::move(o.request);
|
|
|
|
|
|
|
|
assert(o.request->tag == &o);
|
|
|
|
o.request = nullptr;
|
|
|
|
request->tag = this;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::request::tag::~tag()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
/// Called by the controller of the socket with a view of the data received by
|
|
|
|
/// the socket. The location and size of `buffer` is the same or smaller than
|
|
|
|
/// the buffer previously supplied by make_read_buffer().
|
|
|
|
///
|
|
|
|
/// Sometimes make_read_buffer() supplies a buffer that is too large, and some
|
|
|
|
/// data read off the socket does not belong to this tag. In that case, This
|
|
|
|
/// function returns a const_buffer viewing the portion of `buffer` which is
|
|
|
|
/// considered the "overrun," and the socket controller will copy that over to
|
|
|
|
/// the next tag.
|
|
|
|
///
|
|
|
|
/// The tag indicates it is entirely finished with receiving its data by
|
|
|
|
/// setting the value of `done` to true. Otherwise it is assumed false.
|
|
|
|
///
|
|
|
|
ircd::const_buffer
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::request::tag::read_buffer(const const_buffer &buffer,
|
2018-01-14 06:16:49 +01:00
|
|
|
bool &done)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
return !request? const_buffer{}:
|
|
|
|
!request->in.head.status? read_head(buffer, done):
|
|
|
|
read_content(buffer, done);
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
/// An idempotent operation that provides the location of where the socket
|
|
|
|
/// should place the next received data. The tag figures this out based on
|
|
|
|
/// whether it receiving HTTP head data or whether it is in content mode.
|
|
|
|
///
|
|
|
|
/// In head mode, portions of the user's buffer are returned starting with
|
|
|
|
/// the entire buffer. After a call read_buffer() supplying more data from
|
|
|
|
/// the socket, the subsequent invocation of make_read_buffer() will return
|
|
|
|
/// smaller windows for the remaining free space. Head mode is completed when
|
|
|
|
/// an \r\n\r\n terminator is received.
|
|
|
|
///
|
|
|
|
/// In content mode, portions of the user's buffer are returned starting with
|
|
|
|
/// one that is at most content_length. After a call to read_buffer()
|
|
|
|
/// supplying more data, the remaining space of the remaining content_length
|
|
|
|
/// are returned.
|
|
|
|
///
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::request::tag::make_read_buffer()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
assert(request);
|
2018-01-14 06:16:49 +01:00
|
|
|
return !request? mutable_buffer{}:
|
|
|
|
!request->in.head.status? make_head_buffer():
|
|
|
|
make_content_buffer();
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
ircd::const_buffer
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::request::tag::read_head(const const_buffer &buffer,
|
2018-01-14 06:16:49 +01:00
|
|
|
bool &done)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(request);
|
2018-01-13 03:57:58 +01:00
|
|
|
auto &req{*request};
|
2018-01-14 06:16:49 +01:00
|
|
|
auto &head{req.in.head};
|
|
|
|
const auto &head_buffer{req.in.head_buffer};
|
|
|
|
const auto &content_buffer{req.in.content_buffer};
|
2018-01-13 03:57:58 +01:00
|
|
|
|
|
|
|
// informal search for head terminator
|
|
|
|
static const string_view terminator{"\r\n\r\n"};
|
|
|
|
const auto pos
|
|
|
|
{
|
|
|
|
string_view{data(buffer), size(buffer)}.find(terminator)
|
|
|
|
};
|
|
|
|
|
|
|
|
// No terminator found; account for what was received in this buffer
|
|
|
|
// for the next call to make_head_buffer() preparing for the subsequent
|
|
|
|
// invocation of this function with more data.
|
|
|
|
if(pos == string_view::npos)
|
|
|
|
{
|
|
|
|
head_read += size(buffer);
|
2018-01-14 06:16:49 +01:00
|
|
|
return {};
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The given buffer may go past the end of the head and we may already
|
|
|
|
// have part of the head from a previous invocation. This indicates
|
|
|
|
// how much dome was just received from this buffer only, including the
|
|
|
|
// terminator which is considered part of the head.
|
|
|
|
const size_t addl_head_bytes
|
2017-11-30 20:05:04 +01:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
pos + size(terminator)
|
2017-11-30 20:05:04 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 03:03:04 +01:00
|
|
|
assert(addl_head_bytes <= size(buffer));
|
2018-01-13 03:57:58 +01:00
|
|
|
head_read += addl_head_bytes;
|
|
|
|
|
|
|
|
// Setup the capstan and mark the end of the tape
|
2018-01-14 06:16:49 +01:00
|
|
|
parse::buffer pb{mutable_buffer{data(head_buffer), head_read}};
|
2018-01-13 03:57:58 +01:00
|
|
|
parse::capstan pc{pb};
|
|
|
|
pc.read += head_read;
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
head = http::response::head{pc};
|
2018-01-13 03:57:58 +01:00
|
|
|
assert(pb.completed() == head_read);
|
|
|
|
|
|
|
|
const size_t overrun_length
|
|
|
|
{
|
|
|
|
size(buffer) - addl_head_bytes
|
|
|
|
};
|
|
|
|
|
|
|
|
const size_t &content_read
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
std::min(head.content_length, overrun_length)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const const_buffer partial_content
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
data(head_buffer) + head_read, content_read
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Any partial content was written to the head buffer by accident,
|
|
|
|
// that has to be copied over to the content buffer.
|
2018-01-14 06:16:49 +01:00
|
|
|
this->content_read += copy(content_buffer, partial_content);
|
2018-01-13 03:57:58 +01:00
|
|
|
assert(this->content_read == size(partial_content));
|
|
|
|
|
|
|
|
// Anything remaining is not our response and must be given back
|
|
|
|
assert(overrun_length >= content_read);
|
2018-01-14 06:16:49 +01:00
|
|
|
const const_buffer overrun
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
data(head_buffer) + head_read + content_read, overrun_length - content_read
|
2017-10-25 18:39:58 +02:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
if(this->content_read == head.content_length)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
done = true;
|
|
|
|
p.set_value(http::status(head.status));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
return overrun;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
ircd::const_buffer
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::request::tag::read_content(const const_buffer &buffer,
|
2018-01-14 06:16:49 +01:00
|
|
|
bool &done)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(request);
|
2018-01-13 03:57:58 +01:00
|
|
|
auto &req{*request};
|
2018-01-14 06:16:49 +01:00
|
|
|
const auto &head{req.in.head};
|
|
|
|
const auto &content_buffer{req.in.content_buffer};
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
// The amount of remaining content for the response sequence
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(head.content_length >= content_read);
|
2018-01-13 03:57:58 +01:00
|
|
|
const size_t remaining
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
head.content_length - content_read
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
// The amount of content read in this buffer only.
|
|
|
|
const size_t addl_content_read
|
|
|
|
{
|
|
|
|
std::min(size(buffer), remaining)
|
|
|
|
};
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
const size_t overrun_length
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
size(buffer) - addl_content_read
|
|
|
|
};
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
content_read += addl_content_read;
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
// Report anything that doesn't belong to us.
|
2018-01-14 06:16:49 +01:00
|
|
|
const const_buffer overrun
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
data(content_buffer) + content_read, overrun_length
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(content_read <= head.content_length);
|
|
|
|
if(content_read == head.content_length)
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
done = true;
|
|
|
|
p.set_value(http::status(head.status));
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
2018-01-14 06:16:49 +01:00
|
|
|
|
|
|
|
return overrun;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::request::tag::make_head_buffer()
|
|
|
|
const
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(request);
|
2018-01-13 03:57:58 +01:00
|
|
|
const auto &req{*request};
|
2018-01-14 06:16:49 +01:00
|
|
|
const auto &head_buffer{req.in.head_buffer};
|
|
|
|
const auto &content_buffer{req.in.content_buffer};
|
|
|
|
|
|
|
|
if(head_read >= size(head_buffer))
|
2018-01-13 03:57:58 +01:00
|
|
|
return {};
|
|
|
|
|
|
|
|
const size_t remaining
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
size(head_buffer) - head_read
|
2017-10-25 18:39:58 +02:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(remaining <= size(head_buffer));
|
2018-01-13 03:57:58 +01:00
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
data(head_buffer) + head_read, remaining
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
return buffer;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::mutable_buffer
|
|
|
|
ircd::server::request::tag::make_content_buffer()
|
|
|
|
const
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(request);
|
|
|
|
const auto &req{*request};
|
|
|
|
const auto &head{req.in.head};
|
|
|
|
const auto &content_buffer{req.in.content_buffer};
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
// The amount of bytes we still have to read to for the response
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(head.content_length >= content_read);
|
2018-01-13 03:57:58 +01:00
|
|
|
const size_t remaining
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
head.content_length - content_read
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
// The amount of bytes available in the user's buffer.
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(size(content_buffer) >= content_read);
|
2018-01-13 03:57:58 +01:00
|
|
|
const size_t available
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
size(content_buffer) - content_read
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
2017-11-25 22:17:22 +01:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
// For now, this has to trip right here.
|
|
|
|
assert(available >= remaining);
|
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
data(content_buffer) + content_read, std::min(available, remaining)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
return buffer;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-01-13 03:57:58 +01:00
|
|
|
// node
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::node::node()
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::node::~node()
|
|
|
|
noexcept
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::node::submit(request &request)
|
|
|
|
{
|
|
|
|
link &ret(link_get());
|
|
|
|
ret.submit(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::node::cancel(request &request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ircd::server::link &
|
|
|
|
ircd::server::node::link_get()
|
|
|
|
{
|
|
|
|
while(!remote.resolved())
|
|
|
|
dock.wait();
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
if(links.empty())
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
auto &ret(link_add(1));
|
|
|
|
while(!ret.ready())
|
|
|
|
dock.wait();
|
|
|
|
|
|
|
|
return ret;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
2018-01-14 03:03:04 +01:00
|
|
|
else return links.back();
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::link &
|
|
|
|
ircd::server::node::link_add(const size_t &num)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
links.emplace_back(*this);
|
|
|
|
auto &link{links.back()};
|
|
|
|
link.open(remote);
|
|
|
|
return link;
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
|
|
|
void
|
2018-01-14 06:16:49 +01:00
|
|
|
ircd::server::node::handle_open(link &link,
|
|
|
|
std::exception_ptr eptr)
|
|
|
|
try
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
if(eptr)
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
|
|
|
|
dock.notify_all();
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error("node(%p) link(%p) [%s]: open: %s",
|
|
|
|
this,
|
|
|
|
&link,
|
|
|
|
string(remote),
|
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::node::handle_close(link &link,
|
|
|
|
std::exception_ptr eptr)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const unwind remove{[this, &link]
|
|
|
|
{
|
|
|
|
this->del(link);
|
|
|
|
}};
|
|
|
|
|
|
|
|
if(eptr)
|
|
|
|
std::rethrow_exception(eptr);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::error("node(%p) link(%p) [%s]: close: %s",
|
|
|
|
this,
|
|
|
|
&link,
|
|
|
|
string(remote),
|
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::node::handle_error(link &link,
|
|
|
|
const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
log::error("node(%p) link(%p) [%s]: error: %s",
|
|
|
|
this,
|
|
|
|
&link,
|
|
|
|
string(remote),
|
|
|
|
e.what());
|
|
|
|
|
|
|
|
if(!link.busy())
|
|
|
|
{
|
|
|
|
link.close(net::close_opts_default);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::node::del(link &link)
|
|
|
|
{
|
|
|
|
log::debug("node(%p) [%s]: removing link %p",
|
|
|
|
this,
|
|
|
|
string(remote),
|
|
|
|
&link);
|
|
|
|
|
|
|
|
const auto it(std::find_if(begin(links), end(links), [&link]
|
|
|
|
(const auto &link_)
|
|
|
|
{
|
|
|
|
return &link_ == &link;
|
|
|
|
}));
|
|
|
|
|
|
|
|
assert(it != end(links));
|
|
|
|
links.erase(it);
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::resolve(const hostport &hostport)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
auto handler
|
|
|
|
{
|
|
|
|
std::bind(&node::handle_resolve, this, weak_from(*this), ph::_1, ph::_2)
|
|
|
|
};
|
|
|
|
|
|
|
|
net::resolve(hostport, std::move(handler));
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
void
|
|
|
|
ircd::server::node::handle_resolve(std::weak_ptr<node> wp,
|
|
|
|
std::exception_ptr eptr,
|
|
|
|
const ipport &ipport)
|
|
|
|
try
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
const life_guard<node> lg(wp);
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
this->eptr = std::move(eptr);
|
|
|
|
static_cast<net::ipport &>(this->remote) = ipport;
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
dock.notify_all();
|
|
|
|
}
|
|
|
|
catch(const std::bad_weak_ptr &)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
size_t
|
|
|
|
ircd::server::node::num_links_ready()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(links), end(links), size_t(0), []
|
|
|
|
(auto ret, const auto &link)
|
|
|
|
{
|
|
|
|
return ret += link.ready();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::node::num_links_busy()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(links), end(links), size_t(0), []
|
|
|
|
(auto ret, const auto &link)
|
|
|
|
{
|
|
|
|
return ret += link.busy();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::node::num_links()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return links.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ircd::server::node::num_tags()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return std::accumulate(begin(links), end(links), size_t(0), []
|
|
|
|
(auto ret, const auto &link)
|
|
|
|
{
|
|
|
|
return ret += link.queue.size();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
2018-01-13 03:57:58 +01:00
|
|
|
// link
|
2017-10-25 18:39:58 +02:00
|
|
|
//
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::link::link(server::node &node)
|
|
|
|
:node{shared_from(node)}
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::link::~link()
|
2017-10-25 18:39:58 +02:00
|
|
|
noexcept
|
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
assert(!busy());
|
|
|
|
assert(!connected());
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::link::submit(request &request)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
const auto it
|
|
|
|
{
|
|
|
|
queue.emplace(end(queue), request)
|
|
|
|
};
|
|
|
|
|
|
|
|
write(*socket, request.out.head);
|
|
|
|
write(*socket, request.out.content);
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::link::cancel(request &request)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
2017-10-25 18:39:58 +02:00
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
bool
|
|
|
|
ircd::server::link::open(const net::open_opts &open_opts)
|
2017-10-25 18:39:58 +02:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
if(init)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto handler
|
|
|
|
{
|
|
|
|
std::bind(&link::handle_open, this, ph::_1)
|
|
|
|
};
|
|
|
|
|
|
|
|
init = true;
|
|
|
|
fini = false;
|
|
|
|
socket = net::open(open_opts, std::move(handler));
|
|
|
|
return true;
|
2017-11-25 22:17:22 +01:00
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::handle_open(std::exception_ptr eptr)
|
2017-11-25 22:17:22 +01:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
init = false;
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
if(!this->eptr)
|
2018-01-13 03:57:58 +01:00
|
|
|
wait_readable();
|
2018-01-14 06:16:49 +01:00
|
|
|
|
|
|
|
if(node)
|
|
|
|
node->handle_open(*this, std::move(eptr));
|
2017-11-25 22:17:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-01-13 03:57:58 +01:00
|
|
|
ircd::server::link::close(const net::close_opts &close_opts)
|
2017-11-25 22:17:22 +01:00
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
if(!socket)
|
2017-11-25 22:17:22 +01:00
|
|
|
return false;
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
if(fini)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto handler
|
|
|
|
{
|
|
|
|
std::bind(&link::handle_close, this, ph::_1)
|
|
|
|
};
|
|
|
|
|
|
|
|
init = false;
|
|
|
|
fini = true;
|
|
|
|
net::close(*socket, close_opts, std::move(handler));
|
2017-11-25 22:17:22 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::handle_close(std::exception_ptr eptr)
|
|
|
|
{
|
|
|
|
fini = false;
|
2018-01-14 06:16:49 +01:00
|
|
|
|
|
|
|
if(node)
|
|
|
|
node->handle_close(*this, std::move(eptr));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-14 06:16:49 +01:00
|
|
|
ircd::server::link::wait_writable()
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
|
|
|
auto handler
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
std::bind(&link::handle_writable, this, ph::_1)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(ready());
|
2018-01-14 06:16:49 +01:00
|
|
|
net::wait(*socket, net::ready::WRITE, std::move(handler));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-01-14 06:16:49 +01:00
|
|
|
ircd::server::link::handle_writable(const error_code &ec)
|
|
|
|
{
|
|
|
|
std::cout << this << " writable: " << string(ec) << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::link::wait_readable()
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
|
|
|
auto handler
|
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
std::bind(&link::handle_readable, this, ph::_1)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(ready());
|
2018-01-14 06:16:49 +01:00
|
|
|
net::wait(*socket, net::ready::READ, std::move(handler));
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::link::handle_readable(const error_code &ec)
|
2018-01-14 06:16:49 +01:00
|
|
|
try
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
|
|
|
using namespace boost::system::errc;
|
|
|
|
using boost::system::system_category;
|
|
|
|
|
|
|
|
switch(ec.value())
|
|
|
|
{
|
|
|
|
case success:
|
|
|
|
handle_readable_success();
|
|
|
|
wait_readable();
|
|
|
|
break;
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
case operation_canceled:
|
|
|
|
break;
|
|
|
|
|
2018-01-13 03:57:58 +01:00
|
|
|
default:
|
|
|
|
throw boost::system::system_error{ec};
|
|
|
|
}
|
|
|
|
}
|
2018-01-14 06:16:49 +01:00
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
if(node)
|
|
|
|
node->handle_error(*this, e);
|
|
|
|
}
|
|
|
|
catch(const std::exception &e)
|
|
|
|
{
|
|
|
|
log::critical("link::handle_readable(): %s", e.what());
|
|
|
|
assert(0);
|
|
|
|
throw;
|
|
|
|
}
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
/// Process as many read operations from as many tags as possible
|
2018-01-13 03:57:58 +01:00
|
|
|
void
|
|
|
|
ircd::server::link::handle_readable_success()
|
|
|
|
{
|
|
|
|
if(queue.empty())
|
2018-01-14 06:16:49 +01:00
|
|
|
return discard_read();
|
|
|
|
|
|
|
|
// Data pointed to by overrun will remain intact between iterations
|
|
|
|
// because this loop isn't executing in any ircd::ctx.
|
|
|
|
const_buffer overrun; do
|
|
|
|
{
|
|
|
|
if(!process_read(overrun))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while(!queue.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ircd::server::link::discard_read()
|
|
|
|
{
|
|
|
|
const size_t discard
|
|
|
|
{
|
|
|
|
available(*socket)
|
|
|
|
};
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const size_t discarded
|
|
|
|
{
|
|
|
|
discard_any(*socket, discard)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Shouldn't ever be hit because the read() within discard() throws
|
|
|
|
// the pending error like an eof.
|
|
|
|
log::warning("Link discarded %zu of %zu unexpected bytes",
|
|
|
|
discard,
|
|
|
|
discarded);
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
// for non-assert builds just in case; so this doesn't get loopy with
|
|
|
|
// discarding zero with an empty queue...
|
|
|
|
if(unlikely(!discard || !discarded))
|
|
|
|
throw assertive("Queue is empty and nothing to discard.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process as many read operations for one tag as possible
|
|
|
|
bool
|
|
|
|
ircd::server::link::process_read(const_buffer &overrun)
|
|
|
|
try
|
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
auto &tag
|
|
|
|
{
|
|
|
|
queue.front()
|
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
bool done{false}; do
|
|
|
|
{
|
|
|
|
overrun = process_read_next(overrun, tag, done);
|
|
|
|
}
|
|
|
|
while(!done);
|
|
|
|
|
|
|
|
queue.pop_front();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch(const boost::system::system_error &e)
|
|
|
|
{
|
|
|
|
using namespace boost::system::errc;
|
|
|
|
switch(e.code().value())
|
|
|
|
{
|
|
|
|
case resource_unavailable_try_again:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case success:
|
|
|
|
assert(0);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Process one read operation for one tag
|
|
|
|
ircd::const_buffer
|
|
|
|
ircd::server::link::process_read_next(const const_buffer &underrun,
|
|
|
|
struct request::tag &tag,
|
|
|
|
bool &done)
|
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
const mutable_buffer buffer
|
|
|
|
{
|
|
|
|
tag.make_read_buffer()
|
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const size_t copied
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
copy(buffer, underrun)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const mutable_buffer remaining
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
data(buffer) + copied, size(buffer) - copied
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const size_t received
|
2018-01-13 03:57:58 +01:00
|
|
|
{
|
2018-01-14 06:16:49 +01:00
|
|
|
read_one(*socket, remaining)
|
2018-01-13 03:57:58 +01:00
|
|
|
};
|
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const const_buffer view
|
|
|
|
{
|
|
|
|
data(buffer), copied + received
|
|
|
|
};
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
const const_buffer overrun
|
|
|
|
{
|
|
|
|
tag.read_buffer(view, done)
|
|
|
|
};
|
2018-01-13 03:57:58 +01:00
|
|
|
|
2018-01-14 06:16:49 +01:00
|
|
|
assert(done || empty(overrun));
|
|
|
|
return overrun;
|
2018-01-13 03:57:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::busy()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !queue.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ircd::server::link::ready()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return connected() && !init && !fini && !eptr;
|
|
|
|
}
|
|
|
|
|
2017-11-25 22:17:22 +01:00
|
|
|
bool
|
|
|
|
ircd::server::link::connected()
|
|
|
|
const noexcept
|
|
|
|
{
|
2018-01-13 03:57:58 +01:00
|
|
|
return bool(socket) && net::connected(*socket);
|
2017-10-25 18:39:58 +02:00
|
|
|
}
|