1 /*
2  * Copyright (c) 2015, 2021, Oracle and/or its affiliates.
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, version 2.0,
6  * as published by the Free Software Foundation.
7  *
8  * This program is also distributed with certain software (including
9  * but not limited to OpenSSL) that is licensed under separate terms,
10  * as designated in a particular file or component or in included license
11  * documentation.  The authors of MySQL hereby grant you an additional
12  * permission to link the program and your derivative works with the
13  * separately licensed software that they have included with MySQL.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License, version 2.0, for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301  USA
24  */
25 
26 #ifndef NGS_SOCKET_EVENTS_H_
27 #define NGS_SOCKET_EVENTS_H_
28 
29 #include <vector>
30 #include "ngs/socket_events_interface.h"
31 
32 struct event_base;
33 
34 namespace ngs {
35 
36 class Socket_events: public Socket_events_interface {
37 public:
38 #ifdef _WIN32
39   // mimick evutil_socket_t in libevent-2.x
40   typedef intptr_t socket_type;
41 #else
42   typedef int socket_type;
43 #endif
44   Socket_events();
45   ~Socket_events();
46 
47   bool listen(Socket_interface::Shared_ptr s, ngs::function<void (Connection_acceptor_interface &)> callback);
48 
49   void add_timer(const std::size_t delay_ms, ngs::function<bool ()> callback);
50   void loop();
51   void break_loop();
52 
53 private:
54   static void timeout_call(socket_type sock, short which, void *arg);
55   static void socket_data_avaiable(socket_type sock, short which, void *arg);
56 
57   struct Timer_data;
58   struct Socket_data;
59   struct event_base *m_evbase;
60   std::vector<Socket_data *> m_socket_events;
61   std::vector<Timer_data*> m_timer_events;
62   Mutex m_timers_mutex;
63 };
64 
65 } // namespace ngs
66 
67 #endif // NGS_SOCKET_EVENTS_H_
68