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 / ksock.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  10.0 KB  |  347 lines

  1. /*
  2.  *  This file is part of the KDE libraries
  3.  *  Copyright (C) 1997 Torben Weis (weis@kde.org)
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public License
  16.  *  along with this library; see the file COPYING.LIB.  If not, write to
  17.  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18.  *  Boston, MA 02110-1301, USA.
  19.  */
  20. #ifndef KSOCK_H
  21. #define KSOCK_H
  22.  
  23. #include "kdelibs_export.h"
  24.  
  25. #ifdef Q_OS_UNIX
  26.  
  27. #include <qobject.h>
  28. #include <sys/types.h>
  29. // we define STRICT_ANSI to get rid of some warnings in glibc
  30. #ifndef __STRICT_ANSI__
  31. #define __STRICT_ANSI__
  32. #define _WE_DEFINED_IT_
  33. #endif
  34. #include <sys/socket.h>
  35. #ifdef _WE_DEFINED_IT_
  36. #undef __STRICT_ANSI__
  37. #undef _WE_DEFINED_IT_
  38. #endif
  39.  
  40. #include <sys/un.h>
  41.  
  42. #include <netinet/in.h>
  43. class QSocketNotifier;
  44.  
  45. #ifdef KSOCK_NO_BROKEN
  46. // This is here for compatibility with old applications still using the constants
  47. // Never use them in new programs
  48.  
  49. // Here are a whole bunch of hackish macros that allow one to
  50. // get at the correct member of ksockaddr_in
  51. // But since ksockaddr_in is IPv4-only, and deprecated...
  52.  
  53. typedef sockaddr_in ksockaddr_in;
  54. #define get_sin_addr(x) x.sin_addr
  55. #define get_sin_port(x) x.sin_port
  56. #define get_sin_family(x) x.sin_family
  57. #define get_sin_paddr(x) x->sin_addr
  58. #define get_sin_pport(x) x->sin_port
  59. #define get_sin_pfamily(x) x->sin_family
  60. #endif
  61.  
  62. #define KSOCK_DEFAULT_DOMAIN PF_INET
  63.  
  64. class KSocketPrivate;
  65. class KServerSocketPrivate;
  66.  
  67. /** @deprecated
  68.  * You can connect this socket to any Internet address.
  69.  *
  70.  * This class is deprecated and will be removed in the future. For new
  71.  * programs, please use KExtendedSocket class.
  72.  *
  73.  * The socket gives you three signals: When ready for reading,
  74.  * ready for writing or if the connection is broken.
  75.  * Using socket() you get a file descriptor
  76.  * which you can use with the usual UNIX function like write() or
  77.  * read().
  78.  * If you have already such a socket identifier you can construct a KSocket
  79.  * on this identifier.
  80.  *
  81.  * If socket() delivers a value of -1 or less, the connection
  82.  * was not successful.
  83.  *
  84.  * @author Torben Weis <weis@uni-frankfurt.de>
  85.  * @short A TCP/IP client socket.
  86.  */
  87. class KDECORE_EXPORT KSocket : public QObject
  88. {
  89.     Q_OBJECT
  90. public:
  91.     /**
  92.      * Constructs a KSocket with the provided file descriptor.
  93.      * @param _sock    The file descriptor to use.
  94.      */
  95.     KSocket( int _sock ) KDE_DEPRECATED;
  96.     /**
  97.      * Creates a socket and connects to a host.
  98.      * @param _host    The remote host to which to connect.
  99.      * @param _port    The port on the remote host.
  100.      * @param timeOut    The number of seconds waiting for connect (default 30).
  101.      */
  102.     KSocket( const char *_host, unsigned short int _port, int timeOut = 30) KDE_DEPRECATED;
  103.  
  104.     /**
  105.      * Connects to a UNIX domain socket.
  106.      * @param _path    The filename of the socket.
  107.      */
  108.     KSocket( const char * _path ) KDE_DEPRECATED;
  109.  
  110.     /**
  111.      * Destructor. Closes the socket if it is still open.
  112.      */
  113.     virtual ~KSocket();
  114.  
  115.     /**
  116.      * Returns a file descriptor for this socket.
  117.      * @return the file descriptor, or -1 when an error occurred.
  118.      */
  119.     int socket() const { return sock; }
  120.  
  121.     /**
  122.      * Enables the socket for reading.
  123.      *
  124.      * If you enable read mode, the socket will emit the signal
  125.      * readEvent() whenever there is something to read out of this
  126.      * socket.
  127.      * @param enable true to enable reading signals
  128.      */
  129.     void enableRead( bool enable );
  130.  
  131.     /**
  132.      * Enables the socket for writing.
  133.      *
  134.      * If you enable write mode, the socket will emit the signal
  135.      * writeEvent() whenever the socket is ready for writing.
  136.      *
  137.      * Warning: If you forget to call enableWrite(false) when you are
  138.      * not ready to send data, you will get lots of writeEvent() signals,
  139.      * in the order of thousands a second !
  140.      * @param enable true to enable writing signals
  141.      */
  142.     void enableWrite( bool enable );
  143.  
  144. #ifdef KSOCK_NO_BROKEN
  145.     // BCI: remove in libkdecore.so.4
  146.     /**
  147.      * Return address.
  148.      * This function is dumb. Don't ever use it
  149.      * if you need the peer address of this socket, use KExtendedSocket::peerAddress(int)
  150.      * instead
  151.      * @deprecated
  152.      */
  153.     unsigned long ipv4_addr() KDE_DEPRECATED;
  154.  
  155.     // BCI: remove in libkdecore.so.4
  156.     /**
  157.       *  A small wrapper around gethostbyname() and such.
  158.       *  Don't use this in new programs. Use KExtendedSocket::lookup
  159.       *  @deprecated
  160.       */
  161.     static bool initSockaddr(ksockaddr_in *server_name, const char *hostname, unsigned short int port, int domain = PF_INET) KDE_DEPRECATED;
  162. #endif
  163.  
  164. signals:
  165.     /**
  166.      * Data has arrived for reading.
  167.      *
  168.      * This signal will only be raised if enableRead( @p true ) was called
  169.      * first.
  170.      * @param s the KSocket that triggered the event
  171.      */
  172.     void readEvent( KSocket *s );
  173.  
  174.     /**
  175.      * Socket is ready for writing.
  176.      *
  177.      * This signal will only be raised if enableWrite( @p true ) was
  178.      * called first.
  179.      *
  180.      * Warning: If you forget to call enableWrite(false) when you are
  181.      * not ready to send data, you will get lots of writeEvent() signals,
  182.      * in the order of thousands a second !
  183.      * @param s the KSocket that triggered the event
  184.      */
  185.     void writeEvent( KSocket *s );
  186.  
  187.     /**
  188.      * Raised when the connection is broken.
  189.      * @param s the KSocket that triggered the event
  190.      */
  191.     void closeEvent( KSocket *s );
  192.  
  193. public slots:
  194.     /**
  195.      * Connected to the writeNotifier.
  196.      *
  197.      * Called when
  198.      *  the socket is ready for writing.
  199.      * @param x ignored
  200.      */
  201.     void slotWrite( int x);
  202.  
  203.     /**
  204.      * Connected to the readNotifier.
  205.      *
  206.      * Called when
  207.      *  the socket is ready for reading.
  208.      * @param x ignored
  209.      */
  210.     void slotRead( int x );
  211.  
  212. protected:
  213.     bool connect( const QString& _host, unsigned short int _port, int timeout = 0 );
  214.     bool connect( const char *_path );
  215.  
  216.     /******************************************************
  217.      * The file descriptor for this socket. sock may be -1.
  218.      * This indicates that it is not connected.
  219.      */
  220.     int sock;
  221.  
  222. private:
  223.     KSocket(const KSocket&);
  224.     KSocket& operator=(const KSocket&);
  225.  
  226.     KSocketPrivate *d;
  227.  
  228. };
  229.  
  230.  
  231. /**
  232.  * @short Monitors a port for incoming TCP/IP connections.
  233.  *
  234.  * @deprecated
  235.  * This class is deprecated and will be removed in the future.
  236.  * Please use the classes in KNetwork for new programs. 
  237.  * In special, this class is replaced by KNetwork::KStreamSocket
  238.  * and KNetwork::KServerSocket. 
  239.  *
  240.  * You can use a KServerSocket to listen on a port for incoming
  241.  * connections. When a connection arrived in the port, a KSocket
  242.  * is created and the signal accepted is raised. Make sure you
  243.  * always connect to this signal. If you don't the ServerSocket will
  244.  * create new KSocket's and no one will delete them!
  245.  *
  246.  * If socket() is -1 or less the socket was not created properly.
  247.  *
  248.  * @author Torben Weis <weis@stud.uni-frankfurt.de>
  249. */
  250. class KDECORE_EXPORT KServerSocket : public QObject
  251. {
  252.     Q_OBJECT
  253. public:
  254.     /**
  255.      * Constructor.
  256.      * @param _port    the port number to monitor for incoming connections.
  257.      * @param _bind     if false you need to call bindAndListen yourself.
  258.      *                  This gives you the opportunity to set options on the
  259.      *                  socket.
  260.      */
  261.     KServerSocket( unsigned short int _port, bool _bind = true );
  262.  
  263.     /**
  264.      * Creates a UNIX domain server socket.
  265.      * @param _path     path used for the socket.
  266.      * @param _bind     if false you need to call bindAndListen yourself.
  267.      *                  This gives you the opportunity to set options on the
  268.      *                  socket.
  269.      */
  270.     KServerSocket( const char *_path, bool _bind = true);
  271.  
  272.     /**
  273.      * Destructor. Closes the socket if it was not already closed.
  274.      */
  275.     virtual ~KServerSocket();
  276.  
  277.     /**
  278.      * Binds the socket and start listening. This should only be called
  279.      * once when the constructor was called with _bind false.
  280.      * On error the socket will be closed.
  281.      * @return true on success. false on error.
  282.      */
  283.     bool bindAndListen();
  284.  
  285.     /**
  286.      * Returns the file descriptor associated with the socket.
  287.      * @return the file descriptor, -1 when an error occurred during
  288.      *         construction or bindAndListen
  289.      */
  290.     int socket() const { return sock; }
  291.  
  292.     /**
  293.      * Returns the port number which is being monitored.
  294.      * @return the port number
  295.      */
  296.     unsigned short int port();
  297.  
  298. #ifdef KSOCK_NO_BROKEN
  299.     // BCI: remove in libkdecore.so.4
  300.     /**
  301.      * The address.
  302.      * This is dumb. Don't use it
  303.      * Refer to KExtendedSocket::localAddress(int)
  304.      * @deprecated
  305.      */
  306.     unsigned long ipv4_addr();
  307. #endif
  308.  
  309. public slots:
  310.     /**
  311.      * Called when someone connected to our port.
  312.      */
  313.     virtual void slotAccept( int ); // why is this virtual?
  314.  
  315. signals:
  316.     /**
  317.      * A connection has been accepted.
  318.      * It is your task to delete the KSocket if it is no longer needed.
  319.      *
  320.      * WARNING: this signal is always emitted, even if you don't connect
  321.      * anything to it. That would mean memory loss, because the KSockets
  322.      * created go to oblivion.
  323.      * @param s the socket that accepted
  324.      */
  325.     void accepted( KSocket*s );
  326.  
  327. protected:
  328.     bool init( unsigned short int );
  329.     bool init( const char *_path );
  330.  
  331.     /**
  332.      * The file descriptor for this socket. sock may be -1.
  333.      * This indicates that it is not connected.
  334.      */
  335.     int sock;
  336.  
  337. private:
  338.     KServerSocket(const KServerSocket&);
  339.     KServerSocket& operator=(const KServerSocket&);
  340.  
  341.     KServerSocketPrivate *d;
  342. };
  343.  
  344. #endif //Q_OS_UNIX
  345.  
  346. #endif
  347.