summaryrefslogtreecommitdiffstats
path: root/src/socket
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2013-02-23 21:23:16 +0000
committerDavid Robillard <d@drobilla.net>2013-02-23 21:23:16 +0000
commit400bbe503f44d306f5488eb850cde0723f8844f5 (patch)
tree0346e64e6e4a03bc25c3b455408cb086f28c9b97 /src/socket
parentdfad81a3c8aee40a515f0ecefb0180a86368b54a (diff)
downloadingen-400bbe503f44d306f5488eb850cde0723f8844f5.tar.gz
ingen-400bbe503f44d306f5488eb850cde0723f8844f5.tar.bz2
ingen-400bbe503f44d306f5488eb850cde0723f8844f5.zip
Move Socket class to Raul.
git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@5079 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/socket')
-rw-r--r--src/socket/Socket.cpp187
-rw-r--r--src/socket/Socket.hpp104
-rw-r--r--src/socket/SocketClient.hpp8
-rw-r--r--src/socket/SocketReader.cpp6
-rw-r--r--src/socket/SocketReader.hpp25
-rw-r--r--src/socket/SocketServer.hpp9
-rw-r--r--src/socket/SocketWriter.cpp8
-rw-r--r--src/socket/SocketWriter.hpp28
-rw-r--r--src/socket/ingen_socket_client.cpp10
-rw-r--r--src/socket/ingen_socket_server.cpp18
-rw-r--r--src/socket/wscript6
11 files changed, 62 insertions, 347 deletions
diff --git a/src/socket/Socket.cpp b/src/socket/Socket.cpp
deleted file mode 100644
index 689e7eb6..00000000
--- a/src/socket/Socket.cpp
+++ /dev/null
@@ -1,187 +0,0 @@
-/*
- This file is part of Ingen.
- Copyright 2007-2012 David Robillard <http://drobilla.net/>
-
- 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 <http://www.gnu.org/licenses/>.
-*/
-
-#include <errno.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <poll.h>
-#include <stdlib.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-#include <unistd.h>
-
-#include <string>
-
-#include "Socket.hpp"
-
-namespace Ingen {
-namespace Socket {
-
-#ifndef NI_MAXHOST
-# define NI_MAXHOST 1025
-#endif
-#ifndef NI_MAXSERV
-# define NI_MAXSERV 32
-#endif
-
-Socket::Socket(Type t)
- : _type(t)
- , _uri(t == Type::UNIX ? "unix:" : "tcp:")
- , _addr(NULL)
- , _addr_len(0)
- , _sock(-1)
-{
- switch (t) {
- case Type::UNIX:
- _sock = socket(AF_UNIX, SOCK_STREAM, 0);
- break;
- case Type::TCP:
- _sock = socket(AF_INET, SOCK_STREAM, 0);
- break;
- }
-}
-
-Socket::Socket(Type t,
- const Raul::URI& uri,
- struct sockaddr* addr,
- socklen_t addr_len,
- int fd)
- : _type(t)
- , _uri(uri)
- , _addr(addr)
- , _addr_len(addr_len)
- , _sock(fd)
-{
-}
-
-Socket::~Socket()
-{
- free(_addr);
- close();
-}
-
-bool
-Socket::set_addr(const Raul::URI& uri)
-{
- free(_addr);
- if (_type == Type::UNIX && uri.substr(0, strlen("unix://")) == "unix://") {
- const std::string path = uri.substr(strlen("unix://"));
- struct sockaddr_un* uaddr = (struct sockaddr_un*)calloc(
- 1, sizeof(struct sockaddr_un));
- uaddr->sun_family = AF_UNIX;
- strncpy(uaddr->sun_path, path.c_str(), sizeof(uaddr->sun_path) - 1);
- _uri = uri;
- _addr = (sockaddr*)uaddr;
- _addr_len = sizeof(struct sockaddr_un);
- return true;
- } else if (_type == Type::TCP && uri.find("://") != std::string::npos) {
- const std::string authority = uri.substr(uri.find("://") + 3);
- const size_t port_sep = authority.find(':');
- if (port_sep == std::string::npos) {
- return false;
- }
-
- const std::string host = authority.substr(0, port_sep);
- const std::string port = authority.substr(port_sep + 1).c_str();
-
- struct addrinfo* ainfo;
- int st = 0;
- if ((st = getaddrinfo(host.c_str(), port.c_str(), NULL, &ainfo))) {
- return false;
- }
-
- _uri = uri;
- _addr = (struct sockaddr*)malloc(ainfo->ai_addrlen);
- _addr_len = ainfo->ai_addrlen;
- memcpy(_addr, ainfo->ai_addr, ainfo->ai_addrlen);
- freeaddrinfo(ainfo);
- return true;
- }
- return false;
-}
-
-bool
-Socket::bind(const Raul::URI& uri)
-{
- if (set_addr(uri) && ::bind(_sock, _addr, _addr_len) != -1) {
- return true;
- }
-
- return false;
-}
-
-bool
-Socket::connect(const Raul::URI& uri)
-{
- if (set_addr(uri) && ::connect(_sock, _addr, _addr_len) != -1) {
- return true;
- }
-
- return false;
-}
-
-bool
-Socket::listen()
-{
- if (::listen(_sock, 64) == -1) {
- return false;
- } else {
- return true;
- }
-}
-
-SPtr<Socket>
-Socket::accept()
-{
- socklen_t client_addr_len = _addr_len;
- struct sockaddr* client_addr = (struct sockaddr*)calloc(
- 1, client_addr_len);
-
- int conn = ::accept(_sock, client_addr, &client_addr_len);
- if (conn == -1) {
- return SPtr<Socket>();
- }
-
- Raul::URI client_uri = _uri;
- char host[NI_MAXHOST];
- if (getnameinfo(client_addr, client_addr_len,
- host, sizeof(host), NULL, 0, 0)) {
- client_uri = Raul::URI(_uri.scheme() + "://" + host);
- }
-
- return SPtr<Socket>(
- new Socket(_type, client_uri, client_addr, client_addr_len, conn));
-}
-
-void
-Socket::close()
-{
- if (_sock != -1) {
- ::close(_sock);
- _sock = -1;
- }
-}
-
-void
-Socket::shutdown()
-{
- if (_sock != -1) {
- ::shutdown(_sock, SHUT_RDWR);
- }
-}
-
-} // namespace Ingen
-} // namespace Socket
diff --git a/src/socket/Socket.hpp b/src/socket/Socket.hpp
deleted file mode 100644
index ebe9f68f..00000000
--- a/src/socket/Socket.hpp
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- This file is part of Ingen.
- Copyright 2007-2012 David Robillard <http://drobilla.net/>
-
- 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 <http://www.gnu.org/licenses/>.
-*/
-
-#ifndef INGEN_SOCKET_SOCKET_HPP
-#define INGEN_SOCKET_SOCKET_HPP
-
-#include <stdint.h>
-#include <sys/socket.h>
-
-#include "raul/Noncopyable.hpp"
-#include "raul/URI.hpp"
-
-#include "ingen/types.hpp"
-
-namespace Ingen {
-namespace Socket {
-
-/** A safe and simple interface for UNIX or TCP sockets. */
-class Socket : public Raul::Noncopyable {
-public:
- enum class Type {
- UNIX,
- TCP
- };
-
- static Type type_from_uri(const Raul::URI uri) {
- return (uri.scheme() == "unix") ? Type::UNIX : Type::TCP;
- }
-
- /** Create a new unbound/unconnected socket of a given type. */
- explicit Socket(Type t);
-
- /** Wrap an existing open socket. */
- Socket(Type t,
- const Raul::URI& uri,
- struct sockaddr* addr,
- socklen_t addr_len,
- int fd);
-
- ~Socket();
-
- /** Bind a server socket to an address.
- * @param uri Address URI, e.g. unix:///tmp/foo or tcp://somehost:1234
- * @return True on success.
- */
- bool bind(const Raul::URI& uri);
-
- /** Connect a client socket to a server address.
- * @param uri Address URI, e.g. unix:///tmp/foo or tcp://somehost:1234
- * @return True on success.
- */
- bool connect(const Raul::URI& uri);
-
- /** Mark server socket as passive to listen for incoming connections.
- * @return True on success.
- */
- bool listen();
-
- /** Accept a connection.
- * @return An new open socket for the connection.
- */
- SPtr<Socket> accept();
-
- /** Return the file descriptor for the socket. */
- int fd() { return _sock; }
-
- const Raul::URI& uri() const { return _uri; }
-
- /** Close the socket. */
- void close();
-
- /** Shut down the socket.
- * This terminates any connections associated with the sockets, and will
- * (unlike close()) cause a poll on the socket to return.
- */
- void shutdown();
-
-private:
- bool set_addr(const Raul::URI& uri);
-
- Type _type;
- Raul::URI _uri;
- struct sockaddr* _addr;
- socklen_t _addr_len;
- int _sock;
-};
-
-} // namespace Socket
-} // namespace Ingen
-
-#endif // INGEN_SOCKET_SOCKET_HPP
diff --git a/src/socket/SocketClient.hpp b/src/socket/SocketClient.hpp
index f7d8b95e..ae4b47d0 100644
--- a/src/socket/SocketClient.hpp
+++ b/src/socket/SocketClient.hpp
@@ -27,10 +27,10 @@ namespace Socket {
class SocketClient : public SocketWriter
{
public:
- SocketClient(World& world,
- const Raul::URI& uri,
- SPtr<Socket> sock,
- SPtr<Interface> respondee)
+ SocketClient(World& world,
+ const Raul::URI& uri,
+ SPtr<Raul::Socket> sock,
+ SPtr<Interface> respondee)
: SocketWriter(world.uri_map(), world.uris(), uri, sock)
, _respondee(respondee)
, _reader(world, *respondee.get(), sock)
diff --git a/src/socket/SocketReader.cpp b/src/socket/SocketReader.cpp
index 458032c5..b69ae92f 100644
--- a/src/socket/SocketReader.cpp
+++ b/src/socket/SocketReader.cpp
@@ -30,9 +30,9 @@
namespace Ingen {
namespace Socket {
-SocketReader::SocketReader(Ingen::World& world,
- Interface& iface,
- SPtr<Socket> sock)
+SocketReader::SocketReader(Ingen::World& world,
+ Interface& iface,
+ SPtr<Raul::Socket> sock)
: _world(world)
, _iface(iface)
, _inserter(NULL)
diff --git a/src/socket/SocketReader.hpp b/src/socket/SocketReader.hpp
index 489b7e3f..38afedef 100644
--- a/src/socket/SocketReader.hpp
+++ b/src/socket/SocketReader.hpp
@@ -19,10 +19,9 @@
#include <thread>
+#include "raul/Socket.hpp"
#include "sord/sord.h"
-#include "Socket.hpp"
-
namespace Ingen {
class Interface;
@@ -34,9 +33,9 @@ namespace Socket {
class SocketReader
{
public:
- SocketReader(World& world,
- Interface& iface,
- SPtr<Socket> sock);
+ SocketReader(World& world,
+ Interface& iface,
+ SPtr<Raul::Socket> sock);
~SocketReader();
@@ -59,14 +58,14 @@ private:
const SerdNode* object_datatype,
const SerdNode* object_lang);
- World& _world;
- Interface& _iface;
- SerdEnv* _env;
- SordInserter* _inserter;
- SordNode* _msg_node;
- SPtr<Socket> _socket;
- bool _exit_flag;
- std::thread _thread;
+ World& _world;
+ Interface& _iface;
+ SerdEnv* _env;
+ SordInserter* _inserter;
+ SordNode* _msg_node;
+ SPtr<Raul::Socket> _socket;
+ bool _exit_flag;
+ std::thread _thread;
};
} // namespace Ingen
diff --git a/src/socket/SocketServer.hpp b/src/socket/SocketServer.hpp
index 371bd1e0..a977bdbc 100644
--- a/src/socket/SocketServer.hpp
+++ b/src/socket/SocketServer.hpp
@@ -17,8 +17,9 @@
#ifndef INGEN_SOCKET_SOCKET_SERVER_HPP
#define INGEN_SOCKET_SOCKET_SERVER_HPP
+#include "raul/Socket.hpp"
+
#include "../server/EventWriter.hpp"
-#include "Socket.hpp"
#include "SocketReader.hpp"
#include "SocketWriter.hpp"
@@ -29,9 +30,9 @@ namespace Socket {
class SocketServer : public Server::EventWriter, public SocketReader
{
public:
- SocketServer(World& world,
- Server::Engine& engine,
- SPtr<Socket> sock)
+ SocketServer(World& world,
+ Server::Engine& engine,
+ SPtr<Raul::Socket> sock)
: Server::EventWriter(engine)
, SocketReader(world, *this, sock)
, _engine(engine)
diff --git a/src/socket/SocketWriter.cpp b/src/socket/SocketWriter.cpp
index fd099e30..faed014c 100644
--- a/src/socket/SocketWriter.cpp
+++ b/src/socket/SocketWriter.cpp
@@ -40,10 +40,10 @@ socket_sink(const void* buf, size_t len, void* stream)
return ret;
}
-SocketWriter::SocketWriter(URIMap& map,
- URIs& uris,
- const Raul::URI& uri,
- SPtr<Socket> sock)
+SocketWriter::SocketWriter(URIMap& map,
+ URIs& uris,
+ const Raul::URI& uri,
+ SPtr<Raul::Socket> sock)
: AtomWriter(map, uris, *this)
, _map(map)
, _sratom(sratom_new(&map.urid_map_feature()->urid_map))
diff --git a/src/socket/SocketWriter.hpp b/src/socket/SocketWriter.hpp
index 141b0ca5..1b3523d1 100644
--- a/src/socket/SocketWriter.hpp
+++ b/src/socket/SocketWriter.hpp
@@ -23,11 +23,11 @@
#include "ingen/AtomWriter.hpp"
#include "ingen/Interface.hpp"
#include "ingen/types.hpp"
+#include "ingen/types.hpp"
+#include "raul/Socket.hpp"
#include "raul/URI.hpp"
#include "sratom/sratom.h"
-#include "Socket.hpp"
-
namespace Ingen {
namespace Socket {
@@ -36,10 +36,10 @@ namespace Socket {
class SocketWriter : public AtomWriter, public AtomSink
{
public:
- SocketWriter(URIMap& map,
- URIs& uris,
- const Raul::URI& uri,
- SPtr<Socket> sock);
+ SocketWriter(URIMap& map,
+ URIs& uris,
+ const Raul::URI& uri,
+ SPtr<Raul::Socket> sock);
~SocketWriter();
@@ -51,14 +51,14 @@ public:
Raul::URI uri() const { return _uri; }
protected:
- URIMap& _map;
- Sratom* _sratom;
- SerdNode _base;
- SerdURI _base_uri;
- SerdEnv* _env;
- SerdWriter* _writer;
- Raul::URI _uri;
- SPtr<Socket> _socket;
+ URIMap& _map;
+ Sratom* _sratom;
+ SerdNode _base;
+ SerdURI _base_uri;
+ SerdEnv* _env;
+ SerdWriter* _writer;
+ Raul::URI _uri;
+ SPtr<Raul::Socket> _socket;
};
} // namespace Socket
diff --git a/src/socket/ingen_socket_client.cpp b/src/socket/ingen_socket_client.cpp
index 4dd8a491..61a175d4 100644
--- a/src/socket/ingen_socket_client.cpp
+++ b/src/socket/ingen_socket_client.cpp
@@ -19,19 +19,25 @@
#include "ingen/Log.hpp"
#include "ingen/Module.hpp"
#include "ingen/World.hpp"
+#include "raul/Socket.hpp"
-#include "Socket.hpp"
#include "SocketClient.hpp"
namespace Ingen {
namespace Socket {
+static Raul::Socket::Type type_from_uri(const Raul::URI uri) {
+ return (uri.scheme() == "unix")
+ ? Raul::Socket::Type::UNIX
+ : Raul::Socket::Type::TCP;
+}
+
static SPtr<Ingen::Interface>
new_socket_interface(Ingen::World* world,
const Raul::URI& uri,
SPtr<Ingen::Interface> respondee)
{
- SPtr<Socket> sock(new Socket(Socket::type_from_uri(uri)));
+ SPtr<Raul::Socket> sock(new Raul::Socket(type_from_uri(uri)));
if (!sock->connect(uri)) {
world->log().error(fmt("Failed to connect <%1%> (%2%)\n")
% sock->uri() % strerror(errno));
diff --git a/src/socket/ingen_socket_server.cpp b/src/socket/ingen_socket_server.cpp
index e4f14249..440f1597 100644
--- a/src/socket/ingen_socket_server.cpp
+++ b/src/socket/ingen_socket_server.cpp
@@ -23,11 +23,11 @@
#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 "Socket.hpp"
#include "SocketServer.hpp"
#define UNIX_SCHEME "unix://"
@@ -36,7 +36,9 @@ namespace Ingen {
namespace Socket {
static void
-ingen_listen(Ingen::World* world, Socket* unix_sock, Socket* net_sock)
+ingen_listen(Ingen::World* world,
+ Raul::Socket* unix_sock,
+ Raul::Socket* net_sock)
{
const std::string unix_path(world->conf().option("socket").ptr<char>());
@@ -91,14 +93,14 @@ ingen_listen(Ingen::World* world, Socket* unix_sock, Socket* net_sock)
}
if (pfds[0].revents & POLLIN) {
- SPtr<Socket> conn = unix_sock->accept();
+ SPtr<Raul::Socket> conn = unix_sock->accept();
if (conn) {
new SocketServer(*world, *engine, conn);
}
}
if (pfds[1].revents & POLLIN) {
- SPtr<Socket> conn = net_sock->accept();
+ SPtr<Raul::Socket> conn = net_sock->accept();
if (conn) {
new SocketServer(*world, *engine, conn);
}
@@ -109,8 +111,8 @@ ingen_listen(Ingen::World* world, Socket* unix_sock, Socket* net_sock)
struct ServerModule : public Ingen::Module
{
ServerModule()
- : unix_sock(Socket::Type::UNIX)
- , net_sock(Socket::Type::TCP)
+ : unix_sock(Raul::Socket::Type::UNIX)
+ , net_sock(Raul::Socket::Type::TCP)
{}
~ServerModule() {
@@ -127,8 +129,8 @@ struct ServerModule : public Ingen::Module
}
World* world;
- Socket unix_sock;
- Socket net_sock;
+ Raul::Socket unix_sock;
+ Raul::Socket net_sock;
std::unique_ptr<std::thread> thread;
};
diff --git a/src/socket/wscript b/src/socket/wscript
index e503e8a2..25cbf285 100644
--- a/src/socket/wscript
+++ b/src/socket/wscript
@@ -4,8 +4,7 @@ from waflib.extras import autowaf as autowaf
def build(bld):
if bld.is_defined('HAVE_SOCKET'):
obj = bld(features = 'cxx cxxshlib',
- source = ['Socket.cpp',
- 'SocketReader.cpp',
+ source = ['SocketReader.cpp',
'SocketWriter.cpp',
'ingen_socket_server.cpp'],
includes = ['.', '../..'],
@@ -16,8 +15,7 @@ def build(bld):
autowaf.use_lib(bld, obj, 'RAUL')
obj = bld(features = 'cxx cxxshlib',
- source = ['Socket.cpp',
- 'SocketReader.cpp',
+ source = ['SocketReader.cpp',
'SocketWriter.cpp',
'ingen_socket_client.cpp'],
includes = ['.', '../..'],