Fix the build for older macOS.
[dcpomatic.git] / src / lib / nanomsg.cc
1 /*
2     Copyright (C) 2020-2021 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
22 #include "nanomsg.h"
23 #include "dcpomatic_log.h"
24 #include "exceptions.h"
25 #include <nanomsg/nn.h>
26 #include <nanomsg/pair.h>
27 #include <stdexcept>
28 #include <cerrno>
29
30
31 using std::string;
32 using std::runtime_error;
33 using boost::optional;
34
35
36 #define NANOMSG_URL "ipc:///tmp/dcpomatic.ipc"
37
38
39 Nanomsg::Nanomsg (bool server)
40 {
41         _socket = nn_socket (AF_SP, NN_PAIR);
42         if (_socket < 0) {
43                 throw runtime_error("Could not set up nanomsg socket");
44         }
45         if (server) {
46                 if ((_endpoint = nn_bind(_socket, NANOMSG_URL)) < 0) {
47                         throw runtime_error(String::compose("Could not bind nanomsg socket (%1)", errno));
48                 }
49         } else {
50                 if ((_endpoint = nn_connect(_socket, NANOMSG_URL)) < 0) {
51                         throw runtime_error(String::compose("Could not connect nanomsg socket (%1)", errno));
52                 }
53         }
54 }
55
56
57 Nanomsg::~Nanomsg ()
58 {
59         nn_shutdown (_socket, _endpoint);
60         nn_close (_socket);
61 }
62
63
64 bool
65 Nanomsg::send (string s, int timeout)
66 {
67         if (timeout != 0) {
68                 nn_setsockopt (_socket, NN_SOL_SOCKET, NN_SNDTIMEO, &timeout, sizeof(int));
69         }
70
71         int const r = nn_send (_socket, s.c_str(), s.length(), timeout ? 0 : NN_DONTWAIT);
72         if (r < 0) {
73                 if (errno == ETIMEDOUT || errno == EAGAIN) {
74                         return false;
75                 }
76                 throw runtime_error(String::compose("Could not send to nanomsg socket (%1)", errno));
77         } else if (r != int(s.length())) {
78                 throw runtime_error("Could not send to nanomsg socket (message too big)");
79         }
80
81         return true;
82 }
83
84
85 optional<string>
86 Nanomsg::get_from_pending ()
87 {
88         if (_pending.empty()) {
89                 return {};
90         }
91
92         auto const l = _pending.back();
93         _pending.pop_back();
94         return l;
95 }
96
97
98 void
99 Nanomsg::recv_and_parse (int flags)
100 {
101         char* buf = 0;
102         int const received = nn_recv (_socket, &buf, NN_MSG, flags);
103         if (received < 0)
104         {
105                 if (errno == ETIMEDOUT || errno == EAGAIN) {
106                         return;
107                 }
108
109                 LOG_DISK_NC("nn_recv failed");
110                 throw CommunicationFailedError ();
111         }
112
113         char* p = buf;
114         for (int i = 0; i < received; ++i) {
115                 if (*p == '\n') {
116                         _pending.push_front (_current);
117                         _current = "";
118                 } else {
119                         _current += *p;
120                 }
121                 ++p;
122         }
123         nn_freemsg (buf);
124 }
125
126
127 optional<string>
128 Nanomsg::receive (int timeout)
129 {
130         if (timeout != 0) {
131                 nn_setsockopt (_socket, NN_SOL_SOCKET, NN_RCVTIMEO, &timeout, sizeof(int));
132         }
133
134         auto l = get_from_pending ();
135         if (l) {
136                 return *l;
137         }
138
139         recv_and_parse (timeout ? 0 : NN_DONTWAIT);
140
141         return get_from_pending ();
142 }