/
mythsocket.h
118 lines (90 loc) · 3.42 KB
/
mythsocket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/** -*- Mode: c++ -*- */
#ifndef MYTHSOCKET_H
#define MYTHSOCKET_H
#include <QStringList>
#include <QMutex>
#include "msocketdevice.h"
#include "mythsocket_cb.h"
#include "mythbaseexp.h"
template<class T >
class QList;
class QString;
class QHostAddress;
class MythSocketThread;
class MBASE_PUBLIC MythSocket : public MSocketDevice
{
friend class MythSocketThread;
friend class QList<MythSocket*>;
friend void ShutdownRRT(void);
public:
MythSocket(int socket = -1, MythSocketCBs *cb = NULL);
enum State {
Connected,
Connecting,
HostLookup,
Idle
};
void close(void);
bool closedByRemote(void);
void deleteLater(void);
void UpRef(void);
bool DownRef(void);
State state(void) const;
QString stateToString(void) const { return stateToString(state()); }
QString stateToString(const State state) const;
QString errorToString(void) const { return errorToString(error()); }
QString errorToString(const Error error) const;
bool Validate(uint timeout_ms = kMythSocketLongTimeout,
bool error_dialog_desired = false);
void setValidated(bool isValidated=true) { m_isValidated = isValidated; }
bool isValidated(void) { return m_isValidated; }
bool Announce(QStringList &strlist);
QStringList getAnnounce(void) { return m_announce; }
void setAnnounce(QStringList &strlist);
bool isAnnounced(void) { return m_isAnnounced; }
bool isExpectingReply(void) { return m_expectingreply; }
void setSocket(int socket, Type type = MSocketDevice::Stream);
void setCallbacks(MythSocketCBs *cb);
void useReadyReadCallback(bool useReadyReadCallback = true)
{ m_useReadyReadCallback = useReadyReadCallback; }
qint64 readBlock(char *data, quint64 len);
qint64 writeBlock(const char *data, quint64 len);
bool readStringList(QStringList &list, uint timeoutMS = kLongTimeout);
bool readStringList(QStringList &list, bool quickTimeout)
{
return readStringList(
list, quickTimeout ? kShortTimeout : kLongTimeout);
}
bool writeStringList(QStringList &list);
bool SendReceiveStringList(QStringList &list, uint min_reply_length = 0);
bool readData(char *data, quint64 len);
bool writeData(const char *data, quint64 len);
bool connect(const QHostAddress &hadr, quint16 port);
bool connect(const QString &host, quint16 port);
void Lock(void) const;
bool TryLock(bool wakereadyread) const;
void Unlock(bool wakereadyread = true) const;
static const uint kShortTimeout;
static const uint kLongTimeout;
protected:
~MythSocket(); // force refcounting
void setState(const State state);
MythSocketCBs *m_cb;
bool m_useReadyReadCallback;
State m_state;
QHostAddress m_addr;
quint16 m_port;
int m_ref_count;
bool m_notifyread;
QMutex m_ref_lock;
mutable QMutex m_lock; // externally accessible lock
bool m_expectingreply;
bool m_isValidated;
bool m_isAnnounced;
QStringList m_announce;
static const uint kSocketBufferSize;
static QMutex s_readyread_thread_lock;
static MythSocketThread *s_readyread_thread;
static QMap<QString, QHostAddress::SpecialAddress> s_loopback_cache;
};
#endif