Guest User

Untitled

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