2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Class for creating a dedicated GSource
25 #ifndef COMMON_IPC_IPC_GSOURCE_HPP
26 #define COMMON_IPC_IPC_GSOURCE_HPP
29 #if GLIB_CHECK_VERSION(2,36,0)
31 #include "ipc/service.hpp"
32 #include "ipc/types.hpp"
41 * Class for connecting to the glib's loop.
42 * Creates a dedicated GSource.
44 * It's supposed to be constructed ONLY with the static create method
45 * and destructed in a glib callback.
48 * - waiting till the managed object (Client or Service) is destroyed
49 * before IPCGSource stops operating. For now programmer has to ensure this.
53 typedef std::function<void(FileDescriptor fd, short pollEvent)> HandlerCallback;
54 typedef std::shared_ptr<IPCGSource> Pointer;
58 IPCGSource() = delete;
59 IPCGSource(const IPCGSource&) = delete;
60 IPCGSource& operator=(const IPCGSource&) = delete;
63 * New file descriptor to listen on.
65 * @param peerFD file descriptor
67 void addFD(const FileDescriptor peerFD);
70 * Removes the file descriptor from the GSource
72 * @param peerFD file descriptor
74 void removeFD(const FileDescriptor peerFD);
77 * Attach to the glib's GMainContext
79 * @param context where to connect
80 * @return result of the g_source_attach call
82 guint attach(GMainContext* context = nullptr);
85 * Creates the IPCGSource class in the memory allocated by glib.
86 * Calls IPCGSource's constructor
88 * @param fds initial set of file descriptors
89 * @param handlerCallback event handling callback
91 * @return pointer to the IPCGSource
93 static Pointer create(const std::vector<FileDescriptor>& fds,
94 const HandlerCallback& handlerCallback);
99 * GSourceFuncs' callback
101 static gboolean prepare(GSource* source, gint* timeout);
104 * GSourceFuncs' callback
106 static gboolean check(GSource* source);
109 * GSourceFuncs' callback
111 static gboolean dispatch(GSource* source,
112 GSourceFunc callbacks,
116 * GSourceFuncs' callback
118 static void finalize(GSource* source);
122 // Called only from IPCGSource::create
123 IPCGSource(const std::vector<FileDescriptor> fds,
124 const HandlerCallback& handlerCallback);
127 FDInfo(gpointer tag, FileDescriptor fd)
128 : tag(tag), fd(fd) {}
130 bool operator==(const gpointer t)
135 bool operator==(const FileDescriptor f)
145 HandlerCallback mHandlerCallback;
146 std::vector<FDInfo> mFDInfos;
152 #endif // GLIB_CHECK_VERSION
154 #endif // COMMON_IPC_IPC_GSOURCE_HPP