From 79a1f0012b35fde8988f88c5486780922a856e0d Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 7 Feb 2015 05:44:03 +0000 Subject: Add missing files. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@5538 a436a847-0d15-0410-975c-d299462d15a1 --- src/server/SocketListener.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++ src/server/SocketListener.hpp | 57 +++++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 src/server/SocketListener.cpp create mode 100644 src/server/SocketListener.hpp (limited to 'src/server') diff --git a/src/server/SocketListener.cpp b/src/server/SocketListener.cpp new file mode 100644 index 00000000..d09771a4 --- /dev/null +++ b/src/server/SocketListener.cpp @@ -0,0 +1,116 @@ +/* + This file is part of Ingen. + Copyright 2007-2015 David Robillard + + Ingen is free software: you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free + Software Foundation, either version 3 of the License, or any later version. + + Ingen 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 Affero General Public License for details. + + You should have received a copy of the GNU Affero General Public License + along with Ingen. If not, see . +*/ + +#include +#include + +#include +#include + +#include "ingen/Configuration.hpp" +#include "ingen/Module.hpp" +#include "ingen/World.hpp" +#include "raul/Socket.hpp" + +#include "../server/Engine.hpp" +#include "../server/EventWriter.hpp" + +#include "SocketListener.hpp" +#include "SocketServer.hpp" + +namespace Ingen { +namespace Server { + +void +SocketListener::ingen_listen(Engine* engine, + Raul::Socket* unix_sock, + Raul::Socket* net_sock) +{ + Ingen::World* world = engine->world(); + + const std::string unix_path(world->conf().option("socket").ptr()); + + // Bind UNIX socket + const Raul::URI unix_uri(unix_scheme + unix_path); + if (!unix_sock->bind(unix_uri) || !unix_sock->listen()) { + world->log().error("Failed to create UNIX socket\n"); + unix_sock->close(); + } else { + world->log().info(fmt("Listening on socket %1%\n") % unix_uri); + } + + // Bind TCP socket + const int port = world->conf().option("engine-port").get(); + std::ostringstream ss; + ss << "tcp://localhost:"; + ss << port; + if (!net_sock->bind(Raul::URI(ss.str())) || !net_sock->listen()) { + world->log().error("Failed to create TCP socket\n"); + net_sock->close(); + } else { + world->log().info(fmt("Listening on TCP port %1%\n") % port); + } + + if (unix_sock->fd() == -1 && net_sock->fd() == -1) { + return; // No sockets to listen to, exit thread + } + + struct pollfd pfds[2]; + int nfds = 0; + if (unix_sock->fd() != -1) { + pfds[nfds].fd = unix_sock->fd(); + pfds[nfds].events = POLLIN; + pfds[nfds].revents = 0; + ++nfds; + } + if (net_sock->fd() != -1) { + pfds[nfds].fd = net_sock->fd(); + pfds[nfds].events = POLLIN; + pfds[nfds].revents = 0; + ++nfds; + } + + while (true) { + // Wait for input to arrive at a socket + const int ret = poll(pfds, nfds, -1); + if (ret == -1) { + world->log().error(fmt("Poll error: %1%\n") % strerror(errno)); + break; + } else if ((pfds[0].revents & POLLHUP) || pfds[1].revents & POLLHUP) { + break; + } else if (ret == 0) { + world->log().error("Poll returned with no data\n"); + continue; + } + + if (pfds[0].revents & POLLIN) { + SPtr conn = unix_sock->accept(); + if (conn) { + new SocketServer(*world, *engine, conn); + } + } + + if (pfds[1].revents & POLLIN) { + SPtr conn = net_sock->accept(); + if (conn) { + new SocketServer(*world, *engine, conn); + } + } + } +} + +} // namespace Server +} // namespace Ingen diff --git a/src/server/SocketListener.hpp b/src/server/SocketListener.hpp new file mode 100644 index 00000000..71c60d01 --- /dev/null +++ b/src/server/SocketListener.hpp @@ -0,0 +1,57 @@ +/* + This file is part of Ingen. + Copyright 2007-2015 David Robillard + + Ingen is free software: you can redistribute it and/or modify it under the + terms of the GNU Affero General Public License as published by the Free + Software Foundation, either version 3 of the License, or any later version. + + Ingen 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 Affero General Public License for details. + + You should have received a copy of the GNU Affero General Public License + along with Ingen. If not, see . +*/ + +#include +#include + +#include "raul/Socket.hpp" + +#include "Engine.hpp" + +namespace Ingen { +namespace Server { + +/** Listens on main sockets and spawns socket servers for new connections. */ +class SocketListener +{ +public: + SocketListener(Engine& engine) + : unix_sock(Raul::Socket::Type::UNIX) + , net_sock(Raul::Socket::Type::TCP) + , thread(new std::thread(ingen_listen, &engine, &unix_sock, &net_sock)) + {} + + ~SocketListener() { + unix_sock.shutdown(); + net_sock.shutdown(); + thread->join(); + unlink(unix_sock.uri().substr(strlen(unix_scheme)).c_str()); + } + +private: + static constexpr const char* unix_scheme = "unix://"; + + static void ingen_listen(Engine* engine, + Raul::Socket* unix_sock, + Raul::Socket* net_sock); + + Raul::Socket unix_sock; + Raul::Socket net_sock; + std::unique_ptr thread; +}; + +} // namespace Server +} // namespace Ingen -- cgit v1.2.1