00001 /* -*- Mode: C++ -*- 00002 * Worldvisions Weaver Software: 00003 * Copyright (C) 1997-2002 Net Integration Technologies, Inc. 00004 * 00005 */ 00006 #ifndef __WVQTSTREAMCLONE_H 00007 #define __WVQTSTREAMCLONE_H 00008 00009 #include "wvstreamclone.h" 00010 #include <qobject.h> 00011 #include <qintdict.h> 00012 #include <qsocketnotifier.h> 00013 #include <qtimer.h> 00014 00015 /** 00016 * Wraps another WvStream and attaches it to the normal Qt event loop. 00017 * 00018 * If you are using this object exclusively to manage all of your 00019 * streams, then you do not need to have a normal WvStreams 00020 * select()/callback() loop in your application at all. 00021 * 00022 * However, should you leave the Qt event loop and wish to continue 00023 * using this WvStream, call qt_detach() first, then run a normal 00024 * WvStreams event loop. If you do not do this, events may be 00025 * lost! Later, you may resume the Qt event loop at any time 00026 * by leaving your WvStreams event loop and calling qt_attach(). 00027 * 00028 * Multiple instances of this object may coexist to wrap different 00029 * WvStream's or WvStreamList's. 00030 * 00031 */ 00032 class WvQtStreamClone : public QObject, public WvStreamClone 00033 { 00034 Q_OBJECT 00035 int msec_timeout; 00036 00037 SelectInfo si; 00038 bool pending_callback; 00039 bool first_time; 00040 bool select_in_progress; 00041 int last_max_fd; 00042 QIntDict<QSocketNotifier> notify_readable; 00043 QIntDict<QSocketNotifier> notify_writable; 00044 QIntDict<QSocketNotifier> notify_exception; 00045 QTimer select_timer; 00046 00047 public: 00048 /** 00049 * WvQtStreamClone takes ownership of the stream you give it 00050 * just like WvStreamClone. See comments there. 00051 * 00052 * Timeout sets the time between polls with select(). 00053 * A value less than zero is regarded as an infinite timeout. 00054 */ 00055 WvQtStreamClone(WvStream* _cloned = NULL, int msec_timeout = -1); 00056 virtual ~WvQtStreamClone(); 00057 00058 // Call this to stop managing this stream via the Qt event loop. 00059 // Afterwards you may run a normal WvStream event loop based 00060 // on this object. 00061 void qt_detach(); 00062 00063 // Call this to resume managing this stream via the Qt event loop. 00064 // This is the default state when the object is constructed. 00065 void qt_attach(); 00066 00067 // Changes the timeout 00068 // You may need to adjust the timeout when using badly behaved streams 00069 void set_timeout(int msec_timeout); 00070 00071 private: 00072 // Called before the Qt event loop does its select() 00073 void pre_poll(); 00074 // Called after the Qt event loop has finished its notifications 00075 void post_poll(); 00076 00077 private slots: 00078 /** These things mark the beginning of a select pass **/ 00079 // Qt event loop hook (happens before each iteration) 00080 void qt_begin_event_loop_hook(); 00081 00082 /** These things mark the end of a select pass **/ 00083 // Qt select timeout expired 00084 void select_timer_expired(); 00085 // Called when a file descriptor has been marked readable 00086 void fd_readable(int fd); 00087 // Called when a file descriptor has been marked writable 00088 void fd_writable(int fd); 00089 // Called when a file descriptor has been marked with an exception 00090 void fd_exception(int fd); 00091 00092 // Needed or certain assertions fail ;) 00093 virtual void execute(); 00094 }; 00095 00096 #endif // __WVQTSTREAMCLONE_H