PDA

View Full Version : Named pipes in Qt



xenko
10th July 2008, 16:02
Linux and windows can create what are called named pipes. They are basically a cross between a file and a pipe, or a socket.

For example, in linux this can be done using the mkfifo line command. This creates a permanent identifier in the file system which can be open by two or more programs and share data through it, as if it were a file, using the common file writing and reading calls from the system.

Windows offers as well some system calls to create named pipes in the file system, and use them while they are kept open by the application.

We're creating an image and video processing tool which communicates with the MPlayer application through this kind of pipes. Using sockets might be a bit of slow down, and the MPlayer is not prepared to work using common pipes.

To solve this we create a named pipe, and execute a copy of the MPlayer. We launch it properly configured to decode a video file or read from a digital camera and dump the data in an easy readable format to an end of the named pipe.
The other end is connected to an image processing thread created by our application.

We've created a class of the Qt style named QNamedPipe:


class QNamedPipe: public QObject
{
Q_OBJECT;
public:
QNamedPipe();
~QNamedPipe();

QString getInputFilePath() const;
QString getOutputFilePath() const;
};

Its basic usage is as follows: to create a named pipe, create an object from that class. To get an end's name, use functions 'getInputFilePath' and 'getOutputFilePath'. To destroy the named pipe just delete the object.

Could it be interesting to include such a class (or alike) to create named pipes in Qt? How could this be proposed to the trolls?

We've just developed the class to work on linux, but it may be helpful if it were also for windows.

jpn
10th July 2008, 17:30
Have you noticed QLocalSocket?

wysota
10th July 2008, 17:33
There is a QLocalSocket class since Qt4.4 which uses named pipes on Windows and local sockets on Unix. I guess it might be doing more or less what your class does.

seneca
20th July 2012, 09:05
Unfortunately QLocalSocket prevents from connecting to other servers, and only allows to connect to local pipe servers (well, surely to satisfy the "Local" in the class name.

With a small hack that could be fixed however, I think changing the server name adjustment in qlocalsocket_win.cpp could do the trick:

Original:

QString pipePath = QLatin1String("\\\\.\\pipe\\");
if (name.startsWith(pipePath))
d->fullServerName = name;
else
d->fullServerName = pipePath + name;


Hacked:

QString pipePath = QLatin1String("\\\\.\\pipe\\");
if (name.startsWith(QLatin1String("\\\\")))
d->fullServerName = name;
else
d->fullServerName = pipePath + name;