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
6
This file is part of 0MQ.
8
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
10
the Free Software Foundation; either version 3 of the License, or
11
(at your option) any later version.
13
0MQ is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
22
#include "mailbox.hpp"
25
zmq::mailbox_t::mailbox_t ()
27
// Get the pipe into passive state. That way, if the users starts by
28
// polling on the associated file descriptor it will get woken up when
29
// new command is posted.
30
bool ok = cpipe.read (NULL);
35
zmq::mailbox_t::~mailbox_t ()
37
// TODO: Retrieve and deallocate commands inside the cpipe.
40
zmq::fd_t zmq::mailbox_t::get_fd ()
42
return signaler.get_fd ();
45
void zmq::mailbox_t::send (const command_t &cmd_)
48
cpipe.write (cmd_, false);
49
bool ok = cpipe.flush ();
55
int zmq::mailbox_t::recv (command_t *cmd_, int timeout_)
57
// Try to get the command straight away.
59
bool ok = cpipe.read (cmd_);
63
// If there are no more commands available, switch into passive state.
68
// Wait for signal from the command sender.
69
int rc = signaler.wait (timeout_);
70
if (rc != 0 && (errno == EAGAIN || errno == EINTR))
73
// We've got the signal. Now we can switch into active state.
77
errno_assert (rc == 0);
78
bool ok = cpipe.read (cmd_);