2012-03-09 15:46:52 +00:00
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <Poco/Net/Socket.h>
|
2013-10-20 06:04:51 +00:00
|
|
|
|
#include <Poco/Net/NetException.h>
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
|
|
|
|
#include <DB/Core/Exception.h>
|
|
|
|
|
#include <DB/Core/ErrorCodes.h>
|
|
|
|
|
|
|
|
|
|
#include <DB/IO/WriteBuffer.h>
|
|
|
|
|
#include <DB/IO/BufferWithOwnMemory.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Работает с готовым Poco::Net::Socket. Операции блокирующие.
|
|
|
|
|
*/
|
|
|
|
|
class WriteBufferFromPocoSocket : public BufferWithOwnMemory<WriteBuffer>
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
Poco::Net::Socket & socket;
|
2013-11-20 00:49:47 +00:00
|
|
|
|
|
|
|
|
|
/** Для сообщений об ошибках. Нужно получать этот адрес заранее, так как,
|
|
|
|
|
* например, если соединение разорвано, то адрес уже будет получить нельзя
|
|
|
|
|
* (getpeername вернёт ошибку).
|
|
|
|
|
*/
|
|
|
|
|
Poco::Net::SocketAddress peer_address;
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
|
|
|
|
void nextImpl()
|
|
|
|
|
{
|
|
|
|
|
if (!offset())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
size_t bytes_written = 0;
|
2013-11-02 05:39:07 +00:00
|
|
|
|
while (bytes_written < offset())
|
2012-03-09 15:46:52 +00:00
|
|
|
|
{
|
2013-10-20 06:04:51 +00:00
|
|
|
|
ssize_t res = 0;
|
|
|
|
|
|
|
|
|
|
/// Добавляем в эксепшены более подробную информацию.
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
res = socket.impl()->sendBytes(working_buffer.begin() + bytes_written, offset() - bytes_written);
|
|
|
|
|
}
|
|
|
|
|
catch (Poco::Net::NetException & e)
|
|
|
|
|
{
|
2013-11-20 00:49:47 +00:00
|
|
|
|
throw Exception(e.displayText() + " while writing to socket (" + peer_address.toString() + ")", ErrorCodes::NETWORK_ERROR);
|
2013-10-20 06:04:51 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Poco::TimeoutException & e)
|
|
|
|
|
{
|
2013-11-20 00:49:47 +00:00
|
|
|
|
throw Exception("Timeout exceeded while writing to socket (" + peer_address.toString() + ")", ErrorCodes::SOCKET_TIMEOUT);
|
2013-10-20 06:04:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
if (res < 0)
|
2013-11-20 00:49:47 +00:00
|
|
|
|
throw Exception("Cannot write to socket (" + peer_address.toString() + ")", ErrorCodes::CANNOT_WRITE_TO_SOCKET);
|
2012-03-09 15:46:52 +00:00
|
|
|
|
bytes_written += res;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
WriteBufferFromPocoSocket(Poco::Net::Socket & socket_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE)
|
2013-11-20 00:49:47 +00:00
|
|
|
|
: BufferWithOwnMemory<WriteBuffer>(buf_size), socket(socket_), peer_address(socket.peerAddress())
|
|
|
|
|
{
|
|
|
|
|
}
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
|
|
|
|
~WriteBufferFromPocoSocket()
|
|
|
|
|
{
|
2013-11-18 17:17:45 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2012-03-09 15:46:52 +00:00
|
|
|
|
next();
|
2013-11-18 17:17:45 +00:00
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
2013-11-18 19:18:03 +00:00
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
2013-11-18 17:17:45 +00:00
|
|
|
|
}
|
2012-03-09 15:46:52 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|