MythTV  master
bufferedsocketdevice.h
Go to the documentation of this file.
1 // Program Name: bufferedsocketdevice.h
3 // Created : Oct. 1, 2005
4 //
5 // Purpose :
6 //
7 // Copyright (c) 2005 David Blain <dblain@mythtv.org>
8 //
9 // Licensed under the GPL v2 or later, see COPYING for details
10 //
12 
13 #ifndef __BUFFEREDSOCKETDEVICE_H__
14 #define __BUFFEREDSOCKETDEVICE_H__
15 
16 // C++ headers
17 #include <deque>
18 using namespace std;
19 
20 // Qt headers
21 #include <QString>
22 #include <QByteArray>
23 #include <QHostAddress>
24 
25 // MythTV headers
26 #include "mmembuf.h"
27 #include "msocketdevice.h"
28 #include "compat.h"
29 
32 //
33 //
34 //
37 
39 {
40  protected:
41 
42  MSocketDevice *m_pSocket {nullptr};
43 
44  qulonglong m_nMaxReadBufferSize {0};
45  qint64 m_nWriteSize {0};
46  qint64 m_nWriteIndex {0};
47 
48  bool m_bHandleSocketDelete {true};
49 
50  QHostAddress m_DestHostAddress;
51  quint16 m_nDestPort {0};
52 
53  MMembuf m_bufRead;
54  deque<QByteArray*> m_bufWrite;
55 
56  int ReadBytes ( );
57  bool ConsumeWriteBuf( qulonglong nbytes );
58 
59  public:
60 
61  explicit BufferedSocketDevice( int nSocket );
62  explicit BufferedSocketDevice( MSocketDevice *pSocket = nullptr,
63  bool bTakeOwnership = false );
64 
65  virtual ~BufferedSocketDevice( );
66 
67  MSocketDevice *SocketDevice ();
68  void SetSocketDevice ( MSocketDevice *pSocket );
69 
70  void SetDestAddress ( QHostAddress hostAddress,
71  quint16 nPort );
72 
73  bool Connect ( const QHostAddress &addr,
74  quint16 port );
75  void Close ();
76  void Flush ();
77  qint64 Size ();
78  qint64 At () const;
79  bool At ( qlonglong );
80  bool AtEnd ();
81 
82  qulonglong BytesAvailable ();
83  qulonglong WaitForMore ( int msecs,
84  bool *timeout = nullptr );
85 
86  qulonglong BytesToWrite () const;
87  void ClearPendingData ();
88  void ClearReadBuffer ();
89 
90  qlonglong ReadBlock ( char *data,
91  qulonglong maxlen );
92  qlonglong WriteBlock ( const char *data,
93  qulonglong len );
94  qlonglong WriteBlockDirect ( const char *data,
95  qulonglong len );
96 
97  int Getch ();
98  int Putch ( int );
99  int Ungetch (int);
100 
101  bool CanReadLine ();
102  QString ReadLine ();
103  QString ReadLine ( int msecs );
104  qlonglong ReadLine ( char *data,
105  qulonglong maxlen );
106 
107  quint16 Port () const;
108  quint16 PeerPort () const;
109  QHostAddress Address () const;
110  QHostAddress PeerAddress () const;
111 
112  void SetReadBufferSize ( qulonglong );
113  qulonglong ReadBufferSize () const;
114 
115  bool IsValid () { return( ( m_pSocket ) ? m_pSocket->isValid() : false ); }
116  int socket () { return( ( m_pSocket ) ? m_pSocket->socket() : 0 ); }
117 };
118 
119 #endif
deque< QByteArray * > m_bufWrite
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85