summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2011-09-28 17:42:54 (GMT)
committerRemko Tronçon <git@el-tramo.be>2011-09-28 17:43:38 (GMT)
commitbab047c1bef2936124db1346863a902e1064af12 (patch)
treec59de84a76581bd07713eb0591121e6a4aa04e7b /Swiften/Network
parent7b8860c794419b63f827c9b87fbc469a1bcd58ef (diff)
downloadswift-contrib-bab047c1bef2936124db1346863a902e1064af12.zip
swift-contrib-bab047c1bef2936124db1346863a902e1064af12.tar.bz2
Pass read data from connection via shared_ptr.
This should avoid unnecessary copying of the received data while being processed by the event loop.
Diffstat (limited to 'Swiften/Network')
-rw-r--r--Swiften/Network/BoostConnection.cpp10
-rw-r--r--Swiften/Network/BoostConnection.h2
-rw-r--r--Swiften/Network/Connection.h2
-rw-r--r--Swiften/Network/DummyConnection.cpp3
-rw-r--r--Swiften/Network/HTTPConnectProxiedConnection.cpp8
-rw-r--r--Swiften/Network/HTTPConnectProxiedConnection.h2
-rw-r--r--Swiften/Network/SOCKS5ProxiedConnection.cpp12
-rw-r--r--Swiften/Network/SOCKS5ProxiedConnection.h2
-rw-r--r--Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp18
9 files changed, 31 insertions, 28 deletions
diff --git a/Swiften/Network/BoostConnection.cpp b/Swiften/Network/BoostConnection.cpp
index ac3b444..043743e 100644
--- a/Swiften/Network/BoostConnection.cpp
+++ b/Swiften/Network/BoostConnection.cpp
@@ -13,6 +13,7 @@
#include <boost/thread.hpp>
#include <boost/asio/placeholders.hpp>
#include <boost/asio/write.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/Base/Log.h>
#include <Swiften/Base/Algorithm.h>
@@ -50,7 +51,7 @@ class SharedBuffer {
// -----------------------------------------------------------------------------
BoostConnection::BoostConnection(boost::shared_ptr<boost::asio::io_service> ioService, EventLoop* eventLoop) :
- eventLoop(eventLoop), ioService(ioService), socket_(*ioService), readBuffer_(BUFFER_SIZE), writing_(false) {
+ eventLoop(eventLoop), ioService(ioService), socket_(*ioService), writing_(false) {
}
BoostConnection::~BoostConnection() {
@@ -108,16 +109,17 @@ void BoostConnection::handleConnectFinished(const boost::system::error_code& err
}
void BoostConnection::doRead() {
+ readBuffer_ = boost::make_shared<SafeByteArray>(BUFFER_SIZE);
socket_.async_read_some(
- boost::asio::buffer(readBuffer_),
+ boost::asio::buffer(*readBuffer_),
boost::bind(&BoostConnection::handleSocketRead, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void BoostConnection::handleSocketRead(const boost::system::error_code& error, size_t bytesTransferred) {
SWIFT_LOG(debug) << "Socket read " << error << std::endl;
if (!error) {
- eventLoop->postEvent(boost::bind(boost::ref(onDataRead), createSafeByteArray(&readBuffer_[0], bytesTransferred)), shared_from_this());
- std::fill(readBuffer_.begin(), readBuffer_.end(), 0);
+ readBuffer_->resize(bytesTransferred);
+ eventLoop->postEvent(boost::bind(boost::ref(onDataRead), readBuffer_), shared_from_this());
doRead();
}
else if (/*error == boost::asio::error::eof ||*/ error == boost::asio::error::operation_aborted) {
diff --git a/Swiften/Network/BoostConnection.h b/Swiften/Network/BoostConnection.h
index 259fcec..7d5ec60 100644
--- a/Swiften/Network/BoostConnection.h
+++ b/Swiften/Network/BoostConnection.h
@@ -59,7 +59,7 @@ namespace Swift {
EventLoop* eventLoop;
boost::shared_ptr<boost::asio::io_service> ioService;
boost::asio::ip::tcp::socket socket_;
- SafeByteArray readBuffer_;
+ boost::shared_ptr<SafeByteArray> readBuffer_;
boost::mutex writeMutex_;
bool writing_;
SafeByteArray writeQueue_;
diff --git a/Swiften/Network/Connection.h b/Swiften/Network/Connection.h
index 1b9977f..6ad2999 100644
--- a/Swiften/Network/Connection.h
+++ b/Swiften/Network/Connection.h
@@ -36,7 +36,7 @@ namespace Swift {
public:
boost::signal<void (bool /* error */)> onConnectFinished;
boost::signal<void (const boost::optional<Error>&)> onDisconnected;
- boost::signal<void (const SafeByteArray&)> onDataRead;
+ boost::signal<void (boost::shared_ptr<SafeByteArray>)> onDataRead;
boost::signal<void ()> onDataWritten;
};
}
diff --git a/Swiften/Network/DummyConnection.cpp b/Swiften/Network/DummyConnection.cpp
index 9a9d138..09bd06d 100644
--- a/Swiften/Network/DummyConnection.cpp
+++ b/Swiften/Network/DummyConnection.cpp
@@ -7,6 +7,7 @@
#include <Swiften/Network/DummyConnection.h>
#include <boost/bind.hpp>
+#include <boost/smart_ptr/make_shared.hpp>
#include <cassert>
namespace Swift {
@@ -15,7 +16,7 @@ DummyConnection::DummyConnection(EventLoop* eventLoop) : eventLoop(eventLoop) {
}
void DummyConnection::receive(const SafeByteArray& data) {
- eventLoop->postEvent(boost::bind(boost::ref(onDataRead), SafeByteArray(data)), shared_from_this());
+ eventLoop->postEvent(boost::bind(boost::ref(onDataRead), boost::make_shared<SafeByteArray>(data)), shared_from_this());
}
void DummyConnection::listen() {
diff --git a/Swiften/Network/HTTPConnectProxiedConnection.cpp b/Swiften/Network/HTTPConnectProxiedConnection.cpp
index 20819ff..e05a933 100644
--- a/Swiften/Network/HTTPConnectProxiedConnection.cpp
+++ b/Swiften/Network/HTTPConnectProxiedConnection.cpp
@@ -73,10 +73,10 @@ void HTTPConnectProxiedConnection::handleConnectionConnectFinished(bool error) {
}
}
-void HTTPConnectProxiedConnection::handleDataRead(const SafeByteArray& data) {
+void HTTPConnectProxiedConnection::handleDataRead(boost::shared_ptr<SafeByteArray> data) {
if (!connected_) {
- SWIFT_LOG(debug) << byteArrayToString(ByteArray(data.begin(), data.end())) << std::endl;
- std::vector<std::string> tmp = String::split(byteArrayToString(ByteArray(data.begin(), data.end())), ' ');
+ SWIFT_LOG(debug) << byteArrayToString(ByteArray(data->begin(), data->end())) << std::endl;
+ std::vector<std::string> tmp = String::split(byteArrayToString(ByteArray(data->begin(), data->end())), ' ');
if(tmp.size() > 1) {
int status = boost::lexical_cast<int> (tmp[1].c_str());
SWIFT_LOG(debug) << "Proxy Status: " << status << std::endl;
@@ -85,7 +85,7 @@ void HTTPConnectProxiedConnection::handleDataRead(const SafeByteArray& data) {
onConnectFinished(false);
return;
}
- SWIFT_LOG(debug) << "HTTP Proxy returned an error: " << byteArrayToString(ByteArray(data.begin(), data.end())) << std::endl;
+ SWIFT_LOG(debug) << "HTTP Proxy returned an error: " << byteArrayToString(ByteArray(data->begin(), data->end())) << std::endl;
}
disconnect();
onConnectFinished(true);
diff --git a/Swiften/Network/HTTPConnectProxiedConnection.h b/Swiften/Network/HTTPConnectProxiedConnection.h
index 96c6be8..d3f5b7a 100644
--- a/Swiften/Network/HTTPConnectProxiedConnection.h
+++ b/Swiften/Network/HTTPConnectProxiedConnection.h
@@ -41,7 +41,7 @@ namespace Swift {
HTTPConnectProxiedConnection(ConnectionFactory* connectionFactory, HostAddressPort proxy);
void handleConnectionConnectFinished(bool error);
- void handleDataRead(const SafeByteArray& data);
+ void handleDataRead(boost::shared_ptr<SafeByteArray> data);
void handleDisconnected(const boost::optional<Error>& error);
private:
diff --git a/Swiften/Network/SOCKS5ProxiedConnection.cpp b/Swiften/Network/SOCKS5ProxiedConnection.cpp
index f8084ab..163e23a 100644
--- a/Swiften/Network/SOCKS5ProxiedConnection.cpp
+++ b/Swiften/Network/SOCKS5ProxiedConnection.cpp
@@ -86,15 +86,15 @@ void SOCKS5ProxiedConnection::handleConnectionConnectFinished(bool error) {
}
}
-void SOCKS5ProxiedConnection::handleDataRead(const SafeByteArray& data) {
+void SOCKS5ProxiedConnection::handleDataRead(boost::shared_ptr<SafeByteArray> data) {
SafeByteArray socksConnect;
boost::asio::ip::address rawAddress = server_.getAddress().getRawAddress();
assert(rawAddress.is_v4() || rawAddress.is_v6());
if (!connected_) {
if (proxyState_ == ProxyAuthenticating) {
SWIFT_LOG(debug) << "ProxyAuthenticating response received, reply with the connect BYTEs" << std::endl;
- unsigned char choosenMethod = static_cast<unsigned char> (data[1]);
- if (data[0] == 0x05 && choosenMethod != 0xFF) {
+ unsigned char choosenMethod = static_cast<unsigned char> ((*data)[1]);
+ if ((*data)[0] == 0x05 && choosenMethod != 0xFF) {
switch(choosenMethod) { // use the correct Method
case 0x00:
try {
@@ -134,7 +134,7 @@ void SOCKS5ProxiedConnection::handleDataRead(const SafeByteArray& data) {
}
else if (proxyState_ == ProxyConnecting) {
SWIFT_LOG(debug) << "Connect response received, check if successfully." << std::endl;
- SWIFT_LOG(debug) << "Errorbyte: 0x" << std::hex << static_cast<int> (data[1]) << std::dec << std::endl;
+ SWIFT_LOG(debug) << "Errorbyte: 0x" << std::hex << static_cast<int> ((*data)[1]) << std::dec << std::endl;
/*
data.at(1) can be one of the following:
@@ -149,14 +149,14 @@ void SOCKS5ProxiedConnection::handleDataRead(const SafeByteArray& data) {
0x08 Address type not supported (ATYP)
0x09 bis 0xFF unassigned
*/
- if (data[0] == 0x05 && data[1] == 0x0) {
+ if ((*data)[0] == 0x05 && (*data)[1] == 0x0) {
SWIFT_LOG(debug) << "Successfully connected the server via the proxy." << std::endl;
connected_ = true;
onConnectFinished(false);
return;
}
else {
- std::cerr << "SOCKS Proxy returned an error: " << std::hex << data[1] << std::endl;
+ std::cerr << "SOCKS Proxy returned an error: " << std::hex << (*data)[1] << std::endl;
}
return;
}
diff --git a/Swiften/Network/SOCKS5ProxiedConnection.h b/Swiften/Network/SOCKS5ProxiedConnection.h
index 942b6ce..592ce7d 100644
--- a/Swiften/Network/SOCKS5ProxiedConnection.h
+++ b/Swiften/Network/SOCKS5ProxiedConnection.h
@@ -42,7 +42,7 @@ namespace Swift {
SOCKS5ProxiedConnection(ConnectionFactory* connectionFactory, const HostAddressPort& proxy);
void handleConnectionConnectFinished(bool error);
- void handleDataRead(const SafeByteArray& data);
+ void handleDataRead(boost::shared_ptr<SafeByteArray> data);
void handleDisconnected(const boost::optional<Error>& error);
private:
diff --git a/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp b/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp
index 48189ab..133773f 100644
--- a/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp
+++ b/Swiften/Network/UnitTest/HTTPConnectProxiedConnectionTest.cpp
@@ -76,7 +76,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345));
eventLoop->processEvents();
- connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 200 Connection established\r\n\r\n"));
+ connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n"));
eventLoop->processEvents();
CPPUNIT_ASSERT(connectFinished);
@@ -89,7 +89,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345));
eventLoop->processEvents();
- connectionFactory->connections[0]->onDataRead(createSafeByteArray("FLOOP"));
+ connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("FLOOP"));
eventLoop->processEvents();
CPPUNIT_ASSERT(connectFinished);
@@ -102,7 +102,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345));
eventLoop->processEvents();
- connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 401 Unauthorized\r\n\r\n"));
+ connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 401 Unauthorized\r\n\r\n"));
eventLoop->processEvents();
CPPUNIT_ASSERT(connectFinished);
@@ -114,10 +114,10 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
HTTPConnectProxiedConnection::ref testling(createTestling());
testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345));
eventLoop->processEvents();
- connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 200 Connection established\r\n\r\n"));
+ connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n"));
eventLoop->processEvents();
- connectionFactory->connections[0]->onDataRead(createSafeByteArray("abcdef"));
+ connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("abcdef"));
CPPUNIT_ASSERT_EQUAL(createByteArray("abcdef"), dataRead);
}
@@ -126,7 +126,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
HTTPConnectProxiedConnection::ref testling(createTestling());
testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345));
eventLoop->processEvents();
- connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 200 Connection established\r\n\r\n"));
+ connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n"));
eventLoop->processEvents();
connectionFactory->connections[0]->dataWritten.clear();
@@ -151,7 +151,7 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
HTTPConnectProxiedConnection::ref testling(createTestling());
testling->connect(HostAddressPort(HostAddress("2.2.2.2"), 2345));
eventLoop->processEvents();
- connectionFactory->connections[0]->onDataRead(createSafeByteArray("HTTP/1.0 200 Connection established\r\n\r\n"));
+ connectionFactory->connections[0]->onDataRead(createSafeByteArrayRef("HTTP/1.0 200 Connection established\r\n\r\n"));
eventLoop->processEvents();
testling->disconnect();
@@ -180,8 +180,8 @@ class HTTPConnectProxiedConnectionTest : public CppUnit::TestFixture {
disconnectedError = e;
}
- void handleDataRead(const SafeByteArray& d) {
- append(dataRead, d);
+ void handleDataRead(boost::shared_ptr<SafeByteArray> d) {
+ append(dataRead, *d);
}
struct MockConnection : public Connection {