Kylin/AsioZeroMQ/BasicSocket.h
2023-07-21 14:07:27 +08:00

95 lines
3.8 KiB
C++

#ifndef BASICSOCKET_H
#define BASICSOCKET_H
#include "Message.h"
#include "Options.h"
#include <boost/asio/buffer.hpp>
#include <boost/asio/execution_context.hpp>
#include <boost/range/metafunctions.hpp>
#include <zmq.h>
namespace ZeroMQ {
template <typename Service>
class BasicSocket {
public:
using ImplementationType = typename Service::ImplementationType;
BasicSocket(boost::asio::io_context &context, SocketType type);
void connect(std::string_view address, boost::system::error_code &error);
void connect(std::string_view address);
/**
* @brief only see the instance is nullptr or not.
*/
bool connected() const;
void bind(std::string_view address, boost::system::error_code &error);
void bind(std::string_view address);
template <int Option, class T, bool BoolUnit>
void setOption(IntegralOption<Option, T, BoolUnit>, const T &value, boost::system::error_code &error);
template <int Option, class T, bool BoolUnit>
void setOption(IntegralOption<Option, T, BoolUnit>, const T &value);
template <int Option, int NullTerm>
void setOption(ArrayOption<Option, NullTerm>, const std::string_view &buffer, boost::system::error_code &error);
template <int Option, class T, bool BoolUnit>
T option(IntegralOption<Option, T, BoolUnit>, boost::system::error_code &error) const;
template <int Option, class T, bool BoolUnit>
T option(IntegralOption<Option, T, BoolUnit>) const;
size_t send(boost::asio::const_buffer buffer, SendFlags flags, boost::system::error_code &error);
template <typename ConstBufferSequence>
typename boost::enable_if<boost::has_range_const_iterator<ConstBufferSequence>, size_t>::type
send(const ConstBufferSequence &buffers, SendFlags flags, boost::system::error_code &error);
template <typename ConstBufferSequence>
typename boost::enable_if<boost::has_range_const_iterator<ConstBufferSequence>, size_t>::type
send(const ConstBufferSequence &buffers, SendFlags flags = SendFlags::None);
size_t send(Message &&message, SendFlags flags, boost::system::error_code &error);
size_t send(Message &&message, SendFlags flags);
std::size_t receive(Message &message, RecvFlags flags, boost::system::error_code &error);
std::size_t receive(Message &message, RecvFlags flags = RecvFlags::None);
size_t receive(boost::asio::mutable_buffer buffer, RecvFlags flags, boost::system::error_code &error);
size_t receive(const boost::asio::mutable_buffer &buffer, RecvFlags flags = RecvFlags::None);
template <typename MutableBufferSequence>
typename boost::enable_if<boost::has_range_const_iterator<MutableBufferSequence>, std::vector<size_t>>::type
receive(const MutableBufferSequence &buffers, RecvFlags flags, boost::system::error_code &error);
template <typename MutableBufferSequence>
typename boost::enable_if<boost::has_range_const_iterator<MutableBufferSequence>, std::vector<size_t>>::type
receive(const MutableBufferSequence &buffers, RecvFlags flags = RecvFlags::None);
// void read_handler(const boost::system::error_code& ec,std::size_t bytes_transferred)
template <typename ReadHandler>
void asyncReceive(Message &message, ReadHandler &&handler);
template <typename MutableBufferSequence, typename ReadHandler>
void asyncReceive(const MutableBufferSequence &buffers, ReadHandler &&handler);
template <bool CheckN, class OutputIt>
size_t receiveMultipart(OutputIt &out, size_t n, RecvFlags flags, boost::system::error_code &error);
template <class OutputIt, typename ReadHandler>
void asyncReceiveMultipart(OutputIt out, ReadHandler &&handler);
boost::asio::io_context &ioContext() const;
~BasicSocket();
private:
Service &m_service;
ImplementationType m_impl;
};
} // namespace ZeroMQ
#include "BasicSocket.inl"
#endif // BASICSOCKET_H