1/*
2 Copyright (c) 2007-2019 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 "macros.hpp"
32
33#include <limits.h>
34#include <string.h>
35
36#ifndef ZMQ_HAVE_WINDOWS
37#include <unistd.h>
38#endif
39
40#include <new>
41#include <sstream>
42
43#include "raw_engine.hpp"
44#include "io_thread.hpp"
45#include "session_base.hpp"
46#include "v1_encoder.hpp"
47#include "v1_decoder.hpp"
48#include "v2_encoder.hpp"
49#include "v2_decoder.hpp"
50#include "null_mechanism.hpp"
51#include "plain_client.hpp"
52#include "plain_server.hpp"
53#include "gssapi_client.hpp"
54#include "gssapi_server.hpp"
55#include "curve_client.hpp"
56#include "curve_server.hpp"
57#include "raw_decoder.hpp"
58#include "raw_encoder.hpp"
59#include "config.hpp"
60#include "err.hpp"
61#include "ip.hpp"
62#include "tcp.hpp"
63#include "likely.hpp"
64#include "wire.hpp"
65
66zmq::raw_engine_t::raw_engine_t (
67 fd_t fd_,
68 const options_t &options_,
69 const endpoint_uri_pair_t &endpoint_uri_pair_) :
70 stream_engine_base_t (fd_, options_, endpoint_uri_pair_)
71{
72}
73
74zmq::raw_engine_t::~raw_engine_t ()
75{
76}
77
78void zmq::raw_engine_t::plug_internal ()
79{
80 // no handshaking for raw sock, instantiate raw encoder and decoders
81 _encoder = new (std::nothrow) raw_encoder_t (_options.out_batch_size);
82 alloc_assert (_encoder);
83
84 _decoder = new (std::nothrow) raw_decoder_t (_options.in_batch_size);
85 alloc_assert (_decoder);
86
87 _next_msg = &raw_engine_t::pull_msg_from_session;
88 _process_msg = static_cast<int (stream_engine_base_t::*) (msg_t *)> (
89 &raw_engine_t::push_raw_msg_to_session);
90
91 properties_t properties;
92 if (init_properties (properties)) {
93 // Compile metadata.
94 zmq_assert (_metadata == NULL);
95 _metadata = new (std::nothrow) metadata_t (properties);
96 alloc_assert (_metadata);
97 }
98
99 if (_options.raw_notify) {
100 // For raw sockets, send an initial 0-length message to the
101 // application so that it knows a peer has connected.
102 msg_t connector;
103 connector.init ();
104 push_raw_msg_to_session (&connector);
105 connector.close ();
106 session ()->flush ();
107 }
108
109 set_pollin ();
110 set_pollout ();
111 // Flush all the data that may have been already received downstream.
112 in_event ();
113}
114
115bool zmq::raw_engine_t::handshake ()
116{
117 return true;
118}
119
120void zmq::raw_engine_t::error (error_reason_t reason_)
121{
122 if (_options.raw_socket && _options.raw_notify) {
123 // For raw sockets, send a final 0-length message to the application
124 // so that it knows the peer has been disconnected.
125 msg_t terminator;
126 terminator.init ();
127 (this->*_process_msg) (&terminator);
128 terminator.close ();
129 }
130 stream_engine_base_t::error (reason_);
131}
132
133int zmq::raw_engine_t::push_raw_msg_to_session (msg_t *msg_)
134{
135 if (_metadata && _metadata != msg_->metadata ())
136 msg_->set_metadata (_metadata);
137 return push_msg_to_session (msg_);
138}
139