#include "MSNSocket.hpp"
namespace MSNP
{
MSNSocket
::MSNSocket( QObject *parent
){
m_canWrite = true;
m_canRead = false;
m_connected = false;
m_blocksize = 0;
m_buffer.clear();
QObject::connect( m_socket,
SIGNAL( connected
() ),
this, SLOT( msnConnected() ) );
QObject::connect( m_socket,
SIGNAL( disconnected
() ),
this, SLOT( msnDisconnected() ) );
QObject::connect( m_socket,
SIGNAL( readyRead
() ),
this, SLOT( receiveData() ) );
}
MSNSocket::~MSNSocket()
{
}
void MSNSocket::connect( const QString& address, const quint16 port )
{
if ( isConnected() ) {
return;
}
m_blocksize = 0;
m_buffer.clear();
m_socket->connectToHost( address, port );
}
void MSNSocket::disconnect()
{
if ( !isConnected() ) {
return;
}
m_socket->abort();
}
void MSNSocket
::sendCommand( QString command, MSNSocket
::CommandState state
) {
if ( !m_socket || !m_canWrite)
return;
m_msnCommand.parseCommand( command, true );
MSNLog::writeSend(m_msnCommand.toString());
m_socket->write( m_msnCommand.toString().toUtf8() );
m_canWrite = false;
m_canRead = true;
emit parseCommand( m_msnCommand );
}
void MSNSocket::readBlock( quint16 size )
{
if ( m_blocksize ) {
return;
}
m_blocksize = size;
checkBlock();
}
bool MSNSocket::isConnected()
{
return m_connected;
}
void MSNSocket::setConnected( bool status )
{
if ( m_connected == status ) {
return;
}
m_connected = status;
}
MSNCommand& MSNSocket::getLastCommand()
{
return m_msnCommand;
}
void MSNSocket::msnConnected()
{
m_connected = true;
emit connected();
}
void MSNSocket::msnDisconnected()
{
setConnected( false );
emit disconnected();
}
void MSNSocket::receiveData()
{
quint64 available = m_socket->bytesAvailable();
qDebug() << available;
char *buffer = new char[available + 1];
quint64 readBytes = m_socket->read( buffer, available );
m_canWrite = true;
m_canRead = false;
m_msnCommand.parseCommand( buffer, false );
MSNLog::writeReceive(m_msnCommand.toString());
if ( readBytes > 0 ) {
MSNLog
::writeInfo( QString( "Bytes read returned %1" ).
arg( readBytes
) );
} else if ( readBytes == 0 ) {
MSNLog
::writeWarning( QString( "Bytes returned no data" ) );
} else {
if ( available ) {
if ( available != readBytes ) {
MSNLog::writeWarning(
QString( "Bytes read reported %1 bytes available but only read %2" ).
arg( available
).
arg( readBytes
) );
}
}
m_buffer.append( buffer );
}
// clean up.
delete []buffer;
}
bool MSNSocket::checkBlock()
{
if ( !m_blocksize ) {
return false;
} else if ( static_cast<quint64>( m_buffer.size() ) < m_blocksize ) {
MSNLog
::writeInfo( QString( "Waiting for data %1. Received %2/%3" ).
arg( m_buffer.
size() ).
arg( m_blocksize
) );
return true;
}
m_blocksize = 0;
emit blockRead( block );
return false;
}
} // End of MSNP
#include "MSNSocket.hpp"
namespace MSNP
{
MSNSocket::MSNSocket( QObject *parent )
: QObject( parent )
{
m_canWrite = true;
m_canRead = false;
m_connected = false;
m_blocksize = 0;
m_buffer.clear();
m_socket = new QTcpSocket;
QObject::connect( m_socket, SIGNAL( connected() ),
this, SLOT( msnConnected() ) );
QObject::connect( m_socket, SIGNAL( disconnected() ),
this, SLOT( msnDisconnected() ) );
QObject::connect( m_socket, SIGNAL( readyRead() ),
this, SLOT( receiveData() ) );
QObject::connect( m_socket, SIGNAL( error( QAbstractSocket::SocketError ) ),
this, SLOT( socketError( QAbstractSocket::SocketError ) ) );
QObject::connect( m_socket, SIGNAL( stateChanged( QAbstractSocket::SocketState ) ),
this, SLOT( stateWasChanged( QAbstractSocket::SocketState ) ) );
}
MSNSocket::~MSNSocket()
{
}
void MSNSocket::connect( const QString& address, const quint16 port )
{
if ( isConnected() ) {
return;
}
m_blocksize = 0;
m_buffer.clear();
m_socket->connectToHost( address, port );
}
void MSNSocket::disconnect()
{
if ( !isConnected() ) {
return;
}
m_socket->abort();
}
void MSNSocket::sendCommand( QString command, MSNSocket::CommandState state )
{
if ( !m_socket || !m_canWrite)
return;
m_msnCommand.parseCommand( command, true );
MSNLog::writeSend(m_msnCommand.toString());
m_socket->write( m_msnCommand.toString().toUtf8() );
m_canWrite = false;
m_canRead = true;
emit parseCommand( m_msnCommand );
}
void MSNSocket::readBlock( quint16 size )
{
if ( m_blocksize ) {
return;
}
m_blocksize = size;
checkBlock();
}
bool MSNSocket::isConnected()
{
return m_connected;
}
void MSNSocket::setConnected( bool status )
{
if ( m_connected == status ) {
return;
}
m_connected = status;
}
MSNCommand& MSNSocket::getLastCommand()
{
return m_msnCommand;
}
void MSNSocket::msnConnected()
{
m_connected = true;
emit connected();
}
void MSNSocket::msnDisconnected()
{
setConnected( false );
emit disconnected();
}
void MSNSocket::receiveData()
{
quint64 available = m_socket->bytesAvailable();
qDebug() << available;
char *buffer = new char[available + 1];
quint64 readBytes = m_socket->read( buffer, available );
m_canWrite = true;
m_canRead = false;
m_msnCommand.parseCommand( buffer, false );
MSNLog::writeReceive(m_msnCommand.toString());
if ( readBytes > 0 ) {
MSNLog::writeInfo( QString( "Bytes read returned %1" ).arg( readBytes ) );
} else if ( readBytes == 0 ) {
MSNLog::writeWarning( QString( "Bytes returned no data" ) );
} else {
if ( available ) {
if ( available != readBytes ) {
MSNLog::writeWarning(
QString( "Bytes read reported %1 bytes available but only read %2" ).arg( available ).arg( readBytes ) );
}
}
m_buffer.append( buffer );
}
// clean up.
delete []buffer;
}
bool MSNSocket::checkBlock()
{
if ( !m_blocksize ) {
return false;
} else if ( static_cast<quint64>( m_buffer.size() ) < m_blocksize ) {
MSNLog::writeInfo( QString( "Waiting for data %1. Received %2/%3" ).arg( m_buffer.size() ).arg( m_blocksize ) );
return true;
}
QByteArray block = m_buffer.take( m_blocksize );
m_blocksize = 0;
emit blockRead( block );
return false;
}
} // End of MSNP
To copy to clipboard, switch view to plain text mode
Bookmarks