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 / kserversocket.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  14.3 KB  |  436 lines

  1. /*  -*- C++ -*-
  2.  *  Copyright (C) 2003 Thiago Macieira <thiago@kde.org>
  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 KSERVERSOCKET_H
  26. #define KSERVERSOCKET_H
  27.  
  28. #include <qobject.h>
  29. #include "ksocketbase.h"
  30.  
  31. namespace KNetwork {
  32.  
  33. class KSocketDevice;
  34. class KStreamSocket; 
  35. class KResolver;
  36. class KResolverResults;
  37.  
  38. class KServerSocketPrivate;
  39. /**
  40.  * @class KServerSocket kserversocket.h kserversocket.h
  41.  * @brief A server socket for accepting connections.
  42.  *
  43.  * This class provides functionality for creating a socket to
  44.  * listen for incoming connections and subsequently accept them.
  45.  *
  46.  * To use this class, you must first set the parameters for the listening
  47.  * socket's address, then place it in listening mode.
  48.  *
  49.  * A typical example would look like:
  50.  * \code
  51.  *   QString service = "http";
  52.  *   KServerSocket *ss = new KServerSocket(service);
  53.  *   connect(ss, SIGNAL(readyAccept()), this, SLOT(slotReadyAccept()));
  54.  *   connect(ss, SIGNAL(gotError(int)), this, SLOT(slotSocketError(int)));
  55.  *   ss->listen();
  56.  * \endcode
  57.  * 
  58.  * In this case, this class will place the socket into listening mode on the
  59.  * service pointed to by @p service and will emit the @ref readyAccept signal
  60.  * when a connection is ready for accepting. The called slot is responsible for
  61.  * calling @ref accept.
  62.  * 
  63.  * The location of the services file (where @p service is looked up) 
  64.  * is defined by _PATH_SERVICES in /usr/include/netdb.h.  This is
  65.  * usually set to /etc/services.
  66.  * See RFC 1700 for more information on services.
  67.  * You can specify @p service as a port number directly, rather than as a service
  68.  * name.  This is discouraged as it prevents the end user from easily modifying
  69.  * the port number.
  70.  *
  71.  * For another example of usage, this below code attempts to make a connection on any port within a range:
  72.  * \code
  73.  *   KServerSocket *ss = new KServerSocket();
  74.  *   ss->setFamily(KResolver::InetFamily);
  75.  *   bool found = false;
  76.  *   for( unsigned int port = firstport; port <= lastport; ++port) {
  77.  *     ss->setAddress( QString::number( port ) );
  78.  *     bool success = ss->listen();
  79.  *     if( found = ( success && ss->error() == 
  80.  *                              KSocketBase::NoError ) )
  81.  *       break;
  82.  *     ss->close();
  83.  *   }
  84.  *   if( !found ) {
  85.  *     // Couldn't connect to any port.
  86.  *   } else {
  87.  *     connect(ss, SIGNAL(readyAccept()), this, SLOT(slotReadyAccept()));
  88.  *     connect(ss, SIGNAL(gotError(int)), this, SLOT(slotSocketError(int)));
  89.  *     ss->listen();
  90.  *   }
  91.  * \endcode
  92.  *
  93.  * The called slot slotReadyAccept() is responsible for calling
  94.  * @ref accept.
  95.  *
  96.  * It is important to note that @ref accept can return either an
  97.  * object of type KNetwork::KStreamSocket or
  98.  * KNetwork::KBufferedSocket (default). If you want to accept a
  99.  * non-buffered socket, you must first call setAcceptBuffered.
  100.  *
  101.  * @warning If you use KServerSocket in an auxiliary (non-GUI) thread,
  102.  *          you need to accept only KNetwork::KStreamSocket objects.
  103.  *
  104.  * @see KNetwork::KStreamSocket, KNetwork::KBufferedSocket
  105.  * @author Thiago Macieira <thiago@kde.org>
  106.  */
  107. class KDECORE_EXPORT KServerSocket: public QObject, public KPassiveSocketBase
  108. {
  109.   Q_OBJECT
  110. public:
  111.   /**
  112.    * Default constructor.
  113.    *
  114.    * If the binding address isn't changed by setAddress, this socket will
  115.    * bind to all interfaces on this node and the port will be selected by the
  116.    * operating system.
  117.    *
  118.    * @param parent        the parent QObject object
  119.    * @param name        the name of this object
  120.    */
  121.   KServerSocket(QObject* parent = 0L, const char *name = 0L);
  122.  
  123.   /**
  124.    * Construct this object specifying the service to listen on.
  125.    *
  126.    * If the binding address isn't changed by setAddress, this socket will
  127.    * bind to all interfaces and will listen on the port specified by
  128.    * @p service.  This is either a service name (e.g. 'www') or a port
  129.    * number (e.g. '80').
  130.    * 
  131.    * The location of the services file (where @p service is looked up) 
  132.    * is defined by _PATH_SERVICES in /usr/include/netdb.h.  This is
  133.    * usually set to /etc/services.
  134.    * See RFC 1700 for more information on services.
  135.    *
  136.    * @param service        the service name to listen on
  137.    * @param parent        the parent QObject object
  138.    * @param name        the name of this object
  139.    */
  140.   KServerSocket(const QString& service, QObject* parent = 0L, const char *name = 0L);
  141.  
  142.   /**
  143.    * Construct this object specifying the node and service names to listen on.
  144.    *
  145.    * If the binding address isn't changed by setAddress, this socket will
  146.    * bind to the interface specified by @p node and the port specified by
  147.    * @p service.  This is either a service name (e.g. 'www') or a port
  148.    * number (e.g. '80').
  149.    *   
  150.    * The location of the services file (where @p service is looked up) 
  151.    * is defined by _PATH_SERVICES in /usr/include/netdb.h.  This is
  152.    * usually set to /etc/services.
  153.    * See RFC 1700 for more information on services.   
  154.    *
  155.    * @param node        the node to bind to
  156.    * @param service        the service port to listen on
  157.    * @param parent        the parent QObject object
  158.    * @param name        the name of this object
  159.    */
  160.   KServerSocket(const QString& node, const QString& service,
  161.         QObject* parent = 0L, const char *name = 0L);
  162.  
  163.   /**
  164.    * Destructor. This will close the socket, if open.
  165.    *
  166.    * Note, however, that accepted sockets do not get closed when this
  167.    * object closes.
  168.    */
  169.   ~KServerSocket();
  170.  
  171. protected:
  172.   /**
  173.    * Sets the socket options. Reimplemented from KSocketBase.
  174.    */
  175.   virtual bool setSocketOptions(int opts);
  176.  
  177. public:
  178.   /**
  179.    * Returns the internal KResolver object used for
  180.    * looking up the host name and service.
  181.    *
  182.    * This can be used to set extra options to the
  183.    * lookup process other than the default values, as well
  184.    * as obtaining the error codes in case of lookup failure.
  185.    */
  186.   KResolver& resolver() const;
  187.  
  188.   /**
  189.    * Returns the internal list of resolved results for the binding address.
  190.    */
  191.   const KResolverResults& resolverResults() const;
  192.  
  193.   /**
  194.    * Enables or disables name resolution. If this flag is set to true,
  195.    * the @ref bind operation will trigger name lookup
  196.    * operations (i.e., converting a hostname into its binary form).
  197.    * If the flag is set to false, those operations will instead
  198.    * try to convert a string representation of an address without
  199.    * attempting name resolution.
  200.    *
  201.    * This is useful, for instance, when IP addresses are in
  202.    * their string representation (such as "1.2.3.4") or come
  203.    * from other sources like @ref KSocketAddress.
  204.    *
  205.    * @param enable    whether to enable
  206.    */
  207.   void setResolutionEnabled(bool enable);
  208.  
  209.   /**
  210.    * Sets the allowed families for the resolutions.
  211.    *
  212.    * @param families        the families that we want/accept
  213.    * @see KResolver::SocketFamilies for possible values
  214.    */
  215.   void setFamily(int families);
  216.  
  217.   /**
  218.    * Sets the address on which we will listen. The port to listen on is given by
  219.    * @p service, and we will bind to all interfaces. To let the operating system choose a
  220.    * port, set the service to "0".  @p service can either be a service name
  221.    * (e.g. 'www') or a port number (e.g. '80').
  222.    *
  223.    * The location of the services file (where @p service is looked up) 
  224.    * is defined by _PATH_SERVICES in /usr/include/netdb.h.  This is
  225.    * usually set to /etc/services.
  226.    * See RFC 1700 for more information on services.
  227.    *
  228.    * @param service        the service name to listen on
  229.    */
  230.   void setAddress(const QString& service);
  231.  
  232.   /**
  233.    * @overload
  234.    * Sets the address on which we will listen. This will cause the socket to listen
  235.    * only on the interface given by @p node and on the port given by @p service.
  236.    * @p service can either be a service name (e.g. 'www') or a port number
  237.    * (e.g. '80').
  238.    *
  239.    * The location of the services file (where @p service is looked up) 
  240.    * is defined by _PATH_SERVICES in /usr/include/netdb.h.  This is
  241.    * usually set to /etc/services.
  242.    * See RFC 1700 for more information on services. 
  243.    *
  244.    * @param node        the node to bind to
  245.    * @param service        the service port to listen on
  246.    */
  247.   void setAddress(const QString& node, const QString& service);
  248.  
  249.   /**
  250.    * Sets the timeout for accepting. When you call @ref accept,
  251.    * it will wait at most @p msecs milliseconds or return with an error
  252.    * (returning a NULL object).
  253.    *
  254.    * @param msecs        the time in milliseconds to wait, 0 to wait forever
  255.    */
  256.   void setTimeout(int msecs);
  257.  
  258.   /**
  259.    * Starts the lookup for peer and local hostnames as
  260.    * well as their services.
  261.    *
  262.    * If the blocking mode for this object is on, this function will
  263.    * wait for the lookup results to be available (by calling the 
  264.    * @ref KResolver::wait method on the resolver objects).
  265.    *
  266.    * When the lookup is done, the signal @ref hostFound will be
  267.    * emitted (only once, even if we're doing a double lookup).
  268.    * If the lookup failed (for any of the two lookups) the 
  269.    * @ref gotError signal will be emitted with the appropriate
  270.    * error condition (see @ref KSocketBase::SocketError).
  271.    *
  272.    * This function returns true on success and false on error. Note that
  273.    * this is not the lookup result!
  274.    */
  275.   virtual bool lookup();
  276.  
  277.   /**
  278.    * Binds this socket to the given nodename and service,
  279.    * or use the default ones if none are given.
  280.    *
  281.    * Upon successful binding, the @ref bound signal will be
  282.    * emitted. If an error is found, the @ref gotError
  283.    * signal will be emitted.
  284.    *
  285.    * This function returns true on success.
  286.    *
  287.    * @param node    the nodename
  288.    * @param service    the service
  289.    */
  290.   virtual bool bind(const QString& node, const QString& service);
  291.  
  292.   /**
  293.    * Binds the socket to the given service name.
  294.    * @overload
  295.    *
  296.    * @param service    the service
  297.    */
  298.   virtual bool bind(const QString& service);
  299.  
  300.   /**
  301.    * Binds the socket to the addresses previously set with @ref setAddress.
  302.    * @overload
  303.    *
  304.    */
  305.   virtual bool bind();
  306.  
  307.   /**
  308.    * Connect this socket to this specific address. Reimplemented from KSocketBase.
  309.    *
  310.    * Unlike @ref bind(const QString&, const QString&) above, this function
  311.    * really does bind the socket. No lookup is performed. The @ref bound signal
  312.    * will be emitted.
  313.    */
  314.   virtual bool bind(const KResolverEntry& address);
  315.  
  316.   /**
  317.    * Puts this socket into listening mode. Reimplemented from @ref KPassiveSocketBase.
  318.    *
  319.    * Placing a socket into listening mode means it will be able to receive incoming
  320.    * connections through the @ref accept method.
  321.    *
  322.    * If you do not call this method but call @ref accept directly, the socket will
  323.    * be placed into listening mode automatically.
  324.    *
  325.    * @param backlog        the number of connection the system is to
  326.    *                            queue without @ref accept being called
  327.    * @returns true if the socket is now in listening mode.
  328.    */
  329.   virtual bool listen(int backlog = 5);    // 5 is arbitrary
  330.  
  331.   /**
  332.    * Closes this socket.
  333.    */
  334.   virtual void close();
  335.  
  336.   /**
  337.    * Toggles whether the accepted socket will be buffered or not.
  338.    * That is, the @ref accept function will always return a KStreamSocket
  339.    * object or descended from it. If buffering is enabled, the class
  340.    * to be returned will be KBufferedSocket.
  341.    *
  342.    * By default, this flag is set to true.
  343.    *
  344.    * @param enable        whether to set the accepted socket to
  345.    *                buffered mode
  346.    */
  347.   void setAcceptBuffered(bool enable);
  348.  
  349.   /**
  350.    * Accepts one incoming connection and return the associated, open
  351.    * socket.
  352.    *
  353.    * If this function cannot accept a new connection, it will return NULL.
  354.    * The specific object class returned by this function may vary according
  355.    * to the implementation: derived classes may return specialised objects
  356.    * descended from KStreamSocket.
  357.    *
  358.    * @note This function should return a KStreamSocket object, but compiler
  359.    *       deficiencies prevent such an adjustment. Therefore, we return
  360.    *       the base class for active sockets, but it is guaranteed
  361.    *       that the object will be a KStreamSocket or derived from it.
  362.    *
  363.    * @sa KBufferedSocket
  364.    * @sa setAcceptBuffered
  365.    */
  366.   virtual KActiveSocketBase* accept();
  367.  
  368.   /**
  369.    * Returns this socket's local address.
  370.    */
  371.   virtual KSocketAddress localAddress() const;
  372.  
  373.   /**
  374.    * Returns this socket's externally-visible address if know.
  375.    */
  376.   virtual KSocketAddress externalAddress() const;
  377.  
  378. private slots:
  379.   void lookupFinishedSlot();
  380.  
  381. signals:
  382.   /**
  383.    * This signal is emitted when this object finds an error.
  384.    * The @p code parameter contains the error code that can
  385.    * also be found by calling @ref error.
  386.    */
  387.   void gotError(int code);
  388.  
  389.   /**
  390.    * This signal is emitted when the lookup is successfully completed.
  391.    */
  392.   void hostFound();
  393.  
  394.   /**
  395.    * This signal is emitted when the socket successfully binds
  396.    * to an address.
  397.    *
  398.    * @param local    the local address we bound to
  399.    */
  400.   void bound(const KResolverEntry& local);
  401.  
  402.   /**
  403.    * This signal is emitted when the socket completes the
  404.    * closing/shut down process.
  405.    */
  406.   void closed();
  407.  
  408.   /**
  409.    * This signal is emitted whenever the socket is ready for
  410.    * accepting -- i.e., there is at least one connection waiting to
  411.    * be accepted.
  412.    */
  413.   void readyAccept();
  414.  
  415. protected:
  416.   /**
  417.    * Convenience function to set this object's error code to match
  418.    * that of the socket device.
  419.    */
  420.   void copyError();
  421.  
  422. private:
  423.   bool doBind();
  424.   bool doListen();
  425.  
  426. private:
  427.   KServerSocket(const KServerSocket&);
  428.   KServerSocket& operator=(const KServerSocket&);
  429.  
  430.   KServerSocketPrivate *d;
  431. };
  432.  
  433. }                // namespace KNetwork
  434.  
  435. #endif
  436.