ClickHouse/src/IO/WriteBufferFromPocoSocket.h

66 lines
2.2 KiB
C++
Raw Normal View History

2012-03-09 15:46:52 +00:00
#pragma once
#include <Poco/Net/Socket.h>
#include <IO/WriteBuffer.h>
#include <IO/BufferWithOwnMemory.h>
2023-03-03 19:30:43 +00:00
#include <Common/AsyncTaskExecutor.h>
2012-03-09 15:46:52 +00:00
namespace DB
{
2023-03-03 19:30:43 +00:00
using AsyncCallback = std::function<void(int, Poco::Timespan, AsyncEventTimeoutType, const std::string &, uint32_t)>;
2017-05-28 14:29:40 +00:00
/** Works with the ready Poco::Net::Socket. Blocking operations.
2012-03-09 15:46:52 +00:00
*/
class WriteBufferFromPocoSocket : public BufferWithOwnMemory<WriteBuffer>
{
2021-11-10 22:58:56 +00:00
public:
explicit WriteBufferFromPocoSocket(Poco::Net::Socket & socket_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE);
explicit WriteBufferFromPocoSocket(Poco::Net::Socket & socket_, const ProfileEvents::Event & write_event_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE);
2021-11-10 22:58:56 +00:00
~WriteBufferFromPocoSocket() override;
2023-03-03 19:30:43 +00:00
void setAsyncCallback(AsyncCallback async_callback_) { async_callback = std::move(async_callback_); }
using WriteBuffer::write;
void write(const std::string & str) { WriteBuffer::write(str.c_str(), str.size()); }
void write(std::string_view str) { WriteBuffer::write(str.data(), str.size()); }
void write(const char * str) { WriteBuffer::write(str, strlen(str)); }
void writeln(const std::string & str) { write(str); WriteBuffer::write("\n", 1); }
void writeln(std::string_view str) { write(str); WriteBuffer::write("\n", 1); }
void writeln(const char * str) { write(str); WriteBuffer::write("\n", 1); }
2012-03-09 15:46:52 +00:00
protected:
2021-11-10 22:58:56 +00:00
void nextImpl() override;
void socketSendBytes(const char * ptr, size_t size);
void socketSendStr(const std::string & str)
{
2024-05-09 00:07:04 +00:00
socketSendBytes(str.data(), str.size());
}
void socketSendStr(const char * ptr)
{
2024-05-09 00:07:04 +00:00
socketSendBytes(ptr, strlen(ptr));
}
Poco::Net::Socket & socket;
2017-05-28 14:29:40 +00:00
/** For error messages. It is necessary to receive this address in advance, because,
* for example, if the connection is broken, the address will not be received anymore
* (getpeername will return an error).
*/
Poco::Net::SocketAddress peer_address;
2023-02-25 00:18:34 +00:00
Poco::Net::SocketAddress our_address;
2023-03-03 19:30:43 +00:00
ProfileEvents::Event write_event;
2023-03-03 19:30:43 +00:00
private:
AsyncCallback async_callback;
std::string socket_description;
ssize_t socketSendBytesImpl(const char * ptr, size_t size);
2012-03-09 15:46:52 +00:00
};
}