2024-10-29 21:51:37 +08:00
|
|
|
#ifndef __SOCKET_H__
|
|
|
|
#define __SOCKET_H__
|
|
|
|
|
|
|
|
#include "Buffer.h"
|
|
|
|
#include "Protocol.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <nng/nng.h>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
namespace Nng {
|
|
|
|
|
|
|
|
class Socket {
|
|
|
|
friend class Listener;
|
|
|
|
|
|
|
|
public:
|
2024-10-29 23:13:33 +08:00
|
|
|
Socket(Protocol protocol);
|
2024-10-29 21:51:37 +08:00
|
|
|
~Socket();
|
|
|
|
|
2024-10-29 23:13:33 +08:00
|
|
|
int id() const;
|
|
|
|
|
2024-10-29 21:51:37 +08:00
|
|
|
void listen(const std::string_view &url, int flags = 0) {
|
|
|
|
nng_listen(m_socket, url.data(), nullptr, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dial(const std::string_view &url, int flags = 0) {
|
|
|
|
nng_dial(m_socket, url.data(), nullptr, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(const void *data, size_t size, int flags = 0) {
|
|
|
|
nng_send(m_socket, (void *)data, size, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
Buffer recv(int flags = 0) const {
|
|
|
|
void *data;
|
|
|
|
size_t size;
|
|
|
|
int r = nng_recv(m_socket, &data, &size, flags | NNG_FLAG_ALLOC);
|
|
|
|
return Buffer(data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nng_socket m_socket = NNG_SOCKET_INITIALIZER;
|
|
|
|
};
|
|
|
|
|
|
|
|
}; // namespace Nng
|
|
|
|
|
|
|
|
#endif // __SOCKET_H__
|