46 lines
963 B
C++
46 lines
963 B
C++
#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:
|
|
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);
|
|
}
|
|
|
|
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__
|