mailbox.cpp 2.21 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2007-2009 iMatix Corporation
4
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
10 11 12 13 14 15
    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
16
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
17

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

22
#include "mailbox.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
23 24
#include "err.hpp"

25
zmq::mailbox_t::mailbox_t ()
Martin Sustrik's avatar
Martin Sustrik committed
26
{
27 28 29 30 31 32
    //  Get the pipe into passive state. That way, if the users starts by
    //  polling on the associated file descriptor it will get woken up when
    //  new command is posted.
    bool ok = cpipe.read (NULL);
    zmq_assert (!ok);
    active = false;
Martin Sustrik's avatar
Martin Sustrik committed
33 34
}

35
zmq::mailbox_t::~mailbox_t ()
Martin Sustrik's avatar
Martin Sustrik committed
36
{
37
    //  TODO: Retrieve and deallocate commands inside the cpipe.
Martin Sustrik's avatar
Martin Sustrik committed
38 39
}

40
zmq::fd_t zmq::mailbox_t::get_fd ()
41
{
42
    return signaler.get_fd ();
43 44
}

45
void zmq::mailbox_t::send (const command_t &cmd_)
46
{
47 48 49 50 51 52
    sync.lock ();
    cpipe.write (cmd_, false);
    bool ok = cpipe.flush ();
    sync.unlock ();
    if (!ok)
        signaler.send ();
53 54
}

55
int zmq::mailbox_t::recv (command_t *cmd_, int timeout_)
56
{
57 58 59 60 61 62 63 64 65
    //  Try to get the command straight away.
    if (active) {
        bool ok = cpipe.read (cmd_);
        if (ok)
            return 0;

        //  If there are no more commands available, switch into passive state.
        active = false;
        signaler.recv ();
66
    }
67

68 69
    //  Wait for signal from the command sender.
    int rc = signaler.wait (timeout_);
70
    if (rc != 0 && (errno == EAGAIN || errno == EINTR))
71
        return -1;
72

73 74
    //  We've got the signal. Now we can switch into active state.
    active = true;
Martin Sustrik's avatar
Martin Sustrik committed
75

76
    //  Get a command.
77
    errno_assert (rc == 0);
78 79
    bool ok = cpipe.read (cmd_);
    zmq_assert (ok);
80 81
    return 0;
}
Martin Sustrik's avatar
Martin Sustrik committed
82