00001 00002 // Program Name: bufferedsocketdevice.h 00003 // Created : Oct. 1, 2005 00004 // 00005 // Purpose : 00006 // 00007 // Copyright (c) 2005 David Blain <dblain@mythtv.org> 00008 // 00009 // Licensed under the GPL v2 or later, see COPYING for details 00010 // 00012 00013 #ifndef __BUFFEREDSOCKETDEVICE_H__ 00014 #define __BUFFEREDSOCKETDEVICE_H__ 00015 00016 // C++ headers 00017 #include <deque> 00018 using namespace std; 00019 00020 // Qt headers 00021 #include <QString> 00022 #include <QByteArray> 00023 #include <QHostAddress> 00024 00025 // MythTV headers 00026 #include "mmembuf.h" 00027 #include "msocketdevice.h" 00028 #include "compat.h" 00029 00032 // 00033 // 00034 // 00037 00038 class BufferedSocketDevice 00039 { 00040 protected: 00041 00042 MSocketDevice *m_pSocket; 00043 00044 qulonglong m_nMaxReadBufferSize; 00045 qint64 m_nWriteSize; 00046 qint64 m_nWriteIndex; 00047 00048 bool m_bHandleSocketDelete; 00049 00050 QHostAddress m_DestHostAddress; 00051 quint16 m_nDestPort; 00052 00053 MMembuf m_bufRead; 00054 deque<QByteArray*> m_bufWrite; 00055 00056 int ReadBytes ( ); 00057 bool ConsumeWriteBuf( qulonglong nbytes ); 00058 00059 public: 00060 00061 BufferedSocketDevice( int nSocket ); 00062 explicit BufferedSocketDevice( MSocketDevice *pSocket = NULL, 00063 bool bTakeOwnership = false ); 00064 00065 virtual ~BufferedSocketDevice( ); 00066 00067 MSocketDevice *SocketDevice (); 00068 void SetSocketDevice ( MSocketDevice *pSocket ); 00069 00070 void SetDestAddress ( QHostAddress hostAddress, 00071 quint16 nPort ); 00072 00073 bool Connect ( const QHostAddress &addr, 00074 quint16 port ); 00075 void Close (); 00076 void Flush (); 00077 qint64 Size (); 00078 qint64 At () const; 00079 bool At ( qlonglong ); 00080 bool AtEnd (); 00081 00082 qulonglong BytesAvailable (); 00083 qulonglong WaitForMore ( int msecs, 00084 bool *timeout = NULL ); 00085 00086 qulonglong BytesToWrite () const; 00087 void ClearPendingData (); 00088 void ClearReadBuffer (); 00089 00090 qlonglong ReadBlock ( char *data, 00091 qulonglong maxlen ); 00092 qlonglong WriteBlock ( const char *data, 00093 qulonglong len ); 00094 qlonglong WriteBlockDirect ( const char *data, 00095 qulonglong len ); 00096 00097 int Getch (); 00098 int Putch ( int ); 00099 int Ungetch (int); 00100 00101 bool CanReadLine (); 00102 QString ReadLine (); 00103 QString ReadLine ( int msecs ); 00104 qlonglong ReadLine ( char *data, 00105 qulonglong maxlen ); 00106 00107 quint16 Port () const; 00108 quint16 PeerPort () const; 00109 QHostAddress Address () const; 00110 QHostAddress PeerAddress () const; 00111 00112 void SetReadBufferSize ( qulonglong ); 00113 qulonglong ReadBufferSize () const; 00114 00115 bool IsValid () { return( ( m_pSocket ) ? m_pSocket->isValid() : false ); } 00116 int socket () { return( ( m_pSocket ) ? m_pSocket->socket() : 0 ); } 00117 }; 00118 00119 #endif
1.6.3