socket.hh
/* socket.hh - network sockets for use with shevek::main
* Copyright 2003 Bas Wijnen <wijnen@debian.org>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHEVEK_SOCKET_HH
#define SHEVEK_SOCKET_HH
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <netdb.h>
#include <sigc++/sigc++.h>
#include <glibmm.h>
#include "debug.hh"
#include "fd.hh"
#include "error.hh"
#include "avahi.hh"
namespace shevek
{
class socket : public fd
{
public:
typedef sigc::signal0 <void> disconnect_t;
typedef sigc::slot0 <void> listen_t;
static Glib::RefPtr <socket> create (Glib::RefPtr <Glib::MainContext> main = Glib::MainContext::get_default () );
void listen_unix (std::string const &file, listen_t cb, unsigned queue = 10);
void listen_tcp (std::string const &service, listen_t cb, unsigned queue = 10);
void listen_avahi (std::string const &service, Glib::ustring const &protocol, Glib::ustring const &name, listen_t cb, unsigned queue = 10);
void listen (std::string const &port, listen_t cb, unsigned queue = 10);
void connect_unix (std::string const &unix_name);
void connect_tcp (std::string const &host, std::string const &service);
void connect_avahi (avahi::browser::owner const &target, avahi::browser::details const &details = avahi::browser::details ());
void connect (std::string const &port);
void accept (Glib::RefPtr <socket> sock);
std::string get_peer_info (bool numeric = false) const;
std::string get_own_info (bool numeric = false) const;
disconnect_t signal_disconnect ();
void disconnect ();
protected:
socket (Glib::RefPtr <Glib::MainContext> main);
virtual ~socket ();
private:
// Accept a new connection (called from main loop)
void l_listen (listen_t cb);
// transform a service or int to a real int
static int l_service_to_port (std::string const &service);
// Determine hostname and port from sockaddr_in
std::string l_get_socket_info (struct sockaddr_in *addr,
bool numeric) const;
// Finish disconnecting (read buffer is empty).
void l_finish_disconnect ();
// Callback function for the user: data has been read.
read_t m_read;
// Callback function for the user: socket is disconnected.
disconnect_t m_disconnect;
// Is this socket listening for connections?
bool m_listener;
// Remember the name of unix listensockets, to unlink them.
std::string *m_name;
// Callback for listensockets, called when a new connection is made.
listen_t m_listen;
// Avahi object for listening sockets.
Glib::RefPtr <avahi> m_avahi;
};
}
#endif // defined SHEVEK_SOCKET_HH
shevek::file
Use normal files with the fd class.
Definition: file.hh:26
shevek::socket::get_own_info
std::string get_own_info(bool numeric=false) const
Get info about our side of the connection.
shevek::socket::connect_unix
void connect_unix(std::string const &unix_name)
Connect to a UNIX socket. Use connect instead.
shevek::fd::read_t
sigc::slot1< bool, std::string & > read_t
Function pointer to call when fd is ready for reading.
Definition: fd.hh:36
shevek::socket::listen_unix
void listen_unix(std::string const &file, listen_t cb, unsigned queue=10)
Listen for new connections on a UNIX socket. Use listen instead.
shevek::socket::connect_avahi
void connect_avahi(avahi::browser::owner const &target, avahi::browser::details const &details=avahi::browser::details())
Connect to an avahi TCP socket. Use connect instead.
shevek
Definition: args.hh:52
shevek::socket::listen
void listen(std::string const &port, listen_t cb, unsigned queue=10)
Listen for new connections.
shevek::socket::listen_t
sigc::slot0< void > listen_t
New connection callback type.
Definition: socket.hh:42
shevek::socket
Use a unix-domain, tcp or avahi network connection with shevek::fd.
Definition: socket.hh:36
shevek::avahi::browser::owner
Information about a discovered server.
Definition: avahi.hh:101
shevek::fd
The fd class is a generic wrapper for a file descriptor to use it in the Glib event loop...
Definition: fd.hh:29
shevek::socket::connect
void connect(std::string const &port)
Connect to a socket.
shevek::socket::connect_tcp
void connect_tcp(std::string const &host, std::string const &service)
Connect to a TCP socket. Use connect instead.
shevek::socket::listen_avahi
void listen_avahi(std::string const &service, Glib::ustring const &protocol, Glib::ustring const &name, listen_t cb, unsigned queue=10)
Listen for new connections on a TCP socket, and register it with avahi. Use listen instead...
shevek::socket::~socket
virtual ~socket()
Destructor.
shevek::socket::create
static Glib::RefPtr< socket > create(Glib::RefPtr< Glib::MainContext > main=Glib::MainContext::get_default())
Create a new socket.
shevek::socket::get_peer_info
std::string get_peer_info(bool numeric=false) const
Get information about the other side of a connection.
shevek::socket::disconnect
void disconnect()
Disconnect the socket without reconnecting.
shevek::socket::accept
void accept(Glib::RefPtr< socket > sock)
Accept a connection (only allowed on a listening socket).
shevek::avahi::browser::details
Details about a discovered service. These are internally created and may be examined by the applicati...
Definition: avahi.hh:79
shevek::socket::disconnect_t
sigc::signal0< void > disconnect_t
Disconnect signal type.
Definition: socket.hh:40
shevek::socket::signal_disconnect
disconnect_t signal_disconnect()
Schedule a function to be called when the socket is disconnected.
shevek::socket::listen_tcp
void listen_tcp(std::string const &service, listen_t cb, unsigned queue=10)
Listen for new connections on a TCP socket. Use listen instead.
shevek::socket::socket
socket(Glib::RefPtr< Glib::MainContext > main)
Constructor.