1
0
Fork 0
arangodb/arangod/Scheduler/SocketTask.h

163 lines
4.8 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
/// @author Achim Brandt
////////////////////////////////////////////////////////////////////////////////
#ifndef ARANGOD_SCHEDULER_SOCKET_TASK_H
#define ARANGOD_SCHEDULER_SOCKET_TASK_H 1
#include "Basics/Common.h"
#include "Scheduler/Task.h"
#include "Basics/Thread.h"
#include "Statistics/StatisticsAgent.h"
#ifdef _WIN32
#include "Basics/win-utils.h"
#endif
#include "Basics/socket-utils.h"
namespace arangodb {
namespace basics {
class StringBuffer;
}
namespace rest {
////////////////////////////////////////////////////////////////////////////////
/// @brief base class for input-output tasks from sockets
////////////////////////////////////////////////////////////////////////////////
class SocketTask : virtual public Task, public ConnectionStatisticsAgent {
private:
explicit SocketTask(SocketTask const&);
SocketTask& operator=(SocketTask const&);
private:
static size_t const READ_BLOCK_SIZE = 10000;
public:
// @brief constructs a new task with a given socket
SocketTask(TRI_socket_t, double);
protected:
// This method will close the underlying socket.
~SocketTask();
public:
// set a request timeout
void setKeepAliveTimeout(double);
protected:
//////////////////////////////////////////////////////////////////////////////
/// @brief fills the read buffer
///
/// The function should be called by the input task if the scheduler has
/// indicated that new data is available. It will return true, if data could
/// be read and false if the connection has been closed.
//////////////////////////////////////////////////////////////////////////////
virtual bool fillReadBuffer();
virtual bool handleRead() = 0; // called by handleEvent
virtual bool handleWrite(); // called by handleEvent
//////////////////////////////////////////////////////////////////////////////
/// @brief called if write buffer has been sent
///
/// This called is called if the current write buffer has been sent
/// completly to the client.
//////////////////////////////////////////////////////////////////////////////
virtual void completedWriteBuffer() = 0;
// handles a keep-alive timeout
virtual void handleTimeout() = 0;
protected:
//////////////////////////////////////////////////////////////////////////////
/// @brief sets an active write buffer
//////////////////////////////////////////////////////////////////////////////
void setWriteBuffer(basics::StringBuffer*, TRI_request_statistics_t*);
//////////////////////////////////////////////////////////////////////////////
/// @brief checks for presence of an active write buffer
//////////////////////////////////////////////////////////////////////////////
bool hasWriteBuffer() const;
protected:
bool setup(Scheduler*, EventLoop) override;
/// @brief cleans up the task by unregistering all watchers
void cleanup() override;
// calls handleRead and handleWrite
bool handleEvent(EventToken token, EventType) override;
protected:
/// @brief event for keep-alive timeout
EventToken _keepAliveWatcher;
/// @brief event for read
EventToken _readWatcher;
/// @brief event for write
EventToken _writeWatcher;
/// @brief communication socket
TRI_socket_t _commSocket;
/// @brief keep-alive timeout in seconds
double _keepAliveTimeout;
/// @brief the current write buffer
basics::StringBuffer* _writeBuffer;
/// @brief the current write buffer statistics
TRI_request_statistics_t* _writeBufferStatistics;
/// @brief number of bytes already written
size_t _writeLength;
/// @brief read buffer
/// The function fillReadBuffer stores the data in this buffer.
basics::StringBuffer* _readBuffer;
/// @brief client has closed the connection
bool _clientClosed;
private:
//////////////////////////////////////////////////////////////////////////////
/// @brief current thread identifier
//////////////////////////////////////////////////////////////////////////////
TRI_tid_t _tid;
};
}
}
#endif