From 80f860dbfc114005ddc5382581d8942f4ff4511c Mon Sep 17 00:00:00 2001 From: Marco De Marco <demarco@oats.inaf.it> Date: Fri, 6 Dec 2013 15:42:08 +0100 Subject: [PATCH] Request response methods added --- src/PlainSession.cpp | 188 ++++++++++++++++---------------- src/PlainSession.h | 18 ++-- src/SSLSession.cpp | 248 ++++++++++++++++++++++--------------------- src/SSLSession.h | 32 +++--- src/Session.cpp | 220 ++++++++++++++++++++------------------ src/Session.h | 65 +++++++----- 6 files changed, 403 insertions(+), 368 deletions(-) diff --git a/src/PlainSession.cpp b/src/PlainSession.cpp index daa834a..5b76b85 100644 --- a/src/PlainSession.cpp +++ b/src/PlainSession.cpp @@ -69,97 +69,105 @@ void PlainSession::start() { DEBUG_STREAM << "PlainSession::start()" << endl; -// m_remoteEndpoint = boost::lexical_cast<std::string>( -// m_plainSocket.remote_endpoint()); -// -// INFO_STREAM << "PlainSession::start() Connection from " -// << m_remoteEndpoint << endl; -// -// m_protocolManager_sp->setRemoteEndpoint(m_remoteEndpoint); -// -// startReadRequestHeader(); + m_remoteEndpoint = boost::lexical_cast<std::string>( + m_plainSocket.remote_endpoint()); + + INFO_STREAM << "PlainSession::start() Connection from " + << m_remoteEndpoint << endl; + + m_protocolManager_sp->setRemoteEndpoint(m_remoteEndpoint); + + startReadRequestHeader(); +} + +//============================================================================== +// PlainSession::startReadRequestHeader() +//============================================================================== +void PlainSession::startReadRequestHeader() +{ + DEBUG_STREAM << "PlainSession::startReadRequestHeader()" << endl; + + m_readBuff.resize(HEADER_SIZE); + + boost::asio::async_read(m_plainSocket, boost::asio::buffer(m_readBuff), + m_strand.wrap(boost::bind(&PlainSession::handleReadRequestHeader, + shared_from_this(), boost::asio::placeholders::error))); +} + +//============================================================================== +// PlainSession::startReadRequestBody() +//============================================================================== +void PlainSession::startReadRequestBody(boost::uint32_t bodySize) +{ + DEBUG_STREAM << "PlainSession::startReadRequestBody()" << endl; + + m_readBuff.resize(HEADER_SIZE + bodySize); + + boost::asio::mutable_buffers_1 mutableBuffer = + boost::asio::buffer(&m_readBuff[HEADER_SIZE], bodySize); + + #ifdef VERBOSE_DEBUG + INFO_STREAM << "PlainSession::startReadRequestBody() " + << m_remoteEndpoint << " >>>> " << bodySize << " BYTE" << endl; + #endif + + boost::asio::async_read(m_plainSocket, mutableBuffer, + m_strand.wrap(boost::bind(&PlainSession::handleReadRequestBody, + shared_from_this(), boost::asio::placeholders::error))); } -////============================================================================== -//// PlainSession::startReadRequestHeader() -////============================================================================== -//void PlainSession::startReadRequestHeader() -//{ -// DEBUG_STREAM << "PlainSession::startReadRequestHeader()" << endl; -// -// m_readBuff.resize(HEADER_SIZE); -// -// boost::asio::async_read(m_plainSocket, boost::asio::buffer(m_readBuff), -// m_strand.wrap(boost::bind(&PlainSession::handleReadRequestHeader, -// shared_from_this(), boost::asio::placeholders::error))); -//} -// -////============================================================================== -//// PlainSession::startReadRequestBody() -////============================================================================== -//void PlainSession::startReadRequestBody(boost::uint32_t bodySize) -//{ -// DEBUG_STREAM << "PlainSession::startReadRequestBody()" << endl; -// -// m_readBuff.resize(HEADER_SIZE + bodySize); -// -// boost::asio::mutable_buffers_1 mutableBuffer = -// boost::asio::buffer(&m_readBuff[HEADER_SIZE], bodySize); -// -// #ifdef VERBOSE_DEBUG -// INFO_STREAM << "PlainSession::startReadRequestBody() " -// << m_remoteEndpoint << " >>>> " << bodySize << " BYTE" << endl; -// #endif -// -// boost::asio::async_read(m_plainSocket, mutableBuffer, -// m_strand.wrap(boost::bind(&PlainSession::handleReadRequestBody, -// shared_from_this(), boost::asio::placeholders::error))); -//} -// -////============================================================================== -//// PlainSession::startWriteResponse() -////============================================================================== -//void PlainSession::startWriteResponse() -//{ -// DEBUG_STREAM << "PlainSession::startWriteResponse()" << endl; -// -// try -// { -// RequestSP request_sp(new Request); -// -// request_sp->ParseFromArray(&m_readBuff[HEADER_SIZE], m_readBuff.size() - HEADER_SIZE); -// -// ResponseSP response_sp = m_protocolManager_sp->prepareResponse(request_sp); -// -// boost::uint32_t bodySize = response_sp->ByteSize(); -// -// std::vector<boost::uint8_t> writeBuff; -// writeBuff.resize(HEADER_SIZE + bodySize); -// -// encodeHeader(writeBuff, bodySize); -// -// response_sp->SerializeToArray(&writeBuff[HEADER_SIZE], bodySize); -// -// #ifdef VERBOSE_DEBUG -// INFO_STREAM << "PlainSession::startWriteResponse() " -// << m_remoteEndpoint << " <<<< " << bodySize << " byte" << endl; -// #endif -// -// boost::asio::async_write(m_plainSocket, boost::asio::buffer(writeBuff), -// m_strand.wrap(boost::bind(&PlainSession::handleWriteResponse, -// shared_from_this(), boost::asio::placeholders::error))); -// } -// catch(std::exception& ec) -// { -// ERROR_STREAM << "SSLSession::startWriteResponse() " -// << ec.what() << " from " << m_remoteEndpoint << endl; -// } -// catch(...) -// { -// ERROR_STREAM << "SSLSession::startWriteResponse() unknown error from " -// << m_remoteEndpoint << endl; -// -// } -//} +//============================================================================== +// PlainSession::startWriteResponse() +//============================================================================== +void PlainSession::startWriteResponse() +{ + DEBUG_STREAM << "PlainSession::startWriteResponse()" << endl; + + try + { + RequestSP request_sp(new Request); + + request_sp->ParseFromArray(&m_readBuff[HEADER_SIZE], m_readBuff.size() - HEADER_SIZE); + + ResponseSP response_sp = m_protocolManager_sp->prepareResponse(request_sp); + + boost::uint32_t bodySize = response_sp->ByteSize(); + + std::vector<boost::uint8_t> writeBuff; + writeBuff.resize(HEADER_SIZE + bodySize); + + encodeHeader(writeBuff, bodySize); + + response_sp->SerializeToArray(&writeBuff[HEADER_SIZE], bodySize); + + #ifdef VERBOSE_DEBUG + INFO_STREAM << "PlainSession::startWriteResponse() " + << m_remoteEndpoint << " <<<< " << bodySize << " byte" << endl; + #endif + + boost::asio::async_write(m_plainSocket, boost::asio::buffer(writeBuff), + m_strand.wrap(boost::bind(&PlainSession::handleWriteResponse, + shared_from_this(), boost::asio::placeholders::error))); + } + catch(std::exception& ec) + { + ERROR_STREAM << "SSLSession::startWriteResponse() " + << ec.what() << " from " << m_remoteEndpoint << endl; + } + catch(...) + { + ERROR_STREAM << "SSLSession::startWriteResponse() unknown error from " + << m_remoteEndpoint << endl; + + } +} + +//============================================================================== +// PlainSession::startWriteData() +//============================================================================== +void PlainSession::startWriteData() +{ + DEBUG_STREAM << "PlainSession::startWriteData()" << endl; +} } //namespace \ No newline at end of file diff --git a/src/PlainSession.h b/src/PlainSession.h index 6a692d6..0756f40 100644 --- a/src/PlainSession.h +++ b/src/PlainSession.h @@ -48,14 +48,16 @@ public: virtual void start(); protected: -////------------------------------------------------------------------------------ -//// [Protected] Request response methods -////------------------------------------------------------------------------------ -// virtual void startReadRequestHeader(); -// -// virtual void startReadRequestBody(boost::uint32_t); -// -// virtual void startWriteResponse(); +//------------------------------------------------------------------------------ +// [Protected] Request response methods +//------------------------------------------------------------------------------ + virtual void startReadRequestHeader(); + + virtual void startReadRequestBody(boost::uint32_t); + + virtual void startWriteResponse(); + + virtual void startWriteData(); //------------------------------------------------------------------------------ // [Protected] Class variables diff --git a/src/SSLSession.cpp b/src/SSLSession.cpp index 9793c2f..705bc0f 100644 --- a/src/SSLSession.cpp +++ b/src/SSLSession.cpp @@ -74,127 +74,135 @@ void SSLSession::start() { DEBUG_STREAM << "SSLSession::start()" << endl; -// m_remoteEndpoint = boost::lexical_cast<std::string>( -// m_sslSocket.lowest_layer().remote_endpoint()); -// -// INFO_STREAM << "SSLSession::start() Connection from " -// << m_remoteEndpoint << endl; -// -// m_protocolManager_sp->setRemoteEndpoint(m_remoteEndpoint); -// -// startHandShake(); + m_remoteEndpoint = boost::lexical_cast<std::string>( + m_sslSocket.lowest_layer().remote_endpoint()); + + INFO_STREAM << "SSLSession::start() Connection from " + << m_remoteEndpoint << endl; + + m_protocolManager_sp->setRemoteEndpoint(m_remoteEndpoint); + + startHandShake(); } -////============================================================================== -//// SSLSession::startHandShake() -////============================================================================== -//void SSLSession::startHandShake() -//{ -// DEBUG_STREAM << "SSLSession::startHandShake()" << endl; -// -// m_sslSocket.async_handshake(boost::asio::ssl::stream_base::server, -// boost::bind(&SSLSession::handleHandShake, shared_from_this(), -// boost::asio::placeholders::error)); -//} -// -////============================================================================== -//// SSLSession::handleRequest() -////============================================================================== -//void SSLSession::handleHandShake(const boost::system::error_code& errorCode) -//{ -// DEBUG_STREAM << "SSLSession::handleHandShake()" << endl; -// -// if(!errorCode) -// { -// startReadRequestHeader(); -// } -// else -// { -// ERROR_STREAM << "SSLSession::handleHandShake() error " -// << errorCode.message() << " from " << m_remoteEndpoint << endl; -// } -//} -// -////============================================================================== -//// SSLSession::startReadRequestHeader() -////============================================================================== -//void SSLSession::startReadRequestHeader() -//{ -// DEBUG_STREAM << "SSLSession::startReadRequestHeader()" << endl; -// -// m_readBuff.resize(HEADER_SIZE); -// -// boost::asio::async_read(m_sslSocket, boost::asio::buffer(m_readBuff), -// m_strand.wrap(boost::bind(&SSLSession::handleReadRequestHeader, -// shared_from_this(), boost::asio::placeholders::error))); -//} -// -////============================================================================== -//// SSLSession::startReadRequestBody() -////============================================================================== -//void SSLSession::startReadRequestBody(boost::uint32_t bodySize) -//{ -// DEBUG_STREAM << "SSLSession::startReadRequestBody()" << endl; -// -// m_readBuff.resize(HEADER_SIZE + bodySize); -// -// boost::asio::mutable_buffers_1 mutableBuffer = -// boost::asio::buffer(&m_readBuff[HEADER_SIZE], bodySize); -// -// #ifdef VERBOSE_DEBUG -// INFO_STREAM << "SSLSession::startReadRequestBody() " -// << m_remoteEndpoint << " >>>> " << bodySize << " byte" << endl; -// #endif -// -// boost::asio::async_read(m_sslSocket, mutableBuffer, -// m_strand.wrap(boost::bind(&SSLSession::handleReadRequestBody, -// shared_from_this(), boost::asio::placeholders::error))); -//} -// -////============================================================================== -//// SSLSession::startWriteResponse() -////============================================================================== -//void SSLSession::startWriteResponse() -//{ -// DEBUG_STREAM << "SSLSession::startWriteResponse()" << endl; -// -// try -// { -// RequestSP request_sp(new Request); -// -// request_sp->ParseFromArray(&m_readBuff[HEADER_SIZE], -// m_readBuff.size() - HEADER_SIZE); -// -// ResponseSP response_sp = m_protocolManager_sp->prepareResponse(request_sp); -// -// boost::uint32_t bodySize = response_sp->ByteSize(); -// -// std::vector<boost::uint8_t> writeBuff; -// writeBuff.resize(HEADER_SIZE + bodySize); -// -// encodeHeader(writeBuff, bodySize); -// -// response_sp->SerializeToArray(&writeBuff[HEADER_SIZE], bodySize); -// -// #ifdef VERBOSE_DEBUG -// INFO_STREAM << "SSLSession::startWriteResponse() " -// << m_remoteEndpoint << " <<<< " << bodySize << " byte" << endl; -// #endif -// -// boost::asio::async_write(m_sslSocket, boost::asio::buffer(writeBuff), -// m_strand.wrap(boost::bind(&SSLSession::handleWriteResponse, -// shared_from_this(), boost::asio::placeholders::error))); -// } -// catch(std::exception& ec) -// { -// ERROR_STREAM << "SSLSession::startWriteResponse() " -// << ec.what() << " from " << m_remoteEndpoint << endl; -// } -// catch(...) -// { -// ERROR_STREAM << "SSLSession::startWriteResponse() unknown error from " -// << m_remoteEndpoint << endl; -// } -//} +//============================================================================== +// SSLSession::startHandShake() +//============================================================================== +void SSLSession::startHandShake() +{ + DEBUG_STREAM << "SSLSession::startHandShake()" << endl; + + m_sslSocket.async_handshake(boost::asio::ssl::stream_base::server, + boost::bind(&SSLSession::handleHandShake, shared_from_this(), + boost::asio::placeholders::error)); +} + +//============================================================================== +// SSLSession::handleRequest() +//============================================================================== +void SSLSession::handleHandShake(const boost::system::error_code& errorCode) +{ + DEBUG_STREAM << "SSLSession::handleHandShake()" << endl; + + if(!errorCode) + { + startReadRequestHeader(); + } + else + { + ERROR_STREAM << "SSLSession::handleHandShake() error " + << errorCode.message() << " from " << m_remoteEndpoint << endl; + } +} + +//============================================================================== +// SSLSession::startReadRequestHeader() +//============================================================================== +void SSLSession::startReadRequestHeader() +{ + DEBUG_STREAM << "SSLSession::startReadRequestHeader()" << endl; + + m_readBuff.resize(HEADER_SIZE); + + boost::asio::async_read(m_sslSocket, boost::asio::buffer(m_readBuff), + m_strand.wrap(boost::bind(&SSLSession::handleReadRequestHeader, + shared_from_this(), boost::asio::placeholders::error))); +} + +//============================================================================== +// SSLSession::startReadRequestBody() +//============================================================================== +void SSLSession::startReadRequestBody(boost::uint32_t bodySize) +{ + DEBUG_STREAM << "SSLSession::startReadRequestBody()" << endl; + + m_readBuff.resize(HEADER_SIZE + bodySize); + + boost::asio::mutable_buffers_1 mutableBuffer = + boost::asio::buffer(&m_readBuff[HEADER_SIZE], bodySize); + + #ifdef VERBOSE_DEBUG + INFO_STREAM << "SSLSession::startReadRequestBody() " + << m_remoteEndpoint << " >>>> " << bodySize << " byte" << endl; + #endif + + boost::asio::async_read(m_sslSocket, mutableBuffer, + m_strand.wrap(boost::bind(&SSLSession::handleReadRequestBody, + shared_from_this(), boost::asio::placeholders::error))); +} + +//============================================================================== +// SSLSession::startWriteResponse() +//============================================================================== +void SSLSession::startWriteResponse() +{ + DEBUG_STREAM << "SSLSession::startWriteResponse()" << endl; + + try + { + RequestSP request_sp(new Request); + + request_sp->ParseFromArray(&m_readBuff[HEADER_SIZE], + m_readBuff.size() - HEADER_SIZE); + + ResponseSP response_sp = m_protocolManager_sp->prepareResponse(request_sp); + + boost::uint32_t bodySize = response_sp->ByteSize(); + + std::vector<boost::uint8_t> writeBuff; + writeBuff.resize(HEADER_SIZE + bodySize); + + encodeHeader(writeBuff, bodySize); + + response_sp->SerializeToArray(&writeBuff[HEADER_SIZE], bodySize); + + #ifdef VERBOSE_DEBUG + INFO_STREAM << "SSLSession::startWriteResponse() " + << m_remoteEndpoint << " <<<< " << bodySize << " byte" << endl; + #endif + + boost::asio::async_write(m_sslSocket, boost::asio::buffer(writeBuff), + m_strand.wrap(boost::bind(&SSLSession::handleWriteResponse, + shared_from_this(), boost::asio::placeholders::error))); + } + catch(std::exception& ec) + { + ERROR_STREAM << "SSLSession::startWriteResponse() " + << ec.what() << " from " << m_remoteEndpoint << endl; + } + catch(...) + { + ERROR_STREAM << "SSLSession::startWriteResponse() unknown error from " + << m_remoteEndpoint << endl; + } +} + +//============================================================================== +// SSLSession::startWriteData() +//============================================================================== +void SSLSession::startWriteData() +{ + DEBUG_STREAM << "SSLSession::startWriteData()" << endl; +} } //namespace diff --git a/src/SSLSession.h b/src/SSLSession.h index 50aeb56..4eb9022 100644 --- a/src/SSLSession.h +++ b/src/SSLSession.h @@ -54,21 +54,23 @@ public: virtual void start(); protected: -////------------------------------------------------------------------------------ -//// [Protected] SSL handshake initialization methods -////------------------------------------------------------------------------------ -// virtual void startHandShake(); -// -// virtual void handleHandShake(const boost::system::error_code&); -// -////------------------------------------------------------------------------------ -//// [Protected] Request response methods -////------------------------------------------------------------------------------ -// virtual void startReadRequestHeader(); -// -// virtual void startReadRequestBody(boost::uint32_t); -// -// virtual void startWriteResponse(); +//------------------------------------------------------------------------------ +// [Protected] SSL handshake initialization methods +//------------------------------------------------------------------------------ + virtual void startHandShake(); + + virtual void handleHandShake(const boost::system::error_code&); + +//------------------------------------------------------------------------------ +// [Protected] Request response methods +//------------------------------------------------------------------------------ + virtual void startReadRequestHeader(); + + virtual void startReadRequestBody(boost::uint32_t); + + virtual void startWriteResponse(); + + virtual void startWriteData(); //------------------------------------------------------------------------------ // [Protected] Class variables diff --git a/src/Session.cpp b/src/Session.cpp index 93b467c..2e3ce04 100644 --- a/src/Session.cpp +++ b/src/Session.cpp @@ -26,111 +26,119 @@ Session::~Session() DEBUG_STREAM << "Session::~Session()" << endl; } -////============================================================================== -//// Session::handleReadRequestHeader() -////============================================================================== -//void Session::handleReadRequestHeader(const boost::system::error_code& errorCode) -//{ -// DEBUG_STREAM << "Session::handleReadRequestHeader()" << endl; -// -// if(!errorCode) -// { -// boost::uint32_t bodySize = decodeHeader(m_readBuff); -// -// startReadRequestBody(bodySize); -// } -// else if(errorCode == boost::asio::error::eof) -// { -// DEBUG_STREAM << "Session::handleReadRequestBody() end of file from " -// << m_remoteEndpoint << endl; -// } -// else -// { -// ERROR_STREAM << "Session::handleReadRequestHeader() " -// << errorCode.message() << " from " << m_remoteEndpoint << endl; -// } -//} -// -////============================================================================== -//// Session::handleReadRequestBody() -////============================================================================== -//void Session::handleReadRequestBody(const boost::system::error_code& errorCode) -//{ -// DEBUG_STREAM << "Session::handleReadRequestBody()" << endl; -// -// if(!errorCode) -// { -// startWriteResponse(); -// } -// else if(errorCode == boost::asio::error::eof) -// { -// DEBUG_STREAM << "Session::handleReadRequestBody() end of file from" -// << m_remoteEndpoint << endl; -// } -// else -// { -// ERROR_STREAM << "Session::handleReadRequestBody() " -// << errorCode.message() << " from " << m_remoteEndpoint << endl; -// } -//} -// -////============================================================================== -//// Session::handleWriteResponse() -////============================================================================== -//void Session::handleWriteResponse(const boost::system::error_code& errorCode) -//{ -// DEBUG_STREAM << "Session::handleWriteResponse()" << endl; -// -// if(!errorCode) -// { -// startReadRequestHeader(); -// } -// else if(errorCode == boost::asio::error::eof) -// { -// DEBUG_STREAM << "Session::handleWriteResponse() end of file from " -// << m_remoteEndpoint << endl; -// } -// else -// { -// ERROR_STREAM << "Session::handleWriteResponse() " -// << errorCode.message() << " from " << m_remoteEndpoint << endl; -// } -//} -// -////============================================================================== -//// Session::encodeHeader() -////============================================================================== -//void Session::encodeHeader(std::vector<boost::uint8_t>& buf, boost::uint32_t size) -// throw(std::runtime_error) -//{ -// DEBUG_STREAM << "Session::encodeHeader()" << endl; -// -// if(buf.size() < HEADER_SIZE) -// throw std::runtime_error("Buffer to small to contain header!"); -// -// buf[0] = static_cast<boost::uint8_t>((size >> 24) & 0xFF); -// buf[1] = static_cast<boost::uint8_t>((size >> 16) & 0xFF); -// buf[2] = static_cast<boost::uint8_t>((size >> 8) & 0xFF); -// buf[3] = static_cast<boost::uint8_t>(size & 0xFF); -//} -// -////============================================================================== -//// Session::decodeHeader() -////============================================================================== -//boost::uint32_t Session::decodeHeader(std::vector<boost::uint8_t>& buf) -// throw(std::runtime_error) -//{ -// DEBUG_STREAM << "Session::decodeHeader()" << endl; -// -// if(buf.size() < HEADER_SIZE) -// throw std::runtime_error("Buffer to small to contain header!"); -// -// boost::uint32_t size = 0; -// -// for (unsigned i = 0; i < HEADER_SIZE; ++i) -// size = size * 256 + (static_cast<unsigned>(buf[i]) & 0xFF); -// -// return size; -//} +//============================================================================== +// Session::handleReadRequestHeader() +//============================================================================== +void Session::handleReadRequestHeader(const boost::system::error_code& errorCode) +{ + DEBUG_STREAM << "Session::handleReadRequestHeader()" << endl; + + if(!errorCode) + { + boost::uint32_t bodySize = decodeHeader(m_readBuff); + + startReadRequestBody(bodySize); + } + else if(errorCode == boost::asio::error::eof) + { + DEBUG_STREAM << "Session::handleReadRequestBody() end of file from " + << m_remoteEndpoint << endl; + } + else + { + ERROR_STREAM << "Session::handleReadRequestHeader() " + << errorCode.message() << " from " << m_remoteEndpoint << endl; + } +} + +//============================================================================== +// Session::handleReadRequestBody() +//============================================================================== +void Session::handleReadRequestBody(const boost::system::error_code& errorCode) +{ + DEBUG_STREAM << "Session::handleReadRequestBody()" << endl; + + if(!errorCode) + { + startWriteResponse(); + } + else if(errorCode == boost::asio::error::eof) + { + DEBUG_STREAM << "Session::handleReadRequestBody() end of file from" + << m_remoteEndpoint << endl; + } + else + { + ERROR_STREAM << "Session::handleReadRequestBody() " + << errorCode.message() << " from " << m_remoteEndpoint << endl; + } +} + +//============================================================================== +// Session::handleWriteResponse() +//============================================================================== +void Session::handleWriteResponse(const boost::system::error_code& errorCode) +{ + DEBUG_STREAM << "Session::handleWriteResponse()" << endl; + + if(!errorCode) + { + startReadRequestHeader(); + } + else if(errorCode == boost::asio::error::eof) + { + DEBUG_STREAM << "Session::handleWriteResponse() end of file from " + << m_remoteEndpoint << endl; + } + else + { + ERROR_STREAM << "Session::handleWriteResponse() " + << errorCode.message() << " from " << m_remoteEndpoint << endl; + } +} + +//============================================================================== +// Session::handleWriteData() +//============================================================================== +void Session::handleWriteData(const boost::system::error_code&) +{ + DEBUG_STREAM << "Session::handleWriteData()" << endl; +} + +//============================================================================== +// Session::encodeHeader() +//============================================================================== +void Session::encodeHeader(std::vector<boost::uint8_t>& buf, boost::uint32_t size) + throw(std::runtime_error) +{ + DEBUG_STREAM << "Session::encodeHeader()" << endl; + + if(buf.size() < HEADER_SIZE) + throw std::runtime_error("Buffer to small to contain header!"); + + buf[0] = static_cast<boost::uint8_t>((size >> 24) & 0xFF); + buf[1] = static_cast<boost::uint8_t>((size >> 16) & 0xFF); + buf[2] = static_cast<boost::uint8_t>((size >> 8) & 0xFF); + buf[3] = static_cast<boost::uint8_t>(size & 0xFF); +} + +//============================================================================== +// Session::decodeHeader() +//============================================================================== +boost::uint32_t Session::decodeHeader(std::vector<boost::uint8_t>& buf) + throw(std::runtime_error) +{ + DEBUG_STREAM << "Session::decodeHeader()" << endl; + + if(buf.size() < HEADER_SIZE) + throw std::runtime_error("Buffer to small to contain header!"); + + boost::uint32_t size = 0; + + for (unsigned i = 0; i < HEADER_SIZE; ++i) + size = size * 256 + (static_cast<unsigned>(buf[i]) & 0xFF); + + return size; +} } //namespace diff --git a/src/Session.h b/src/Session.h index 8cc21be..69b9fef 100644 --- a/src/Session.h +++ b/src/Session.h @@ -41,35 +41,42 @@ public: virtual void start() = 0; protected: -////------------------------------------------------------------------------------ -//// [Protected] Read request header methods -////------------------------------------------------------------------------------ -// virtual void startReadRequestHeader() = 0; -// -// virtual void handleReadRequestHeader(const boost::system::error_code&); -// -////------------------------------------------------------------------------------ -//// [Protected] Read request body methods -////------------------------------------------------------------------------------ -// virtual void startReadRequestBody(boost::uint32_t) = 0; -// -// virtual void handleReadRequestBody(const boost::system::error_code&); -// -////------------------------------------------------------------------------------ -//// [Protected] Write response methods -////------------------------------------------------------------------------------ -// virtual void startWriteResponse() = 0; -// -// virtual void handleWriteResponse(const boost::system::error_code&); -// -////------------------------------------------------------------------------------ -//// [Protected] Encode decode header methods -////------------------------------------------------------------------------------ -// virtual void encodeHeader(std::vector<boost::uint8_t>&, boost::uint32_t) -// throw(std::runtime_error); -// -// virtual boost::uint32_t decodeHeader(std::vector<boost::uint8_t>&) -// throw(std::runtime_error); +//------------------------------------------------------------------------------ +// [Protected] Read request header methods +//------------------------------------------------------------------------------ + virtual void startReadRequestHeader() = 0; + + virtual void handleReadRequestHeader(const boost::system::error_code&); + +//------------------------------------------------------------------------------ +// [Protected] Read request body methods +//------------------------------------------------------------------------------ + virtual void startReadRequestBody(boost::uint32_t) = 0; + + virtual void handleReadRequestBody(const boost::system::error_code&); + +//------------------------------------------------------------------------------ +// [Protected] Write response methods +//------------------------------------------------------------------------------ + virtual void startWriteResponse() = 0; + + virtual void handleWriteResponse(const boost::system::error_code&); + +//------------------------------------------------------------------------------ +// [Protected] Write data methods +//------------------------------------------------------------------------------ + virtual void startWriteData() = 0; + + virtual void handleWriteData(const boost::system::error_code&); + +//------------------------------------------------------------------------------ +// [Protected] Encode decode header methods +//------------------------------------------------------------------------------ + virtual void encodeHeader(std::vector<boost::uint8_t>&, boost::uint32_t) + throw(std::runtime_error); + + virtual boost::uint32_t decodeHeader(std::vector<boost::uint8_t>&) + throw(std::runtime_error); //------------------------------------------------------------------------------ // [Protected] Class variables -- GitLab