Logo Search packages:      
Sourcecode: kmess version File versions


                          directconnectionbase.h -  description
    begin                : Tue 12 27 2005
    copyright            : (C) 2005 by Diederik van der Boor
    email                : vdboor --at-- codingdomain.com

 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *


#include <qobject.h>
#include <qtimer.h>

class KExtendedSocket;

 * The class for receiving and sending files.
 * @author Diederik van der Boor
 * @ingroup NetworkExtra
00032 class DirectConnectionBase : public QObject

    // The constructor
                           DirectConnectionBase(QObject *parent = 0, const char *name = 0);
    // The destructor
    virtual               ~DirectConnectionBase();

    // Close the connection
    virtual void           closeConnection();
    // Initialize the connection, return true when this was succesful
    virtual bool           initialize();
    // Return whether the connection login was successful.
    virtual bool           isAuthorized() const;
    // Return true if a connection is active
    bool                   isConnected() const;
    // Return true if this class acts as server, false if it acts as client.
    bool                   isServer() const;
    // Get the server port that will be used
    int                    getLocalServerPort();
    // Get the remote ip the socket is connected with.
    QString                getRemoteIp() const;
    // Get the remote port the socket is connected with.
    QString                getRemotePort() const;
    // Return the error description
    QString                getSocketError() const;
    // Connect to a host
    bool                   openConnection(const QString &ipAddress, int port, bool async = false);
    // Wait for an incoming connection
    bool                   openServerPort();

  protected slots:
    // This is called when the connection is established
    virtual void           slotConnectionEstablished();
    // This is called when the connection could not be made.
    virtual void           slotConnectionFailed();
    // This is called when data is received from the socket.
    virtual void           slotDataReceived() = 0;

  private slots:
    // Accept incoming connections on the socket.
    void                   slotAcceptConnection();
    // A timeout occured to connect a socket
    void                   slotConnectionTimeout();
    // Signal that the connection was established
    void                   slotSocketConnected();
    // Signal that the connection could not be made.
    virtual void           slotSocketFailed();

  protected: // Private methods
    // Register a slot that will be called each time the write buffer is empty (for autonomous/asynchonous writing).
    void                   connectWriteHandler(QObject *receiver, const char *slot);
    // Close and delete the server socket
    void                   closeServerSocket();
    // Return the number of bytes which are already received in the local buffer
    int                    getBytesAvailable() const;
    // Return the name of the locally opened port.
    QString                getListeningServiceName() const;
    // Read data from the socket
    int                    readBlock( char *buffer, const int size );
    // Read data from the socket (uses the QByteArray size() as block size)
    int                    readBlock( QByteArray &buffer, const int maxSize = 0, const int offset = 0 );
    // Mark the remote host as authorized (usually after the handshake was successful)
    void                   setAuthorized(bool authorized);
    // Write data to the socket
    bool                   writeBlock( const char *block, const int size );
    // Write data to the socket
    bool                   writeBlock( const QByteArray &block );

  private: // Private attributes
    // Whether the connection handshake was successful
    bool                   authorized_;
    // The location openConnection() is connecting to, for debugging
    QString                connectingTo_;
    // The timeout handling for openConnection()
    QTimer                 connectionTimer_;
    // Whether the class acts as server or client
    bool                   isServer_;
    // The server socket which listens for incoming connections.
    KExtendedSocket       *server_;
    // The server port this class will use
    int                    serverPort_;
    // The next server port used with a openServerPort() call.
    static int             nextServerPort;
    // The socket over which data is sent or received.
    KExtendedSocket       *socket_;
    // Whether an timeout occured
    bool                   timeout_;
    // True if the user cancelled the session
    bool                   userCancelled_;

    // Signal that the connection was autorized
    void                   connectionAuthorized();
    // Signal that the connection was closed
    void                   connectionClosed();
    // Signal that the connection was established
    void                   connectionEstablished();
    // Signal that the connection was failed
    void                   connectionFailed();


Generated by  Doxygen 1.6.0   Back to index