summaryrefslogtreecommitdiff
path: root/src/zmq_engine.cpp
blob: 2c5eb1cc6dece467ce32fb8b45dae279ece1ebbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
    Copyright (c) 2007-2009 FastMQ Inc.

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
    the terms of the Lesser GNU General Public License as published by
    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
    Lesser GNU General Public License for more details.

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

#include "zmq_engine.hpp"
#include "io_thread.hpp"
#include "i_inout.hpp"
#include "config.hpp"
#include "err.hpp"

zmq::zmq_engine_t::zmq_engine_t (io_thread_t *parent_, fd_t fd_) :
    io_object_t (parent_),
    insize (0),
    inpos (0),
    outsize (0),
    outpos (0),
    inout (NULL)
{
    //  Allocate read & write buffer.
    inbuf = (unsigned char*) malloc (in_batch_size);
    zmq_assert (inbuf);
    outbuf = (unsigned char*) malloc (out_batch_size);
    zmq_assert (outbuf);

    //  Initialise the underlying socket.
    int rc = tcp_socket.open (fd_);
    zmq_assert (rc == 0);
}

zmq::zmq_engine_t::~zmq_engine_t ()
{
    free (outbuf);
    free (inbuf);
}

void zmq::zmq_engine_t::plug (i_inout *inout_)
{
    zmq_assert (!inout);

    encoder.set_inout (inout_);
    decoder.set_inout (inout_);

    handle = add_fd (tcp_socket.get_fd ());
    set_pollin (handle);
    set_pollout (handle);

    inout = inout_;

    //  Flush all the data that may have been already received downstream.
    in_event ();
}

void zmq::zmq_engine_t::unplug ()
{
    rm_fd (handle);
    encoder.set_inout (NULL);
    decoder.set_inout (NULL);
    inout = NULL;
}

void zmq::zmq_engine_t::in_event ()
{
    //  If there's no data to process in the buffer, read new data.
    if (inpos == insize) {

        //  Read as much data as possible to the read buffer.
        insize = tcp_socket.read (inbuf, in_batch_size);
        inpos = 0;

        //  Check whether the peer has closed the connection.
        if (insize == -1) {
            insize = 0;
            error ();
            return;
        }
    }

    //  Following code should be executed even if there's not a single byte in
    //  the buffer. There still can be a decoded messages stored in the decoder.

    //  Push the data to the decoder.
    int nbytes = decoder.write (inbuf + inpos, insize - inpos);

    //  Adjust read position. Stop polling for input if we got stuck.
    inpos += nbytes;
    if (inpos < insize)
        reset_pollin (handle);

    //  Flush all messages the decoder may have produced.
    inout->flush ();
}

void zmq::zmq_engine_t::out_event ()
{
    //  If write buffer is empty, try to read new data from the encoder.
    if (outpos == outsize) {

        outsize = encoder.read (outbuf, out_batch_size);
        outpos = 0;

        //  If there is no data to send, stop polling for output.
        if (outsize == 0)
            reset_pollout (handle);
    }

    //  If there are any data to write in write buffer, write as much as
    //  possible to the socket.
    if (outpos < outsize) {
        int nbytes = tcp_socket.write (outbuf + outpos, outsize - outpos);

        //  Handle problems with the connection.
        if (nbytes == -1) {
            error ();
            return;
        }

        outpos += nbytes;
    }
}

void zmq::zmq_engine_t::revive ()
{
    set_pollout (handle);
}

void zmq::zmq_engine_t::error ()
{
    zmq_assert (inout);
    inout->detach ();
    unplug ();
    delete this;
}