Use nanomsg instead of std{in,out} for communication between dist and writer.
[dcpomatic.git] / src / lib / nanomsg.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "nanomsg.h"
22 #include "dcpomatic_log.h"
23 #include <nanomsg/nn.h>
24 #include <nanomsg/pair.h>
25 #include <stdexcept>
26 #include <cerrno>
27
28 using std::string;
29 using std::runtime_error;
30 using boost::optional;
31
32 #define NANOMSG_URL "ipc:///tmp/dcpomatic.ipc"
33
34 Nanomsg::Nanomsg (bool server)
35 {
36         _socket = nn_socket (AF_SP, NN_PAIR);
37         if (_socket < 0) {
38                 throw runtime_error("Could not set up nanomsg socket");
39         }
40         if (server) {
41                 if (nn_bind(_socket, NANOMSG_URL) < 0) {
42                         throw runtime_error(String::compose("Could not bind nanomsg socket (%1)", errno));
43                 }
44         } else {
45                 if (nn_connect(_socket, NANOMSG_URL) < 0) {
46                         throw runtime_error(String::compose("Could not connect nanomsg socket (%1)", errno));
47                 }
48         }
49 }
50
51 void
52 Nanomsg::send (string s)
53 {
54         int const r = nn_send (_socket, s.c_str(), s.length(), 0);
55         if (r < 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)");
59         }
60 }
61
62 optional<string>
63 Nanomsg::get_from_pending ()
64 {
65         if (_pending.empty()) {
66                 return optional<string>();
67         }
68
69         string const l = _pending.back();
70         _pending.pop_back();
71         return l;
72 }
73
74 void
75 Nanomsg::recv_and_parse (bool blocking)
76 {
77         char* buf = 0;
78         int const received = nn_recv (_socket, &buf, NN_MSG, blocking ? 0 : NN_DONTWAIT);
79         if (received < 0)
80         {
81                 if (!blocking && errno == EAGAIN) {
82                         return;
83                 }
84
85                 throw runtime_error ("Could not communicate with subprocess");
86         }
87
88         char* p = buf;
89         for (int i = 0; i < received; ++i) {
90                 if (*p == '\n') {
91                         _pending.push_front (_current);
92                         _current = "";
93                 } else {
94                         _current += *p;
95                 }
96                 ++p;
97         }
98         nn_freemsg (buf);
99 }
100
101 string
102 Nanomsg::blocking_get ()
103 {
104         optional<string> l = get_from_pending ();
105         if (l) {
106                 return *l;
107         }
108
109         recv_and_parse (true);
110
111         l = get_from_pending ();
112         if (!l) {
113                 throw runtime_error ("Could not communicate with subprocess");
114         }
115
116         return *l;
117 }
118
119 optional<string>
120 Nanomsg::nonblocking_get ()
121 {
122         optional<string> l = get_from_pending ();
123         if (l) {
124                 return *l;
125         }
126
127         recv_and_parse (false);
128         return get_from_pending ();
129 }