summaryrefslogtreecommitdiffstats
path: root/Limber
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-07-14 17:27:32 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-07-14 17:27:32 (GMT)
commitee3a46975986865fe5064f9d87a27277fc6c235a (patch)
tree51bae2ed1669ca881f761ca7547fbaf22432ae12 /Limber
parentc34f50d3371f47c88d2ee927a5d59af8e135304c (diff)
downloadswift-ee3a46975986865fe5064f9d87a27277fc6c235a.zip
swift-ee3a46975986865fe5064f9d87a27277fc6c235a.tar.bz2
Consolidating IncomingConnection & Connection.
BoostConnections no longer have their own thread, but are managed from a central Boost IO thread instead.
Diffstat (limited to 'Limber')
-rw-r--r--Limber/main.cpp91
1 files changed, 6 insertions, 85 deletions
diff --git a/Limber/main.cpp b/Limber/main.cpp
index be7387e..200f2e4 100644
--- a/Limber/main.cpp
+++ b/Limber/main.cpp
@@ -1,12 +1,8 @@
-
#include <iostream>
#include <string>
#include <boost/asio.hpp>
-#include <boost/signal.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
-#include <boost/enable_shared_from_this.hpp>
-#include <boost/thread.hpp>
#include "Swiften/Elements/IQ.h"
#include "Swiften/Elements/RosterPayload.h"
@@ -18,6 +14,7 @@
#include "Swiften/EventLoop/SimpleEventLoop.h"
#include "Swiften/Elements/Stanza.h"
#include "Swiften/Network/ConnectionServer.h"
+#include "Swiften/Network/BoostConnection.h"
#include "Swiften/Network/BoostIOServiceThread.h"
#include "Swiften/Server/ServerFromClientSession.h"
#include "Swiften/Parser/PayloadParsers/FullPayloadParserFactoryCollection.h"
@@ -25,82 +22,6 @@
using namespace Swift;
-static const size_t BUFFER_SIZE = 4096;
-
-// A reference-counted non-modifiable buffer class.
-class SharedBuffer {
- public:
- SharedBuffer(const ByteArray& data) :
- data_(new std::vector<char>(data.begin(), data.end())),
- buffer_(boost::asio::buffer(*data_)) {
- }
-
- // ConstBufferSequence requirements.
- typedef boost::asio::const_buffer value_type;
- typedef const boost::asio::const_buffer* const_iterator;
- const boost::asio::const_buffer* begin() const { return &buffer_; }
- const boost::asio::const_buffer* end() const { return &buffer_ + 1; }
-
- private:
- boost::shared_ptr< std::vector<char> > data_;
- boost::asio::const_buffer buffer_;
-};
-
-class IncomingBoostConnection : public IncomingConnection, public boost::enable_shared_from_this<IncomingBoostConnection> {
- public:
- typedef boost::shared_ptr<IncomingBoostConnection> pointer;
-
- static pointer create(boost::asio::io_service& ioService) {
- return pointer(new IncomingBoostConnection(ioService));
- }
-
- boost::asio::ip::tcp::socket& getSocket() {
- return socket_;
- }
-
- void write(const ByteArray& data) {
- boost::asio::async_write(socket_, SharedBuffer(data),
- boost::bind(
- &IncomingBoostConnection::handleDataWritten,
- shared_from_this(),
- boost::asio::placeholders::error));
- }
-
- void start() {
- read();
- }
-
- private:
- 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 && 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 {
public:
BoostConnectionServer(int port, boost::asio::io_service& ioService) : acceptor_(ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) {
@@ -109,15 +30,15 @@ class BoostConnectionServer : public ConnectionServer {
private:
void acceptNextConnection() {
- IncomingBoostConnection::pointer newConnection = IncomingBoostConnection::create(acceptor_.io_service());
+ boost::shared_ptr<BoostConnection> newConnection(new BoostConnection(&acceptor_.io_service()));
acceptor_.async_accept(newConnection->getSocket(),
boost::bind(&BoostConnectionServer::handleAccept, this, newConnection, boost::asio::placeholders::error));
}
- void handleAccept(IncomingBoostConnection::pointer newConnection, const boost::system::error_code& error) {
+ void handleAccept(boost::shared_ptr<BoostConnection> newConnection, const boost::system::error_code& error) {
if (!error) {
MainEventLoop::postEvent(boost::bind(boost::ref(onNewConnection), newConnection), this);
- newConnection->start();
+ newConnection->listen();
acceptNextConnection();
}
}
@@ -137,7 +58,7 @@ class Server {
}
private:
- void handleNewConnection(boost::shared_ptr<IncomingConnection> c) {
+ void handleNewConnection(boost::shared_ptr<Connection> c) {
ServerFromClientSession* session = new ServerFromClientSession(idGenerator_.generateID(), c, &payloadParserFactories_, &payloadSerializers_, userRegistry_);
serverFromClientSessions_.push_back(session);
session->onStanzaReceived.connect(boost::bind(&Server::handleStanzaReceived, this, _1, session));
@@ -192,5 +113,5 @@ int main() {
userRegistry.addUser(JID("remko@limber"), "pass");
Server server(&userRegistry);
eventLoop.run();
- return 0;
+ return 0;
}