From 56cefcb650f165fb24e44b0d521f537d9756b7c5 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 13 Jan 2018 18:03:04 -0800 Subject: [PATCH] ircd::server: Move into directory; various cleanup. --- include/ircd/server/link.h | 58 +++++++++++ include/ircd/server/node.h | 46 +++++++++ include/ircd/{server.h => server/request.h} | 102 ++------------------ include/ircd/server/server.h | 53 ++++++++++ ircd/ircd.cc | 42 ++++---- ircd/server.cc | 17 +--- 6 files changed, 187 insertions(+), 131 deletions(-) create mode 100644 include/ircd/server/link.h create mode 100644 include/ircd/server/node.h rename include/ircd/{server.h => server/request.h} (57%) create mode 100644 include/ircd/server/server.h diff --git a/include/ircd/server/link.h b/include/ircd/server/link.h new file mode 100644 index 000000000..41a70fa3d --- /dev/null +++ b/include/ircd/server/link.h @@ -0,0 +1,58 @@ +// 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. + +#pragma once +#define HAVE_IRCD_SERVER_LINK_H + +/// A single connection to a remote node. +/// +struct ircd::server::link +{ + bool init {false}; ///< link is connecting + bool fini {false}; ///< link is disconnecting + std::shared_ptr node; ///< backreference to node + std::exception_ptr eptr; ///< error from socket + std::shared_ptr socket; ///< link's socket + std::deque queue; ///< link's work queue + + bool connected() const noexcept; + bool ready() const; + bool busy() const; + + protected: + void handle_writable(const error_code &); + void wait_writable(); + + void handle_readable_success(); + void handle_readable(const error_code &); + void wait_readable(); + + void handle_close(std::exception_ptr); + void handle_open(std::exception_ptr); + + public: + bool close(const net::close_opts &); + bool open(const net::open_opts &); + + void cancel(request &); + void submit(request &); + + link(server::node &); + ~link() noexcept; +}; diff --git a/include/ircd/server/node.h b/include/ircd/server/node.h new file mode 100644 index 000000000..4f6f4acb9 --- /dev/null +++ b/include/ircd/server/node.h @@ -0,0 +1,46 @@ +// 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. + +#pragma once +#define HAVE_IRCD_SERVER_NODE_H + +/// Remote entity. +/// +struct ircd::server::node +:std::enable_shared_from_this +{ + std::exception_ptr eptr; + net::remote remote; + std::list links; + ctx::dock dock; + + void handle_resolve(std::weak_ptr, std::exception_ptr, const ipport &); + void resolve(const hostport &); + + public: + link &link_add(const size_t &num = 1); + void link_del(const size_t &num = 1); + link &link_get(); + + void cancel(request &); + void submit(request &); + + node(); + ~node() noexcept; +}; diff --git a/include/ircd/server.h b/include/ircd/server/request.h similarity index 57% rename from include/ircd/server.h rename to include/ircd/server/request.h index 7c63f376f..00e1e5007 100644 --- a/include/ircd/server.h +++ b/include/ircd/server/request.h @@ -18,41 +18,29 @@ // POSSIBILITY OF SUCH DAMAGE. #pragma once -#define HAVE_IRCD_SERVER_H +#define HAVE_IRCD_SERVER_REQUEST_H /// The interface for when IRCd plays the role of client to other servers /// namespace ircd::server { - struct init; - struct node; - struct link; - struct request; - struct out; struct in; - - IRCD_EXCEPTION(ircd::error, error); - - extern ircd::log::log log; - extern std::map> nodes; - - bool exists(const net::hostport &); - node &find(const net::hostport &); - node &get(const net::hostport &); + struct out; + struct request; } -struct ircd::server::out -{ - const_buffer head; - const_buffer content; -}; - struct ircd::server::in { mutable_buffer head; mutable_buffer content; }; +struct ircd::server::out +{ + const_buffer head; + const_buffer content; +}; + /// This is a handle for being a client to another server. This handle will /// attempt to find an existing connection pool for the remote server otherwise /// one will be created. Then it will multiplex your requests and demultiplex @@ -105,75 +93,3 @@ struct ircd::server::request::tag tag &operator=(const tag &) = delete; ~tag() noexcept; }; - -/// Internal representation of a remote server. -/// -struct ircd::server::node -:std::enable_shared_from_this -{ - std::exception_ptr eptr; - net::remote remote; - std::list links; - ctx::dock dock; - - void handle_resolve(std::weak_ptr, std::exception_ptr, const ipport &); - void resolve(const hostport &); - - public: - link &link_add(const size_t &num = 1); - void link_del(const size_t &num = 1); - link &link_get(); - - void cancel(request &); - void submit(request &); - - node(); - ~node() noexcept; -}; - -/// Internal representation of a single connection to a remote. -/// -struct ircd::server::link -{ - bool init {false}; ///< link is connecting - bool fini {false}; ///< link is disconnecting - std::shared_ptr node; ///< backreference to node - std::exception_ptr eptr; ///< error from socket - std::shared_ptr socket; ///< link's socket - std::deque queue; ///< link's work queue - - bool connected() const noexcept; - bool ready() const; - bool busy() const; - - protected: - void handle_writable(const error_code &); - void wait_writable(); - - void handle_readable_success(); - void handle_readable(const error_code &); - void wait_readable(); - - void handle_close(std::exception_ptr); - void handle_open(std::exception_ptr); - - public: - bool close(const net::close_opts &); - bool open(const net::open_opts &); - - void cancel(request &); - void submit(request &); - - link(server::node &); - ~link() noexcept; -}; - -/// Subsystem initialization / destruction from ircd::main -/// -struct ircd::server::init -{ - void interrupt(); - - init(); - ~init() noexcept; -}; diff --git a/include/ircd/server/server.h b/include/ircd/server/server.h new file mode 100644 index 000000000..92966054e --- /dev/null +++ b/include/ircd/server/server.h @@ -0,0 +1,53 @@ +// 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. + +#pragma once +#define HAVE_IRCD_SERVER_H + +/// The interface for when IRCd plays the role of client to other nodes +/// +namespace ircd::server +{ + struct init; + struct link; + struct node; + + IRCD_EXCEPTION(ircd::error, error); + + extern ircd::log::log log; + extern std::map> nodes; + + bool exists(const net::hostport &); + node &find(const net::hostport &); + node &get(const net::hostport &); +} + +#include "request.h" +#include "link.h" +#include "node.h" + +/// Subsystem initialization / destruction from ircd::main +/// +struct ircd::server::init +{ + void interrupt(); + + init(); + ~init() noexcept; +}; diff --git a/ircd/ircd.cc b/ircd/ircd.cc index 73ac5eee8..258d019a5 100644 --- a/ircd/ircd.cc +++ b/ircd/ircd.cc @@ -1,27 +1,21 @@ -/* - * charybdis: A slightly useful ircd. - * ircd.c: Starts up and runs the ircd. - * - * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center - * Copyright (C) 1996-2002 Hybrid Development Team - * Copyright (C) 2002-2008 ircd-ratbox development team - * Copyright (C) 2005-2013 charybdis development team - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA - */ +// 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. #include #include diff --git a/ircd/server.cc b/ircd/server.cc index b7ce4faa2..525dcb949 100644 --- a/ircd/server.cc +++ b/ircd/server.cc @@ -17,7 +17,7 @@ // IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -#include +#include #include namespace ircd::server @@ -221,10 +221,8 @@ ircd::server::request::tag::read_head(const const_buffer &buffer, { pos + size(terminator) }; - assert(addl_head_bytes <= size(buffer)); - // The head bytes accounting can be updated and this will be the final - // value of what is legitimate head in the req.in.head buffer. + assert(addl_head_bytes <= size(buffer)); head_read += addl_head_bytes; // Setup the capstan and mark the end of the tape @@ -232,25 +230,19 @@ ircd::server::request::tag::read_head(const const_buffer &buffer, parse::capstan pc{pb}; pc.read += head_read; - // The HTTP head is parsed here and saved in the user's object but they - // do not know about it yet and shouldn't be touching it. req.head = http::response::head{pc}; assert(pb.completed() == head_read); - // As stated, the buffer may contain data past the head, which includes - // our content or the next response which doesn't even belong to us. const size_t overrun_length { size(buffer) - addl_head_bytes }; - // Calculate the amount of overrun which belongs to our content. const size_t &content_read { std::min(req.head.content_length, overrun_length) }; - // Where the partial content would be written to const const_buffer partial_content { data(req.in.head) + head_read, content_read @@ -268,8 +260,6 @@ ircd::server::request::tag::read_head(const const_buffer &buffer, data(req.in.head) + head_read + content_read, overrun_length - content_read }; - // When lucky, the content was recieved already (or there is no content) and - // we can notify the user in one shot. if(this->content_read == req.head.content_length) { p.set_value(http::status(req.head.status)); @@ -411,8 +401,7 @@ ircd::server::node::link_get() return ret; } - else - return links.back(); + else return links.back(); } ircd::server::link &