mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-04-22 08:52:12 +02:00
* Prevent shutting down socket before all data has been read.
This commit is contained in:
parent
bafdc244a7
commit
23d7eeeb02
@ -1,10 +1,11 @@
|
||||
#include <QDebug>
|
||||
#include "bufferiodevice.h"
|
||||
|
||||
|
||||
BufferIODevice::BufferIODevice( unsigned int size, QObject *parent ) :
|
||||
QIODevice( parent ),
|
||||
m_size(size),
|
||||
m_received(0)
|
||||
m_size( size ),
|
||||
m_received( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
@ -27,7 +28,6 @@ BufferIODevice::close()
|
||||
|
||||
qDebug() << Q_FUNC_INFO;
|
||||
QIODevice::close();
|
||||
// TODO ?
|
||||
}
|
||||
|
||||
|
||||
@ -39,6 +39,7 @@ BufferIODevice::inputComplete( const QString& errmsg )
|
||||
emit readChannelFinished();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BufferIODevice::addData( QByteArray ba )
|
||||
{
|
||||
@ -57,10 +58,8 @@ BufferIODevice::bytesAvailable() const
|
||||
qint64
|
||||
BufferIODevice::readData( char * data, qint64 maxSize )
|
||||
{
|
||||
// qDebug() << Q_FUNC_INFO << maxSize;
|
||||
|
||||
//qDebug() << Q_FUNC_INFO << maxSize;
|
||||
QMutexLocker lock( &m_mut );
|
||||
// qDebug() << "readData begins, bufersize:" << m_buffer.length();
|
||||
|
||||
qint64 size = maxSize;
|
||||
if ( m_buffer.length() < maxSize )
|
||||
@ -69,7 +68,7 @@ BufferIODevice::readData( char * data, qint64 maxSize )
|
||||
memcpy( data, m_buffer.data(), size );
|
||||
m_buffer.remove( 0, size );
|
||||
|
||||
// qDebug() << "readData ends, bufersize:" << m_buffer.length();
|
||||
//qDebug() << "readData ends, bufersize:" << m_buffer.length();
|
||||
return size;
|
||||
}
|
||||
|
||||
@ -93,13 +92,14 @@ qint64 BufferIODevice::size() const
|
||||
return m_size;
|
||||
}
|
||||
|
||||
|
||||
bool BufferIODevice::atEnd() const
|
||||
{
|
||||
QMutexLocker lock( &m_mut );
|
||||
return m_size == m_received &&
|
||||
m_buffer.length() == 0;
|
||||
return ( m_size == m_received && m_buffer.length() == 0 );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
BufferIODevice::clear()
|
||||
{
|
||||
|
@ -21,7 +21,6 @@ public:
|
||||
void addData( QByteArray ba );
|
||||
void clear();
|
||||
|
||||
bool isOpen() const { qDebug() << "isOpen"; return true; }
|
||||
OpenMode openMode() const { qDebug() << "openMode"; return QIODevice::ReadWrite; }
|
||||
|
||||
void inputComplete( const QString& errmsg = "" );
|
||||
|
@ -266,10 +266,17 @@ Connection::socketDisconnected()
|
||||
void
|
||||
Connection::socketDisconnectedError(QAbstractSocket::SocketError e)
|
||||
{
|
||||
qDebug() << "SOCKET ERROR CODE" << e << this->name() << " CALLING Connection::shutdown(false)";
|
||||
if ( e == QAbstractSocket::RemoteHostClosedError )
|
||||
return;
|
||||
|
||||
qDebug() << "SOCKET ERROR CODE" << e << this->name() << "CALLING Connection::shutdown(false)";
|
||||
|
||||
m_peer_disconnected = true;
|
||||
|
||||
emit socketErrored(e);
|
||||
shutdown(false);
|
||||
emit socketClosed();
|
||||
|
||||
shutdown( false );
|
||||
}
|
||||
|
||||
|
||||
|
@ -65,8 +65,8 @@ public:
|
||||
qint64 bytesSent() const { return m_tx_bytes; }
|
||||
qint64 bytesReceived() const { return m_rx_bytes; }
|
||||
|
||||
void setMsgProcessorModeOut( quint32 m ) { m_msgprocessor_out.setMode(m); }
|
||||
void setMsgProcessorModeIn( quint32 m ) { m_msgprocessor_in.setMode(m); }
|
||||
void setMsgProcessorModeOut( quint32 m ) { m_msgprocessor_out.setMode( m ); }
|
||||
void setMsgProcessorModeIn( quint32 m ) { m_msgprocessor_in.setMode( m ); }
|
||||
|
||||
signals:
|
||||
void ready();
|
||||
@ -74,7 +74,7 @@ signals:
|
||||
void finished();
|
||||
void statsTick( qint64 tx_bytes_sec, qint64 rx_bytes_sec );
|
||||
void socketClosed();
|
||||
void socketErrored(QAbstractSocket::SocketError);
|
||||
void socketErrored( QAbstractSocket::SocketError );
|
||||
|
||||
protected:
|
||||
virtual void setup() = 0;
|
||||
@ -93,7 +93,7 @@ private slots:
|
||||
void handleIncomingQueueEmpty();
|
||||
void sendMsg_now( msg_ptr );
|
||||
void socketDisconnected();
|
||||
void socketDisconnectedError(QAbstractSocket::SocketError);
|
||||
void socketDisconnectedError( QAbstractSocket::SocketError );
|
||||
void readyRead();
|
||||
void doSetup();
|
||||
void authCheckTimeout();
|
||||
|
@ -16,8 +16,7 @@
|
||||
using namespace Tomahawk;
|
||||
|
||||
|
||||
FileTransferConnection::FileTransferConnection( Servent* s, ControlConnection* cc,
|
||||
QString fid, unsigned int size )
|
||||
FileTransferConnection::FileTransferConnection( Servent* s, ControlConnection* cc, QString fid, unsigned int size )
|
||||
: Connection( s )
|
||||
, m_cc( cc )
|
||||
, m_fid( fid )
|
||||
@ -27,8 +26,9 @@ FileTransferConnection::FileTransferConnection( Servent* s, ControlConnection* c
|
||||
, m_allok( false )
|
||||
{
|
||||
qDebug() << Q_FUNC_INFO;
|
||||
BufferIODevice * bio = new BufferIODevice(size);
|
||||
m_iodev = QSharedPointer<QIODevice>( bio ); // device audio data gets written to
|
||||
|
||||
BufferIODevice* bio = new BufferIODevice( size );
|
||||
m_iodev = QSharedPointer<QIODevice>( bio ); // device audio data gets written to
|
||||
m_iodev->open( QIODevice::ReadWrite );
|
||||
|
||||
APP->servent().registerFileTransferConnection( this );
|
||||
@ -41,13 +41,18 @@ FileTransferConnection::FileTransferConnection( Servent* s, ControlConnection* c
|
||||
connect( this, SIGNAL( finished() ), SLOT( deleteLater() ), Qt::QueuedConnection );
|
||||
|
||||
// don't fuck with our messages at all. No compression, no parsing, nothing:
|
||||
this->setMsgProcessorModeIn( MsgProcessor::NOTHING );
|
||||
this->setMsgProcessorModeIn ( MsgProcessor::NOTHING );
|
||||
this->setMsgProcessorModeOut( MsgProcessor::NOTHING );
|
||||
}
|
||||
|
||||
|
||||
FileTransferConnection::FileTransferConnection( Servent* s, QString fid )
|
||||
: Connection(s), m_fid(fid), m_type(SENDING), m_badded(0), m_bsent(0), m_allok( false )
|
||||
: Connection( s )
|
||||
, m_fid( fid )
|
||||
, m_type( SENDING )
|
||||
, m_badded( 0 )
|
||||
, m_bsent( 0 )
|
||||
, m_allok( false )
|
||||
{
|
||||
APP->servent().registerFileTransferConnection( this );
|
||||
// auto delete when connection closes:
|
||||
@ -65,7 +70,10 @@ FileTransferConnection::~FileTransferConnection()
|
||||
|
||||
// protected, we could expose it:
|
||||
//m_iodev->setErrorString("FTConnection providing data went away mid-transfer");
|
||||
|
||||
((BufferIODevice*)m_iodev.data())->inputComplete();
|
||||
}
|
||||
|
||||
APP->servent().fileTransferFinished( this );
|
||||
}
|
||||
|
||||
@ -73,9 +81,9 @@ FileTransferConnection::~FileTransferConnection()
|
||||
QString
|
||||
FileTransferConnection::id() const
|
||||
{
|
||||
return QString("FTC[%1 %2]")
|
||||
.arg( m_type == SENDING ? "TX" : "RX" )
|
||||
.arg(m_fid);
|
||||
return QString( "FTC[%1 %2]" )
|
||||
.arg( m_type == SENDING ? "TX" : "RX" )
|
||||
.arg( m_fid );
|
||||
}
|
||||
|
||||
|
||||
@ -85,8 +93,8 @@ FileTransferConnection::showStats( qint64 tx, qint64 rx )
|
||||
if( tx > 0 || rx > 0 )
|
||||
{
|
||||
qDebug() << id()
|
||||
<< QString("Down: %L1 bytes/sec, ").arg(rx)
|
||||
<< QString("Up: %L1 bytes/sec").arg(tx);
|
||||
<< QString( "Down: %L1 bytes/sec," ).arg( rx )
|
||||
<< QString( "Up: %L1 bytes/sec" ).arg( tx );
|
||||
}
|
||||
}
|
||||
|
||||
@ -95,7 +103,7 @@ void
|
||||
FileTransferConnection::setup()
|
||||
{
|
||||
connect( this, SIGNAL( statsTick( qint64, qint64 ) ), SLOT( showStats( qint64, qint64 ) ) );
|
||||
if(m_type == RECEIVING)
|
||||
if( m_type == RECEIVING )
|
||||
{
|
||||
qDebug() << "in RX mode";
|
||||
return;
|
||||
@ -103,25 +111,25 @@ FileTransferConnection::setup()
|
||||
|
||||
qDebug() << "in TX mode, fid:" << m_fid;
|
||||
|
||||
DatabaseCommand_LoadFile * cmd = new DatabaseCommand_LoadFile(m_fid);
|
||||
connect(cmd, SIGNAL(result(QVariantMap)), this, SLOT(startSending(QVariantMap)));
|
||||
TomahawkApp::instance()->database()->enqueue(QSharedPointer<DatabaseCommand>(cmd));
|
||||
DatabaseCommand_LoadFile* cmd = new DatabaseCommand_LoadFile( m_fid );
|
||||
connect( cmd, SIGNAL( result( QVariantMap ) ), SLOT( startSending( QVariantMap ) ) );
|
||||
TomahawkApp::instance()->database()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
FileTransferConnection::startSending( QVariantMap f )
|
||||
{
|
||||
Tomahawk::result_ptr result(new Tomahawk::Result(f, collection_ptr()));
|
||||
Tomahawk::result_ptr result( new Tomahawk::Result( f, collection_ptr() ) );
|
||||
qDebug() << "Starting to transmit" << result->url();
|
||||
QSharedPointer<QIODevice> io = TomahawkApp::instance()->getIODeviceForUrl(result);
|
||||
if(!io)
|
||||
QSharedPointer<QIODevice> io = TomahawkApp::instance()->getIODeviceForUrl( result );
|
||||
if( !io )
|
||||
{
|
||||
qDebug() << "Couldn't read from source:" << result->url();
|
||||
shutdown();
|
||||
return;
|
||||
}
|
||||
m_readdev = QSharedPointer<QIODevice>(io);
|
||||
m_readdev = QSharedPointer<QIODevice>( io );
|
||||
sendSome();
|
||||
}
|
||||
|
||||
@ -129,7 +137,7 @@ FileTransferConnection::startSending( QVariantMap f )
|
||||
void
|
||||
FileTransferConnection::handleMsg( msg_ptr msg )
|
||||
{
|
||||
Q_ASSERT(m_type == FileTransferConnection::RECEIVING);
|
||||
Q_ASSERT( m_type == FileTransferConnection::RECEIVING );
|
||||
Q_ASSERT( msg->is( Msg::RAW ) );
|
||||
|
||||
m_badded += msg->payload().length();
|
||||
@ -139,13 +147,11 @@ FileTransferConnection::handleMsg( msg_ptr msg )
|
||||
// << "payload len" << msg->payload().length()
|
||||
// << "written to device so far: " << m_badded;
|
||||
|
||||
|
||||
if( !msg->is( Msg::FRAGMENT ) )
|
||||
{
|
||||
qDebug() << endl
|
||||
<< "*** Got last msg in filetransfer. added" << m_badded
|
||||
<< "io size" << m_iodev->size()
|
||||
<< endl;
|
||||
qDebug() << "*** Got last msg in filetransfer. added" << m_badded
|
||||
<< "io size" << m_iodev->size();
|
||||
|
||||
m_allok = true;
|
||||
// tell our iodev there is no more data to read, no args meaning a success:
|
||||
((BufferIODevice*)m_iodev.data())->inputComplete();
|
||||
@ -156,23 +162,24 @@ FileTransferConnection::handleMsg( msg_ptr msg )
|
||||
|
||||
Connection* FileTransferConnection::clone()
|
||||
{
|
||||
Q_ASSERT(false); return 0;
|
||||
Q_ASSERT( false );
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void FileTransferConnection::sendSome()
|
||||
{
|
||||
Q_ASSERT(m_type == FileTransferConnection::SENDING);
|
||||
Q_ASSERT( m_type == FileTransferConnection::SENDING );
|
||||
|
||||
QByteArray ba = m_readdev->read(BLOCKSIZE);
|
||||
QByteArray ba = m_readdev->read( BLOCKSIZE );
|
||||
m_bsent += ba.length();
|
||||
//qDebug() << "Sending " << ba.length() << " bytes of audiofile";
|
||||
//qDebug() << "Sending" << ba.length() << "bytes of audiofile";
|
||||
|
||||
if( m_readdev->atEnd() )
|
||||
{
|
||||
sendMsg( Msg::factory( ba, Msg::RAW ) );
|
||||
qDebug() << "Sent all. DONE. " << m_bsent;
|
||||
shutdown(true);
|
||||
qDebug() << "Sent all. DONE." << m_bsent;
|
||||
shutdown( true );
|
||||
return;
|
||||
}
|
||||
else
|
||||
|
@ -32,10 +32,7 @@ public:
|
||||
void setup();
|
||||
Connection* clone();
|
||||
|
||||
const QSharedPointer<QIODevice>& iodevice()
|
||||
{
|
||||
return m_iodev;
|
||||
}
|
||||
const QSharedPointer<QIODevice>& iodevice() { return m_iodev; }
|
||||
|
||||
Type type() const { return m_type; }
|
||||
QString fid() const { return m_fid; }
|
||||
|
Loading…
x
Reference in New Issue
Block a user