From 8c24b153cbef68bf235d00e75b21b1c81766895c Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 9 May 2012 17:50:15 +0000 Subject: Factor out Socket from SocketListener and make interface more general. git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@4330 a436a847-0d15-0410-975c-d299462d15a1 --- src/socket/Socket.hpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/socket/Socket.hpp (limited to 'src/socket/Socket.hpp') diff --git a/src/socket/Socket.hpp b/src/socket/Socket.hpp new file mode 100644 index 00000000..fa2b6972 --- /dev/null +++ b/src/socket/Socket.hpp @@ -0,0 +1,70 @@ +/* + This file is part of Ingen. + Copyright 2007-2012 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 + +namespace Ingen { +namespace Socket { + +class Socket { +public: + Socket() : _addr(NULL), _addr_len(0), _sock(-1) {} + ~Socket() { close(); } + + /** Open UNIX socket and bind to address. + * @param uri URI used for identification and log output. + * @param path Socket path. + * @return True on success + */ + bool open_unix(const std::string& uri, const std::string& path); + + /** Open TCP socket and bind to address. + * @param uri URI used for identification and log output. + * @param port Port number. + * @return True on success + */ + bool open_tcp(const std::string& uri, uint16_t port); + + /** Mark server socket as passive to listen for incoming connections. + * @return True on success. + */ + bool listen(); + + /** Accept a connection. + * @return The socket file descriptor, or -1 on error. + */ + int accept(); + + /** Return the file descriptor for the socket. */ + int fd() { return _sock; } + + /** Close the socket. */ + void close(); + +private: + bool bind(); + + std::string _uri; + struct sockaddr* _addr; + socklen_t _addr_len; + int _sock; +}; + +} // namespace Socket +} // namespace Ingen -- cgit v1.2.1