libshevek

- Main Page
- Classes
- Files

- File List

src/socket.hh

    00001 /* socket.hh - network sockets for use with shevek::main
    00002  * Copyright 2003 Bas Wijnen <wijnen@debian.org>
    00003  *
    00004  * This program is free software: you can redistribute it and/or modify
    00005  * it under the terms of the GNU General Public License as published by
    00006  * the Free Software Foundation, either version 3 of the License, or
    00007  * (at your option) any later version.
    00008  *
    00009  * This program is distributed in the hope that it will be useful,
    00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  * GNU General Public License for more details.
    00013  *
    00014  * You should have received a copy of the GNU General Public License
    00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    00016  */
    00017
    00018 #ifndef SHEVEK_SOCKET_HH
    00019 #define SHEVEK_SOCKET_HH
    00020
    00021 #include <sys/types.h>
    00022 #include <sys/socket.h>
    00023 #include <netinet/in.h>
    00024 #include <sys/un.h>
    00025 #include <netdb.h>
    00026 #include <sigc++/sigc++.h>
    00027 #include <glibmm.h>
    00028 #include "debug.hh"
    00029 #include "fd.hh"
    00030 #include "error.hh"
    00031 #include "avahi.hh"
    00032
    00033 namespace shevek
    00034 {
    00036   class socket : public fd
    00037   {
    00038   public:
    00040     typedef sigc::signal0 <void> disconnect_t;
    00042     typedef sigc::slot0 <void> listen_t;
    00044     static Glib::RefPtr <socket> create (Glib::RefPtr <Glib::MainContext> main = Glib::MainContext::get_default () );
    00046     void listen_unix (std::string const &file, listen_t cb, unsigned queue = 10);
    00048     void listen_tcp (std::string const &service, listen_t cb, unsigned queue = 10);
    00050     void listen_avahi (std::string const &service, Glib::ustring const &protocol, Glib::ustring const &name, listen_t cb, unsigned queue = 10);
    00052
    00059     void listen (std::string const &port, listen_t cb, unsigned queue = 10);
    00061     void connect_unix (std::string const &unix_name);
    00063     void connect_tcp (std::string const &host, std::string const &service);
    00065     void connect_avahi (avahi::browser::owner const &target, avahi::browser::details const &details = avahi::browser::details ());
    00067
    00073     void connect (std::string const &port);
    00075     void accept (Glib::RefPtr <socket> sock);
    00077     std::string get_peer_info (bool numeric = false) const;
    00079     std::string get_own_info (bool numeric = false) const;
    00081     disconnect_t signal_disconnect ();
    00083     void disconnect ();
    00084   protected:
    00086     socket (Glib::RefPtr <Glib::MainContext> main);
    00088     virtual ~socket ();
    00089   private:
    00090     // Accept a new connection (called from main loop)
    00091     void l_listen (listen_t cb);
    00092     // transform a service or int to a real int
    00093     static int l_service_to_port (std::string const &service);
    00094     // Determine hostname and port from sockaddr_in
    00095     std::string l_get_socket_info (struct sockaddr_in *addr,
    00096                                    bool numeric) const;
    00097     // Finish disconnecting (read buffer is empty).
    00098     void l_finish_disconnect ();
    00099     // Callback function for the user: data has been read.
    00100     read_t m_read;
    00101     // Callback function for the user: socket is disconnected.
    00102     disconnect_t m_disconnect;
    00103     // Is this socket listening for connections?
    00104     bool m_listener;
    00105     // Remember the name of unix listensockets, to unlink them.
    00106     std::string *m_name;
    00107     // Callback for listensockets, called when a new connection is made.
    00108     listen_t m_listen;
    00109     // Avahi object for listening sockets.
    00110     Glib::RefPtr <avahi> m_avahi;
    00111   };
    00112 }
    00113
    00114 #endif // defined SHEVEK_SOCKET_HH