summaryrefslogtreecommitdiffstats
path: root/src/client/net/serverconnection.h
blob: 9db9b0298a91d60fa1f5fd812e2d420e8f62ce70 (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
#ifndef _PVSSERVERCONNECTION_H_
#define _PVSSERVERCONNECTION_H_

#include <QSslSocket>
#include "../../shared/networkmessage.h"
#include "../connectwindow/connectwindow.h"

class BlankScreen;

class ServerConnection : public QObject
{
	Q_OBJECT

private:
	QSslSocket *_socket;
	BlankScreen *_blank;
	int _timerId, _timerDelete, _timerConnectionCheck;
	int _jpegQuality;
	int _authed;
	bool _autoConnect;
	int _isLocalConnection;
	qint64 _lastData;

	NetworkMessage _fromServer, _toServer;
	QByteArray _expectedFingerprint;
	QString _passwd;
	QByteArray _myChallenge;
	QByteArray _sessionName;
	QByteArray _certHash;

	void handleMsg();

	void checkLocalConnection();

public:
	ServerConnection(const QString& host, const quint16 port, const QByteArray& sessionName, const QByteArray& certHash, bool autoConnect);
	void disconnectFromServer();
	~ServerConnection();
	inline bool isConnected() const
	{
		return _socket != NULL && _socket->state() == QAbstractSocket::ConnectedState;
	}

	const inline QString getPeerAdress() const
	{
		return _socket->peerAddress().toString();
	}

	bool isLocalConnection() {
		if (_isLocalConnection == -1) {
			checkLocalConnection();
		}
		return _isLocalConnection == 1;
	}

	void sendMessage(NetworkMessage& message);
	void sendAttention(bool on);

protected:
	void timerEvent(QTimerEvent *event);

private slots:
	void sslErrors(const QList<QSslError> & errors); // triggered for errors that occur during SSL negotiation
	void sock_dataArrival(); // triggered if data is available for reading
	void sock_closed(); // triggered if the socket is closed
	void sock_error(QAbstractSocket::SocketError errcode); // triggered if an error occurs on the socket
	void sock_connected(); // triggered if the connection is established and ready to use

	void onVncServerStartStop(int port, QString& ropass, QString& rwpass); // triggered if the local vnc server was started

	void onVncViewerStartStop(const bool started, const int clientId);

signals:
	void openVnc(const QString& host, int port, const QString& passwd, bool ro, bool fullscreen, const QString& caption, const int clientId, const QByteArray& rawThumb);
	void closeVnc();
	void stateChange(ConnectWindow::ConnectionState state);
	void disconnected(ServerConnection* connection);
	void attentionChanged(bool state);
};

#endif