1 | /* |
2 | Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file |
3 | |
4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
5 | |
6 | libzmq is free software; you can redistribute it and/or modify it under |
7 | the terms of the GNU Lesser General Public License (LGPL) as published |
8 | by the Free Software Foundation; either version 3 of the License, or |
9 | (at your option) any later version. |
10 | |
11 | As a special exception, the Contributors give you permission to link |
12 | this library with independent modules to produce an executable, |
13 | regardless of the license terms of these independent modules, and to |
14 | copy and distribute the resulting executable under terms of your choice, |
15 | provided that you also meet, for each linked independent module, the |
16 | terms and conditions of the license of that module. An independent |
17 | module is a module which is not derived from or based on this library. |
18 | If you modify this library, you must extend this exception to your |
19 | version of the library. |
20 | |
21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
24 | License for more details. |
25 | |
26 | You should have received a copy of the GNU Lesser General Public License |
27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
28 | */ |
29 | |
30 | #include "precompiled.hpp" |
31 | #include "stream_listener_base.hpp" |
32 | #include "session_base.hpp" |
33 | #include "socket_base.hpp" |
34 | #include "zmtp_engine.hpp" |
35 | #include "raw_engine.hpp" |
36 | |
37 | #ifndef ZMQ_HAVE_WINDOWS |
38 | #include <unistd.h> |
39 | #else |
40 | #include <winsock2.h> |
41 | #endif |
42 | |
43 | zmq::stream_listener_base_t::stream_listener_base_t ( |
44 | zmq::io_thread_t *io_thread_, |
45 | zmq::socket_base_t *socket_, |
46 | const zmq::options_t &options_) : |
47 | own_t (io_thread_, options_), |
48 | io_object_t (io_thread_), |
49 | _s (retired_fd), |
50 | _handle (static_cast<handle_t> (NULL)), |
51 | _socket (socket_) |
52 | { |
53 | } |
54 | |
55 | zmq::stream_listener_base_t::~stream_listener_base_t () |
56 | { |
57 | zmq_assert (_s == retired_fd); |
58 | zmq_assert (!_handle); |
59 | } |
60 | |
61 | int zmq::stream_listener_base_t::get_local_address (std::string &addr_) const |
62 | { |
63 | addr_ = get_socket_name (_s, socket_end_local); |
64 | return addr_.empty () ? -1 : 0; |
65 | } |
66 | |
67 | void zmq::stream_listener_base_t::process_plug () |
68 | { |
69 | // Start polling for incoming connections. |
70 | _handle = add_fd (_s); |
71 | set_pollin (_handle); |
72 | } |
73 | |
74 | void zmq::stream_listener_base_t::process_term (int linger_) |
75 | { |
76 | rm_fd (_handle); |
77 | _handle = static_cast<handle_t> (NULL); |
78 | close (); |
79 | own_t::process_term (linger_); |
80 | } |
81 | |
82 | int zmq::stream_listener_base_t::close () |
83 | { |
84 | // TODO this is identical to stream_connector_base_t::close |
85 | |
86 | zmq_assert (_s != retired_fd); |
87 | #ifdef ZMQ_HAVE_WINDOWS |
88 | const int rc = closesocket (_s); |
89 | wsa_assert (rc != SOCKET_ERROR); |
90 | #else |
91 | const int rc = ::close (_s); |
92 | errno_assert (rc == 0); |
93 | #endif |
94 | _socket->event_closed (make_unconnected_bind_endpoint_pair (_endpoint), _s); |
95 | _s = retired_fd; |
96 | |
97 | return 0; |
98 | } |
99 | |
100 | void zmq::stream_listener_base_t::create_engine (fd_t fd_) |
101 | { |
102 | const endpoint_uri_pair_t endpoint_pair ( |
103 | get_socket_name (fd_, socket_end_local), |
104 | get_socket_name (fd_, socket_end_remote), endpoint_type_bind); |
105 | |
106 | i_engine *engine; |
107 | if (options.raw_socket) |
108 | engine = new (std::nothrow) raw_engine_t (fd_, options, endpoint_pair); |
109 | else |
110 | engine = new (std::nothrow) zmtp_engine_t (fd_, options, endpoint_pair); |
111 | alloc_assert (engine); |
112 | |
113 | // Choose I/O thread to run connecter in. Given that we are already |
114 | // running in an I/O thread, there must be at least one available. |
115 | io_thread_t *io_thread = choose_io_thread (options.affinity); |
116 | zmq_assert (io_thread); |
117 | |
118 | // Create and launch a session object. |
119 | session_base_t *session = |
120 | session_base_t::create (io_thread, false, _socket, options, NULL); |
121 | errno_assert (session); |
122 | session->inc_seqnum (); |
123 | launch_child (session); |
124 | send_attach (session, engine, false); |
125 | |
126 | _socket->event_accepted (endpoint_pair, fd_); |
127 | } |
128 | |