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:
00071         enum Type
00072         {
00073                 Read, 
00074                 Write 
00075         };
00076 
00077         QPipeDevice(QObject *parent = 0);
00078         ~QPipeDevice();
00079 
00080         Type type() const;                     // Read or Write
00081         bool isValid() const;                  // indicates if a pipe is held
00082         Q_PIPE_ID id() const;                  // pipe id (Win=HANDLE, Unix=int)
00083         int idAsInt() const;                   // pipe id turned into an integer
00084 
00085         void take(Q_PIPE_ID id, Type t);       // take over the pipe id, close the old
00086         void enable();                         // enables usage (read/write) of the pipe
00087         void close();                          // close the pipe
00088         void release();                        // let go of the pipe but don't close
00089         bool setInheritable(bool enabled);     // note: on windows, this operation changes the id
00090 
00091         int bytesAvailable() const;            // bytes available to read
00092         int read(char *data, int maxsize);     // return number read, 0 = EOF, -1 = error
00093         int write(const char *data, int size); // return number taken, ptr must stay valid. -1 on error
00094         int writeResult(int *written) const;   // 0 = success (wrote all), -1 = error (see written)
00095 
00096 Q_SIGNALS:
00097         void notify();                         // can read or can write, depending on type
00098 
00099 private:
00100         Q_DISABLE_COPY(QPipeDevice)
00101 
00102         class Private;
00103         friend class Private;
00104         Private *d;
00105 };
00106 
00116 class QCA_EXPORT QPipeEnd : public QObject
00117 {
00118         Q_OBJECT
00119 public:
00120 
00124         enum Error
00125         {
00126                 ErrorEOF,    
00127                 ErrorBroken  
00128         };
00129 
00133         QPipeEnd(QObject *parent = 0);
00134 
00135         ~QPipeEnd();
00136 
00140         void reset();
00141 
00145         QPipeDevice::Type type() const;
00146 
00153         bool isValid() const;
00154 
00158         Q_PIPE_ID id() const;
00159 
00163         int idAsInt() const;
00164 
00171         void take(Q_PIPE_ID id, QPipeDevice::Type t);
00172 
00173 #ifdef QPIPE_SECURE
00174 
00180         void setSecurityEnabled(bool secure);
00181 #endif
00182 
00189         void enable();
00190 
00196         void close();
00197 
00204         void release();
00205 
00212         bool setInheritable(bool enabled);
00213 
00217         void finalize();
00218 
00222         void finalizeAndRelease();
00223 
00232         int bytesAvailable() const;
00233 
00242         int bytesToWrite() const;
00243 
00254         QByteArray read(int bytes = -1);
00255 
00265         void write(const QByteArray &a);
00266 
00267 #ifdef QPIPE_SECURE
00268 
00278         SecureArray readSecure(int bytes = -1);
00279 
00289         void writeSecure(const SecureArray &a);
00290 #endif
00291 
00298         QByteArray takeBytesToWrite();
00299 
00300 #ifdef QPIPE_SECURE
00301 
00307         SecureArray takeBytesToWriteSecure();
00308 #endif
00309 
00310 Q_SIGNALS:
00317         void readyRead();
00318 
00325         void bytesWritten(int bytes);
00326 
00338         void closed();
00339 
00346         void error(QCA::QPipeEnd::Error e);
00347 
00348 private:
00349         Q_DISABLE_COPY(QPipeEnd)
00350 
00351         class Private;
00352         friend class Private;
00353         Private *d;
00354 };
00355 
00372 class QCA_EXPORT QPipe
00373 {
00374 public:
00382         QPipe(QObject *parent = 0);
00383 
00384         ~QPipe();
00385 
00392         void reset();
00393 
00394 #ifdef QPIPE_SECURE
00395 
00400         bool create(bool secure = false);
00401 #else
00402 
00405         bool create();
00406 #endif
00407 
00411         QPipeEnd & readEnd() { return i; }
00412 
00416         QPipeEnd & writeEnd() { return o; }
00417 
00418 private:
00419         Q_DISABLE_COPY(QPipe)
00420 
00421         QPipeEnd i, o;
00422 };
00423 
00424 }
00425 
00426 #endif

Generated on Tue Aug 28 08:19:59 2007 for Qt Cryptographic Architecture by  doxygen 1.5.2