Tidy up Nanomsg more correctly.
authorCarl Hetherington <cth@carlh.net>
Wed, 26 May 2021 21:06:42 +0000 (23:06 +0200)
committerCarl Hetherington <cth@carlh.net>
Wed, 26 May 2021 21:06:42 +0000 (23:06 +0200)
src/lib/nanomsg.cc
src/lib/nanomsg.h

index 1180f69369930958ba3ad44cef56da5de4f4f70a..61e6c08ce9e66fa738300b9a417267356f71a8b6 100644 (file)
@@ -43,17 +43,24 @@ Nanomsg::Nanomsg (bool 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 {
-               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));
                }
        }
 }
 
 
+Nanomsg::~Nanomsg ()
+{
+       nn_shutdown (_socket, _endpoint);
+       nn_close (_socket);
+}
+
+
 bool
 Nanomsg::send (string s, int timeout)
 {
index 64ce4ea06649bb2786f064aab0e8f9874b828a11..8d89d6d99fee05b641ed5213d83d9145f42d4b32 100644 (file)
@@ -28,6 +28,7 @@ class Nanomsg
 {
 public:
        explicit Nanomsg (bool server);
+       ~Nanomsg ();
 
        Nanomsg (Nanomsg const&) = delete;
        Nanomsg& operator= (Nanomsg const&) = delete;
@@ -49,6 +50,7 @@ private:
        void recv_and_parse (int flags);
 
        int _socket;
+       int _endpoint;
        std::list<std::string> _pending;
        std::string _current;
 };