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