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/FileTransfer
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/FileTransfer')
-rw-r--r--Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp10
-rw-r--r--Swiften/FileTransfer/SOCKS5BytestreamClientSession.h2
-rw-r--r--Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp8
-rw-r--r--Swiften/FileTransfer/SOCKS5BytestreamServerSession.h2
-rw-r--r--Swiften/FileTransfer/UnitTest/SOCKS5BytestreamClientSessionTest.cpp30
5 files changed, 26 insertions, 26 deletions
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp b/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp
index a18b998..db3d83f 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp
+++ b/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp
@@ -203,27 +203,27 @@ void SOCKS5BytestreamClientSession::handleConnectFinished(bool error) {
finish(true);
} else {
SWIFT_LOG(debug) << "Successfully connected via TCP" << addressPort.toString() << "." << std::endl;
weFailedTimeout->start();
connection->onDataRead.connect(boost::bind(&SOCKS5BytestreamClientSession::handleDataRead, this, _1));
process();
}
}
-void SOCKS5BytestreamClientSession::handleDataRead(const SafeByteArray& data) {
- SWIFT_LOG(debug) << "state: " << state << " data.size() = " << data.size() << std::endl;
+void SOCKS5BytestreamClientSession::handleDataRead(boost::shared_ptr<SafeByteArray> data) {
+ SWIFT_LOG(debug) << "state: " << state << " data.size() = " << data->size() << std::endl;
if (state != Reading) {
- append(unprocessedData, data);
+ append(unprocessedData, *data);
process();
}
else {
- writeBytestream->write(createByteArray(vecptr(data), data.size()));
- onBytesReceived(data.size());
+ writeBytestream->write(createByteArray(vecptr(*data), data->size()));
+ onBytesReceived(data->size());
}
}
void SOCKS5BytestreamClientSession::handleDisconnected(const boost::optional<Connection::Error>& error) {
SWIFT_LOG(debug) << (error ? (error == Connection::ReadError ? "Read Error" : "Write Error") : "No Error") << std::endl;
if (error) {
finish(true);
}
}
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamClientSession.h b/Swiften/FileTransfer/SOCKS5BytestreamClientSession.h
index 894e977..ea45955 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamClientSession.h
+++ b/Swiften/FileTransfer/SOCKS5BytestreamClientSession.h
@@ -60,19 +60,19 @@ public:
boost::signal<void (int)> onBytesSent;
boost::signal<void (int)> onBytesReceived;
private:
void process();
void hello();
void authenticate();
void handleConnectFinished(bool error);
- void handleDataRead(const SafeByteArray&);
+ void handleDataRead(boost::shared_ptr<SafeByteArray>);
void handleDisconnected(const boost::optional<Connection::Error>&);
void handleWeFailedTimeout();
void finish(bool error);
void sendData();
private:
boost::shared_ptr<Connection> connection;
HostAddressPort addressPort;
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp b/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp
index fa7e054..def9e33 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp
+++ b/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp
@@ -59,25 +59,25 @@ void SOCKS5BytestreamServerSession::startTransfer() {
} else {
SWIFT_LOG(debug) << "Not ready for transfer!" << std::endl;
}
}
HostAddressPort SOCKS5BytestreamServerSession::getAddressPort() const {
return connection->getLocalAddress();
}
-void SOCKS5BytestreamServerSession::handleDataRead(const SafeByteArray& data) {
+void SOCKS5BytestreamServerSession::handleDataRead(boost::shared_ptr<SafeByteArray> data) {
if (state != ReadingData) {
- append(unprocessedData, data);
+ append(unprocessedData, *data);
process();
} else {
- writeBytestream->write(createByteArray(vecptr(data), data.size()));
- onBytesReceived(data.size());
+ writeBytestream->write(createByteArray(vecptr(*data), data->size()));
+ onBytesReceived(data->size());
}
}
void SOCKS5BytestreamServerSession::handleDisconnected(const boost::optional<Connection::Error>& error) {
SWIFT_LOG(debug) << (error ? (error == Connection::ReadError ? "Read Error" : "Write Error") : "No Error") << std::endl;
if (error) {
finish(true);
}
}
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerSession.h b/Swiften/FileTransfer/SOCKS5BytestreamServerSession.h
index 3e1018f..4557a36 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamServerSession.h
+++ b/Swiften/FileTransfer/SOCKS5BytestreamServerSession.h
@@ -46,19 +46,19 @@ namespace Swift {
HostAddressPort getAddressPort() const;
boost::signal<void (boost::optional<FileTransferError>)> onFinished;
boost::signal<void (int)> onBytesSent;
boost::signal<void (int)> onBytesReceived;
private:
void finish(bool error);
void process();
- void handleDataRead(const SafeByteArray&);
+ void handleDataRead(boost::shared_ptr<SafeByteArray>);
void handleDisconnected(const boost::optional<Connection::Error>&);
void sendData();
private:
boost::shared_ptr<Connection> connection;
SOCKS5BytestreamRegistry* bytestreams;
ByteArray unprocessedData;
State state;
int chunkSize;
diff --git a/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamClientSessionTest.cpp b/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamClientSessionTest.cpp
index 75b9faf..527e0ca 100644
--- a/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamClientSessionTest.cpp
+++ b/Swiften/FileTransfer/UnitTest/SOCKS5BytestreamClientSessionTest.cpp
@@ -158,19 +158,19 @@ public:
serverRespondRequestOK();
eventLoop->processEvents();
CPPUNIT_ASSERT_EQUAL(true, helper.sessionReadyCalled);
CPPUNIT_ASSERT_EQUAL(false, helper.sessionReadyError);
boost::shared_ptr<ByteArrayWriteBytestream> output = boost::make_shared<ByteArrayWriteBytestream>();
clientSession->startReceiving(output);
ByteArray transferData = generateRandomByteArray(1024);
- connection->onDataRead(createSafeByteArray(transferData.data(), transferData.size()));
+ connection->onDataRead(createSafeByteArrayRef(transferData.data(), transferData.size()));
CPPUNIT_ASSERT_EQUAL(transferData, output->getData());
}
void testReadBytestream() {
TestHelper helper;
connection->onDataSent.connect(boost::bind(&TestHelper::handleConnectionDataWritten, &helper, _1));
SOCKS5BytestreamClientSession::ref clientSession = boost::make_shared<SOCKS5BytestreamClientSession>(connection, destinationAddressPort, destination, timerFactory);
clientSession->onSessionReady.connect(boost::bind(&TestHelper::handleSessionReady, &helper, _1));
@@ -206,47 +206,47 @@ private:
ByteArray result(len);
for (size_t i=0; i < len; ++i ) {
result[i] = randomByte();
}
return result;
}
// Server responses
void serverRespondHelloOK() {
- connection->onDataRead(createSafeByteArray("\x05\00", 2));
+ connection->onDataRead(createSafeByteArrayRef("\x05\00", 2));
}
void serverRespondHelloAuthFail() {
- connection->onDataRead(createSafeByteArray("\x05\xFF", 2));
+ connection->onDataRead(createSafeByteArrayRef("\x05\xFF", 2));
}
void serverRespondRequestOK() {
- SafeByteArray dataToSend = createSafeByteArray("\x05\x00\x00\x03", 4);
- append(dataToSend, createSafeByteArray(destination.size()));
- append(dataToSend, createSafeByteArray(destination));
- append(dataToSend, createSafeByteArray("\x00", 1));
+ boost::shared_ptr<SafeByteArray> dataToSend = createSafeByteArrayRef("\x05\x00\x00\x03", 4);
+ append(*dataToSend, createSafeByteArray(destination.size()));
+ append(*dataToSend, createSafeByteArray(destination));
+ append(*dataToSend, createSafeByteArray("\x00", 1));
connection->onDataRead(dataToSend);
}
void serverRespondRequestFail() {
- SafeByteArray correctData = createSafeByteArray("\x05\x00\x00\x03", 4);
- append(correctData, createSafeByteArray(destination.size()));
- append(correctData, createSafeByteArray(destination));
- append(correctData, createSafeByteArray("\x00", 1));
+ boost::shared_ptr<SafeByteArray> correctData = createSafeByteArrayRef("\x05\x00\x00\x03", 4);
+ append(*correctData, createSafeByteArray(destination.size()));
+ append(*correctData, createSafeByteArray(destination));
+ append(*correctData, createSafeByteArray("\x00", 1));
- SafeByteArray dataToSend;
+ boost::shared_ptr<SafeByteArray> dataToSend;
//ByteArray failingData = Hexify::unhexify("8417947d1d305c72c11520ea7d2c6e787396705e72c312c6ccc3f66613d7cae1b91b7ab48e8b59a17d559c15fb51");
//append(dataToSend, failingData);
//SWIFT_LOG(debug) << "hexed: " << Hexify::hexify(failingData) << std::endl;
do {
- ByteArray rndArray = generateRandomByteArray(correctData.size());
- dataToSend = createSafeByteArray(rndArray.data(), rndArray.size());
- } while (dataToSend == correctData);
+ ByteArray rndArray = generateRandomByteArray(correctData->size());
+ dataToSend = createSafeByteArrayRef(rndArray.data(), rndArray.size());
+ } while (*dataToSend == *correctData);
connection->onDataRead(dataToSend);
}
private:
struct TestHelper {
TestHelper() : sessionReadyCalled(false), sessionReadyError(false) {}
ByteArray unprocessedInput;
bool sessionReadyCalled;
bool sessionReadyError;