add nng message.

This commit is contained in:
amass 2024-10-29 23:13:33 +08:00
parent 22b81a9a0b
commit 3676f7defd
6 changed files with 128 additions and 15 deletions

View File

@ -2,6 +2,7 @@ add_library(Nng
Buffer.h Buffer.cpp Buffer.h Buffer.cpp
ErrorCode.h ErrorCode.cpp ErrorCode.h ErrorCode.cpp
Listener.h Listener.cpp Listener.h Listener.cpp
Message.h Message.cpp
Socket.h Socket.cpp Socket.h Socket.cpp
SocketAisoWrapper.h SocketAisoWrapper.cpp SocketAisoWrapper.h SocketAisoWrapper.cpp
) )

60
Nng/Message.cpp Normal file
View File

@ -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

41
Nng/Message.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef __MESSAGE_H__
#define __MESSAGE_H__
#include <cstdint>
#include <nng/nng.h>
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__

View File

@ -1,10 +1,29 @@
#include "Socket.h" #include "Socket.h"
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/reqrep0/req.h>
namespace Nng { 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() { Socket::~Socket() {
if (m_socket.id != 0) { if (m_socket.id != 0) {
nng_close(m_socket); nng_close(m_socket);
} }
} }
int Socket::id() const {
return nng_socket_id(m_socket);
}
} // namespace Nng } // namespace Nng

View File

@ -5,8 +5,6 @@
#include "Protocol.h" #include "Protocol.h"
#include <cassert> #include <cassert>
#include <nng/nng.h> #include <nng/nng.h>
#include <nng/protocol/reqrep0/rep.h>
#include <nng/protocol/reqrep0/req.h>
#include <string_view> #include <string_view>
namespace Nng { namespace Nng {
@ -15,19 +13,11 @@ class Socket {
friend class Listener; friend class Listener;
public: public:
Socket(Protocol protocol) { 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();
int id() const;
void listen(const std::string_view &url, int flags = 0) { void listen(const std::string_view &url, int flags = 0) {
nng_listen(m_socket, url.data(), nullptr, flags); nng_listen(m_socket, url.data(), nullptr, flags);
} }

View File

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
base_path=$(pwd) 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" libraries_root="/opt/Libraries"
if [ $base_path == /home/* ]; then if [ $base_path == /home/* ]; then
build_path=${base_path}/build 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 " -DQt5Svg_DIR=${qt_prefix_path}/lib/cmake/Qt5Svg "
else else
cmake_qt_parameters="" cmake_qt_parameters=""
echo "please install qt6.6.2 or qt5.15.2 ..." echo "please install qt6.8.0 or qt5.15.2 ..."
fi fi
function cmake_scan() { function cmake_scan() {
@ -48,6 +48,8 @@ function cmake_scan() {
-B ${build_path} \ -B ${build_path} \
-DCMAKE_BUILD_TYPE=Debug \ -DCMAKE_BUILD_TYPE=Debug \
-DUNIT_TEST=ON \ -DUNIT_TEST=ON \
-DINDEPENDENT_BUILD=ON \
-DKYLIN_WITH_NNG=ON \
-DBOOST_ROOT=${libraries_root}/boost_1_86_0 \ -DBOOST_ROOT=${libraries_root}/boost_1_86_0 \
-DZeroMQ_ROOT=${libraries_root}/zeromq-4.3.4_debug \ -DZeroMQ_ROOT=${libraries_root}/zeromq-4.3.4_debug \
${cmake_qt_parameters} ${cmake_qt_parameters}