Logo Search packages:      
Sourcecode: kmess version File versions


                          msnconnection.h  -  description
    begin                : Thu Jan 23 2003
    copyright            : (C) 2003 by Mike K. Bennett
    email                : mkb137b@hotmail.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 "kmessbuffer.h"
#include "multipacketmessage.h"

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

#include <kextsock.h>

#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <string.h>

// Forward declarations
class CurrentAccount;
class KMessTest;
class MimeMessage;
class QMutex;

 * The class provides basic networking commands to the particular connection types.
 * @author Mike K. Bennett
 * @ingroup NetworkCore
00050 class MsnConnection : public QObject

  friend class KMessTest;

    //only when Dest Server is connected(not just Proxy Server), throw CONNTECTED

    // The destructor
    // Whether or not the class is connected
    bool                 isConnected() const;

  protected: // Protected methods
    // Ack type parameter for sendMimeMessage()
    enum AckType
      // If ordering is changed, update sendMimeMessage()

    // The constructor
                         MsnConnection(QString identifier);
    // Replace any "%" values with their regular values, i.e. "%20" to " ".
    void                 addPercents(QString &word) const;
    // Close the connection.
    virtual void         closeConnection() = 0;
    // Connect to the given server via the socket
    bool                 connectToServer(const QString& server, const int& port);
    // Disconnect from the server, if connected
    void                 disconnectFromServer( bool isTransfer = false );
    // Get the IP address of this machine.
    QString              getLocalIp() const;
    // Get whether we're sending pings or not
    bool                 getSendPings() const;
    // Extract some information from a message (like "Name: Joe" - extract the "Joe")
    QString              getValueFromMessage(const QString& field, const QString& message, bool goToEndOfLine = false) const;
    // Initialize the object
    bool                 initialize();
    // Test whether the given command is a payload command
    virtual bool         isPayloadCommand(const QString &command) const;
    // Parse a regular command
    virtual void         parseCommand(const QStringList& command) = 0;
    // Parse a message command
    virtual void         parseMessage(const QStringList& command, const MimeMessage &message) = 0;
    // Parse a payload message
    virtual void         parsePayloadMessage(const QStringList &command, const QByteArray &messageData) = 0;
    // Show a message that the proxy failed
    virtual void         proxyFailed();
    // Insert "%" values for certain regular expressions, i.e. "%20" for " ".
    void                 removePercents(QString &word) const;
    // Send a command to the server, returning the ack
    int                  sendCommand(const QString& prefix, const QString &text = "\r\n");
    // Send a command to the server, returning the ack
    int                  sendMimeMessage(AckType ackType, const MimeMessage &message);
    // Send a payload command to the server, convenience function
    int                  sendPayloadMessage(const QString &prefix, const QString &arguments, const QString &payload);
    // Send a payload command to the server
    int                  sendPayloadMessage(const QString &prefix, const QString &arguments, const QByteArray &payload);
    // Set whether we're sending pings or not (also resets ping timer)
    void                 setSendPings( bool sendPings );

  protected: // Protected attributes
    // A pointer to the instance of the current account
    CurrentAccount      *currentAccount_;

  protected slots: // Protected slots
    // Send all waiting messages and allow further writes to the socket
    virtual void         connectionSuccess();

  private: // Private methods
    // Connect to the given server, not using the proxy
    bool                 connectToServerDirectly(const QString& server, const int& port);
    // Connect to the given server via the proxy
    bool                 connectToServerViaProxy(const QString& server, const int& port);
    // Return an acknowledgement number
    int                  getAck();
    // Resets the ping timer
    void                 resetPingTimer();
    // Connect proper slots to socket_'s signals after the host has been connected
    void                 reconnectSocketSignal(bool hostconnected);
    // Dealing different proxy state, implemented in class MsnNotificationConnection
    void                 setProxyState(PROXYSTATE state);
    // Send authentication info to the socks5 server
    void                 socks5_auth(void);
    // Finally connect to the destination through the socks5 server
    void                 socks5_connect(void);
    // Send different request(send authentication info, connect to the destination)
    // to the socks5 server according to its reply
    void                 socks5_reply(const char *buf,int nread);
    // Write data to the socket
    void                 writeData(const QString& data);
    // Write data to the socket without conversions
    void                 writeBinaryData(const QByteArray& data);
    // Write data to the socket dealing requests and replies with the proxy
    void                 writeProxyData(const char *buf,int len);

  private slots: // Private slots
    // Detect a socket error
    void                 connectionFailed(int error);
    // Read data from the socket
    void                 dataReceived();
     // Send a "ping" to the server
    void                 sendPing();
     // Proxy connected, now send furthur requests
    void                 proxyConnected(void);
    // Read proxy reply data from the socket
    void                 proxyDataReceived(void);

  private: // Private attributes
    // An acknowledgement number
    int                  ack_;
    // Save the port of the host that we want to connect through a proxy
    int                  destPort_;
    // Save the name of the host  that we want to connect through a proxy
    QString              destServer_;
    // State variable to detect whether the client or server disconnected
    bool                 disconnected_;
    // Whether or not the object was initialized
    bool                 initialized_;
    // Number of errant pings since last good one
    int                  missedPings_;
    // Whether the last ping send got a reply
    bool                 pingReceived_;
    // A timer to regularly "ping" the server
    QTimer               pingTimer_;
    // Are we sending pings?
    bool                 sendPings_;
    // The socket
    KExtendedSocket     *socket_;
    // A buffer for multi-packet messages
    QDict<MultiPacketMessage> multiPacketBuffer_;
    // Protect the write process to the socket
    QMutex              *mproxywriteLocked_;
    // Indicate which phase we are at during the interaction with the proxy
    int                  proxyState_;
    // Store the host's info and send it to the proxy
    struct               sockaddr_in addr_destServer_;  // TODO: Make sure this is not needed. Causes compile error in FreeBSD (or include netinet/in.h, but I don't see why this struct is needed here)
    // A lock on the write command so that hopefully two messages aren't sent simultaneously.
    bool                 writeLocked_;
    // A buffer where we save the data until a command is received complete
    KMessBuffer          buffer_;

  signals: // Public signals
    // Signal that the server has disconnected
    void                 disconnected();
    // Signal some connection information
    void                 statusMessage( QString message, int connectStatus );



Generated by  Doxygen 1.6.0   Back to index