libshevek
socket.hh
1 /* socket.hh - network sockets for use with shevek::main
2  * Copyright 2003 Bas Wijnen <wijnen@debian.org>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef SHEVEK_SOCKET_HH
19 #define SHEVEK_SOCKET_HH
20 
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <sys/un.h>
25 #include <netdb.h>
26 #include <sigc++/sigc++.h>
27 #include <glibmm.h>
28 #include "debug.hh"
29 #include "fd.hh"
30 #include "error.hh"
31 #include "avahi.hh"
32 
33 namespace shevek
34 {
36  class socket : public fd
37  {
38  public:
40  typedef sigc::signal0 <void> disconnect_t;
42  typedef sigc::slot0 <void> listen_t;
44  static Glib::RefPtr <socket> create (Glib::RefPtr <Glib::MainContext> main = Glib::MainContext::get_default () );
46  void listen_unix (std::string const &file, listen_t cb, unsigned queue = 10);
48  void listen_tcp (std::string const &service, listen_t cb, unsigned queue = 10);
50  void listen_avahi (std::string const &service, Glib::ustring const &protocol, Glib::ustring const &name, listen_t cb, unsigned queue = 10);
52 
59  void listen (std::string const &port, listen_t cb, unsigned queue = 10);
61  void connect_unix (std::string const &unix_name);
63  void connect_tcp (std::string const &host, std::string const &service);
67 
73  void connect (std::string const &port);
75  void accept (Glib::RefPtr <socket> sock);
77  std::string get_peer_info (bool numeric = false) const;
79  std::string get_own_info (bool numeric = false) const;
83  void disconnect ();
84  protected:
86  socket (Glib::RefPtr <Glib::MainContext> main);
88  virtual ~socket ();
89  private:
90  // Accept a new connection (called from main loop)
91  void l_listen (listen_t cb);
92  // transform a service or int to a real int
93  static int l_service_to_port (std::string const &service);
94  // Determine hostname and port from sockaddr_in
95  std::string l_get_socket_info (struct sockaddr_in *addr,
96  bool numeric) const;
97  // Finish disconnecting (read buffer is empty).
98  void l_finish_disconnect ();
99  // Callback function for the user: data has been read.
100  read_t m_read;
101  // Callback function for the user: socket is disconnected.
102  disconnect_t m_disconnect;
103  // Is this socket listening for connections?
104  bool m_listener;
105  // Remember the name of unix listensockets, to unlink them.
106  std::string *m_name;
107  // Callback for listensockets, called when a new connection is made.
108  listen_t m_listen;
109  // Avahi object for listening sockets.
110  Glib::RefPtr <avahi> m_avahi;
111  };
112 }
113 
114 #endif // defined SHEVEK_SOCKET_HH
Use normal files with the fd class.
Definition: file.hh:26
std::string get_own_info(bool numeric=false) const
Get info about our side of the connection.
void connect_unix(std::string const &unix_name)
Connect to a UNIX socket. Use connect instead.
sigc::slot1< bool, std::string & > read_t
Function pointer to call when fd is ready for reading.
Definition: fd.hh:36
void listen_unix(std::string const &file, listen_t cb, unsigned queue=10)
Listen for new connections on a UNIX socket. Use listen instead.
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.
void listen(std::string const &port, listen_t cb, unsigned queue=10)
Listen for new connections.
sigc::slot0< void > listen_t
New connection callback type.
Definition: socket.hh:42
Use a unix-domain, tcp or avahi network connection with shevek::fd.
Definition: socket.hh:36
Information about a discovered server.
Definition: avahi.hh:101
The fd class is a generic wrapper for a file descriptor to use it in the Glib event loop...
Definition: fd.hh:29
void connect(std::string const &port)
Connect to a socket.
void connect_tcp(std::string const &host, std::string const &service)
Connect to a TCP socket. Use connect instead.
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...
virtual ~socket()
Destructor.
static Glib::RefPtr< socket > create(Glib::RefPtr< Glib::MainContext > main=Glib::MainContext::get_default())
Create a new socket.
std::string get_peer_info(bool numeric=false) const
Get information about the other side of a connection.
void disconnect()
Disconnect the socket without reconnecting.
void accept(Glib::RefPtr< socket > sock)
Accept a connection (only allowed on a listening socket).
Details about a discovered service. These are internally created and may be examined by the applicati...
Definition: avahi.hh:79
sigc::signal0< void > disconnect_t
Disconnect signal type.
Definition: socket.hh:40
disconnect_t signal_disconnect()
Schedule a function to be called when the socket is disconnected.
void listen_tcp(std::string const &service, listen_t cb, unsigned queue=10)
Listen for new connections on a TCP socket. Use listen instead.
socket(Glib::RefPtr< Glib::MainContext > main)
Constructor.