我正在尝试实现自定义 QIODevice。
我有检测器,它将使用 2 个 tcp 套接字,一个用于发送命令和接收响应,另一个用于读取数据。
我该怎么做?我试图创建我的类,从 QIODevice 派生它并实现纯虚拟方法,但我遇到了一些困难。
我有以下代码:
class Detector : public QIODevice {
Q_OBJECT
Q_DISABLE_COPY(MasterDevice)
public:
enum CHANNEL_TYPE {
DataChannel,
ControlChannel
};
private:
QTcpSocket *controlDevice;
QTcpSocket *dataDevice;
QHostAddress hostAddress;
quint16 dataPort;
quint16 controlPort;
public:
explicit Detector(QObject *parent, QHostAddress hostAddress, quint16 dataPort, quint16 controlPort)
: QIODevice(parent)
, hostAddress(hostAddress)
, dataPort(dataPort)
, controlPort(controlPort)
{
controlDevice = new QTcpSocket(this);
connect(controlDevice, SIGNAL(readyRead()),this, SLOT(controlChannelReadReady()));
dataDevice = new QTcpSocket(this);
connect(dataDevice, SIGNAL(readyRead()),this, SLOT(dataChannelReadReady()));
}
virtual ~Detector() {}
bool open(OpenMode mode) override {
QIODevice::open(mode);
controlDevice->connectToHost(hostAddress, controlPort, QTcpSocket::ReadWrite);
dataDevice->connectToHost(hostAddress, dataPort, QTcpSocket::ReadOnly);
}
qint64 readData(char *data, qint64 maxSize) override {
QTcpSocket *socket;
switch ( currentReadChannel() ) {
case DataChannel:
socket = dataDevice;
break;
case ControlChannel:
socket = controlDevice;
break;
default:
return -1;
break;
}
return socket->read(data, maxSize);
}
qint64 writeData(const char * data, qint64 maxSize) override {
QTcpSocket *socket;
switch ( currentWriteChannel() ) {
case DataChannel:
socket = dataDevice;
break;
case ControlChannel:
socket = controlDevice;
break;
default:
return -1;
break;
}
return socket->write(data, maxSize);
}
private slots:
void controlChannelReadReady() {
emit channelReadyRead(ControlChannel);
}
void dataChannelReadReady() {
emit channelReadyRead(DataChannel);
}
};
所以基本上我的问题是,我如何处理这些渠道,因为例如qt documentation
我有这个
bool QIODevice::open(OpenMode mode)
打开设备并将其 OpenMode 设置为 mode。成功则返回真;否则返回假。应该从 open() 的任何重新实现或打开设备的其他函数中调用此函数。
所以我必须从我的重写Open()
方法中调用它,但是我查看了这个方法的实现,我看到它正在将通道数设置为 1。我该如何更改这个数字?
我从 QIODevice 继承的那些缓冲区有什么用,我应该如何使用它们?
我错过了创建自定义 QIODevice 的意义吗?如果是这样,请给我解释一下。