Guest User

Untitled

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