56 lines
1.3 KiB
C
56 lines
1.3 KiB
C
|
#ifndef __SOCKET_H__
|
||
|
#define __SOCKET_H__
|
||
|
|
||
|
#include "Buffer.h"
|
||
|
#include "Protocol.h"
|
||
|
#include <cassert>
|
||
|
#include <nng/nng.h>
|
||
|
#include <nng/protocol/reqrep0/rep.h>
|
||
|
#include <nng/protocol/reqrep0/req.h>
|
||
|
#include <string_view>
|
||
|
|
||
|
namespace Nng {
|
||
|
|
||
|
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();
|
||
|
|
||
|
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__
|