projects
/
dcpomatic.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix crashes when using templates in some cases (#2491).
[dcpomatic.git]
/
src
/
lib
/
nanomsg.cc
diff --git
a/src/lib/nanomsg.cc
b/src/lib/nanomsg.cc
index 4cc071370d0f9674e8de56a6b86df0912e06eab1..8061e2f8462c2b101f01e9ea71e48ca5724c71a1 100644
(file)
--- a/
src/lib/nanomsg.cc
+++ b/
src/lib/nanomsg.cc
@@
-1,5
+1,5
@@
/*
/*
- Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2020
-2021
Carl Hetherington <cth@carlh.net>
This file is part of DCP-o-matic.
This file is part of DCP-o-matic.
@@
-18,20
+18,24
@@
*/
*/
-#include "nanomsg.h"
+
#include "dcpomatic_log.h"
#include "exceptions.h"
#include "dcpomatic_log.h"
#include "exceptions.h"
+#include "nanomsg.h"
#include <nanomsg/nn.h>
#include <nanomsg/pair.h>
#include <nanomsg/nn.h>
#include <nanomsg/pair.h>
-#include <stdexcept>
#include <cerrno>
#include <cerrno>
+#include <stdexcept>
+
-using std::string;
using std::runtime_error;
using std::runtime_error;
+using std::string;
using boost::optional;
using boost::optional;
+
#define NANOMSG_URL "ipc:///tmp/dcpomatic.ipc"
#define NANOMSG_URL "ipc:///tmp/dcpomatic.ipc"
+
Nanomsg::Nanomsg (bool server)
{
_socket = nn_socket (AF_SP, NN_PAIR);
Nanomsg::Nanomsg (bool server)
{
_socket = nn_socket (AF_SP, NN_PAIR);
@@
-39,16
+43,24
@@
Nanomsg::Nanomsg (bool server)
throw runtime_error("Could not set up nanomsg socket");
}
if (server) {
throw runtime_error("Could not set up nanomsg socket");
}
if (server) {
- if (
nn_bind(_socket, NANOMSG_URL
) < 0) {
+ if (
(_endpoint = nn_bind(_socket, NANOMSG_URL)
) < 0) {
throw runtime_error(String::compose("Could not bind nanomsg socket (%1)", errno));
}
} else {
throw runtime_error(String::compose("Could not bind nanomsg socket (%1)", errno));
}
} else {
- if (
nn_connect(_socket, NANOMSG_URL
) < 0) {
+ if (
(_endpoint = nn_connect(_socket, NANOMSG_URL)
) < 0) {
throw runtime_error(String::compose("Could not connect nanomsg socket (%1)", errno));
}
}
}
throw runtime_error(String::compose("Could not connect nanomsg socket (%1)", errno));
}
}
}
+
+Nanomsg::~Nanomsg ()
+{
+ nn_shutdown (_socket, _endpoint);
+ nn_close (_socket);
+}
+
+
bool
Nanomsg::send (string s, int timeout)
{
bool
Nanomsg::send (string s, int timeout)
{
@@
-69,18
+81,20
@@
Nanomsg::send (string s, int timeout)
return true;
}
return true;
}
+
optional<string>
Nanomsg::get_from_pending ()
{
if (_pending.empty()) {
optional<string>
Nanomsg::get_from_pending ()
{
if (_pending.empty()) {
- return
optional<string>()
;
+ return
{}
;
}
}
-
string
const l = _pending.back();
+
auto
const l = _pending.back();
_pending.pop_back();
return l;
}
_pending.pop_back();
return l;
}
+
void
Nanomsg::recv_and_parse (int flags)
{
void
Nanomsg::recv_and_parse (int flags)
{
@@
-92,6
+106,7
@@
Nanomsg::recv_and_parse (int flags)
return;
}
return;
}
+ LOG_DISK_NC("nn_recv failed");
throw CommunicationFailedError ();
}
throw CommunicationFailedError ();
}
@@
-108,6
+123,7
@@
Nanomsg::recv_and_parse (int flags)
nn_freemsg (buf);
}
nn_freemsg (buf);
}
+
optional<string>
Nanomsg::receive (int timeout)
{
optional<string>
Nanomsg::receive (int timeout)
{
@@
-115,7
+131,7
@@
Nanomsg::receive (int timeout)
nn_setsockopt (_socket, NN_SOL_SOCKET, NN_RCVTIMEO, &timeout, sizeof(int));
}
nn_setsockopt (_socket, NN_SOL_SOCKET, NN_RCVTIMEO, &timeout, sizeof(int));
}
-
optional<string>
l = get_from_pending ();
+
auto
l = get_from_pending ();
if (l) {
return *l;
}
if (l) {
return *l;
}