14 zmq::ws_decoder_t::ws_decoder_t (
size_t bufsize_,
18 decoder_base_t<ws_decoder_t, shared_message_memory_allocator> (bufsize_),
20 _zero_copy (zero_copy_),
21 _max_msg_size (maxmsgsize_),
22 _must_mask (must_mask_),
30 next_step (
_tmpbuf, 1, &ws_decoder_t::opcode_ready);
33 zmq::ws_decoder_t::~ws_decoder_t ()
35 const int rc = _in_progress.close ();
39 int zmq::ws_decoder_t::opcode_ready (
unsigned char const *)
41 const bool final = (_tmpbuf[0] & 0x80) != 0;
65 next_step (_tmpbuf, 1, &ws_decoder_t::size_first_byte_ready);
70 int zmq::ws_decoder_t::size_first_byte_ready (
unsigned char const *read_from_)
72 const bool is_masked = (_tmpbuf[0] & 0x80) != 0;
74 if (is_masked != _must_mask)
77 _size =
static_cast<uint64_t
> (_tmpbuf[0] & 0x7F);
81 next_step (_tmpbuf, 4, &ws_decoder_t::mask_ready);
85 next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready);
87 return size_ready (read_from_);
88 }
else if (_size == 126)
89 next_step (_tmpbuf, 2, &ws_decoder_t::short_size_ready);
91 next_step (_tmpbuf, 8, &ws_decoder_t::long_size_ready);
97 int zmq::ws_decoder_t::short_size_ready (
unsigned char const *read_from_)
99 _size = (_tmpbuf[0] << 8) | _tmpbuf[1];
102 next_step (_tmpbuf, 4, &ws_decoder_t::mask_ready);
106 next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready);
108 return size_ready (read_from_);
113 int zmq::ws_decoder_t::long_size_ready (
unsigned char const *read_from_)
120 next_step (_tmpbuf, 4, &ws_decoder_t::mask_ready);
124 next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready);
126 return size_ready (read_from_);
131 int zmq::ws_decoder_t::mask_ready (
unsigned char const *read_from_)
133 memcpy (_mask, _tmpbuf, 4);
139 next_step (_tmpbuf, 1, &ws_decoder_t::flags_ready);
141 return size_ready (read_from_);
146 int zmq::ws_decoder_t::flags_ready (
unsigned char const *read_from_)
151 flags = _tmpbuf[0] ^ _mask[0];
162 return size_ready (read_from_);
166 int zmq::ws_decoder_t::size_ready (
unsigned char const *read_pos_)
169 if (_max_msg_size >= 0)
170 if (
unlikely (_size >
static_cast<uint64_t
> (_max_msg_size))) {
176 if (
unlikely (_size !=
static_cast<size_t> (_size))) {
181 int rc = _in_progress.close ();
187 shared_message_memory_allocator &allocator = get_allocator ();
188 if (
unlikely (!_zero_copy || allocator.data () > read_pos_
189 ||
static_cast<size_t> (read_pos_ - allocator.data ())
191 || _size >
static_cast<size_t> (
192 allocator.data () + allocator.size () - read_pos_))) {
196 rc = _in_progress.init_size (
static_cast<size_t> (_size));
201 rc = _in_progress.init (
202 const_cast<unsigned char *
> (read_pos_),
static_cast<size_t> (_size),
204 allocator.provide_content ());
207 if (_in_progress.is_zcmsg ()) {
208 allocator.advance_content ();
209 allocator.inc_ref ();
215 rc = _in_progress.init ();
221 _in_progress.set_flags (_msg_flags);
228 next_step (_in_progress.data (), _in_progress.size (),
229 &ws_decoder_t::message_ready);
234 int zmq::ws_decoder_t::message_ready (
unsigned char const *)
239 unsigned char *
data =
240 static_cast<unsigned char *
> (_in_progress.data ());
241 for (
size_t i = 0;
i < _size; ++
i, mask_index++)
247 next_step (_tmpbuf, 1, &ws_decoder_t::opcode_ready);