diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-07-13 07:17:57 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-07-13 07:18:39 (GMT) |
commit | 6ca206b0d0645e50a8a2c59ebd134f9c0f164b9b (patch) | |
tree | 1c4d785f79941b07e7c7d7473fe98d09229b5fa6 /Limber | |
parent | 72858ab262a16aa8db209c2898ab0d3c951a9829 (diff) | |
download | swift-contrib-6ca206b0d0645e50a8a2c59ebd134f9c0f164b9b.zip swift-contrib-6ca206b0d0645e50a8a2c59ebd134f9c0f164b9b.tar.bz2 |
Server stream header support.
Diffstat (limited to 'Limber')
-rw-r--r-- | Limber/main.cpp | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/Limber/main.cpp b/Limber/main.cpp index 6dad151..969a390 100644 --- a/Limber/main.cpp +++ b/Limber/main.cpp @@ -9,6 +9,7 @@ #include <boost/thread.hpp> #include "Swiften/Base/ByteArray.h" +#include "Swiften/Base/IDGenerator.h" #include "Swiften/EventLoop/MainEventLoop.h" #include "Swiften/EventLoop/SimpleEventLoop.h" #include "Swiften/Network/ConnectionServer.h" @@ -19,6 +20,8 @@ using namespace Swift; +static const size_t BUFFER_SIZE = 4096; + // A reference-counted non-modifiable buffer class. class SharedBuffer { public: @@ -58,17 +61,39 @@ class IncomingBoostConnection : public IncomingConnection, public boost::enable_ boost::asio::placeholders::error)); } + void start() { + read(); + } + private: - IncomingBoostConnection(boost::asio::io_service& ioService) : socket_(ioService) { + IncomingBoostConnection(boost::asio::io_service& ioService) : socket_(ioService), readBuffer_(BUFFER_SIZE) { + } + + void read() { + socket_.async_read_some( + boost::asio::buffer(readBuffer_), + boost::bind(&IncomingBoostConnection::handleDataRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + } + + void handleDataRead(const boost::system::error_code& error, size_t bytesTransferred) { + if (!error) { + MainEventLoop::postEvent(boost::bind(boost::ref(onDataRead), ByteArray(&readBuffer_[0], bytesTransferred)), this); + read(); + } + else if (error != boost::asio::error::operation_aborted) { + //MainEventLoop::postEvent(boost::bind(boost::ref(onError), ReadError), this); + } } void handleDataWritten(const boost::system::error_code& error) { - if (error) { - std::cerr << "ERROR: Unable to write data to socket" << std::endl; + if (error && error != boost::asio::error::operation_aborted) { + //std::cerr << "ERROR: Unable to write data to socket" << std::endl; + //MainEventLoop::postEvent(boost::bind(boost::ref(onError), ReadError), this); } } boost::asio::ip::tcp::socket socket_; + std::vector<char> readBuffer_; }; class BoostConnectionServer : public ConnectionServer { @@ -87,6 +112,7 @@ class BoostConnectionServer : public ConnectionServer { void handleAccept(IncomingBoostConnection::pointer newConnection, const boost::system::error_code& error) { if (!error) { MainEventLoop::postEvent(boost::bind(boost::ref(onNewConnection), newConnection), this); + newConnection->start(); acceptNextConnection(); } } @@ -107,7 +133,7 @@ class Server { private: void handleNewConnection(boost::shared_ptr<IncomingConnection> c) { - ServerFromClientSession* session = new ServerFromClientSession(c, &payloadParserFactories_, &payloadSerializers_); + ServerFromClientSession* session = new ServerFromClientSession(idGenerator_.generateID(), c, &payloadParserFactories_, &payloadSerializers_); serverFromClientSessions_.push_back(session); session->onSessionFinished.connect(boost::bind(&Server::handleSessionFinished, this, session)); } @@ -118,6 +144,7 @@ class Server { } private: + IDGenerator idGenerator_; BoostIOServiceThread boostIOServiceThread_; BoostConnectionServer* serverFromClientConnectionServer_; std::vector<ServerFromClientSession*> serverFromClientSessions_; |