summaryrefslogtreecommitdiffstats
path: root/src/net/pvsServerConnection.h
blob: 017e485c9fd8aa646ac642e26bc413e32eb6d56e (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
/**
  *              PVSServerConnection
  *
  *	holds and maintains a Connection to a PVSListenServer (or derivate)
  *
  */



#ifndef _PVSSERVERCONNECTION_H_
#define _PVSSERVERCONNECTION_H_

#include "src/util/dispatcher.h"
#include "src/net/pvsMsg.h"
#include <QtNetwork/QSslSocket>

class PVS;
class PVSDiscoveredServer;

class PVSServerConnection : public QObject
{
    Q_OBJECT
public:
    PVSServerConnection(PVS *parent);
    ~PVSServerConnection() {}
    bool connectToServer(PVSDiscoveredServer* server, QString passwd = QString());
    void disconnectFromServer();
    bool isConnected()
    {
        return _socket != NULL && _socket->state() == QAbstractSocket::ConnectedState;
    }

    QString getServerName();

    virtual void loadCommands();
    void onID(PVSMsg idmsg);
    void onPing(PVSMsg pingmsg);
    template<class T> void addLoginHandler(QString ident, T* who, void (T :: *func)(PVSMsg))
    {
        _loginDispatcher.addListener(ident, who, func);
    };
    template<class T> void removeLoginHandler(QString ident, T* who, void (T :: *func)(PVSMsg))
    {
        _loginDispatcher.removeListener(ident, who, func);
    };
    template<class T> void addChatHandler(QString ident, T* who, void (T :: *func)(PVSMsg))
    {
        _chatDispatcher.addListener(ident, who, func);
    };
    template<class T> void removeChatHandler(QString ident, T* who, void (T :: *func)(PVSMsg))
    {
        _chatDispatcher.removeListener(ident, who, func);
    };
    template<class T> void addCommandHandler(QString ident, T* who, void (T :: *func)(PVSMsg))
    {
        _commandDispatcher.addListener(ident, who, func);
    };
    template<class T> void removeCommandHandler(QString ident, T* who, void (T :: *func)(PVSMsg))
    {
        _commandDispatcher.removeListener(ident, who, func);
    };

public Q_SLOTS:
	void sendMessage(PVSMsg newMessage);

protected:
    void timerEvent(QTimerEvent *event);

private Q_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

private:

    inline void timerStop();
    void handleClientMsg(PVSMsg receiver);
    void handleDisconnectInternal();

    EventIdentDispatcher<PVSMsg> _commandDispatcher;
    EventIdentDispatcher<PVSMsg> _chatDispatcher;
    EventIdentDispatcher<PVSMsg> _loginDispatcher;
    PVSMsg *_incomplete;
    QSslSocket *_socket;
    unsigned int _id;
    int _timerId;
    //QString _host;
    //quint16 _port;
    int _timer;
    PVS *_client;
    bool _wasConnected;
    QByteArray _expectedFingerprint;
    QString _name;
    QString _passwd;
    time_t _lastData;
};

#endif