Connection.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Connection.cpp
  3. * nirc
  4. *
  5. * Created by Niklas Bölter on 02.02.10.
  6. * Copyright 2010 Frubar Corporation.
  7. *
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. *
  15. *
  16. */
  17. #include "Connection.h"
  18. Connection::Connection(int cid)
  19. {
  20. socket = new QSslSocket(this);
  21. connect(socket, SIGNAL( connected() ), this, SLOT( connected() ));
  22. connect(socket, SIGNAL( disconnected() ), this, SLOT( disconnected() ));
  23. connect(socket, SIGNAL( error(QAbstractSocket::SocketError) ), this, SLOT( socketError() ));
  24. connect(socket, SIGNAL( sslErrors(QList<QSslError>) ), this, SLOT( sslErrors(QList<QSslError>) ));
  25. connect(socket, SIGNAL( encrypted() ), this, SLOT( encrypted() ));
  26. connect(socket, SIGNAL( readyRead() ), this, SLOT( readyRead() ));
  27. this->id = cid;
  28. }
  29. void Connection::open(ConnectionSettings settings)
  30. {
  31. emit message(id, QString("Connecting to %1 on port %2").arg(settings.host).arg(settings.port));
  32. this->ssl = settings.isSSL;
  33. this->ignoreSSLErrors = settings.ignoreSSLErrors;
  34. if(this->ssl)
  35. {
  36. socket->connectToHostEncrypted(settings.host, settings.port);
  37. }
  38. else
  39. {
  40. socket->connectToHost(settings.host, settings.port);
  41. }
  42. }
  43. void Connection::connected()
  44. {
  45. if(this->ssl) return; // Wait for SSL Handshake
  46. emit connected(id);
  47. }
  48. void Connection::encrypted()
  49. {
  50. emit connected(id);
  51. }
  52. void Connection::disconnected()
  53. {
  54. emit disconnected(id);
  55. }
  56. void Connection::socketError()
  57. {
  58. emit message(id, QString("Socket Error: %1").arg(socket->errorString()));
  59. }
  60. void Connection::sslErrors(QList<QSslError> errors)
  61. {
  62. for(int i = 0; i < errors.size(); i++)
  63. {
  64. emit message(id, QString("SSL Error: %1").arg(errors.at(i).errorString()));
  65. }
  66. if (this->ignoreSSLErrors)
  67. {
  68. socket->ignoreSslErrors();
  69. emit message(id, QString("- Ignoring SSL Errors -"));
  70. }
  71. }
  72. void Connection::sendLine(QString cmd)
  73. {
  74. writeBuffer.append(QString("%1\r\n").arg(cmd).toUtf8());
  75. sendBuffer();
  76. }
  77. void Connection::sendBuffer()
  78. {
  79. if(writeBuffer.size() == 0) return;
  80. int written = socket->write(writeBuffer, writeBuffer.size());
  81. if(written == -1)
  82. {
  83. qWarning() << "Could not write to socket!";
  84. socket->disconnectFromHost();
  85. return;
  86. }
  87. if(written < writeBuffer.size())
  88. {
  89. qDebug() << "Could not write all bytes to socket!";
  90. writeBuffer = writeBuffer.mid(written);
  91. QTimer::singleShot(100, this, SLOT(sendBuffer()));
  92. return;
  93. }
  94. writeBuffer.clear();
  95. }
  96. void Connection::readyRead()
  97. {
  98. QByteArray buf = socket->readAll();
  99. readBuffer.append(QString::fromUtf8(buf.data()));
  100. emit dataReady(this->id);
  101. }
  102. void Connection::splitLines()
  103. {
  104. if(readBuffer.indexOf("\n") == -1) return;
  105. QStringList linebuffer = readBuffer.split("\n");
  106. QString line;
  107. int size = (linebuffer.size() - 1); // Do not touch last element, it is not yet terminated by a newline
  108. for(int i = 0; i < size; i++)
  109. {
  110. line = linebuffer.at(i).trimmed();
  111. if(line.size() == 0) continue;
  112. lines.enqueue(line);
  113. }
  114. readBuffer = linebuffer.at(size); // Remove all parsed lines from the buffer
  115. }
  116. bool Connection::hasLines()
  117. {
  118. return !lines.isEmpty();
  119. }
  120. QString Connection::nextLine()
  121. {
  122. // splitLines(); always call hasLines() first (!)
  123. return lines.dequeue();
  124. }
  125. int Connection::getID()
  126. {
  127. return this->id;
  128. }