Guest User

Untitled

a guest
Apr 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.31 KB | None | 0 0
  1. #include "MSNSocket.h"
  2.  
  3. namespace MSNP
  4. {
  5. MSNSocket::MSNSocket ( QObject *parent )
  6. : QObject ( parent )
  7. {
  8. m_state = MSNSocket::Unknown;
  9. m_buffer.clear();
  10. m_blocksize = 0;
  11. m_socket = new QTcpSocket;
  12.  
  13. QObject::connect ( m_socket, SIGNAL ( readyRead() ), this, SLOT ( readCommand() ) );
  14. QObject::connect ( m_socket, SIGNAL ( stateChanged ( QAbstractSocket::SocketState ) ),
  15. this, SLOT ( socketStateChanged ( QAbstractSocket::SocketState ) ) );
  16. QObject::connect ( m_socket, SIGNAL ( error ( QAbstractSocket::SocketError ) ),
  17. this, SLOT ( socketError ( QAbstractSocket::SocketError ) ) );
  18. }
  19.  
  20. MSNSocket::~MSNSocket()
  21. {
  22. }
  23.  
  24. void
  25. MSNSocket::connect ( const QString& address, const quint16 port )
  26. {
  27. m_socket->connectToHost ( address, port );
  28. }
  29.  
  30. void
  31. MSNSocket::disconnect()
  32. {
  33. m_socket->abort();
  34. }
  35.  
  36. bool
  37. MSNSocket::sendCommand ( QString command )
  38. {
  39. if ( command.isEmpty() ) {
  40. return false;
  41. }
  42.  
  43. bool status = false;
  44. m_unmodifiedCommand = command;
  45.  
  46. if ( m_state == MSNSocket::Sending ) {
  47. qDebug() << "HMM";
  48. // arse the command string.
  49. m_command.parseCommand ( command, MSNCommand::Send );
  50. // check to see if the lengths are the same to ensure the command was sent successfully.
  51. status = ( m_socket->write ( m_command.toString().toUtf8() ) == m_command.toString().toUtf8().size() );
  52. qDebug() << "[SENT] " << m_command.toString();
  53. // set the state to a new one so receiving a command is possible.
  54. m_state = MSNSocket::Receiving;
  55. // emit the state for derived classes.
  56. emit socketState ( m_state );
  57. }
  58. return status;
  59. }
  60.  
  61. void
  62. MSNSocket::processData ( )
  63. {
  64. // check to see if the buffer is finished.
  65. if ( !finishedBuffer ( ) ) {
  66. m_command.parseCommand ( m_buffer, MSNCommand::Read );
  67. if ( m_command.getError() != "No error" ) {
  68. emit errorState ( m_command.getErrorNumber(), m_command.getError() );
  69. qDebug() << QString ( "Error num: %1 Error String: %2" ).arg ( m_command.getErrorNumber() ).arg ( m_command.getError() );
  70. } else {
  71. qDebug() << "[RECV] " << m_command.toString();
  72. m_state = MSNSocket::Sending;
  73. emit socketState ( m_state );
  74. emit parseCommand ( m_command );
  75. }
  76. m_state = MSNSocket::Receiving;
  77. emit socketState ( m_state );
  78. }
  79. }
  80.  
  81. void
  82. MSNSocket::readCommand()
  83. {
  84. // make sure the command isn't ready if sending is still performing.
  85. if ( m_state == MSNSocket::Sending ) {
  86. return;
  87. }
  88.  
  89. // Now we can send a command.
  90. if ( m_state == MSNSocket::Receiving ) {
  91. // Get the available bytes.
  92. qint32 available = m_socket->bytesAvailable();
  93.  
  94. // read only the available size.
  95. m_buffer = m_socket->read ( available );
  96.  
  97. // check to see if the buffer is empty.
  98. if ( m_buffer.isEmpty() ) {
  99. qDebug() << "readCommand()::m_buffer is empty";
  100. } else if ( available ) {
  101. // check to see if the buffer size contains as much data as was available.
  102. if ( m_buffer.size() != available ) {
  103. qDebug() << QString ( "Reported %1 but only read %2" ).arg ( available ).arg ( m_buffer.size() );
  104. }
  105. // process the data that was available.
  106. processData ( );
  107. }
  108. }
  109. }
  110.  
  111. MSNCommand&
  112. MSNSocket::getLastCommand()
  113. {
  114. return m_command;
  115. }
  116.  
  117. MSNSocket::ConnectionState
  118. MSNSocket::getState() const
  119. {
  120. return m_state;
  121. }
  122.  
  123. void
  124. MSNSocket::readIntoBlock ( qint32 blocksize, qint32 beg )
  125. {
  126. if ( m_blocksize ) {
  127. return;
  128. }
  129.  
  130. m_blocksize = blocksize;
  131. m_blockBeg = beg;
  132.  
  133. finishedBuffer ( );
  134. }
  135.  
  136. void
  137. MSNSocket::socketStateChanged ( QAbstractSocket::SocketState state )
  138. {
  139. switch ( state ) {
  140. case QAbstractSocket::ConnectingState:
  141. qDebug() << "[STATE] " << state;
  142. m_state = MSNSocket::Connecting;
  143. emit socketState ( m_state );
  144. break;
  145. case QAbstractSocket::ConnectedState:
  146. qDebug() << "[STATE] " << state;
  147. m_state = MSNSocket::Connected;
  148. emit socketState ( m_state );
  149. m_state = MSNSocket::Sending;
  150. emit socketState ( m_state );
  151. sendCommand ( m_unmodifiedCommand );
  152. break;
  153. case QAbstractSocket::ClosingState:
  154. qDebug() << "[STATE] " << state;
  155. m_state = MSNSocket::Disconnecting;
  156. emit socketState ( m_state );
  157. break;
  158. case QAbstractSocket::UnconnectedState:
  159. qDebug() << "[STATE] " << state;
  160. m_state = MSNSocket::Disconnected;
  161. emit socketState ( m_state );
  162. break;
  163. default:
  164. qDebug() << "[STATE] " << state;
  165. break;
  166. }
  167. }
  168.  
  169. void
  170. MSNSocket::socketError ( QAbstractSocket::SocketError error )
  171. {
  172. qDebug() << "[ERROR] " << error;
  173. }
  174.  
  175. bool
  176. MSNSocket::finishedBuffer ( )
  177. {
  178. if ( !m_blocksize ) {
  179. return false;
  180. }
  181.  
  182. if ( m_blockBuffer.size() != m_blocksize ) {
  183. qDebug() << QString ( "Waiting for data: Received: %1 Total: %2 / %3" ).arg ( m_socket->bytesAvailable() ).arg ( m_blocksize - m_blockBuffer.size() ).arg ( m_blocksize );
  184. m_blockBuffer.append ( m_buffer );
  185.  
  186. if ( m_blockBuffer.size() >= m_blocksize ) {
  187. QByteArray block;
  188.  
  189. for ( int i = 0; i < m_blocksize; ++i ) {
  190. block.append ( m_blockBuffer.data() [m_blockBeg + i] );
  191. }
  192.  
  193. m_blockBuffer.remove ( 0, m_blocksize + m_blockBeg );
  194.  
  195. m_command.parseCommand ( m_blockBuffer );
  196.  
  197. qDebug() << "[RECV] " << m_command.toString();
  198.  
  199. emit readBlock ( block );
  200. m_state = MSNSocket::Sending;
  201. emit parseCommand ( m_command );
  202.  
  203. m_blocksize = 0;
  204. m_blockBuffer.clear();
  205. }
  206. return true;
  207. }
  208. return false;
  209. }
  210.  
  211. } // MSNP namespace.
Add Comment
Please, Sign In to add comment