home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kclientsocketbase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  15.2 KB  |  518 lines

  1. /*  -*- C++ -*-
  2.  *  Copyright (C) 2003 Thiago Macieira <thiago.macieira@kdemail.net>
  3.  *
  4.  *
  5.  *  Permission is hereby granted, free of charge, to any person obtaining
  6.  *  a copy of this software and associated documentation files (the
  7.  *  "Software"), to deal in the Software without restriction, including
  8.  *  without limitation the rights to use, copy, modify, merge, publish,
  9.  *  distribute, sublicense, and/or sell copies of the Software, and to
  10.  *  permit persons to whom the Software is furnished to do so, subject to
  11.  *  the following conditions:
  12.  *
  13.  *  The above copyright notice and this permission notice shall be included 
  14.  *  in all copies or substantial portions of the Software.
  15.  *
  16.  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17.  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18.  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19.  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20.  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21.  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22.  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25. #ifndef KCLIENTSOCKETBASE_H
  26. #define KCLIENTSOCKETBASE_H
  27.  
  28. #include <qobject.h>
  29. #include <qstring.h>
  30.  
  31. #include "ksocketbase.h"
  32. #include "kresolver.h"
  33. #include <kdelibs_export.h>
  34.  
  35. namespace KNetwork {
  36.  
  37. class KClientSocketBasePrivate;
  38. /** @class KClientSocketBase kclientsocketbase.h kclientsocketbase.h
  39.  *  @brief Abstract client socket class.
  40.  *
  41.  * This class provides the base functionality for client sockets,
  42.  * such as, and especially, name resolution and signals.
  43.  *
  44.  * @note This class is abstract. If you're looking for a normal,
  45.  *       client socket class, see @ref KStreamSocket and KBufferedSocket
  46.  *
  47.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  48.  */
  49. class KDECORE_EXPORT KClientSocketBase : public QObject, public KActiveSocketBase
  50. {
  51.   Q_OBJECT
  52.  
  53. public:
  54.   /**
  55.    * Socket states.
  56.    *
  57.    * These are the possible states for a KClientSocketBase:
  58.    * - Idle: socket is not connected
  59.    * - HostLookup: socket is doing host lookup prior to connecting
  60.    * - HostFound: name lookup is complete
  61.    * - Bound: the socket is locally bound
  62.    * - Connecting: socket is attempting connection
  63.    * - Open: socket is open
  64.    * - Connected (=Open): socket is connected
  65.    * - Connection (=Open): yet another name for a connected socket
  66.    * - Closing: socket is shutting down
  67.    *
  68.    * Whenever the socket state changes, the @ref stateChanged(int) signal
  69.    * will be emitted.
  70.    */
  71.   enum SocketState
  72.   {
  73.     Idle,
  74.     HostLookup,
  75.     HostFound,
  76.     Bound,
  77.     Connecting,
  78.     Open,
  79.     Closing,
  80.  
  81.     Unconnected = Bound,
  82.     Connected = Open,
  83.     Connection = Open
  84.   };
  85.  
  86. public:
  87.   /**
  88.    * Default constructor.
  89.    *
  90.    * @param parent    the parent QObject object
  91.    * @param name    the name of this object
  92.    */
  93.   KClientSocketBase(QObject* parent, const char *name);
  94.  
  95.   /**
  96.    * Destructor.
  97.    */
  98.   virtual ~KClientSocketBase();
  99.  
  100.   /**
  101.    * Returns the current state for this socket.
  102.    * @see SocketState
  103.    */
  104.   SocketState state() const;
  105.  
  106. protected:
  107.   /**
  108.    * Sets the socket options. Reimplemented from KSocketBase.
  109.    */
  110.   virtual bool setSocketOptions(int opts);
  111.  
  112. public:
  113.   /**
  114.    * Returns the internal KResolver object used for
  115.    * looking up the peer host name and service.
  116.    *
  117.    * This can be used to set extra options to the
  118.    * lookup process other than the default values, as well
  119.    * as obtaining the error codes in case of lookup failure.
  120.    */
  121.   KResolver& peerResolver() const;
  122.  
  123.   /**
  124.    * Returns the internal list of resolved results for the peer address.
  125.    */
  126.   const KResolverResults& peerResults() const;
  127.  
  128.   /**
  129.    * Returns the internal KResolver object used for
  130.    * looking up the local host name and service.
  131.    *
  132.    * This can be used to set extra options to the
  133.    * lookup process other than the default values, as well
  134.    * as obtaining the error codes in case of lookup failure.
  135.    */
  136.   KResolver& localResolver() const;
  137.  
  138.   /**
  139.    * Returns the internal list of resolved results for the local address.
  140.    */
  141.   const KResolverResults& localResults() const;
  142.  
  143.   /**
  144.    * Enables or disables name resolution. If this flag is set to true,
  145.    * @ref bind and @ref connect operations will trigger name lookup
  146.    * operations (i.e., converting a hostname into its binary form).
  147.    * If the flag is set to false, those operations will instead
  148.    * try to convert a string representation of an address without
  149.    * attempting name resolution.
  150.    *
  151.    * This is useful, for instance, when IP addresses are in
  152.    * their string representation (such as "1.2.3.4") or come
  153.    * from other sources like @ref KSocketAddress.
  154.    *
  155.    * @param enable    whether to enable
  156.    */
  157.   void setResolutionEnabled(bool enable);
  158.  
  159.   /**
  160.    * Sets the allowed families for the resolutions.
  161.    *
  162.    * @param families        the families that we want/accept
  163.    * @see KResolver::SocketFamilies for possible values
  164.    */
  165.   void setFamily(int families);
  166.  
  167.   /**
  168.    * Starts the lookup for peer and local hostnames as
  169.    * well as their services.
  170.    *
  171.    * If the blocking mode for this object is on, this function will
  172.    * wait for the lookup results to be available (by calling the 
  173.    * @ref KResolver::wait method on the resolver objects).
  174.    *
  175.    * When the lookup is done, the signal @ref hostFound will be
  176.    * emitted (only once, even if we're doing a double lookup).
  177.    * If the lookup failed (for any of the two lookups) the 
  178.    * @ref gotError signal will be emitted with the appropriate
  179.    * error condition (see @ref KSocketBase::SocketError).
  180.    *
  181.    * This function returns true on success and false on error. Note that
  182.    * this is not the lookup result!
  183.    */
  184.   virtual bool lookup();
  185.  
  186.   /**
  187.    * Binds this socket to the given nodename and service,
  188.    * or use the default ones if none are given.
  189.    *
  190.    * Upon successful binding, the @ref bound signal will be
  191.    * emitted. If an error is found, the @ref gotError
  192.    * signal will be emitted.
  193.    *
  194.    * @note Due to the internals of the name lookup and binding
  195.    *       mechanism, some (if not most) implementations of this function
  196.    *       do not actually bind the socket until the connection
  197.    *       is requested (see @ref connect). They only set the values
  198.    *       for future reference.
  199.    *
  200.    * This function returns true on success.
  201.    *
  202.    * @param node    the nodename
  203.    * @param service    the service
  204.    */
  205.   virtual bool bind(const QString& node = QString::null,
  206.             const QString& service = QString::null) = 0;
  207.  
  208.   /**
  209.    * Reimplemented from KSocketBase. Connect this socket to this
  210.    * specific address.
  211.    *
  212.    * Unlike @ref bind(const QString&, const QString&) above, this function
  213.    * really does bind the socket. No lookup is performed. The @ref bound
  214.    * signal will be emitted.
  215.    */
  216.   virtual bool bind(const KResolverEntry& address);
  217.  
  218.   /**
  219.    * Attempts to connect to the these hostname and service,
  220.    * or use the default ones if none are given. If a connection attempt
  221.    * is already in progress, check on its state and set the error status
  222.    * (NoError or InProgress).
  223.    *
  224.    * If the blocking mode for this object is on, this function will only
  225.    * return when all the resolved peer addresses have been tried or when
  226.    * a connection is established.
  227.    *
  228.    * Upon successfully connecting, the @ref connected signal 
  229.    * will be emitted. If an error is found, the @ref gotError
  230.    * signal will be emitted.
  231.    *
  232.    * @par Note for derived classes:
  233.    *      Derived classes must implement this function. The implementation
  234.    *      will set the parameters for the lookup (using the peer KResolver
  235.    *      object) and call @ref lookup to start it.
  236.    *
  237.    * @par
  238.    *      The implementation should use the @ref hostFound
  239.    *      signal to be notified of the completion of the lookup process and
  240.    *      then proceed to start the connection itself. Care should be taken
  241.    *      regarding the value of @ref blocking flag.
  242.    *
  243.    * @param node    the nodename
  244.    * @param service    the service
  245.    */
  246.   virtual bool connect(const QString& node = QString::null,
  247.                const QString& service = QString::null) = 0;
  248.  
  249.   /**
  250.    * @overload
  251.    * Reimplemented from KSocketBase.
  252.    */
  253.   virtual bool connect(const KResolverEntry& address);
  254.  
  255.   /**
  256.    * @deprecated
  257.    * This is a convenience function provided to ease migrating from
  258.    * Qt 3.x's QSocket class.
  259.    */
  260.   inline void connectToHost(const QString& host, Q_UINT16 port)
  261.   { connect(host, QString::number(port)); }
  262.  
  263.   /**
  264.    * Disconnects the socket.
  265.    * Note that not all socket types can disconnect.
  266.    */
  267.   virtual bool disconnect();
  268.  
  269.   /**
  270.    * Opens the socket. Reimplemented from QIODevice.
  271.    *
  272.    * You should not call this function; instead, use @ref connect
  273.    */
  274.   virtual inline bool open(int)
  275.   { return connect(); }
  276.  
  277.   /**
  278.    * Closes the socket. Reimplemented from QIODevice.
  279.    *
  280.    * The closing of the socket causes the emission of the
  281.    * signal @ref closed.
  282.    */
  283.   virtual void close();
  284.  
  285.   /**
  286.    * This call is not supported on sockets. Reimplemented from QIODevice.
  287.    */
  288.   virtual void flush()
  289.   { }
  290.  
  291.   /**
  292.    * Returns the number of bytes available on this socket.
  293.    * Reimplemented from KSocketBase.
  294.    */
  295.   virtual Q_LONG bytesAvailable() const;
  296.  
  297.   /**
  298.    * Waits for more data. Reimplemented from KSocketBase.
  299.    */
  300.   virtual Q_LONG waitForMore(int msecs, bool *timeout = 0L);
  301.  
  302.   /**
  303.    * Reads data from a socket. Reimplemented from KSocketBase.
  304.    */
  305.   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen);
  306.  
  307.   /**
  308.    * @overload
  309.    * Reads data from a socket. Reimplemented from KSocketBase.
  310.    */
  311.   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen, KSocketAddress& from);
  312.  
  313.   /**
  314.    * Peeks data from the socket. Reimplemented from KSocketBase.
  315.    */
  316.   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen);
  317.  
  318.   /**
  319.    * @overload
  320.    * Peeks data from the socket. Reimplemented from KSocketBase.
  321.    */
  322.   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen, KSocketAddress &from);
  323.  
  324.   /**
  325.    * Writes data to the socket. Reimplemented from KSocketBase.
  326.    */
  327.   virtual Q_LONG writeBlock(const char *data, Q_ULONG len);
  328.  
  329.   /**
  330.    * @overload
  331.    * Writes data to the socket. Reimplemented from KSocketBase.
  332.    */
  333.   virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to);
  334.  
  335.   /**
  336.    * Returns the local socket address. Reimplemented from KSocketBase.
  337.    */
  338.   virtual KSocketAddress localAddress() const;
  339.  
  340.   /**
  341.    * Returns the peer socket address. Reimplemented from KSocketBase.
  342.    */
  343.   virtual KSocketAddress peerAddress() const;
  344.  
  345.   /**
  346.    * Returns true if the readyRead signal is set to be emitted.
  347.    */
  348.   bool emitsReadyRead() const;
  349.  
  350.   /**
  351.    * Enables the emission of the readyRead signal.
  352.    * By default, this signal is enabled.
  353.    *
  354.    * @param enable    whether to enable the signal
  355.    */
  356.   virtual void enableRead(bool enable);
  357.  
  358.   /**
  359.    * Returns true if the readyWrite signal is set to be emitted.
  360.    */
  361.   bool emitsReadyWrite() const;
  362.  
  363.   /**
  364.    * Enables the emission of the readyWrite signal.
  365.    * By default, this signal is disabled.
  366.    *
  367.    * @param enable    whether to enable the signal
  368.    */
  369.   virtual void enableWrite(bool enable);
  370.  
  371. protected slots:
  372.   // protected slots
  373.  
  374.   /**
  375.    * This slot is connected to the read notifier's signal meaning
  376.    * the socket can read more data. 
  377.    *
  378.    * The default implementation only emits the readyRead signal.
  379.    *
  380.    * Override if your class requires processing of incoming
  381.    * data.
  382.    */
  383.   virtual void slotReadActivity();
  384.  
  385.   /**
  386.    * This slot is connected to the write notifier's signal
  387.    * meaning the socket can write more data.
  388.    *
  389.    * The default implementation only emits the readyWrite signal.
  390.    *
  391.    * Override if your class writes data from another source
  392.    * (like a buffer).
  393.    */
  394.   virtual void slotWriteActivity();
  395.  
  396. private slots:
  397.   void lookupFinishedSlot();
  398.  
  399. signals:
  400.   /**
  401.    * This signal is emitted whenever the socket state changes.
  402.    *
  403.    * Note: do not delete this object inside the slot called by this
  404.    * signal.
  405.    *
  406.    * @param newstate    the new state of the socket object
  407.    */
  408.   void stateChanged(int newstate);
  409.  
  410.   /**
  411.    * This signal is emitted when this object finds an error.
  412.    * The @p code parameter contains the error code that can
  413.    * also be found by calling @ref error.
  414.    */
  415.   void gotError(int code);
  416.  
  417.   /**
  418.    * This signal is emitted when the lookup is successfully completed.
  419.    */
  420.   void hostFound();
  421.  
  422.   /**
  423.    * This signal is emitted when the socket successfully binds
  424.    * to an address.
  425.    *
  426.    * @param local    the local address we bound to
  427.    */
  428.   void bound(const KResolverEntry& local);
  429.  
  430.   /**
  431.    * This signal is emitted when the socket is about to connect
  432.    * to an address (but before doing so).
  433.    * 
  434.    * The @p skip parameter can be used to make the loop skip this address.
  435.    * Its value is initially false: change it to true if you want to
  436.    * skip the current address (as given by @p remote).
  437.    *
  438.    * This function is also useful if one wants to reset the timeout.
  439.    *
  440.    * @param remote    the address we're about to connect to
  441.    * @param skip    set to true if you want to skip this address
  442.    * @note if the connection is successful, the @ref connected signal will be
  443.    *       emitted.
  444.    */
  445.   void aboutToConnect(const KResolverEntry& remote, bool& skip);
  446.  
  447.   /**
  448.    * This socket is emitted when the socket successfully connects
  449.    * to a remote address.
  450.    *
  451.    * @param remote    the remote address we did connect to
  452.    */
  453.   void connected(const KResolverEntry& remote);
  454.  
  455.   /**
  456.    * This signal is emitted when the socket completes the
  457.    * closing/shut down process.
  458.    */
  459.   void closed();
  460.  
  461.   /**
  462.    * This signal is emitted whenever the socket is ready for
  463.    * reading -- i.e., there is data to be read in the buffers.
  464.    * The subsequent read operation is guaranteed to be non-blocking.
  465.    *
  466.    * You can toggle the emission of this signal with the @ref enableRead
  467.    * function. This signal is by default enabled.
  468.    */
  469.   void readyRead();
  470.  
  471.   /**
  472.    * This signal is emitted whenever the socket is ready for 
  473.    * writing -- i.e., whenever there's space available in the buffers
  474.    * to receive more data. The subsequent write operation is
  475.    * guaranteed to be non-blocking.
  476.    *
  477.    * You can toggle the emission of this signal with the @ref enableWrite
  478.    * function. This signal is by default disabled. You will
  479.    * want to disable this signal after the first reception, since
  480.    * it'll probably fire at every event loop.
  481.    */
  482.   void readyWrite();
  483.  
  484. protected:
  485.   /**
  486.    * Sets the socket state to @p state. This function does not
  487.    * emit the @ref stateChanged signal.
  488.    */
  489.   void setState(SocketState state);
  490.  
  491.   /**
  492.    * This function is called by @ref setState whenever the state
  493.    * changes. You should override it if you need to specify any
  494.    * actions to be done when the state changes.
  495.    *
  496.    * The default implementation acts for these states only:
  497.    *  - Connected: it sets up the socket notifiers to fire readyRead and 
  498.    *               readyWrite signals.
  499.    */
  500.   virtual void stateChanging(SocketState newState);
  501.  
  502.   /**
  503.    * Convenience function to set this object's error code to match
  504.    * that of the socket device.
  505.    */
  506.   void copyError();
  507.  
  508. private:
  509.   KClientSocketBase(const KClientSocketBase&);
  510.   KClientSocketBase& operator=(const KClientSocketBase&);
  511.  
  512.   KClientSocketBasePrivate *d;
  513. };
  514.  
  515. }                // namespace KNetwork
  516.  
  517. #endif
  518.