MythTV master
bufferedsocketdevice.h
Go to the documentation of this file.
1
2// 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 LICENSE for details
10//
12
13#ifndef BUFFEREDSOCKETDEVICE_H
14#define BUFFEREDSOCKETDEVICE_H
15
16// C++ headers
17#include <chrono>
18#include <deque>
19
20// Qt headers
21#include <QString>
22#include <QByteArray>
23#include <QHostAddress>
24
25// MythTV headers
26#include "libmythbase/compat.h"
27
28#include "mmembuf.h"
29#include "msocketdevice.h"
30
33//
34//
35//
38
40{
41 protected:
42
43 MSocketDevice *m_pSocket {nullptr};
44
45 qulonglong m_nMaxReadBufferSize {0};
46 qint64 m_nWriteSize {0};
47 qint64 m_nWriteIndex {0};
48
50
51 QHostAddress m_destHostAddress;
52 quint16 m_nDestPort {0};
53
54 MMembuf m_bufRead;
55 std::deque<QByteArray*> m_bufWrite;
56
57 int ReadBytes ( );
58 bool ConsumeWriteBuf( qulonglong nbytes );
59
60 public:
61
62 explicit BufferedSocketDevice( int nSocket );
63 explicit BufferedSocketDevice( MSocketDevice *pSocket = nullptr,
64 bool bTakeOwnership = false );
65
66 virtual ~BufferedSocketDevice( );
67
68 MSocketDevice *SocketDevice ();
69 void SetSocketDevice ( MSocketDevice *pSocket );
70
71 void SetDestAddress ( QHostAddress hostAddress,
72 quint16 nPort );
73
74 bool Connect ( const QHostAddress &addr,
75 quint16 port );
76 void Close ();
77 void Flush ();
78 qint64 Size ();
79 static qint64 At () ;
80 bool At ( qlonglong index );
81 bool AtEnd ();
82
83 qulonglong BytesAvailable ();
84 qulonglong WaitForMore ( std::chrono::milliseconds msecs,
85 bool *timeout = nullptr );
86
87 qulonglong BytesToWrite () const;
88 void ClearPendingData ();
89 void ClearReadBuffer ();
90
91 qlonglong ReadBlock ( char *data,
92 qulonglong maxlen );
93 qlonglong WriteBlock ( const char *data,
94 qulonglong len );
95 qlonglong WriteBlockDirect ( const char *data,
96 qulonglong len );
97
98 int Getch ();
99 int Putch ( int ch );
100 int Ungetch ( int ch );
101
102 bool CanReadLine ();
103 QString ReadLine ();
104 QString ReadLine ( std::chrono::milliseconds msecs );
105 qlonglong ReadLine ( char *data,
106 qulonglong maxlen );
107
108 quint16 Port () const;
109 quint16 PeerPort () const;
110 QHostAddress Address () const;
111 QHostAddress PeerAddress () const;
112
113 void SetReadBufferSize ( qulonglong bufSize );
114 qulonglong ReadBufferSize () const;
115
116 bool IsValid () { return( ( m_pSocket ) ? m_pSocket->isValid() : false ); }
117 int socket () { return( ( m_pSocket ) ? m_pSocket->socket() : 0 ); }
118};
119
120#endif // BUFFEREDSOCKETDEVICE_H
void SetReadBufferSize(qulonglong bufSize)
qlonglong ReadLine(char *data, qulonglong maxlen)
qulonglong WaitForMore(std::chrono::milliseconds msecs, bool *timeout=nullptr)
QHostAddress Address() const
qlonglong WriteBlock(const char *data, qulonglong len)
std::deque< QByteArray * > m_bufWrite
qint64 m_nWriteIndex
write index
qlonglong WriteBlockDirect(const char *data, qulonglong len)
MSocketDevice * SocketDevice()
qulonglong BytesToWrite() const
qulonglong ReadBufferSize() const
bool ConsumeWriteBuf(qulonglong nbytes)
qint64 m_nWriteSize
write total buf size
bool Connect(const QHostAddress &addr, quint16 port)
void SetDestAddress(QHostAddress hostAddress, quint16 nPort)
qlonglong ReadBlock(char *data, qulonglong maxlen)
QHostAddress PeerAddress() const
void SetSocketDevice(MSocketDevice *pSocket)