summaryrefslogblamecommitdiffstats
path: root/src/net/pvsServerConnection.h
blob: 017e485c9fd8aa646ac642e26bc413e32eb6d56e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13












                                                                           
                           

                               















                                                                                      





























                                                                                                 


                                            






























                                                                                                              
                     


      
/**
  *              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