~ubuntu-branches/ubuntu/saucy/zeromq3/saucy

« back to all changes in this revision

Viewing changes to src/mailbox.cpp

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2012-06-04 21:21:09 UTC
  • Revision ID: package-import@ubuntu.com-20120604212109-b7b3m0rn21o8oo2q
Tags: upstream-3.1.0~beta+dfsg
ImportĀ upstreamĀ versionĀ 3.1.0~beta+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
5
 
 
6
    This file is part of 0MQ.
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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/>.
 
20
*/
 
21
 
 
22
#include "mailbox.hpp"
 
23
#include "err.hpp"
 
24
 
 
25
zmq::mailbox_t::mailbox_t ()
 
26
{
 
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);
 
31
    zmq_assert (!ok);
 
32
    active = false;
 
33
}
 
34
 
 
35
zmq::mailbox_t::~mailbox_t ()
 
36
{
 
37
    //  TODO: Retrieve and deallocate commands inside the cpipe.
 
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
    sync.lock ();
 
48
    cpipe.write (cmd_, false);
 
49
    bool ok = cpipe.flush ();
 
50
    sync.unlock ();
 
51
    if (!ok)
 
52
        signaler.send ();
 
53
}
 
54
 
 
55
int zmq::mailbox_t::recv (command_t *cmd_, int timeout_)
 
56
{
 
57
    //  Try to get the command straight away.
 
58
    if (active) {
 
59
        bool ok = cpipe.read (cmd_);
 
60
        if (ok)
 
61
            return 0;
 
62
 
 
63
        //  If there are no more commands available, switch into passive state.
 
64
        active = false;
 
65
        signaler.recv ();
 
66
    }
 
67
 
 
68
    //  Wait for signal from the command sender.
 
69
    int rc = signaler.wait (timeout_);
 
70
    if (rc != 0 && (errno == EAGAIN || errno == EINTR))
 
71
        return -1;
 
72
 
 
73
    //  We've got the signal. Now we can switch into active state.
 
74
    active = true;
 
75
 
 
76
    //  Get a command.
 
77
    errno_assert (rc == 0);
 
78
    bool ok = cpipe.read (cmd_);
 
79
    zmq_assert (ok);
 
80
    return 0;
 
81
}
 
82