c++ - Writing custom QIODevice with 2 channels -


i trying implement custom qiodevice.

i have detector using 2 tcp sockets, 1 sending commands , receiving responses, other 1 reading data.

how should it? tried create class, derive qiodevice , implement pure virtual methods, faced dificulties.

i have following piece of code:

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);     } }; 

so question is, how handle channels, because example qt documentation have

bool qiodevice::open(openmode mode)

opens device , sets openmode mode. returns true if successful; otherwise returns false. function should called reimplementations of open() or other functions open device.

so have invoke overrided open() method, looked @ implementation of method , saw setting number of channels 1. how can change number?

what buffers inheriting qiodevice, how should use them?

am missing point of creating custom qiodevice? if so, please explain me.

unlikely need subclass qiodevice here, design , implement interface semantic need, e.g.:

sendcommand(); responsereceived(); dataready(); ... etc 

qiodevice common interface reading/writing byte streams from/to different sources files, sockets, serial ports, etc


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -