File: HandleStream.h

package info (click to toggle)
storm-lang 0.7.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,100 kB
  • sloc: ansic: 261,471; cpp: 140,438; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (239 lines) | stat: -rw-r--r-- 5,380 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#pragma once
#include "Stream.h"
#include "PeekStream.h"
#include "OS/Handle.h"
#include "Core/EnginePtr.h"
#include "Core/Timing.h"

namespace storm {
	STORM_PKG(core.io);

	/**
	 * Streams wrapping a OS handle. These are used in various places in the implementation, but are
	 * not intended for direct manipulation by the user.
	 */


	/**
	 * Handle input stream.
	 */
	class HandleIStream : public PeekIStream {
		STORM_CLASS;
	public:
		// Create.
		HandleIStream(os::Handle handle);
		HandleIStream(os::Handle handle, os::Thread attachedTo);

		// Copy.
		HandleIStream(const HandleIStream &o);

		// Destroy.
		virtual ~HandleIStream();

		// More data?
		virtual Bool STORM_FN more();

		// Close this stream.
		virtual void STORM_FN close();

		// Get error.
		virtual sys::ErrorCode STORM_FN error() const {
			return currError;
		}

		// Don't close the handle automatically.
		void skipClose() { noClose = true; }

		// Ignore file positioning (mainly on Windows).
		void skipPos() { noPos = true; }

	protected:
		// Our handle.
		UNKNOWN(PTR_NOGC) os::Handle handle;

		// Is our handle added to a thread?
		UNKNOWN(PTR_NOGC) os::Thread attachedTo;

		// Don't close the handle?
		Bool noClose;

		// This stream has no position, ignore such syscalls.
		Bool noPos;

		// Do read operations.
		virtual PeekReadResult doRead(byte *to, Nat count);

		// Current error.
		sys::ErrorCode currError;
	};


	/**
	 * Random access Handle input stream.
	 */
	class HandleRIStream : public RIStream {
		STORM_CLASS;
	public:
		// Create.
		HandleRIStream(os::Handle handle);
		HandleRIStream(os::Handle handle, os::Thread attachedTo);

		// Copy.
		HandleRIStream(const HandleRIStream &o);

		// Destroy.
		virtual ~HandleRIStream();

		// Deep copy.
		void STORM_FN deepCopy(CloneEnv *e);

		// Are we at the end of the stream?
		virtual Bool STORM_FN more();

		// Read a buffer from the stream. Returns the number of bytes read.
		using IStream::read;
		virtual Buffer STORM_FN read(Buffer to);

		// Peek data.
		using IStream::peek;
		virtual Buffer STORM_FN peek(Buffer to);

		// Get a random access IStream. May return the same stream!
		virtual RIStream *STORM_FN randomAccess();

		// Close this stream.
		virtual void STORM_FN close();

		// Seek relative the start.
		virtual void STORM_FN seek(Word to);

		// Get current position.
		virtual Word STORM_FN tell();

		// Get length.
		virtual Word STORM_FN length();

		// Get error.
		virtual sys::ErrorCode STORM_FN error() const override {
			return currError;
		}

		// Don't close the handle automatically.
		void skipClose() { noClose = true; }

		// Ignore file positioning (mainly on Windows).
		void skipPos() { noPos = true; }

	protected:
		// Our handle.
		UNKNOWN(PTR_NOGC) os::Handle handle;

		// Is our handle added to a thread?
		UNKNOWN(PTR_NOGC) os::Thread attachedTo;

		// Don't close the handle?
		Bool noClose;

		// This stream has no position, ignore such syscalls.
		Bool noPos;

		// Current error.
		sys::ErrorCode currError;
	};


	/**
	 * Input stream that supports timeouts. Used for sockets, for example.
	 */
	class HandleTimeoutIStream : public HandleIStream {
		STORM_CLASS;
	public:
		// Create.
		HandleTimeoutIStream(os::Handle handle);
		HandleTimeoutIStream(os::Handle handle, os::Thread attachedTo);

		// Timeout used for new reads from the stream. The function 'readAll' respects the timeout
		// for individual read operations, meaning that the time taken for 'readAll' could be longer
		// than a single timeout if the remote end of a socket is slow, for example. As such, think
		// of the timeout as "time since the last byte was received". A zero (or negative) timeout
		// means that the timeout is disabled.
		Duration timeout;

	protected:
		// Do read operations.
		virtual PeekReadResult doRead(byte *to, Nat count);
	};

	/**
	 * Handle output stream.
	 */
	class HandleOStream : public OStream {
		STORM_CLASS;
	public:
		// Create.
		HandleOStream(os::Handle h);
		HandleOStream(os::Handle h, os::Thread attachedTo);

		// Copy.
		HandleOStream(const HandleOStream &o);

		// Destroy.
		virtual ~HandleOStream();

		// Write data.
		using OStream::write;
		virtual Nat STORM_FN write(Buffer buf, Nat start);

		// Close.
		virtual void STORM_FN close();

		// Get error.
		virtual sys::ErrorCode STORM_FN error() const {
			return currError;
		}

		// Don't close the handle automatically.
		void skipClose() { noClose = true; }

		// Skip handling of file positioning.
		void skipPos() { noPos = true; }

	protected:
		// Our handle.
		UNKNOWN(PTR_NOGC) os::Handle handle;

		// Is our handle added to a thread?
		UNKNOWN(PTR_NOGC) os::Thread attachedTo;

		// Skip close.
		Bool noClose;

		// Skip file positioning.
		Bool noPos;

		// Current error.
		sys::ErrorCode currError;
	};


	/**
	 * Output stream with an explicit timeout.
	 */
	class HandleTimeoutOStream : public HandleOStream {
		STORM_CLASS;
	public:
		// Create.
		HandleTimeoutOStream(os::Handle handle);
		HandleTimeoutOStream(os::Handle handle, os::Thread attachedTo);

		// Timeout used for new individual write operations to the stream.
		Duration timeout;

		// Custom write.
		using HandleOStream::write;
		virtual Nat STORM_FN write(Buffer buf, Nat start);
	};

	// Helper to close and detach file descriptors properly.
	void close(os::Handle &handle, os::Thread &attachedTo);
}