encoder.cpp 2.89 KB
Newer Older
1
/*
2 3
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
4 5 6 7

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
8
    the terms of the GNU Lesser General Public License as published by
9 10 11 12 13 14
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU Lesser General Public License for more details.
16

17
    You should have received a copy of the GNU Lesser General Public License
18 19 20
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

21
#include "encoder.hpp"
22
#include "session.hpp"
23 24
#include "wire.hpp"

25 26
zmq::encoder_t::encoder_t (size_t bufsize_) :
    encoder_base_t <encoder_t> (bufsize_),
27
    session (NULL)
28
{
29 30
    int rc = in_progress.init ();
    errno_assert (rc == 0);
31 32

    //  Write 0 bytes to the batch and go to message_ready state.
33
    next_step (NULL, 0, &encoder_t::message_ready, true);
34 35
}

36
zmq::encoder_t::~encoder_t ()
37
{
38 39
    int rc = in_progress.close ();
    errno_assert (rc == 0);
40 41
}

42
void zmq::encoder_t::set_session (session_t *session_)
43
{
44
    session = session_;
45 46
}

47
bool zmq::encoder_t::size_ready ()
48 49
{
    //  Write message body into the buffer.
50
    next_step (in_progress.data (), in_progress.size (),
51
        &encoder_t::message_ready, false);
52 53 54
    return true;
}

55
bool zmq::encoder_t::message_ready ()
56
{
57
    //  Destroy content of the old message.
58 59
    int rc = in_progress.close ();
    errno_assert (rc == 0);
60

61
    //  Read new message. If there is none, return false.
62 63 64
    //  Note that new state is set only if write is successful. That way
    //  unsuccessful write will cause retry on the next state machine
    //  invocation.
65
    if (!session || !session->read (&in_progress)) {
66 67
        rc = in_progress.init ();
        errno_assert (rc == 0);
68
        return false;
69
    }
Martin Sustrik's avatar
Martin Sustrik committed
70

71
    //  Get the message size.
72
    size_t size = in_progress.size ();
73

74 75 76
    //  Account for the 'flags' byte.
    size++;

77 78
    //  For messages less than 255 bytes long, write one byte of message size.
    //  For longer messages write 0xff escape character followed by 8-byte
79
    //  message size. In both cases 'flags' field follows.
80 81
    if (size < 255) {
        tmpbuf [0] = (unsigned char) size;
82
        tmpbuf [1] = (in_progress.flags () & ~msg_t::shared);
83
        next_step (tmpbuf, 2, &encoder_t::size_ready,
84
            !(in_progress.flags () & (msg_t::more | msg_t::label)));
85 86 87 88
    }
    else {
        tmpbuf [0] = 0xff;
        put_uint64 (tmpbuf + 1, size);
89
        tmpbuf [9] = (in_progress.flags () & ~msg_t::shared);
90
        next_step (tmpbuf, 10, &encoder_t::size_ready,
91
            !(in_progress.flags () & (msg_t::more | msg_t::label)));
92 93 94
    }
    return true;
}