Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MSNSocket.h"
- namespace MSNP
- {
- MSNSocket::MSNSocket ( QObject *parent )
- : QObject ( parent )
- {
- m_state = MSNSocket::Unknown;
- m_buffer.clear();
- m_blocksize = 0;
- m_socket = new QTcpSocket;
- QObject::connect ( m_socket, SIGNAL ( readyRead() ), this, SLOT ( readCommand() ) );
- QObject::connect ( m_socket, SIGNAL ( stateChanged ( QAbstractSocket::SocketState ) ),
- this, SLOT ( socketStateChanged ( QAbstractSocket::SocketState ) ) );
- QObject::connect ( m_socket, SIGNAL ( error ( QAbstractSocket::SocketError ) ),
- this, SLOT ( socketError ( QAbstractSocket::SocketError ) ) );
- }
- MSNSocket::~MSNSocket()
- {
- }
- void
- MSNSocket::connect ( const QString& address, const quint16 port )
- {
- m_socket->connectToHost ( address, port );
- }
- void
- MSNSocket::disconnect()
- {
- m_socket->abort();
- }
- bool
- MSNSocket::sendCommand ( QString command )
- {
- if ( command.isEmpty() ) {
- return false;
- }
- bool status = false;
- m_unmodifiedCommand = command;
- if ( m_state == MSNSocket::Sending ) {
- qDebug() << "HMM";
- // arse the command string.
- m_command.parseCommand ( command, MSNCommand::Send );
- // check to see if the lengths are the same to ensure the command was sent successfully.
- status = ( m_socket->write ( m_command.toString().toUtf8() ) == m_command.toString().toUtf8().size() );
- qDebug() << "[SENT] " << m_command.toString();
- // set the state to a new one so receiving a command is possible.
- m_state = MSNSocket::Receiving;
- // emit the state for derived classes.
- emit socketState ( m_state );
- }
- return status;
- }
- void
- MSNSocket::processData ( )
- {
- // check to see if the buffer is finished.
- if ( !finishedBuffer ( ) ) {
- m_command.parseCommand ( m_buffer, MSNCommand::Read );
- if ( m_command.getError() != "No error" ) {
- emit errorState ( m_command.getErrorNumber(), m_command.getError() );
- qDebug() << QString ( "Error num: %1 Error String: %2" ).arg ( m_command.getErrorNumber() ).arg ( m_command.getError() );
- } else {
- qDebug() << "[RECV] " << m_command.toString();
- m_state = MSNSocket::Sending;
- emit socketState ( m_state );
- emit parseCommand ( m_command );
- }
- m_state = MSNSocket::Receiving;
- emit socketState ( m_state );
- }
- }
- void
- MSNSocket::readCommand()
- {
- // make sure the command isn't ready if sending is still performing.
- if ( m_state == MSNSocket::Sending ) {
- return;
- }
- // Now we can send a command.
- if ( m_state == MSNSocket::Receiving ) {
- // Get the available bytes.
- qint32 available = m_socket->bytesAvailable();
- // read only the available size.
- m_buffer = m_socket->read ( available );
- // check to see if the buffer is empty.
- if ( m_buffer.isEmpty() ) {
- qDebug() << "readCommand()::m_buffer is empty";
- } else if ( available ) {
- // check to see if the buffer size contains as much data as was available.
- if ( m_buffer.size() != available ) {
- qDebug() << QString ( "Reported %1 but only read %2" ).arg ( available ).arg ( m_buffer.size() );
- }
- // process the data that was available.
- processData ( );
- }
- }
- }
- MSNCommand&
- MSNSocket::getLastCommand()
- {
- return m_command;
- }
- MSNSocket::ConnectionState
- MSNSocket::getState() const
- {
- return m_state;
- }
- void
- MSNSocket::readIntoBlock ( qint32 blocksize, qint32 beg )
- {
- if ( m_blocksize ) {
- return;
- }
- m_blocksize = blocksize;
- m_blockBeg = beg;
- finishedBuffer ( );
- }
- void
- MSNSocket::socketStateChanged ( QAbstractSocket::SocketState state )
- {
- switch ( state ) {
- case QAbstractSocket::ConnectingState:
- qDebug() << "[STATE] " << state;
- m_state = MSNSocket::Connecting;
- emit socketState ( m_state );
- break;
- case QAbstractSocket::ConnectedState:
- qDebug() << "[STATE] " << state;
- m_state = MSNSocket::Connected;
- emit socketState ( m_state );
- m_state = MSNSocket::Sending;
- emit socketState ( m_state );
- sendCommand ( m_unmodifiedCommand );
- break;
- case QAbstractSocket::ClosingState:
- qDebug() << "[STATE] " << state;
- m_state = MSNSocket::Disconnecting;
- emit socketState ( m_state );
- break;
- case QAbstractSocket::UnconnectedState:
- qDebug() << "[STATE] " << state;
- m_state = MSNSocket::Disconnected;
- emit socketState ( m_state );
- break;
- default:
- qDebug() << "[STATE] " << state;
- break;
- }
- }
- void
- MSNSocket::socketError ( QAbstractSocket::SocketError error )
- {
- qDebug() << "[ERROR] " << error;
- }
- bool
- MSNSocket::finishedBuffer ( )
- {
- if ( !m_blocksize ) {
- return false;
- }
- if ( m_blockBuffer.size() != m_blocksize ) {
- qDebug() << QString ( "Waiting for data: Received: %1 Total: %2 / %3" ).arg ( m_socket->bytesAvailable() ).arg ( m_blocksize - m_blockBuffer.size() ).arg ( m_blocksize );
- m_blockBuffer.append ( m_buffer );
- if ( m_blockBuffer.size() >= m_blocksize ) {
- QByteArray block;
- for ( int i = 0; i < m_blocksize; ++i ) {
- block.append ( m_blockBuffer.data() [m_blockBeg + i] );
- }
- m_blockBuffer.remove ( 0, m_blocksize + m_blockBeg );
- m_command.parseCommand ( m_blockBuffer );
- qDebug() << "[RECV] " << m_command.toString();
- emit readBlock ( block );
- m_state = MSNSocket::Sending;
- emit parseCommand ( m_command );
- m_blocksize = 0;
- m_blockBuffer.clear();
- }
- return true;
- }
- return false;
- }
- } // MSNP namespace.
Add Comment
Please, Sign In to add comment