00001
00002 #ifndef MYTHSOCKET_H
00003 #define MYTHSOCKET_H
00004
00005 #include <QStringList>
00006 #include <QMutex>
00007
00008 #include "msocketdevice.h"
00009 #include "mythsocket_cb.h"
00010 #include "mythbaseexp.h"
00011
00012 template<class T >
00013 class QList;
00014 class QString;
00015 class QHostAddress;
00016 class MythSocketThread;
00017
00018 class MBASE_PUBLIC MythSocket : public MSocketDevice
00019 {
00020 friend class MythSocketThread;
00021 friend class QList<MythSocket*>;
00022 friend void ShutdownRRT(void);
00023
00024 public:
00025 MythSocket(int socket = -1, MythSocketCBs *cb = NULL);
00026
00027 enum State {
00028 Connected,
00029 Connecting,
00030 HostLookup,
00031 Idle
00032 };
00033
00034 void close(void);
00035 bool closedByRemote(void);
00036 void deleteLater(void);
00037
00038 void UpRef(void);
00039 bool DownRef(void);
00040
00041 State state(void) const;
00042 QString stateToString(void) const { return stateToString(state()); }
00043 QString stateToString(const State state) const;
00044
00045 QString errorToString(void) const { return errorToString(error()); }
00046 QString errorToString(const Error error) const;
00047
00048 bool Validate(uint timeout_ms = kMythSocketLongTimeout,
00049 bool error_dialog_desired = false);
00050 void setValidated(bool isValidated=true) { m_isValidated = isValidated; }
00051 bool isValidated(void) { return m_isValidated; }
00052
00053 bool Announce(QStringList &strlist);
00054 QStringList getAnnounce(void) { return m_announce; }
00055 void setAnnounce(QStringList &strlist);
00056 bool isAnnounced(void) { return m_isAnnounced; }
00057
00058 bool isExpectingReply(void) { return m_expectingreply; }
00059
00060 void setSocket(int socket, Type type = MSocketDevice::Stream);
00061 void setCallbacks(MythSocketCBs *cb);
00062 void useReadyReadCallback(bool useReadyReadCallback = true)
00063 { m_useReadyReadCallback = useReadyReadCallback; }
00064
00065 qint64 readBlock(char *data, quint64 len);
00066 qint64 writeBlock(const char *data, quint64 len);
00067
00068 bool readStringList(QStringList &list, uint timeoutMS = kLongTimeout);
00069 bool readStringList(QStringList &list, bool quickTimeout)
00070 {
00071 return readStringList(
00072 list, quickTimeout ? kShortTimeout : kLongTimeout);
00073 }
00074 bool writeStringList(QStringList &list);
00075 bool SendReceiveStringList(QStringList &list, uint min_reply_length = 0);
00076 bool readData(char *data, quint64 len);
00077 bool writeData(const char *data, quint64 len);
00078
00079 bool connect(const QHostAddress &hadr, quint16 port);
00080 bool connect(const QString &host, quint16 port);
00081
00082 void Lock(void) const;
00083 bool TryLock(bool wakereadyread) const;
00084 void Unlock(bool wakereadyread = true) const;
00085
00086 static const uint kShortTimeout;
00087 static const uint kLongTimeout;
00088
00089 protected:
00090 ~MythSocket();
00091
00092 void setState(const State state);
00093
00094 MythSocketCBs *m_cb;
00095 bool m_useReadyReadCallback;
00096 State m_state;
00097 QHostAddress m_addr;
00098 quint16 m_port;
00099 int m_ref_count;
00100
00101 bool m_notifyread;
00102 QMutex m_ref_lock;
00103 mutable QMutex m_lock;
00104
00105 bool m_expectingreply;
00106 bool m_isValidated;
00107 bool m_isAnnounced;
00108 QStringList m_announce;
00109
00110 static const uint kSocketBufferSize;
00111 static QMutex s_readyread_thread_lock;
00112 static MythSocketThread *s_readyread_thread;
00113
00114 static QMap<QString, QHostAddress::SpecialAddress> s_loopback_cache;
00115 };
00116
00117 #endif
00118