Guest User

Untitled

a guest
Apr 23rd, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 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. : QTcpSocket ( parent )
  27. {
  28. m_state = MSNSocket::Unknown;
  29. m_unmodifiedCommand.clear();
  30. m_buffer.clear();
  31. m_blocksize = 0;
  32.  
  33. QObject::connect ( this, SIGNAL ( readyRead() ),
  34. this, SLOT ( readCommand() ) );
  35. QObject::connect ( this, SIGNAL ( stateChanged ( QAbstractSocket::SocketState ) ),
  36. this, SLOT ( socketStateChanged ( QAbstractSocket::SocketState ) ) );
  37. QObject::connect ( this, SIGNAL ( error ( QAbstractSocket::SocketError ) ),
  38. this, SLOT ( socketError ( QAbstractSocket::SocketError ) ) );
  39. }
  40.  
  41. MSNSocket::~MSNSocket()
  42. {
  43. }
  44.  
  45. void
  46. MSNSocket::connect ( const QString& address, const quint16 port )
  47. {
  48. connectToHost ( address, port );
  49. }
  50.  
  51. void
  52. MSNSocket::disconnect()
  53. {
  54. abort();
  55. }
  56.  
  57. bool
  58. MSNSocket::sendCommand ( QString command )
  59. {
  60. if ( command.isEmpty() ) {
  61. return false;
  62. }
  63.  
  64. bool status = false;
  65. m_unmodifiedCommand = command;
  66.  
  67. if ( m_state == MSNSocket::Sending ) {
  68. m_command.parseCommand ( command, MSNCommand::Send );
  69. status = ( write ( m_command.toString().toUtf8() ) == m_command.toString().toUtf8().size() );
  70. qDebug() << "<<< " << m_command.toString();
  71.  
  72. m_state = MSNSocket::Receiving;
  73. emit socketState ( m_state );
  74. }
  75. return status;
  76. }
  77.  
  78. void
  79. MSNSocket::readCommand()
  80. {
  81. if ( m_state == MSNSocket::Sending ) {
  82. return;
  83. }
  84.  
  85. if ( m_state == MSNSocket::Receiving ) {
  86. qint32 available = bytesAvailable();
  87.  
  88. m_buffer = readAll();
  89.  
  90. if(m_buffer.isEmpty()) {
  91. qDebug() << "m_buffer is empty.";
  92. } else if(available) {
  93. if(m_buffer.size() != available) {
  94. qDebug() << QString("Reported %1 but only read %2").arg(available).arg(m_buffer.size());
  95. }
  96. processData();
  97. }
  98. }
  99. }
  100.  
  101. MSNCommand&
  102. MSNSocket::getLastCommand()
  103. {
  104. return m_command;
  105. }
  106.  
  107. MSNSocket::ConnectionState
  108. MSNSocket::getState() const
  109. {
  110. return m_state;
  111. }
  112.  
  113. void
  114. MSNSocket::socketStateChanged ( QAbstractSocket::SocketState state )
  115. {
  116. switch ( state ) {
  117. case QAbstractSocket::ConnectingState:
  118. //qDebug() << "[STATE]" << state;
  119. m_state = MSNSocket::Connecting;
  120. emit socketState ( m_state );
  121. break;
  122. case QAbstractSocket::ConnectedState:
  123. //qDebug() << "[STATE]" << state;
  124. m_state = MSNSocket::Connected;
  125. emit socketState ( m_state );
  126. m_state = MSNSocket::Sending;
  127. emit socketState ( m_state );
  128. sendCommand ( m_unmodifiedCommand );
  129. break;
  130. case QAbstractSocket::ClosingState:
  131. //qDebug() << "[STATE]" << state;
  132. m_state = MSNSocket::Disconnecting;
  133. emit socketState ( m_state );
  134. break;
  135. case QAbstractSocket::UnconnectedState:
  136. //qDebug() << "[STATE]" << state;
  137. m_state = MSNSocket::Disconnected;
  138. emit socketState ( m_state );
  139. default:
  140. //qDebug() << "[STATE]" << state;
  141. break;
  142. }
  143. }
  144.  
  145. void
  146. MSNSocket::socketError ( QAbstractSocket::SocketError error )
  147. {
  148. qDebug() << error;
  149. }
  150.  
  151. void
  152. MSNSocket::processData()
  153. {
  154. if(!finishBuffer()) {
  155. if(m_buffer.contains("\r\n")) {
  156. m_buffer.replace("\r\n", "");
  157. m_command.parseCommand(m_buffer, MSNCommand::Read);
  158. if(m_command.getError() != "No error") {
  159. qDebug() << ">>> " << m_command.toString();
  160. emit errorState(m_command.getErrorNumber(), m_command.getError());
  161. } else {
  162. qDebug() << ">>> " << m_command.toString();
  163. processData();
  164. m_state = MSNSocket::Sending;
  165. emit socketState(m_state);
  166. emit parseCommand(m_command);
  167. }
  168. m_state = MSNSocket::Receiving;
  169. emit socketState(m_state);
  170. }
  171. }
  172. }
  173.  
  174. void
  175. MSNSocket::connected()
  176. {
  177. }
  178.  
  179. void
  180. MSNSocket::disconnected()
  181. {
  182. }
  183.  
  184. void
  185. MSNSocket::readIntoBuffer ( qint32 buffer )
  186. {
  187. if(m_blocksize) {
  188. return;
  189. }
  190.  
  191. m_blocksize = buffer;
  192. qDebug() << "m_blocksize: " << m_blocksize;
  193.  
  194. finishBuffer();
  195. }
  196.  
  197. bool
  198. MSNSocket::finishBuffer()
  199. {
  200. if(!m_blocksize) {
  201. return false;
  202. } else if(m_blockBuffer.size() <= m_blocksize) {
  203. qDebug() << "Waiting for data. Received: " << m_blockBuffer.size() << ". Required: " << m_blocksize << "Left: " <<
  204. m_blocksize - m_blockBuffer.size();
  205. m_blockBuffer.append(m_buffer);
  206. m_buffer.clear();
  207. qDebug() << m_blockBuffer;
  208. return true;
  209. }
  210.  
  211. qDebug() << "Hmm: " << m_blockBuffer;
  212.  
  213. QByteArray block;
  214.  
  215. for(int i = 0; i < m_blocksize; ++i) {
  216. block.append(m_blockBuffer[i]);
  217. }
  218.  
  219. m_blocksize = 0;
  220. emit readBlock(block);
  221. m_blockBuffer.clear();
  222. return false;
  223. }
  224.  
  225. } // MSNP namespace.
Add Comment
Please, Sign In to add comment