zxs 2 months ago
parent
commit
db949a9c41
2 changed files with 60 additions and 11 deletions
  1. 6 11
      robot/robotics/nexus_net_client.hpp
  2. 54 0
      robot/robotics/singleton.hpp

+ 6 - 11
robot/robotics/nexus_net_client.hpp

@@ -850,18 +850,13 @@ namespace robotics {
              * @param data
              */
             void on_authenticate(nexus_net_message data) {
-                try {
-                    data.args >> is_heartbeat_ >> parent_code_ >> parent_name_;
-                    if (is_heartbeat_) {
-                        authorized_event(true, parent_code_, parent_name_);
-                        subscribe();
-                    }
-                    else {
-                        authorized_event(false, "", "");
-                    }
+                data.args >> is_heartbeat_ >> parent_code_ >> parent_name_;
+                if (is_heartbeat_) {
+                    authorized_event(true, parent_code_, parent_name_);
+                    subscribe();
                 }
-                catch (std::exception const& ec) {
-                    LOG_ERROR << ec;
+                else {
+                    authorized_event(false, "", "");
                 }
             }
             /**

+ 54 - 0
robot/robotics/singleton.hpp

@@ -0,0 +1,54 @@
+/**
+*
+*  @file singleton.hpp
+*  @author zxw
+*  Copyright 2024, yjrobotics.  All rights reserved.
+*
+*  robotics
+*
+*/
+#pragma once
+//stl
+#include <iostream>
+#ifdef WINDOWS_BUILD
+#include <windows.h>
+#elif LINUX_BUILD
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdio.h>
+#endif
+
+namespace robotics {
+    namespace v3 {
+        class singleton {
+        public:
+            explicit singleton() {
+            }
+            bool locker(std::string const& value) {
+#ifdef WINDOWS_BUILD
+                mutex_ = CreateMutex(NULL, TRUE, value.c_str());
+                return GetLastError() == ERROR_ALREADY_EXISTS;
+#elif LINUX_BUILD
+                std::string file = "/tmp/" + value + ".lock";
+                fd_ = open(file.c_str(), O_CREAT | O_RDWR, 0666);
+                return flock(fd, LOCK_EX | LOCK_NB) == -1;
+#endif
+            }
+            ~singleton() {
+#ifdef WINDOWS_BUILD
+                ReleaseMutex(mutex_);
+                CloseHandle(mutex_);
+#elif LINUX_BUILD
+                flock(fd_, LOCK_UN);
+                close(fd_);
+#endif
+            }
+        private:
+#ifdef WINDOWS_BUILD
+            HANDLE mutex_ = nullptr;
+#elif LINUX_BUILD
+            int fd_ = 0;
+#endif
+        };
+    }
+}