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/ReadBuffer.h>
|
|
|
|
|
#include <DB/IO/BufferWithOwnMemory.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** Работает с готовым Poco::Net::Socket. Операции блокирующие.
|
|
|
|
|
*/
|
|
|
|
|
class ReadBufferFromPocoSocket : public BufferWithOwnMemory<ReadBuffer>
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
Poco::Net::Socket & socket;
|
|
|
|
|
|
|
|
|
|
bool nextImpl()
|
|
|
|
|
{
|
2013-10-20 06:04:51 +00:00
|
|
|
|
ssize_t bytes_read = 0;
|
|
|
|
|
|
|
|
|
|
/// Добавляем в эксепшены более подробную информацию.
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
bytes_read = socket.impl()->receiveBytes(internal_buffer.begin(), internal_buffer.size());
|
|
|
|
|
}
|
|
|
|
|
catch (Poco::Net::NetException & e)
|
|
|
|
|
{
|
|
|
|
|
throw Exception(e.displayText(), " while reading from socket (" + socket.address().toString() + ")", ErrorCodes::NETWORK_ERROR);
|
|
|
|
|
}
|
|
|
|
|
catch (Poco::TimeoutException & e)
|
|
|
|
|
{
|
|
|
|
|
throw Exception("Timeout exceeded while reading from socket (" + socket.address().toString() + ")", ErrorCodes::SOCKET_TIMEOUT);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-09 15:46:52 +00:00
|
|
|
|
if (bytes_read < 0)
|
2013-10-20 07:02:23 +00:00
|
|
|
|
throw Exception("Cannot read from socket (" + socket.address().toString() + ")", ErrorCodes::CANNOT_READ_FROM_SOCKET);
|
2012-03-09 15:46:52 +00:00
|
|
|
|
|
|
|
|
|
if (bytes_read)
|
|
|
|
|
working_buffer.resize(bytes_read);
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
ReadBufferFromPocoSocket(Poco::Net::Socket & socket_, size_t buf_size = DBMS_DEFAULT_BUFFER_SIZE)
|
|
|
|
|
: BufferWithOwnMemory<ReadBuffer>(buf_size), socket(socket_) {}
|
2012-05-09 08:16:09 +00:00
|
|
|
|
|
|
|
|
|
bool poll(size_t timeout_microseconds)
|
|
|
|
|
{
|
|
|
|
|
return offset() != buffer().size() || socket.poll(timeout_microseconds, Poco::Net::Socket::SELECT_READ | Poco::Net::Socket::SELECT_ERROR);
|
|
|
|
|
}
|
2012-03-09 15:46:52 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|