Created MemoryReadByteStream

This commit is contained in:
HanzZ 2011-10-03 10:59:29 +02:00
parent 46f5c51d00
commit 03c7d96eab
4 changed files with 123 additions and 44 deletions

View file

@ -0,0 +1,48 @@
/**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#pragma once
#include <string>
#include <map>
#include "Swiften/Swiften.h"
namespace Transport {
class MemoryReadBytestream : public Swift::ReadBytestream {
public:
MemoryReadBytestream();
virtual ~MemoryReadBytestream();
unsigned long appendData(const std::string &data);
virtual boost::shared_ptr<std::vector<unsigned char> > read(size_t size);
bool isFinished() const;
boost::signal<void ()> onDataNeeded;
private:
bool m_finished;
std::string m_data;
bool neededData;
};
}

View file

@ -137,45 +137,4 @@ class NetworkPluginServer {
FileTransferManager *m_ftManager;
};
class DummyReadBytestream : public Swift::ReadBytestream {
public:
DummyReadBytestream(NetworkPluginServer::Backend *b, unsigned long ftid) {neededData = false; m_finished = false; this->b = b; this->ftid = ftid;}
virtual ~DummyReadBytestream() {}
unsigned long appendData(const std::string &data) {
m_data += data;
onDataAvailable();
neededData = false;
return m_data.size();
}
virtual boost::shared_ptr<std::vector<unsigned char> > read(size_t size) {
if (m_data.empty()) {
return boost::shared_ptr<std::vector<unsigned char> >(new std::vector<unsigned char>());
}
if (m_data.size() < size) {
boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.end()));
m_data.clear();
onDataNeeded(b, ftid);
return ptr;
}
boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.begin() + size));
m_data.erase(m_data.begin(), m_data.begin() + size);
if (m_data.size() < 500000 && !neededData) {
neededData = true;
onDataNeeded(b, ftid);
}
return ptr;
}
boost::signal<void (NetworkPluginServer::Backend *b, unsigned long ftid)> onDataNeeded;
virtual bool isFinished() const { return m_finished; }
private:
bool m_finished;
NetworkPluginServer::Backend *b;
unsigned long ftid;
std::string m_data;
bool neededData;
};
}

View file

@ -0,0 +1,71 @@
/**
* libtransport -- C++ library for easy XMPP Transports development
*
* Copyright (C) 2011, Jan Kaluza <hanzz.k@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "transport/memoryreadbytestream.h"
#include "log4cxx/logger.h"
#include "memoryusage.h"
#include <boost/foreach.hpp>
using namespace log4cxx;
namespace Transport {
MemoryReadBytestream::MemoryReadBytestream() {
neededData = false;
m_finished = false;
}
MemoryReadBytestream::~MemoryReadBytestream() {
}
unsigned long MemoryReadBytestream::appendData(const std::string &data) {
m_data += data;
onDataAvailable();
neededData = false;
return m_data.size();
}
boost::shared_ptr<std::vector<unsigned char> > MemoryReadBytestream::read(size_t size) {
if (m_data.empty()) {
onDataNeeded();
return boost::shared_ptr<std::vector<unsigned char> >(new std::vector<unsigned char>());
}
if (m_data.size() < size) {
boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.end()));
m_data.clear();
onDataNeeded();
return ptr;
}
boost::shared_ptr<std::vector<unsigned char> > ptr(new std::vector<unsigned char>(m_data.begin(), m_data.begin() + size));
m_data.erase(m_data.begin(), m_data.begin() + size);
if (m_data.size() < 500000 && !neededData) {
neededData = true;
onDataNeeded();
}
return ptr;
}
bool MemoryReadBytestream::isFinished() const {
return m_finished;
}
}

View file

@ -30,6 +30,7 @@
#include "transport/conversation.h"
#include "transport/vcardresponder.h"
#include "transport/rosterresponder.h"
#include "transport/memoryreadbytestream.h"
#include "blockresponder.h"
#include "Swiften/Swiften.h"
#include "Swiften/Server/ServerStanzaChannel.h"
@ -600,8 +601,8 @@ void NetworkPluginServer::handleFTStartPayload(const std::string &data) {
fileInfo.setName(payload.filename());
Backend *c = (Backend *) user->getData();
boost::shared_ptr<DummyReadBytestream> bytestream(new DummyReadBytestream(c, bytestream_id + 1));
bytestream->onDataNeeded.connect(boost::bind(&NetworkPluginServer::handleFTDataNeeded, this, _1, _2));
boost::shared_ptr<MemoryReadBytestream> bytestream(new MemoryReadBytestream());
bytestream->onDataNeeded.connect(boost::bind(&NetworkPluginServer::handleFTDataNeeded, this, c, bytestream_id + 1));
LOG4CXX_INFO(logger, "jid=" << buddy->getJID());
@ -628,7 +629,7 @@ void NetworkPluginServer::handleFTDataPayload(Backend *b, const std::string &dat
// return;
FileTransferManager::Transfer &transfer = m_filetransfers[payload.ftid()];
DummyReadBytestream *bytestream = (DummyReadBytestream *) transfer.readByteStream.get();
MemoryReadBytestream *bytestream = (MemoryReadBytestream *) transfer.readByteStream.get();
if (bytestream->appendData(payload.data()) > 5000000) {
pbnetwork::FileTransferData f;