]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
msg: add seastar messenger error code framework
authorCasey Bodley <cbodley@redhat.com>
Sun, 22 Oct 2017 07:29:08 +0000 (03:29 -0400)
committerKefu Chai <kchai@redhat.com>
Wed, 13 Jun 2018 06:09:21 +0000 (14:09 +0800)
Signed-off-by: Casey Bodley <cbodley@redhat.com>
src/CMakeLists.txt
src/crimson/CMakeLists.txt [new file with mode: 0644]
src/crimson/net/CMakeLists.txt [new file with mode: 0644]
src/crimson/net/Errors.cc [new file with mode: 0644]
src/crimson/net/Errors.h [new file with mode: 0644]
src/crimson/net/Fwd.h

index b372759b7752562a9aa55d7ff76ce93b9a0ffe55..b15c64be394218d8c8c7b58a06de20921289b1e2 100644 (file)
@@ -391,6 +391,7 @@ include_directories(SYSTEM "${CMAKE_SOURCE_DIR}/src/googletest/googletest/includ
 if(WITH_SEASTAR)
   set(SEASTAR_ENABLE_TESTS OFF CACHE BOOL "disable seastar testing")
   add_subdirectory(seastar)
+  add_subdirectory(crimson)
 endif()
 
 set(xio_common_srcs)
diff --git a/src/crimson/CMakeLists.txt b/src/crimson/CMakeLists.txt
new file mode 100644 (file)
index 0000000..e5162ef
--- /dev/null
@@ -0,0 +1 @@
+add_subdirectory(net)
diff --git a/src/crimson/net/CMakeLists.txt b/src/crimson/net/CMakeLists.txt
new file mode 100644 (file)
index 0000000..a7f3c71
--- /dev/null
@@ -0,0 +1,7 @@
+set(crimson_net_srcs
+  Errors.cc)
+add_library(crimson_net_objs OBJECT ${crimson_net_srcs})
+target_compile_definitions(crimson_net_objs
+  PUBLIC $<TARGET_PROPERTY:Seastar::seastar,INTERFACE_COMPILE_DEFINITIONS>)
+target_include_directories(crimson_net_objs
+  PUBLIC $<TARGET_PROPERTY:Seastar::seastar,INTERFACE_INCLUDE_DIRECTORIES>)
diff --git a/src/crimson/net/Errors.cc b/src/crimson/net/Errors.cc
new file mode 100644 (file)
index 0000000..1ac6fe8
--- /dev/null
@@ -0,0 +1,91 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2017 Red Hat, Inc
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#include "Errors.h"
+
+namespace ceph::net {
+
+const std::error_category& net_category()
+{
+  struct category : public std::error_category {
+    const char* name() const noexcept override {
+      return "ceph::net";
+    }
+
+    std::string message(int ev) const override {
+      switch (static_cast<error>(ev)) {
+        case error::connection_aborted:
+          return "connection aborted";
+        case error::connection_refused:
+          return "connection refused";
+        case error::connection_reset:
+          return "connection reset";
+        default:
+          return "unknown";
+      }
+    }
+
+    // unfortunately, seastar throws connection errors with the system category,
+    // rather than the generic category that would match their counterparts
+    // in std::errc. we add our own errors for them, so we can match either
+    std::error_condition default_error_condition(int ev) const noexcept override {
+      switch (static_cast<error>(ev)) {
+        case error::connection_aborted:
+          return std::errc::connection_aborted;
+        case error::connection_refused:
+          return std::errc::connection_refused;
+        case error::connection_reset:
+          return std::errc::connection_reset;
+        default:
+          return std::error_condition(ev, *this);
+      }
+    }
+
+    bool equivalent(int code, const std::error_condition& cond) const noexcept override {
+      switch (static_cast<error>(code)) {
+        case error::connection_aborted:
+          return cond == std::errc::connection_aborted
+              || cond == std::error_condition(ECONNABORTED, std::system_category());
+        case error::connection_refused:
+          return cond == std::errc::connection_refused
+              || cond == std::error_condition(ECONNREFUSED, std::system_category());
+        case error::connection_reset:
+          return cond == std::errc::connection_reset
+              || cond == std::error_condition(ECONNRESET, std::system_category());
+        default:
+          return false;
+      }
+    }
+
+    bool equivalent(const std::error_code& code, int cond) const noexcept override {
+      switch (static_cast<error>(cond)) {
+        case error::connection_aborted:
+          return code == std::errc::connection_aborted
+              || code == std::error_code(ECONNABORTED, std::system_category());
+        case error::connection_refused:
+          return code == std::errc::connection_refused
+              || code == std::error_code(ECONNREFUSED, std::system_category());
+        case error::connection_reset:
+          return code == std::errc::connection_reset
+              || code == std::error_code(ECONNRESET, std::system_category());
+        default:
+          return false;
+      }
+    }
+  };
+  static category instance;
+  return instance;
+}
+
+} // namespace ceph::net
diff --git a/src/crimson/net/Errors.h b/src/crimson/net/Errors.h
new file mode 100644 (file)
index 0000000..e02e6cd
--- /dev/null
@@ -0,0 +1,49 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2017 Red Hat, Inc
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#pragma once
+
+#include <system_error>
+
+namespace ceph::net {
+
+/// net error codes
+enum class error {
+  connection_aborted,
+  connection_refused,
+  connection_reset,
+};
+
+/// net error category
+const std::error_category& net_category();
+
+inline std::error_code make_error_code(error e)
+{
+  return {static_cast<int>(e), net_category()};
+}
+
+inline std::error_condition make_error_condition(error e)
+{
+  return {static_cast<int>(e), net_category()};
+}
+
+} // namespace ceph::net
+
+namespace std {
+
+/// enables implicit conversion to std::error_condition
+template <>
+struct is_error_condition_enum<ceph::net::error> : public true_type {};
+
+} // namespace std
index 44f4c76a3813d35e117672e5ce97f86b5d87ce9b..c928bdebaf0f96e3b601178a230201fa983056ce 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <boost/intrusive_ptr.hpp>
 
+#include "Errors.h"
 #include "msg/msg_types.h"
 
 class Message;