From 3676f7defd5eb591d1cb094459923e779661a1ca Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Tue, 29 Oct 2024 23:13:33 +0800 Subject: [PATCH] add nng message. --- Nng/CMakeLists.txt | 1 + Nng/Message.cpp | 60 ++++++++++++++++++++++++++++++++++++++++++++++ Nng/Message.h | 41 +++++++++++++++++++++++++++++++ Nng/Socket.cpp | 19 +++++++++++++++ Nng/Socket.h | 16 +++---------- resource/deploy.sh | 6 +++-- 6 files changed, 128 insertions(+), 15 deletions(-) create mode 100644 Nng/Message.cpp create mode 100644 Nng/Message.h diff --git a/Nng/CMakeLists.txt b/Nng/CMakeLists.txt index 86f80cc..74f5d77 100644 --- a/Nng/CMakeLists.txt +++ b/Nng/CMakeLists.txt @@ -2,6 +2,7 @@ add_library(Nng Buffer.h Buffer.cpp ErrorCode.h ErrorCode.cpp Listener.h Listener.cpp + Message.h Message.cpp Socket.h Socket.cpp SocketAisoWrapper.h SocketAisoWrapper.cpp ) diff --git a/Nng/Message.cpp b/Nng/Message.cpp new file mode 100644 index 0000000..8aaa3d1 --- /dev/null +++ b/Nng/Message.cpp @@ -0,0 +1,60 @@ +#include "Message.h" + +namespace Nng { + +Message::Message(std::size_t size) { + nng_msg_alloc(&m_message, size); +} + +Message::Message(const Message &rhs) { + nng_msg_dup(&m_message, rhs.m_message); +} + +Message &Message::operator=(const Message &rhs) { + if (this != &rhs) { + if (m_message != nullptr) nng_msg_free(m_message); + nng_msg_dup(&m_message, rhs.m_message); + } + return *this; +} + +Message &Message::operator=(Message &&rhs) noexcept { + if (this != &rhs) { + if (m_message != nullptr) nng_msg_free(m_message); + m_message = rhs.m_message; + rhs.m_message = nullptr; + } + return *this; +} + +Message::~Message() { + if (m_message != nullptr) nng_msg_free(m_message); +} + +void Message::realloc(std::size_t size) const { + nng_msg_realloc(m_message, size); +} + +Message::Header Message::header() const { + return Header(m_message); +} + +Message::Body Message::body() const { + return Body(m_message); +} + +Message::Header::Header(nng_msg *message) : m_message(message) { +} + +std::size_t Message::Body::capacity() const { + return nng_msg_capacity(m_message); +} + +void Message::Body::clear() const noexcept { + nng_msg_clear(m_message); +} + +Message::Body::Body(nng_msg *message) : m_message(message) { +} + +} // namespace Nng diff --git a/Nng/Message.h b/Nng/Message.h new file mode 100644 index 0000000..39273d3 --- /dev/null +++ b/Nng/Message.h @@ -0,0 +1,41 @@ +#ifndef __MESSAGE_H__ +#define __MESSAGE_H__ + +#include +#include + +namespace Nng { + +class Message { +public: + class Header { + friend class Message; + Header(nng_msg *message); + nng_msg *m_message = nullptr; + }; + + class Body { + public: + std::size_t capacity() const; + void clear() const noexcept; + + private: + friend class Message; + Body(nng_msg *message); + nng_msg *m_message = nullptr; + }; + + Message(std::size_t size); + Message(const Message &rhs); + Message &operator=(const Message &rhs); + Message &operator=(Message &&rhs) noexcept; + ~Message(); + void realloc(std::size_t size) const; + Header header() const; + Body body() const; + +private: + nng_msg *m_message = nullptr; +}; +} // namespace Nng +#endif // __MESSAGE_H__ \ No newline at end of file diff --git a/Nng/Socket.cpp b/Nng/Socket.cpp index cf72c61..1ab0406 100644 --- a/Nng/Socket.cpp +++ b/Nng/Socket.cpp @@ -1,10 +1,29 @@ #include "Socket.h" +#include +#include namespace Nng { +Socket::Socket(Protocol protocol) { + if (protocol == Protocol::Bus) { + } else if (protocol == Protocol::Publisher) { + } else if (protocol == Protocol::Request) { + nng_req0_open(&m_socket); + } else if (protocol == Protocol::Reply) { + nng_rep0_open(&m_socket); + } else { + assert(true || "ggg"); + } +} + Socket::~Socket() { if (m_socket.id != 0) { nng_close(m_socket); } } + +int Socket::id() const { + return nng_socket_id(m_socket); +} + } // namespace Nng diff --git a/Nng/Socket.h b/Nng/Socket.h index 35b4ec1..ad056ee 100644 --- a/Nng/Socket.h +++ b/Nng/Socket.h @@ -5,8 +5,6 @@ #include "Protocol.h" #include #include -#include -#include #include namespace Nng { @@ -15,19 +13,11 @@ class Socket { friend class Listener; public: - Socket(Protocol protocol) { - if (protocol == Protocol::Bus) { - } else if (protocol == Protocol::Publisher) { - } else if (protocol == Protocol::Request) { - nng_req0_open(&m_socket); - } else if (protocol == Protocol::Reply) { - nng_rep0_open(&m_socket); - } else { - assert(true || "ggg"); - } - } + Socket(Protocol protocol); ~Socket(); + int id() const; + void listen(const std::string_view &url, int flags = 0) { nng_listen(m_socket, url.data(), nullptr, flags); } diff --git a/resource/deploy.sh b/resource/deploy.sh index 2eb8ace..b615625 100755 --- a/resource/deploy.sh +++ b/resource/deploy.sh @@ -1,7 +1,7 @@ #!/bin/bash base_path=$(pwd) -qt_prefix_path="/opt/Qt/6.6.2/gcc_64" +qt_prefix_path="/opt/Qt/6.8.0/gcc_64" libraries_root="/opt/Libraries" if [ $base_path == /home/* ]; then build_path=${base_path}/build @@ -35,7 +35,7 @@ elif [ -d "/opt/Qt/5.15.2/gcc_64" ]; then -DQt5Svg_DIR=${qt_prefix_path}/lib/cmake/Qt5Svg " else cmake_qt_parameters="" - echo "please install qt6.6.2 or qt5.15.2 ..." + echo "please install qt6.8.0 or qt5.15.2 ..." fi function cmake_scan() { @@ -48,6 +48,8 @@ function cmake_scan() { -B ${build_path} \ -DCMAKE_BUILD_TYPE=Debug \ -DUNIT_TEST=ON \ + -DINDEPENDENT_BUILD=ON \ + -DKYLIN_WITH_NNG=ON \ -DBOOST_ROOT=${libraries_root}/boost_1_86_0 \ -DZeroMQ_ROOT=${libraries_root}/zeromq-4.3.4_debug \ ${cmake_qt_parameters}