summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2011-09-30 18:46:42 (GMT)
committerRemko Tronçon <git@el-tramo.be>2011-09-30 18:46:42 (GMT)
commit1b255f6ba90847240b92126deb5ea0174ad269b0 (patch)
tree9d56e647ebc35c0f0941d19a302b888c1185ce1c /Swiften/FileTransfer
parentaf976a75b5e1b504a340602659ef930109cd2c48 (diff)
downloadswift-contrib-1b255f6ba90847240b92126deb5ea0174ad269b0.zip
swift-contrib-1b255f6ba90847240b92126deb5ea0174ad269b0.tar.bz2
Avoid a copy when reading from a byte array.
Diffstat (limited to 'Swiften/FileTransfer')
-rw-r--r--Swiften/FileTransfer/ByteArrayReadBytestream.h8
-rw-r--r--Swiften/FileTransfer/FileReadBytestream.cpp14
-rw-r--r--Swiften/FileTransfer/FileReadBytestream.h2
-rw-r--r--Swiften/FileTransfer/IBBSendSession.cpp9
-rw-r--r--Swiften/FileTransfer/ReadBytestream.h3
-rw-r--r--Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp6
-rw-r--r--Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp2
7 files changed, 25 insertions, 19 deletions
diff --git a/Swiften/FileTransfer/ByteArrayReadBytestream.h b/Swiften/FileTransfer/ByteArrayReadBytestream.h
index 6cbdef0..9311099 100644
--- a/Swiften/FileTransfer/ByteArrayReadBytestream.h
+++ b/Swiften/FileTransfer/ByteArrayReadBytestream.h
@@ -1,36 +1,38 @@
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#pragma once
#include <vector>
+#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/Base/Algorithm.h>
#include <Swiften/FileTransfer/ReadBytestream.h>
+#include <Swiften/Base/ByteArray.h>
namespace Swift {
class ByteArrayReadBytestream : public ReadBytestream {
public:
ByteArrayReadBytestream(const std::vector<unsigned char>& data) : data(data), position(0), dataComplete(true) {
}
- virtual std::vector<unsigned char> read(size_t size) {
+ virtual boost::shared_ptr<ByteArray> read(size_t size) {
size_t readSize = size;
if (position + readSize > data.size()) {
readSize = data.size() - position;
}
- std::vector<unsigned char> result(data.begin() + position, data.begin() + position + readSize);
+ boost::shared_ptr<ByteArray> result = boost::make_shared<ByteArray>(data.begin() + position, data.begin() + position + readSize);
- onRead(result);
+ onRead(*result);
position += readSize;
return result;
}
virtual bool isFinished() const {
return position >= data.size() && dataComplete;
}
virtual void setDataComplete(bool b) {
diff --git a/Swiften/FileTransfer/FileReadBytestream.cpp b/Swiften/FileTransfer/FileReadBytestream.cpp
index f0139b8..a8946a0 100644
--- a/Swiften/FileTransfer/FileReadBytestream.cpp
+++ b/Swiften/FileTransfer/FileReadBytestream.cpp
@@ -1,41 +1,43 @@
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <boost/filesystem/fstream.hpp>
#include <cassert>
+#include <boost/smart_ptr/make_shared.hpp>
#include <Swiften/FileTransfer/FileReadBytestream.h>
+#include <Swiften/Base/ByteArray.h>
namespace Swift {
FileReadBytestream::FileReadBytestream(const boost::filesystem::path& file) : file(file), stream(NULL) {
}
FileReadBytestream::~FileReadBytestream() {
if (stream) {
stream->close();
stream = NULL;
}
}
-std::vector<unsigned char> FileReadBytestream::read(size_t size) {
+boost::shared_ptr<ByteArray> FileReadBytestream::read(size_t size) {
if (!stream) {
stream = new boost::filesystem::ifstream(file, std::ios_base::in|std::ios_base::binary);
}
- std::vector<unsigned char> result;
- result.resize(size);
+ boost::shared_ptr<ByteArray> result = boost::make_shared<ByteArray>();
+ result->resize(size);
assert(stream->good());
- stream->read(reinterpret_cast<char*>(&result[0]), size);
- result.resize(stream->gcount());
- onRead(result);
+ stream->read(reinterpret_cast<char*>(vecptr(*result)), size);
+ result->resize(stream->gcount());
+ onRead(*result);
return result;
}
bool FileReadBytestream::isFinished() const {
return stream && !stream->good();
}
}
diff --git a/Swiften/FileTransfer/FileReadBytestream.h b/Swiften/FileTransfer/FileReadBytestream.h
index bb24879..e9db2a4 100644
--- a/Swiften/FileTransfer/FileReadBytestream.h
+++ b/Swiften/FileTransfer/FileReadBytestream.h
@@ -11,17 +11,17 @@
#include <Swiften/FileTransfer/ReadBytestream.h>
namespace Swift {
class FileReadBytestream : public ReadBytestream {
public:
FileReadBytestream(const boost::filesystem::path& file);
~FileReadBytestream();
- virtual std::vector<unsigned char> read(size_t size);
+ virtual boost::shared_ptr< std::vector<unsigned char> > read(size_t size);
virtual bool isFinished() const;
private:
boost::filesystem::path file;
boost::filesystem::ifstream* stream;
};
}
diff --git a/Swiften/FileTransfer/IBBSendSession.cpp b/Swiften/FileTransfer/IBBSendSession.cpp
index 3a1390c..c24cc0a 100644
--- a/Swiften/FileTransfer/IBBSendSession.cpp
+++ b/Swiften/FileTransfer/IBBSendSession.cpp
@@ -2,18 +2,19 @@
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#include <Swiften/FileTransfer/IBBSendSession.h>
#include <boost/bind.hpp>
+#include <Swiften/Base/ByteArray.h>
#include <Swiften/Queries/IQRouter.h>
#include <Swiften/FileTransfer/IBBRequest.h>
#include <Swiften/FileTransfer/BytestreamException.h>
namespace Swift {
IBBSendSession::IBBSendSession(const std::string& id, const JID& from, const JID& to, boost::shared_ptr<ReadBytestream> bytestream, IQRouter* router) : id(id), from(from), to(to), bytestream(bytestream), router(router), blockSize(4096), sequenceNumber(0), active(false), waitingForData(false) {
bytestream->onDataAvailable.connect(boost::bind(&IBBSendSession::handleDataAvailable, this));
}
@@ -46,26 +47,26 @@ void IBBSendSession::handleIBBResponse(IBB::ref, ErrorPayload::ref error) {
}
}
else {
finish(FileTransferError(FileTransferError::PeerError));
}
}
void IBBSendSession::sendMoreData() {
try {
- std::vector<unsigned char> data = bytestream->read(blockSize);
- if (!data.empty()) {
+ boost::shared_ptr<ByteArray> data = bytestream->read(blockSize);
+ if (!data->empty()) {
waitingForData = false;
- IBBRequest::ref request = IBBRequest::create(from, to, IBB::createIBBData(id, sequenceNumber, data), router);
+ IBBRequest::ref request = IBBRequest::create(from, to, IBB::createIBBData(id, sequenceNumber, *data), router);
sequenceNumber++;
request->onResponse.connect(boost::bind(&IBBSendSession::handleIBBResponse, this, _1, _2));
request->send();
- onBytesSent(data.size());
+ onBytesSent(data->size());
}
else {
waitingForData = true;
}
}
catch (const BytestreamException&) {
finish(FileTransferError(FileTransferError::ReadError));
}
}
diff --git a/Swiften/FileTransfer/ReadBytestream.h b/Swiften/FileTransfer/ReadBytestream.h
index 0e95f7b..c94e4d3 100644
--- a/Swiften/FileTransfer/ReadBytestream.h
+++ b/Swiften/FileTransfer/ReadBytestream.h
@@ -1,30 +1,31 @@
/*
* Copyright (c) 2010 Remko Tronçon
* Licensed under the GNU General Public License v3.
* See Documentation/Licenses/GPLv3.txt for more information.
*/
#pragma once
+#include <boost/shared_ptr.hpp>
#include <vector>
#include <Swiften/Base/boost_bsignals.h>
namespace Swift {
class ReadBytestream {
public:
virtual ~ReadBytestream();
/**
* Return an empty vector if no more data is available.
* Use onDataAvailable signal for signaling there is data available again.
*/
- virtual std::vector<unsigned char> read(size_t size) = 0;
+ virtual boost::shared_ptr< std::vector<unsigned char> > read(size_t size) = 0;
virtual bool isFinished() const = 0;
public:
boost::signal<void ()> onDataAvailable;
boost::signal<void (const std::vector<unsigned char>&)> onRead;
};
}
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp b/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp
index db3d83f..cd555e5 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp
+++ b/Swiften/FileTransfer/SOCKS5BytestreamClientSession.cpp
@@ -159,21 +159,21 @@ void SOCKS5BytestreamClientSession::startSending(boost::shared_ptr<ReadBytestrea
}
HostAddressPort SOCKS5BytestreamClientSession::getAddressPort() const {
return addressPort;
}
void SOCKS5BytestreamClientSession::sendData() {
if (!readBytestream->isFinished()) {
try {
- SafeByteArray dataToSend = createSafeByteArray(readBytestream->read(chunkSize));
- connection->write(dataToSend);
- onBytesSent(dataToSend.size());
+ boost::shared_ptr<ByteArray> dataToSend = readBytestream->read(chunkSize);
+ connection->write(createSafeByteArray(*dataToSend));
+ onBytesSent(dataToSend->size());
}
catch (const BytestreamException&) {
finish(true);
}
}
else {
finish(false);
}
}
diff --git a/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp b/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp
index def9e33..f660fda 100644
--- a/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp
+++ b/Swiften/FileTransfer/SOCKS5BytestreamServerSession.cpp
@@ -139,19 +139,19 @@ void SOCKS5BytestreamServerSession::process() {
}
}
}
}
}
void SOCKS5BytestreamServerSession::sendData() {
if (!readBytestream->isFinished()) {
try {
- SafeByteArray dataToSend = createSafeByteArray(readBytestream->read(chunkSize));
+ SafeByteArray dataToSend = createSafeByteArray(*readBytestream->read(chunkSize));
connection->write(dataToSend);
onBytesSent(dataToSend.size());
}
catch (const BytestreamException&) {
finish(true);
}
}
else {
finish(false);