| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by The qGo Project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | #include "quickconnection.h" |
| 24 | #include "networkconnection.h" |
| 25 | |
| 26 | QuickConnection::QuickConnection(QString hostname, qint16 port, void * m, NetworkConnection * c, QuickConnectionType t) : |
| 27 | msginfo(m), connection(c), type(t) |
| 28 | { |
| 29 | qsocket = new QTcpSocket(); //try with no parent passed for now |
| 30 | if(!qsocket) |
| 31 | { |
| 32 | success = 0; |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | connect(qsocket, &QTcpSocket::connected, this, &QuickConnection::OnConnected); |
| 37 | connect(qsocket, &QTcpSocket::readyRead, this, &QuickConnection::OnReadyRead); |
| 38 | connect(qsocket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error), this, &QuickConnection::OnError); |
| 39 | |
| 40 | qDebug() << QString("QuickConnection::QuickConnection : Connecting to") << hostname << ":" << port; |
| 41 | |
| 42 | qsocket->connectToHost(hostname, port); |
| 43 | |
| 44 | success = !(qsocket->state() != QTcpSocket::UnconnectedState); |
| 45 | } |
| 46 | |
| 47 | QuickConnection::QuickConnection(QTcpSocket * q, void * m, NetworkConnection * c, QuickConnectionType t) : |
| 48 | qsocket(q), msginfo(m), connection(c), type(t) |
| 49 | { |
| 50 | disconnect(qsocket, 0, connection, 0); |
| 51 | |
| 52 | connect(qsocket, &QTcpSocket::connected, this, &QuickConnection::OnConnected); |
| 53 | connect(qsocket, &QTcpSocket::readyRead, this, &QuickConnection::OnReadyRead); |
| 54 | connect(qsocket, QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error), this, &QuickConnection::OnError); |
| 55 | |
| 56 | success = !(qsocket->state() != QTcpSocket::UnconnectedState); |
| 57 | if(success != 0) |
| 58 | { |
| 59 | qDebug("QC passed bad connection"); |
| 60 | return; |
| 61 | } |
| 62 | OnConnected(); |
| 63 | } |
| 64 | |
| 65 | int QuickConnection::checkSuccess(void) |
| 66 | { |
| 67 | return success; |
| 68 | } |
| 69 | |
| 70 | void QuickConnection::OnConnected(void) |
| 71 | { |
| 72 | int size; |
| 73 | char * packet = 0; |
| 74 | //qDebug("QC: OnConnected()"); |
| 75 | switch(type) |
| 76 | { |
| 77 | case sendRequestAccountInfo: |
| 78 | packet = connection->sendRequestAccountInfo(&size, msginfo); |
| 79 | break; |
| 80 | case sendAddFriend: |
| 81 | packet = connection->sendAddFriend(&size, msginfo); |
| 82 | break; |
| 83 | case sendRemoveFriend: |
| 84 | packet = connection->sendRemoveFriend(&size, msginfo); |
| 85 | break; |
| 86 | case sendAddBlock: |
| 87 | packet = connection->sendAddFriend(&size, msginfo); |
| 88 | break; |
| 89 | case sendRemoveBlock: |
| 90 | packet = connection->sendRemoveFriend(&size, msginfo); |
| 91 | break; |
| 92 | } |
| 93 | if (qsocket->write(packet, size) < 0) |
| 94 | qDebug("QuickConnection write failed"); |
| 95 | delete[] packet; |
| 96 | } |
| 97 | |
| 98 | void QuickConnection::OnReadyRead(void) |
| 99 | { |
| 100 | int bytes = qsocket->bytesAvailable(); |
| 101 | if(bytes > 1) |
| 102 | { |
| 103 | char * packet = new char[bytes]; |
| 104 | qsocket->read(packet, bytes); |
| 105 | switch(type) |
| 106 | { |
| 107 | case sendRequestAccountInfo: |
| 108 | connection->handleAccountInfoMsg(bytes, packet); |
| 109 | break; |
| 110 | case sendAddFriend: |
| 111 | case sendRemoveFriend: |
| 112 | case sendAddBlock: |
| 113 | case sendRemoveBlock: |
| 114 | connection->recvFriendResponse(bytes, packet); |
| 115 | break; |
| 116 | } |
| 117 | delete[] packet; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void QuickConnection::OnError(QAbstractSocket::SocketError err) |
| 122 | { |
| 123 | switch(err) |
| 124 | { |
| 125 | case QTcpSocket::ConnectionRefusedError: qDebug("QuickConnection::OnError : connection refused"); |
| 126 | break; |
| 127 | case QTcpSocket::HostNotFoundError: qDebug("QuickConnection::OnError : host not found"); |
| 128 | break; |
| 129 | case QTcpSocket::SocketTimeoutError: qDebug("QuickConnection::OnError : socket time out"); |
| 130 | break; |
| 131 | case QTcpSocket::RemoteHostClosedError: qDebug("QuickConnection::OnError : connection closed by host"); |
| 132 | break; |
| 133 | default: qDebug("QuickConnection::OnError : unknown error"); |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | QuickConnection::~QuickConnection() |
| 139 | { |
| 140 | if(qsocket) |
| 141 | { |
| 142 | qsocket->close(); |
| 143 | qsocket->deleteLater(); |
| 144 | } |
| 145 | } |