Qore Programming Language  1.7.0
StreamPipe.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  StreamPipe.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2016 - 2022 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_STREAMPIPE_H
33 #define _QORE_STREAMPIPE_H
34 
35 #include <stdint.h>
36 #include "qore/InputStream.h"
37 #include "qore/OutputStream.h"
38 
45 
46 public:
47  DLLLOCAL StreamPipe(bool syncClose, int64 timeout, int64 bufferSize, ExceptionSink *xsink);
48  DLLLOCAL void reportError(const QoreHashNode* ex);
49  DLLLOCAL void rethrow(ExceptionSink *xsink);
50 
51 private:
52  QoreThreadLock mutex;
53  QoreCondition readCondVar;
54  QoreCondition writeCondVar;
55  std::vector<unsigned char> buffer;
56  bool broken;
57  bool outputClosed;
58  bool closeFinished;
59  int64 size;
60  int64 count;
61  int64 readPtr;
62  int64 timeout;
64 
65  friend class PipeInputStream;
66  friend class PipeOutputStream;
67 };
68 
72 class PipeInputStream : public InputStream {
73 
74 public:
75  DLLLOCAL PipeInputStream(StreamPipe *pipe) : pipe(pipe) {
76  }
77 
78  DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override;
79  DLLLOCAL int64 peek(ExceptionSink *xsink) override;
80  DLLLOCAL void finishClose();
81  DLLLOCAL void reportError(const QoreHashNode* ex) { pipe->reportError(ex); }
82  DLLLOCAL virtual const char *getName() override {
83  return "PipeInputStream";
84  }
85 
86 protected:
87  ~PipeInputStream();
88 
89 private:
91 };
92 
97 
98 public:
99  DLLLOCAL PipeOutputStream(StreamPipe *pipe) : pipe(pipe) {
100  }
101 
102  DLLLOCAL void close(ExceptionSink* xsink) override;
103  DLLLOCAL void write(const void *ptr, int64 count, ExceptionSink *xsink) override;
104  DLLLOCAL void reportError(const QoreHashNode* ex) { pipe->reportError(ex); }
105  DLLLOCAL bool isClosed() override {
106  return false;
107  }
108  DLLLOCAL virtual const char *getName() override {
109  return "PipeInputStream";
110  }
111 
112 protected:
113  ~PipeOutputStream();
114 
115 private:
117 };
118 
119 #endif // _QORE_STREAMPIPE_H
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
Interface for private data of input streams.
Definition: InputStream.h:44
Interface for private data of output streams.
Definition: OutputStream.h:44
Private data for the Qore::PipeInputStream class.
Definition: StreamPipe.h:72
DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override
Reads up to `limit` bytes from the input stream.
DLLLOCAL int64 peek(ExceptionSink *xsink) override
Peeks the next byte from the input stream.
virtual DLLLOCAL const char * getName() override
Returns the name of the class.
Definition: StreamPipe.h:82
Private data for the Qore::PipeOutputStream class.
Definition: StreamPipe.h:96
DLLLOCAL bool isClosed() override
Returns true is the stream has been closed.
Definition: StreamPipe.h:105
virtual DLLLOCAL const char * getName() override
Returns the name of the class.
Definition: StreamPipe.h:108
DLLLOCAL void write(const void *ptr, int64 count, ExceptionSink *xsink) override
Writes bytes to the output stream.
DLLLOCAL void close(ExceptionSink *xsink) override
Flushes any buffered (unwritten) bytes, closes the output stream and releases resources.
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
Private data for the Qore::StreamPipe class.
Definition: StreamPipe.h:44
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260