qpipe.h
Go to the documentation of this file.
/*
* Copyright (C) 2003-2007 Justin Karneges <justin@affinix.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
#ifndef QPIPE_H
#define QPIPE_H
#ifndef DOXYGEN_SHOULD_SKIP_THIS
#ifndef QPIPE_NO_SECURE
#define QPIPE_SECURE
#endif
#ifdef QPIPE_SECURE
#include "QtCrypto"
#else
#define QCA_EXPORT
#endif
// defs adapted qprocess_p.h
#ifdef Q_OS_WIN
#include <windows.h>
typedef HANDLE Q_PIPE_ID;
#define INVALID_Q_PIPE_ID INVALID_HANDLE_VALUE
#else
typedef int Q_PIPE_ID;
#define INVALID_Q_PIPE_ID -1
#endif
#endif
// Note: for Windows console, I/O must be in UTF-8. Reads are guaranteed to
// to completely decode (no partial characters). Likewise, writes must
// not contain partial characters.
namespace QCA {
class QCA_EXPORT QPipeDevice : public QObject
{
Q_OBJECT
public:
enum Type
{
Read,
Write
};
QPipeDevice(QObject *parent = nullptr);
~QPipeDevice() override;
Type type() const;
bool isValid() const;
Q_PIPE_ID id() const;
int idAsInt() const;
void take(Q_PIPE_ID id, Type t);
void enable();
void close();
void release();
bool setInheritable(bool enabled);
int bytesAvailable() const;
int read(char *data, int maxsize);
int write(const char *data, int size);
int writeResult(int *written) const;
Q_SIGNALS:
void notify();
private:
Q_DISABLE_COPY(QPipeDevice)
class Private;
friend class Private;
Private *d;
};
class QCA_EXPORT QPipeEnd : public QObject
{
Q_OBJECT
public:
enum Error
{
ErrorEOF,
ErrorBroken
};
QPipeEnd(QObject *parent = nullptr);
~QPipeEnd() override;
void reset();
QPipeDevice::Type type() const;
bool isValid() const;
Q_PIPE_ID id() const;
int idAsInt() const;
void take(Q_PIPE_ID id, QPipeDevice::Type t);
#ifdef QPIPE_SECURE
void setSecurityEnabled(bool secure);
#endif
void enable();
void close();
void release();
bool setInheritable(bool enabled);
void finalize();
void finalizeAndRelease();
int bytesAvailable() const;
int bytesToWrite() const;
QByteArray read(int bytes = -1);
void write(const QByteArray &a);
#ifdef QPIPE_SECURE
SecureArray readSecure(int bytes = -1);
void writeSecure(const SecureArray &a);
#endif
QByteArray takeBytesToWrite();
#ifdef QPIPE_SECURE
SecureArray takeBytesToWriteSecure();
#endif
Q_SIGNALS:
void readyRead();
void bytesWritten(int bytes);
void closed();
void error(QCA::QPipeEnd::Error e);
private:
Q_DISABLE_COPY(QPipeEnd)
class Private;
friend class Private;
Private *d;
};
class QCA_EXPORT QPipe
{
public:
QPipe(QObject *parent = nullptr);
~QPipe();
void reset();
#ifdef QPIPE_SECURE
bool create(bool secure = false);
#else
bool create();
#endif
QPipeEnd &readEnd()
{
return i;
}
QPipeEnd &writeEnd()
{
return o;
}
private:
Q_DISABLE_COPY(QPipe)
QPipeEnd i, o;
};
}
#endif
QCA::QPipeDevice::release
void release()
Release the pipe end, but do not close it.
QCA::QPipeDevice::QPipeDevice
QPipeDevice(QObject *parent=nullptr)
Standard constructor.
QCA::QPipeDevice::idAsInt
int idAsInt() const
The low level identification for this pipe, returned as an integer.
QCA::QPipeDevice::take
void take(Q_PIPE_ID id, Type t)
Take over an existing pipe id, closing the old pipe if any.
QCA::QPipeDevice::notify
void notify()
Emitted when the pipe end can be read from or written to (depending on its Type).
QCA::QPipeDevice::setInheritable
bool setInheritable(bool enabled)
Set the pipe end to be inheritable.
QCA::QPipeDevice::bytesAvailable
int bytesAvailable() const
Obtain the number of bytes available to be read.
QCA::QPipeDevice::write
int write(const char *data, int size)
Write to the pipe end.
QCA::QPipeDevice::read
int read(char *data, int maxsize)
Read from the pipe end.
QCA::QPipeDevice::close
void close()
Close the pipe end.
QCA::QPipeDevice::id
Q_PIPE_ID id() const
The low level identification for this pipe.
QCA::QPipeDevice::writeResult
int writeResult(int *written) const
The result of a write operation.
QCA::QPipeDevice::isValid
bool isValid() const
Test whether this object corresponds to a valid pipe.
QCA::QPipeDevice::type
Type type() const
The Type of the pipe device (that is, read or write).
QCA::QPipeDevice::enable
void enable()
Enable the pipe for reading or writing (depending on Type).
QCA::QPipeDevice::Type
Type
The type of device.
Definition qpipe.h:82
QCA::QPipeDevice::Write
@ Write
The pipe end can be written to.
Definition qpipe.h:84
QCA::QPipeDevice::Read
@ Read
The pipe end can be read from.
Definition qpipe.h:83
QCA::QPipeEnd
A buffered higher-level pipe end.
Definition qpipe.h:218
QCA::QPipeEnd::QPipeEnd
QPipeEnd(QObject *parent=nullptr)
Standard constructor.
QCA::QPipeEnd::error
void error(QCA::QPipeEnd::Error e)
Emitted when the pipe encounters an error trying to read or write, or if the other end of the pipe ha...
QCA::QPipeEnd::takeBytesToWrite
QByteArray takeBytesToWrite()
Returns any unsent bytes queued for writing.
QCA::QPipeEnd::readyRead
void readyRead()
Emitted when there are bytes available to be read from the read end of the pipe.
QCA::QPipeEnd::bytesWritten
void bytesWritten(int bytes)
Emitted when bytes have been written to the write end of the pipe.
QCA::QPipeEnd::finalizeAndRelease
void finalizeAndRelease()
Clear the contents of the pipe, and release the pipe.
QCA::QPipeEnd::takeBytesToWriteSecure
SecureArray takeBytesToWriteSecure()
Returns any unsent bytes queued for writing.
QCA::QPipeEnd::isValid
bool isValid() const
Determine whether the pipe end is valid.
QCA::QPipeEnd::bytesAvailable
int bytesAvailable() const
Determine how many bytes are available to be read.
QCA::QPipeEnd::closed
void closed()
Emitted when this end of the pipe is closed as a result of calling close().
QCA::QPipeEnd::write
void write(const QByteArray &a)
Write bytes to the pipe.
QCA::QPipeEnd::readSecure
SecureArray readSecure(int bytes=-1)
Read bytes from the pipe.
QCA::QPipeEnd::finalize
void finalize()
Clear the contents of the pipe, and invalidate the pipe.
QCA::QPipeEnd::writeSecure
void writeSecure(const SecureArray &a)
Write bytes to the pipe.
QCA::QPipeEnd::setSecurityEnabled
void setSecurityEnabled(bool secure)
Sets whether the pipe uses secure memory for read/write.
QCA::QPipeEnd::release
void release()
Let go of the active pipe handle, but don't close it.
QCA::QPipeEnd::id
Q_PIPE_ID id() const
Pipe identification.
QCA::QPipeEnd::reset
void reset()
Reset the pipe end to an inactive state.
QCA::QPipeEnd::Error
Error
The type of error.
Definition qpipe.h:225
QCA::QPipeEnd::ErrorBroken
@ ErrorBroken
Broken pipe error.
Definition qpipe.h:227
QCA::QPipeEnd::ErrorEOF
@ ErrorEOF
End of file error.
Definition qpipe.h:226
QCA::QPipeEnd::bytesToWrite
int bytesToWrite() const
Returns the number of bytes pending to write.
QCA::QPipeEnd::close
void close()
Close the end of the pipe.
QCA::QPipeEnd::setInheritable
bool setInheritable(bool enabled)
Sets whether the pipe should be inheritable to child processes.
QCA::QPipeEnd::idAsInt
int idAsInt() const
Pipe identification.
QCA::QPipeEnd::read
QByteArray read(int bytes=-1)
Read bytes from the pipe.
QCA::QPipeEnd::take
void take(Q_PIPE_ID id, QPipeDevice::Type t)
Take over an existing pipe handle.
QCA::QPipeEnd::enable
void enable()
Enable the endpoint for the pipe.
QCA::QPipeEnd::type
QPipeDevice::Type type() const
The type of pipe end (either read or write).
QCA::QPipe
A FIFO buffer (named pipe) abstraction.
Definition qpipe.h:479
QCA::QPipe::writeEnd
QPipeEnd & writeEnd()
The write end of the pipe.
Definition qpipe.h:525
QCA::QPipe::reset
void reset()
Reset the pipe.
QCA::QPipe::QPipe
QPipe(QObject *parent=nullptr)
Standard constructor.
QCA::QPipe::readEnd
QPipeEnd & readEnd()
The read end of the pipe.
Definition qpipe.h:517
QCA::QPipe::create
bool create(bool secure=false)
Create the pipe.
QCA::SecureArray
Secure array of bytes.
Definition qca_tools.h:317
QObject
QCA
QCA - the Qt Cryptographic Architecture.
Definition qca_basic.h:41