Note that there are some explanatory texts on larger screens.

plurals
  1. POPersistent Qt Local Socket IPC
    text
    copied!<p>I'm developing an application that uses IPC between a local server and a client application. There is nothing particular to it, as it's structured like the Qt documentation and examples.</p> <p>The problem is that the client sends packets frequently and connecting/disconnecting from the server local socket (named pipe on NT) is very slow. So what I'm trying to achieve is a "persistent" connection between the two applications.</p> <p>The client application connects to the local server (QLocalServer) without any problem:</p> <pre><code>void IRtsClientImpl::ConnectToServer(const QString&amp; name) { connect(_socket, SIGNAL(connected()), this, SIGNAL(connected())); _blockSize = 0; _socket-&gt;abort(); _socket-&gt;connectToServer(name, QIODevice::ReadWrite); } </code></pre> <p>And sends requests also in the traditional Qt manner:</p> <pre><code>void IRtsClientImpl::SendRequest( quint8 cmd, const QVariant* const param_array, unsigned int cParams ) { // Send data through socket QByteArray hdr(PROTO_BLK_HEADER_PROJ); QByteArray dataBlock; QDataStream out(&amp;dataBlock, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_5); quint8 command = cmd; out &lt;&lt; blocksize_t(0) // block size &lt;&lt; hdr // header &lt;&lt; quint32(PROTO_VERSION_PROJ) // protocol version &lt;&lt; command // command &lt;&lt; cParams; // number of valid parameters for (unsigned int i = 0; i &lt; cParams; ++i) out &lt;&lt; param_array[i]; // Write the current block size out.device()-&gt;seek(0); out &lt;&lt; dataBlock.size() - sizeof(blocksize_t); _socket-&gt;write(dataBlock); } </code></pre> <p>No problem. But the trick resides on the readyRead() signal in the server-side. Here's the current implementation of the readyRead() handling slot:</p> <pre><code>void IRtsServerImpl::onReadyRead() { QDataStream in(_lsock); in.setVersion(QDataStream::Qt_4_5); if (_blocksize == 0) { qDebug("Bytes Available on socket: %d", _lsock-&gt;bytesAvailable()); if (_lsock-&gt;bytesAvailable() &lt; sizeof(blocksize_t)) return; in &gt;&gt; _blocksize; } // We need more data? if (_lsock-&gt;bytesAvailable() &lt; _blocksize) return; ReadRequest(in); // Reset _blocksize = 0; } </code></pre> <p>Without setting <code>_blocksize</code> to zero I could not receive more data, only the first block group (I would expect an entire block to arrive without segmentation since this is through a pipe, but it does not, go figure). I expect that behavior, sure, since the _blocksize does not represent the current stream flow anymore. All right, resetting _blocksize does the trick, but I can't resend another packet from the client without getting an increasing array of bytes on the socket. What I want is to process the request in ReadRequest and receive the next data blocks without resorting to connecting/reconnecting the applications involved.</p> <p>Maybe I should 'regulate' the rate of the incoming data?</p> <p>Thank you very much.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload