YAMI4 Core
listener.h
1 // Copyright Maciej Sobczak 2008-2019.
2 // This file is part of YAMI4.
3 //
4 // YAMI4 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 // YAMI4 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 YAMI4. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef YAMICORE_LISTENER_H_INCLUDED
18 #define YAMICORE_LISTENER_H_INCLUDED
19 
20 #include "core.h"
21 #include "details-fwd.h"
22 #include <cstddef>
23 
24 #ifdef YAMI4_WITH_OPEN_SSL
25 #ifdef _WIN32
26 // this is necessary to avoid conflicts between
27 // WinSock2.h (used by YAMI4) and winsock.h (used by OpenSSL):
28 #include <WinSock2.h>
29 #endif // _WIN32
30 #include <openssl/ssl.h>
31 #endif // YAMI4_WITH_OPEN_SSL
32 
33 // selected per platform
34 #include <details-types.h>
35 
36 namespace yami
37 {
38 
39 namespace details
40 {
41 
42 class listener
43 {
44 public:
45 
46  void init(allocator & alloc,
47  channel_group & group, mutex & mtx,
49  void * connection_hook_hint,
50  core::io_error_function io_error_callback,
51  void * io_error_callback_hint);
52 
53  core::result prepare(const char * target);
54 
55  core::result do_some_work();
56 
57  void close_resource();
58  void clean();
59 
60  const char * get_target() const { return target_; }
61  protocol get_protocol() const { return protocol_; }
62 
63  io_descriptor_type get_io_descriptor() const { return fd_; }
64 
65  void set_selector_index(int index) { selector_index_ = index; }
66  int get_selector_index() const { return selector_index_; }
67 
68 #ifdef YAMI4_WITH_QNX
69  bool is_qnx_receiver_ready() const;
70 #endif // YAMI4_WITH_QNX
71 
72  void inc_ref() { ++ref_count_; }
73  void dec_ref() { --ref_count_; }
74  bool can_be_removed() const { return ref_count_ == 0; }
75 
76  listener * next;
77 
78 private:
79 
80  protocol protocol_;
81 
82  core::result prepare_tcp(const char * address);
83  core::result prepare_udp(const char * address);
84  core::result prepare_unix(const char * path);
85 
86 #ifdef YAMI4_WITH_QNX
87  core::result prepare_qnx();
88 #endif // YAMI4_WITH_QNX
89 
90  core::result accept_tcp();
91  core::result accept_udp();
92  core::result accept_unix();
93 
94 #ifdef YAMI4_WITH_QNX
95  core::result accept_qnx();
96 #endif // YAMI4_WITH_QNX
97 
98 #ifdef YAMI4_WITH_OPEN_SSL
99  core::result prepare_tcps(const char * address);
100  core::result accept_tcps();
101 #endif // YAMI4_WITH_OPEN_SSL
102 
103  allocator * alloc_;
104  mutex * mtx_;
105  std::size_t ref_count_;
106 
107  const char * target_;
108 
109  int tcp_port_; // network port (in local order)
110 
111  io_descriptor_type fd_;
112 
113  int selector_index_;
114 
115  channel_group * group_;
116 
117  // used only for UDP
118  char * frame_buffer_;
119  std::size_t frame_buffer_size_;
120 
121 #ifdef YAMI4_WITH_QNX
122  int channel_id_;
123  msg_receiver msg_receiver_;
124 #endif // YAMI4_WITH_QNX
125 
127  void * connection_hook_hint_;
128 
129  core::io_error_function io_error_callback_;
130  void * io_error_callback_hint_;
131 };
132 
133 } // namespace details
134 
135 } // namespace yami
136 
137 #endif // YAMICORE_LISTENER_H_INCLUDED
void(* io_error_function)(void *hint, int error_code, const char *description)
Type of function callback for internal I/O error logging.
Definition: core.h:149
void(* new_incoming_connection_function)(void *hint, const char *source, std::size_t index, std::size_t sequence_number)
Definition: core.h:90
Namespace devoted for everything related to YAMI4.
Definition: agent.h:25
result
General type for reporting success and error states.
Definition: core.h:32