summaryrefslogtreecommitdiffstats
path: root/src/net/pvsClientConnection.cpp
blob: 9f66562e18ee96d3d658e164b118f3a79770590b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
# Copyright (c) 2009,2010 - OpenSLX Project, Computer Center University of
# Freiburg
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
# pvsClientConnection.cpp
#    - ???.
# -----------------------------------------------------------------------------
*/

#include "pvsClientConnection.h"
#include "pvsListenServer.h"
#include "src/util/consoleLogger.h"

#include <cassert>
#include <QtNetwork/QHostAddress>


PVSClientConnection::PVSClientConnection(PVSListenServer* server, QSslSocket* sock)
{
    assert(sock);
    assert(server);
    _socket = sock;
    _incomplete = NULL;
    _toldDisconnect = false;
    _server = server;
    _lastData = time(NULL);
    sock->setParent(this); // for automatic deletion
    connect(_socket, SIGNAL(readyRead()), this, SLOT(sock_dataArrival()));
    connect(_socket, SIGNAL(disconnected()), this, SLOT(sock_closed()));
    connect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(sock_error(QAbstractSocket::SocketError)));
}

PVSClientConnection::~PVSClientConnection()
{
    this->closeConnection();
    if (_incomplete != NULL) delete _incomplete;
    if (_socket != NULL)
    {
        disconnect(_socket, SIGNAL(readyRead()), this, SLOT(sock_dataArrival()));
        disconnect(_socket, SIGNAL(disconnected()), this, SLOT(sock_closed()));
        disconnect(_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(sock_error(QAbstractSocket::SocketError)));
    	_socket->abort();
    }
}

void PVSClientConnection::closeConnection()
{
	if (_toldDisconnect) return; // we already got served
    if (_socket == NULL || _socket->state() == QAbstractSocket::UnconnectedState)
    {
        // should not happen but handled anyway
        _toldDisconnect = true;
        _server->disconnectClient(this);
        return;
    }
    if (_socket->state() == QAbstractSocket::ClosingState) return; // sock_close() slot should eventually be called, nothing to do
    if (_socket->state() == QAbstractSocket::ConnectedState)   // this is what should be the case normally
    {
        _socket->disconnectFromHost(); // graceful shutdown of socket, sock_close() will be called once complete
        return;
    }
    // otherwise the socket is in some unknown state, so do a harsh shutdown
    _socket->abort();
    _toldDisconnect = true;
    _server->disconnectClient(this);
}

void PVSClientConnection::sock_dataArrival()
{
    if (_socket == NULL || _socket->state() != QAbstractSocket::ConnectedState)
    {
        ConsoleLog writeError("dataArrival called in bad state");
        return;
    }

    _lastData = time(NULL);
    while (_socket->bytesAvailable())
    {
        int retval = 0;
        do
        {
            if (_incomplete == NULL) _incomplete = new PVSMsg(); // we need a pvsmsg object
            retval = _incomplete->readMessage(_socket); // let the message read data from socket
            if (retval == -1)   // error parsing msg, disconnect client!
            {
                this->closeConnection();
                return;
            }
            if (retval == 1)   // message is complete
            {
                _server->handleClientMsg(_id, *_incomplete);
                delete _incomplete; // ...and delete...
                _incomplete = NULL; // ...so the next msg can be parsed
            }
        } while (retval == 1);
    }
}

void PVSClientConnection::sock_closed()
{
    // should this be unreliable in some way i suggest using the signal "stateChanged()" instead
    // and check if the state changed to unconnected.
    if (_toldDisconnect) return;
    _toldDisconnect = true;
    _server->disconnectClient(this);
}

void PVSClientConnection::sock_error(QAbstractSocket::SocketError errcode)
{
    this->closeConnection();
}

/*
void PVSClientConnection::push_back_receive(PVSMsg newMsg)
{
    _recQueue.push_back(newMsg);
}
*/

bool PVSClientConnection::push_back_send(PVSMsg newMsg)
{
    if (_socket == NULL || _socket->state() != QAbstractSocket::ConnectedState) return false;
    newMsg.setSndID(_id);
    int len;
    char data[2000];
    char *tmp = data;
    if (!newMsg.getBinaryData(tmp, len))
    {
        qDebug("Message empty. Ignored.");
        return false;
    }
    QByteArray ba;
    ba.append(tmp, len);
    int ret = (int)_socket->write(ba);
    //printf("Sent %d of %d bytes.\n", ret, len);
    if (ret == -1)
    {
        this->closeConnection();
        return false;
    }
    return true;
}

QSslSocket* PVSClientConnection::getSocket()
{
    return _socket;
}

void PVSClientConnection::setID(unsigned int id)
{
    _id = id;
    this->push_back_send(PVSMsg(PVSLOGIN, "ID", int2String(_id)));
}

void PVSClientConnection::ping()
{
    this->push_back_send(PVSMsg(PVSCOMMAND, "PING", "HELLO?", _id));
}

QString PVSClientConnection::getAddress()
{
    if (_socket == NULL) return QString();
    return _socket->peerAddress().toString();
}

QString PVSClientConnection::getNameUser()
{
	return getUserName();
}