Rabbit Remote Control 0.0.30
Loading...
Searching...
No Matches
Channel.cpp
1// Author: Kang Lin <kl222@126.com>
2
3#include "Channel.h"
4#include <QtGlobal>
5#include <QCoreApplication>
6#include <QLoggingCategory>
7#include <QTranslator>
8#include "RabbitCommonTools.h"
9
10static Q_LOGGING_CATEGORY(log, "Channel")
11
12QSharedPointer<QTranslator> g_Translator = nullptr;
13
14CChannel::CChannel(QObject *parent)
15 : QIODevice(parent),
16 m_pSocket(nullptr)
17{}
18
19CChannel::CChannel(QTcpSocket *pSocket, QObject *parent)
20 : QIODevice(parent),
21 m_pSocket(pSocket)
22{
23 qDebug(log) << "CChannel::CChannel";
24 Q_ASSERT(m_pSocket);
25}
26
27CChannel::~CChannel()
28{
29 qDebug(log) << "CChannel::~CChannel";
30}
31
32qint64 CChannel::readData(char *data, qint64 maxlen)
33{
34 int nRet = 0;
35 /*
36 qDebug(log) << "CChannel::readData:"
37 << maxlen; //*/
38 if(m_pSocket)
39 nRet = m_pSocket->read(data, maxlen);
40 if(nRet < 0) {
41 setErrorString(m_pSocket->errorString());
42 }
43 return nRet;
44}
45
46qint64 CChannel::writeData(const char *data, qint64 len)
47{
48 //qDebug(log) << "CChannel::writeData:" << len;
49 if(nullptr == data || 0 >= len) {
50 qCritical(log) << "writeData fail. len:" << len;
51 return 0;
52 }
53
54 int nRet = 0;
55 if(m_pSocket)
56 nRet = m_pSocket->write(data, len);
57 if(nRet < 0)
58 setErrorString(m_pSocket->errorString());
59 return nRet;
60}
61
62bool CChannel::isSequential() const
63{
64 return true;
65}
66
67void CChannel::slotError(QAbstractSocket::SocketError e)
68{
69 QString szError;
70 if(m_pSocket)
71 szError = m_pSocket->errorString();
72 /*
73 qDebug(log) << "CChannel::slotError()" << e
74 << m_pSocket->errorString();//*/
75 emit sigError(e, szError);
76}
77
78bool CChannel::open(OpenMode mode)
79{
80 bool check = false;
81
82 if(!m_pSocket) return false;
83
84 check = connect(m_pSocket, SIGNAL(readyRead()),
85 this, SIGNAL(readyRead()));
86 Q_ASSERT(check);
87 check = connect(m_pSocket, SIGNAL(connected()),
88 this, SIGNAL(sigConnected()));
89 Q_ASSERT(check);
90 check = connect(m_pSocket, SIGNAL(disconnected()),
91 this, SIGNAL(sigDisconnected()));
92 Q_ASSERT(check);
93 check = connect(m_pSocket,
94 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
95 SIGNAL(errorOccurred(QAbstractSocket::SocketError)),
96 #else
97 SIGNAL(error(QAbstractSocket::SocketError)),
98 #endif
99 this, SLOT(slotError(QAbstractSocket::SocketError)));
100 Q_ASSERT(check);
101 return QIODevice::open(mode);
102}
103
104void CChannel::close()
105{
106 if(m_pSocket)
107 m_pSocket->close();
108 QIODevice::close();
109}
110
111QString CChannel::GetDetails()
112{
113 return QString();
114}
115
116int CChannel::InitTranslation()
117{
118 if(!g_Translator)
119 g_Translator = RabbitCommon::CTools::Instance()->InstallTranslator(
120 "Channel", RabbitCommon::CTools::TranslationType::Library);
121 return 0;
122}
123
124int CChannel::RemoveTranslation()
125{
126 if(g_Translator)
127 RabbitCommon::CTools::Instance()->RemoveTranslator(g_Translator);
128 return 0;
129}
The channel interface class.
Definition Channel.h:25
void sigConnected()
emit when the channel is connected.
void sigDisconnected()
emit when the channel is disconnected
void sigError(int nErr, const QString &szErr)
emit when the channel is error
virtual bool open(OpenMode mode) override
Definition Channel.cpp:78
CChannel(QTcpSocket *pSocket, QObject *parent=nullptr)
CChannel.
Definition Channel.cpp:19