1 #ifndef _GIOMM_SOCKETSOURCE_H
2 #define _GIOMM_SOCKETSOURCE_H
3 
4 /* Copyright (C) 2014 The giomm Development Team
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <glibmm/refptr.h>
21 #include <glibmm/main.h>
22 #include <glibmm/priorities.h>
23 #include <giomm/cancellable.h>
24 #include <sigc++/sigc++.h>
25 
26 namespace Gio
27 {
28 class GIOMM_API Socket;
29 
30 /** @newin{2,42}
31  * @ingroup NetworkIO
32  */
33 class GIOMM_API SignalSocket
34 {
35 public:
36 #ifndef DOXYGEN_SHOULD_SKIP_THIS
37   explicit inline SignalSocket(GMainContext* context);
38 #endif
39 
40   /** Connects an I/O handler that watches a socket.
41    * @code
42    * bool io_handler(Glib::IOCondition io_condition) { ... }
43    * Gio::signal_socket().connect(sigc::ptr_fun(&io_handler), socket, Glib::IO_IN | Glib::IO_OUT);
44    * @endcode
45    * is equivalent to:
46    * @code
47    * bool io_handler(Glib::IOCondition io_condition) { ... }
48    * const auto socket_source = Gio::SocketSource::create(socket, Glib::IO_IN | Glib::IO_OUT);
49    * socket_source->connect(sigc::ptr_fun(&io_handler));
50    * socket_source->attach(Glib::MainContext::get_default());
51    * @endcode
52    *
53    * This method is not thread-safe. You should call it, or manipulate the
54    * returned sigc::connection object, only from the thread where the SignalSocket
55    * object's MainContext runs.
56    *
57    * @newin{2,42}
58    *
59    * @param slot A slot to call when polling @a socket results in an event that matches @a
60    * condition.
61    * The event will be passed as a parameter to @a slot.
62    * If <tt>io_handler()</tt> returns <tt>false</tt> the handler is disconnected.
63    * @param socket The Socket object to watch.
64    * @param condition The conditions to watch for.
65    * @param cancellable A Cancellable object which can be used to cancel the source,
66    *        which will cause the source to trigger, reporting the current condition
67    *        (which is likely 0 unless cancellation happened at the same time as a condition change).
68    *        You can check for this in the callback using Cancellable::is_cancelled().
69    * @param priority The priority of the new event source.
70    * @return A connection handle, which can be used to disconnect the handler.
71    */
72   sigc::connection connect(const sigc::slot<bool, Glib::IOCondition>& slot,
73     const Glib::RefPtr<Socket>& socket, Glib::IOCondition condition,
74     const Glib::RefPtr<Cancellable>& cancellable = Glib::RefPtr<Cancellable>(),
75     int priority = Glib::PRIORITY_DEFAULT);
76 
77 private:
78   GMainContext* context_;
79 
80   // no copy assignment
81   SignalSocket& operator=(const SignalSocket&);
82 };
83 
84 /** Convenience socket signal.
85  * @param context The main context to which the signal shall be attached.
86  * @return A signal proxy; you want to use SignalSocket::connect().
87  *
88  * @newin{2,42}
89  * @ingroup NetworkIO
90  */
91 GIOMM_API
92 SignalSocket signal_socket(
93   const Glib::RefPtr<Glib::MainContext>& context = Glib::RefPtr<Glib::MainContext>());
94 
95 /** An event source that can monitor a Gio::Socket.
96  * @see Gio::Socket::create_source().
97  *
98  * @newin{2,42}
99  * @ingroup NetworkIO
100  */
101 class GIOMM_API SocketSource : public Glib::IOSource
102 {
103 public:
104   using CppObjectType = Gio::SocketSource;
105 
106   static Glib::RefPtr<SocketSource> create(const Glib::RefPtr<Socket>& socket,
107     Glib::IOCondition condition,
108     const Glib::RefPtr<Cancellable>& cancellable = Glib::RefPtr<Cancellable>());
109 
110 protected:
111   SocketSource(const Glib::RefPtr<Socket>& socket, Glib::IOCondition condition,
112     const Glib::RefPtr<Cancellable>& cancellable);
113   ~SocketSource() noexcept override;
114 };
115 
116 } // namespace Gio
117 
118 #endif /* _GIOMM_SOCKETSOURCE_H */
119