2 Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
22 #include "dcpomatic_log.h"
23 #include <nanomsg/nn.h>
24 #include <nanomsg/pair.h>
29 using std::runtime_error;
30 using boost::optional;
32 #define NANOMSG_URL "ipc:///tmp/dcpomatic.ipc"
34 Nanomsg::Nanomsg (bool server)
36 _socket = nn_socket (AF_SP, NN_PAIR);
38 throw runtime_error("Could not set up nanomsg socket");
41 if (nn_bind(_socket, NANOMSG_URL) < 0) {
42 throw runtime_error(String::compose("Could not bind nanomsg socket (%1)", errno));
45 if (nn_connect(_socket, NANOMSG_URL) < 0) {
46 throw runtime_error(String::compose("Could not connect nanomsg socket (%1)", errno));
52 Nanomsg::blocking_send (string s)
54 int const r = nn_send (_socket, s.c_str(), s.length(), 0);
56 throw runtime_error(String::compose("Could not send to nanomsg socket (%1)", errno));
57 } else if (r != int(s.length())) {
58 throw runtime_error("Could not send to nanomsg socket (message too big)");
63 Nanomsg::nonblocking_send (string s)
65 int const r = nn_send (_socket, s.c_str(), s.length(), NN_DONTWAIT);
67 if (errno == EAGAIN) {
70 throw runtime_error(String::compose("Could not send to nanomsg socket (%1)", errno));
71 } else if (r != int(s.length())) {
72 throw runtime_error("Could not send to nanomsg socket (message too big)");
79 Nanomsg::get_from_pending ()
81 if (_pending.empty()) {
82 return optional<string>();
85 string const l = _pending.back();
91 Nanomsg::recv_and_parse (bool blocking)
94 int const received = nn_recv (_socket, &buf, NN_MSG, blocking ? 0 : NN_DONTWAIT);
97 if (!blocking && errno == EAGAIN) {
101 throw runtime_error ("Could not communicate with subprocess");
105 for (int i = 0; i < received; ++i) {
107 _pending.push_front (_current);
118 Nanomsg::blocking_get ()
120 optional<string> l = get_from_pending ();
125 recv_and_parse (true);
127 l = get_from_pending ();
129 throw runtime_error ("Could not communicate with subprocess");
136 Nanomsg::nonblocking_get ()
138 optional<string> l = get_from_pending ();
143 recv_and_parse (false);
144 return get_from_pending ();