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 / kbufferedsocket.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  6.4 KB  |  254 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 KBUFFEREDSOCKET_H
  26. #define KBUFFEREDSOCKET_H
  27.  
  28. #include <qobject.h>
  29. #include <qcstring.h>
  30. #include <qvaluelist.h>
  31. #include "kstreamsocket.h"
  32. #include <kdelibs_export.h>
  33.  
  34. class KIOBufferBase;
  35.  
  36. namespace KNetwork {
  37.  
  38. class KBufferedSocketPrivate;
  39. /** @class KBufferedSocket kbufferedsocket.h kbufferedsocket.h
  40.  *  @brief Buffered stream sockets.
  41.  *
  42.  * This class allows the user to create and operate buffered stream sockets
  43.  * such as those used in most Internet connections. This class is
  44.  * also the one that resembles the most to the old @ref QSocket
  45.  * implementation.
  46.  *
  47.  * Objects of this type operate only in non-blocking mode. A call to
  48.  * setBlocking(true) will result in an error.
  49.  *
  50.  * @note Buffered sockets only make sense if you're using them from
  51.  *       the main (event-loop) thread. This is actually a restriction
  52.  *       imposed by Qt's QSocketNotifier. If you want to use a socket
  53.  *       in an auxiliary thread, please use KStreamSocket.
  54.  *
  55.  * @see KNetwork::KStreamSocket, KNetwork::KServerSocket
  56.  * @author Thiago Macieira <thiago@kde.org>
  57.  */
  58. class KDECORE_EXPORT KBufferedSocket: public KStreamSocket
  59. {
  60.   Q_OBJECT
  61. public:
  62.   /**
  63.    * Default constructor.
  64.    *
  65.    * @param node    destination host
  66.    * @param service    destination service to connect to
  67.    * @param parent      the parent object for this object
  68.    * @param name        the internal name for this object
  69.    */
  70.   KBufferedSocket(const QString& node = QString::null, const QString& service = QString::null,
  71.           QObject* parent = 0L, const char *name = 0L);
  72.  
  73.   /**
  74.    * Destructor.
  75.    */
  76.   virtual ~KBufferedSocket();
  77.  
  78.   /**
  79.    * Be sure to catch new devices.
  80.    */
  81.   virtual void setSocketDevice(KSocketDevice* device);
  82.  
  83. protected:
  84.   /**
  85.    * Buffered sockets can only operate in non-blocking mode.
  86.    */
  87.   virtual bool setSocketOptions(int opts);
  88.  
  89. public:
  90.   /**
  91.    * Closes the socket for new data, but allow data that had been buffered
  92.    * for output with @ref writeBlock to be still be written.
  93.    *
  94.    * @sa closeNow
  95.    */
  96.   virtual void close();
  97.  
  98.   /**
  99.    * Make use of the buffers.
  100.    */
  101.   virtual Q_LONG bytesAvailable() const;
  102.  
  103.   /**
  104.    * Make use of buffers.
  105.    */
  106.   virtual Q_LONG waitForMore(int msecs, bool *timeout = 0L);
  107.  
  108.   /**
  109.    * Reads data from the socket. Make use of buffers.
  110.    */
  111.   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen);
  112.  
  113.   /**
  114.    * @overload
  115.    * Reads data from a socket.
  116.    *
  117.    * The @p from parameter is always set to @ref peerAddress()
  118.    */
  119.   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen, KSocketAddress& from);
  120.  
  121.   /**
  122.    * Peeks data from the socket.
  123.    */
  124.   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen);
  125.  
  126.   /**
  127.    * @overload
  128.    * Peeks data from the socket.
  129.    *
  130.    * The @p from parameter is always set to @ref peerAddress()
  131.    */
  132.   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen, KSocketAddress &from);
  133.  
  134.   /**
  135.    * Writes data to the socket.
  136.    */
  137.   virtual Q_LONG writeBlock(const char *data, Q_ULONG len);
  138.  
  139.   /**
  140.    * @overload
  141.    * Writes data to the socket.
  142.    *
  143.    * The @p to parameter is discarded.
  144.    */
  145.   virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to);
  146.  
  147.   /**
  148.    * Catch changes.
  149.    */
  150.   virtual void enableRead(bool enable);
  151.  
  152.   /**
  153.    * Catch changes.
  154.    */
  155.   virtual void enableWrite(bool enable);
  156.  
  157.   /**
  158.    * Sets the use of input buffering.
  159.    */
  160.   void setInputBuffering(bool enable);
  161.  
  162.   /**
  163.    * Retrieves the input buffer object.
  164.    */
  165.   KIOBufferBase* inputBuffer();
  166.  
  167.   /**
  168.    * Sets the use of output buffering.
  169.    */
  170.   void setOutputBuffering(bool enable);
  171.  
  172.   /**
  173.    * Retrieves the output buffer object.
  174.    */
  175.   KIOBufferBase* outputBuffer();
  176.  
  177.   /**
  178.    * Returns the length of the output buffer.
  179.    */
  180.   virtual Q_ULONG bytesToWrite() const;
  181.  
  182.   /**
  183.    * Closes the socket and discards any output data that had been buffered
  184.    * with @ref writeBlock but that had not yet been written.
  185.    *
  186.    * @sa close
  187.    */
  188.   virtual void closeNow();
  189.  
  190.   /**
  191.    * Returns true if a line can be read with @ref readLine
  192.    */
  193.   bool canReadLine() const;
  194.  
  195.   /**
  196.    * Reads a line of data from the socket buffers.
  197.    */
  198.   QCString readLine();
  199.  
  200.   // KDE4: make virtual, add timeout to match the Qt4 signature
  201.   //       and move to another class up the hierarchy
  202.   /**
  203.    * Blocks until the connection is either established, or completely
  204.    * failed.
  205.    */
  206.   void waitForConnect();
  207.  
  208. protected:
  209.   /**
  210.    * Catch connection to clear the buffers
  211.    */
  212.   virtual void stateChanging(SocketState newState);
  213.  
  214. protected slots:
  215.   /**
  216.    * Slot called when there's read activity.
  217.    */
  218.   virtual void slotReadActivity();
  219.  
  220.   /**
  221.    * Slot called when there's write activity.
  222.    */
  223.   virtual void slotWriteActivity();
  224.  
  225. signals:
  226.   /**
  227.    * This signal is emitted whenever data is written.
  228.    */
  229.   void bytesWritten(int bytes);
  230.  
  231. private:
  232.   KBufferedSocket(const KBufferedSocket&);
  233.   KBufferedSocket& operator=(const KBufferedSocket&);
  234.  
  235.   KBufferedSocketPrivate *d;
  236.  
  237. public:
  238.   // KDE4: remove this function
  239.   /**
  240.    * @deprecated
  241.    * Closes the socket.
  242.    *
  243.    * This function is provided to ease porting from KExtendedSocket,
  244.    * which required a call to reset() in order to be able to connect again
  245.    * using the same device. This is not necessary in KBufferedSocket any more.
  246.    */
  247.   inline void reset()
  248.   { closeNow(); }
  249. };
  250.  
  251. }                // namespace KNetwork
  252.  
  253. #endif
  254.