qpipe.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003-2007  Justin Karneges <justin@affinix.com>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00017  *
00018  */
00019 
00030 #ifndef QPIPE_H
00031 #define QPIPE_H
00032 
00033 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00034 
00035 #ifndef QPIPE_NO_SECURE
00036 # define QPIPE_SECURE
00037 #endif
00038 
00039 #ifdef QPIPE_SECURE
00040 # include <QtCrypto>
00041 #else
00042 # define QCA_EXPORT
00043 #endif
00044 
00045 // defs adapted qprocess_p.h
00046 #ifdef Q_OS_WIN
00047 #include <windows.h>
00048 typedef HANDLE Q_PIPE_ID;
00049 #define INVALID_Q_PIPE_ID INVALID_HANDLE_VALUE
00050 #else
00051 typedef int Q_PIPE_ID;
00052 #define INVALID_Q_PIPE_ID -1
00053 #endif
00054 
00055 #endif
00056 
00057 // Note: for Windows console, I/O must be in UTF-8.  Reads are guaranteed to
00058 //   to completely decode (no partial characters).  Likewise, writes must
00059 //   not contain partial characters.
00060 
00061 namespace QCA {
00062 
00063 // unbuffered direct pipe
00064 class QCA_EXPORT QPipeDevice : public QObject
00065 {
00066         Q_OBJECT
00067 public:
00068         enum Type
00069         {
00070                 Read,
00071                 Write
00072         };
00073 
00074         QPipeDevice(QObject *parent = 0);
00075         ~QPipeDevice();
00076 
00077         Type type() const;                     // Read or Write
00078         bool isValid() const;                  // indicates if a pipe is held
00079         Q_PIPE_ID id() const;                  // pipe id (Win=HANDLE, Unix=int)
00080         int idAsInt() const;                   // pipe id turned into an integer
00081 
00082         void take(Q_PIPE_ID id, Type t);       // take over the pipe id, close the old
00083         void enable();                         // enables usage (read/write) of the pipe
00084         void close();                          // close the pipe
00085         void release();                        // let go of the pipe but don't close
00086         bool setInheritable(bool enabled);     // note: on windows, this operation changes the id
00087 
00088         int bytesAvailable() const;            // bytes available to read
00089         int read(char *data, int maxsize);     // return number read, 0 = EOF, -1 = error
00090         int write(const char *data, int size); // return number taken, ptr must stay valid. -1 on error
00091         int writeResult(int *written) const;   // 0 = success (wrote all), -1 = error (see written)
00092 
00093 Q_SIGNALS:
00094         void notify();                         // can read or can write, depending on type
00095 
00096 private:
00097         Q_DISABLE_COPY(QPipeDevice)
00098 
00099         class Private;
00100         friend class Private;
00101         Private *d;
00102 };
00103 
00104 // buffered higher-level pipe.  use this one.
00105 class QCA_EXPORT QPipeEnd : public QObject
00106 {
00107         Q_OBJECT
00108 public:
00109         enum Error
00110         {
00111                 ErrorEOF,
00112                 ErrorBroken
00113         };
00114 
00115         QPipeEnd(QObject *parent = 0);
00116         ~QPipeEnd();
00117 
00118         void reset();
00119 
00120         QPipeDevice::Type type() const;
00121         bool isValid() const;
00122         Q_PIPE_ID id() const;
00123         int idAsInt() const;
00124 
00125         void take(Q_PIPE_ID id, QPipeDevice::Type t);
00126 #ifdef QPIPE_SECURE
00127         void setSecurityEnabled(bool secure);
00128 #endif
00129         void enable();
00130         void close();
00131         void release();
00132         bool setInheritable(bool enabled);
00133 
00134         void finalize(); // do an immediate read, and invalidate
00135         void finalizeAndRelease(); // same as above, but don't close pipe
00136 
00137         int bytesAvailable() const;
00138         int bytesToWrite() const;
00139 
00140         // normal i/o
00141         QByteArray read(int bytes = -1);
00142         void write(const QByteArray &a);
00143 
00144 #ifdef QPIPE_SECURE
00145         // secure i/o
00146         SecureArray readSecure(int bytes = -1);
00147         void writeSecure(const SecureArray &a);
00148 #endif
00149 
00150         QByteArray takeBytesToWrite();
00151 
00152 #ifdef QPIPE_SECURE
00153         SecureArray takeBytesToWriteSecure();
00154 #endif
00155 
00156 Q_SIGNALS:
00157         void readyRead();
00158         void bytesWritten(int bytes);
00159         void closed();
00160         void error(QCA::QPipeEnd::Error e);
00161 
00162 private:
00163         Q_DISABLE_COPY(QPipeEnd)
00164 
00165         class Private;
00166         friend class Private;
00167         Private *d;
00168 };
00169 
00179 class QCA_EXPORT QPipe
00180 {
00181 public:
00187         QPipe(QObject *parent = 0);
00188 
00189         ~QPipe();
00190 
00194         void reset();
00195 
00196 #ifdef QPIPE_SECURE
00197 
00202         bool create(bool secure = false);
00203 #else
00204 
00207         bool create();
00208 #endif
00209 
00213         QPipeEnd & readEnd() { return i; }
00214 
00218         QPipeEnd & writeEnd() { return o; }
00219 
00220 private:
00221         Q_DISABLE_COPY(QPipe)
00222 
00223         QPipeEnd i, o;
00224 };
00225 
00226 }
00227 
00228 #endif

Generated on Fri Jul 6 13:22:42 2007 for Qt Cryptographic Architecture by  doxygen 1.4.6