ObjFW
OFStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This file is part of ObjFW. It may be distributed under the terms of the
7  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
8  * the packaging of this file.
9  *
10  * Alternatively, it may be distributed under the terms of the GNU General
11  * Public License, either version 2 or 3, which can be found in the file
12  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
13  * file.
14  */
15 
16 #ifndef __STDC_LIMIT_MACROS
17 # define __STDC_LIMIT_MACROS
18 #endif
19 #ifndef __STDC_CONSTANT_MACROS
20 # define __STDC_CONSTANT_MACROS
21 #endif
22 
23 #include <stdarg.h>
24 
25 #import "OFObject.h"
26 #import "OFString.h"
27 #import "OFRunLoop.h"
28 #ifdef OF_HAVE_SOCKETS
29 # import "OFKernelEventObserver.h"
30 #endif
31 
32 OF_ASSUME_NONNULL_BEGIN
33 
36 @class OFStream;
37 @class OFData;
38 
39 #if defined(OF_HAVE_SOCKETS) && defined(OF_HAVE_BLOCKS)
49 typedef bool (^of_stream_async_read_block_t)(size_t length,
50  id _Nullable exception);
51 
62 typedef bool (^of_stream_async_read_line_block_t)(OFString *_Nullable line,
63  id _Nullable exception);
64 
78  OFData *_Nonnull data, size_t bytesWritten, id _Nullable exception);
79 
93  OFString *_Nonnull string, size_t bytesWritten, id _Nullable exception);
94 #endif
95 
102 @optional
113 - (bool)stream: (OFStream *)stream
114  didReadIntoBuffer: (void *)buffer
115  length: (size_t)length
116  exception: (nullable id)exception;
117 
128 - (bool)stream: (OFStream *)stream
129  didReadLine: (nullable OFString *)line
130  exception: (nullable id)exception;
131 
144 - (nullable OFData *)stream: (OFStream *)stream
145  didWriteData: (OFData *)data
146  bytesWritten: (size_t)bytesWritten
147  exception: (nullable id)exception;
148 
162 - (nullable OFString *)stream: (OFStream *)stream
163  didWriteString: (OFString *)string
164  encoding: (of_string_encoding_t)encoding
165  bytesWritten: (size_t)bytesWritten
166  exception: (nullable id)exception;
167 @end
168 
190 {
191  bool _canBlock;
192  id _Nullable _delegate;
193 #ifndef OF_SEEKABLE_STREAM_M
194 @private
195 #endif
196  char *_Nullable _readBuffer, *_Nullable _readBufferMemory;
197  char *_Nullable _writeBuffer;
198  size_t _readBufferLength, _writeBufferLength;
199  bool _buffersWrites, _waitingForDelimiter;
200  OF_RESERVE_IVARS(OFStream, 4)
201 }
202 
206 @property (readonly, nonatomic, getter=isAtEndOfStream) bool atEndOfStream;
207 
211 @property (nonatomic, nonatomic) bool buffersWrites;
212 
216 @property (readonly, nonatomic) bool hasDataInReadBuffer;
217 
224 @property (nonatomic) bool canBlock;
225 
232 @property OF_NULLABLE_PROPERTY (assign, nonatomic)
233  id <OFStreamDelegate> delegate;
234 
251 - (size_t)readIntoBuffer: (void *)buffer length: (size_t)length;
252 
268  - (void)readIntoBuffer: (void *)buffer exactLength: (size_t)length;
269 
270 #ifdef OF_HAVE_SOCKETS
291 - (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length;
292 
314 - (void)asyncReadIntoBuffer: (void *)buffer
315  length: (size_t)length
316  runLoopMode: (of_run_loop_mode_t)runLoopMode;
317 
334 - (void)asyncReadIntoBuffer: (void *)buffer exactLength: (size_t)length;
335 
353 - (void)asyncReadIntoBuffer: (void *)buffer
354  exactLength: (size_t)length
355  runLoopMode: (of_run_loop_mode_t)runLoopMode;
356 
357 # ifdef OF_HAVE_BLOCKS
383 - (void)asyncReadIntoBuffer: (void *)buffer
384  length: (size_t)length
385  block: (of_stream_async_read_block_t)block;
386 
413 - (void)asyncReadIntoBuffer: (void *)buffer
414  length: (size_t)length
415  runLoopMode: (of_run_loop_mode_t)runLoopMode
416  block: (of_stream_async_read_block_t)block;
417 
439 - (void)asyncReadIntoBuffer: (void *)buffer
440  exactLength: (size_t)length
441  block: (of_stream_async_read_block_t)block;
442 
465 - (void)asyncReadIntoBuffer: (void *)buffer
466  exactLength: (size_t)length
467  runLoopMode: (of_run_loop_mode_t)runLoopMode
468  block: (of_stream_async_read_block_t)block;
469 # endif
470 #endif
471 
480 - (uint8_t)readInt8;
481 
490 - (uint16_t)readBigEndianInt16;
491 
500 - (uint32_t)readBigEndianInt32;
501 
510 - (uint64_t)readBigEndianInt64;
511 
520 - (float)readBigEndianFloat;
521 
530 - (double)readBigEndianDouble;
531 
544 - (size_t)readBigEndianInt16sIntoBuffer: (uint16_t *)buffer
545  count: (size_t)count;
546 
559 - (size_t)readBigEndianInt32sIntoBuffer: (uint32_t *)buffer
560  count: (size_t)count;
561 
574 - (size_t)readBigEndianInt64sIntoBuffer: (uint64_t *)buffer
575  count: (size_t)count;
576 
589 - (size_t)readBigEndianFloatsIntoBuffer: (float *)buffer count: (size_t)count;
590 
603 - (size_t)readBigEndianDoublesIntoBuffer: (double *)buffer count: (size_t)count;
604 
613 - (uint16_t)readLittleEndianInt16;
614 
623 - (uint32_t)readLittleEndianInt32;
624 
633 - (uint64_t)readLittleEndianInt64;
634 
643 - (float)readLittleEndianFloat;
644 
653 - (double)readLittleEndianDouble;
654 
667 - (size_t)readLittleEndianInt16sIntoBuffer: (uint16_t *)buffer
668  count: (size_t)count;
669 
682 - (size_t)readLittleEndianInt32sIntoBuffer: (uint32_t *)buffer
683  count: (size_t)count;
684 
697 - (size_t)readLittleEndianInt64sIntoBuffer: (uint64_t *)buffer
698  count: (size_t)count;
699 
712 - (size_t)readLittleEndianFloatsIntoBuffer: (float *)buffer
713  count: (size_t)count;
714 
727 - (size_t)readLittleEndianDoublesIntoBuffer: (double *)buffer
728  count: (size_t)count;
729 
740 - (OFData *)readDataWithCount: (size_t)count;
741 
753 - (OFData *)readDataWithItemSize: (size_t)itemSize count: (size_t)count;
754 
761 - (OFData *)readDataUntilEndOfStream;
762 
777 - (OFString *)readStringWithLength: (size_t)length;
778 
794 - (OFString *)readStringWithLength: (size_t)length
795  encoding: (of_string_encoding_t)encoding;
796 
803 - (nullable OFString *)readLine;
804 
813 - (nullable OFString *)readLineWithEncoding: (of_string_encoding_t)encoding;
814 
815 #ifdef OF_HAVE_SOCKETS
823 - (void)asyncReadLine;
824 
834 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding;
835 
846 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
847  runLoopMode: (of_run_loop_mode_t)runLoopMode;
848 
849 # ifdef OF_HAVE_BLOCKS
863 - (void)asyncReadLineWithBlock: (of_stream_async_read_line_block_t)block;
864 
879 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
880  block: (of_stream_async_read_line_block_t)block;
881 
897 - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding
898  runLoopMode: (of_run_loop_mode_t)runLoopMode
899  block: (of_stream_async_read_line_block_t)block;
900 # endif
901 #endif
902 
910 - (nullable OFString *)tryReadLine;
911 
921 - (nullable OFString *)tryReadLineWithEncoding: (of_string_encoding_t)encoding;
922 
931 - (nullable OFString *)readTillDelimiter: (OFString *)delimiter;
932 
942 - (nullable OFString *)readTillDelimiter: (OFString *)delimiter
943  encoding: (of_string_encoding_t)encoding;
944 
954 - (nullable OFString *)tryReadTillDelimiter: (OFString *)delimiter;
955 
966 - (nullable OFString *)tryReadTillDelimiter: (OFString *)delimiter
967  encoding: (of_string_encoding_t)encoding;
968 
972 - (void)flushWriteBuffer;
973 
982 - (size_t)writeBuffer: (const void *)buffer length: (size_t)length;
983 
984 #ifdef OF_HAVE_SOCKETS
993 - (void)asyncWriteData: (OFData *)data;
994 
1004 - (void)asyncWriteData: (OFData *)data
1005  runLoopMode: (of_run_loop_mode_t)runLoopMode;
1006 
1015 - (void)asyncWriteString: (OFString *)string;
1016 
1028 - (void)asyncWriteString: (OFString *)string
1029  encoding: (of_string_encoding_t)encoding;
1030 
1043 - (void)asyncWriteString: (OFString *)string
1044  encoding: (of_string_encoding_t)encoding
1045  runLoopMode: (of_run_loop_mode_t)runLoopMode;
1046 
1047 # ifdef OF_HAVE_BLOCKS
1059 - (void)asyncWriteData: (OFData *)data
1060  block: (of_stream_async_write_data_block_t)block;
1061 
1074 - (void)asyncWriteData: (OFData *)data
1075  runLoopMode: (of_run_loop_mode_t)runLoopMode
1076  block: (of_stream_async_write_data_block_t)block;
1077 
1089 - (void)asyncWriteString: (OFString *)string
1091 
1106 - (void)asyncWriteString: (OFString *)string
1107  encoding: (of_string_encoding_t)encoding
1109 
1125 - (void)asyncWriteString: (OFString *)string
1126  encoding: (of_string_encoding_t)encoding
1127  runLoopMode: (of_run_loop_mode_t)runLoopMode
1129 # endif
1130 #endif
1131 
1137 - (void)writeInt8: (uint8_t)int8;
1138 
1144 - (void)writeBigEndianInt16: (uint16_t)int16;
1145 
1151 - (void)writeBigEndianInt32: (uint32_t)int32;
1152 
1158 - (void)writeBigEndianInt64: (uint64_t)int64;
1159 
1165 - (void)writeBigEndianFloat: (float)float_;
1166 
1172 - (void)writeBigEndianDouble: (double)double_;
1173 
1183 - (size_t)writeBigEndianInt16s: (const uint16_t *)buffer count: (size_t)count;
1184 
1194 - (size_t)writeBigEndianInt32s: (const uint32_t *)buffer count: (size_t)count;
1195 
1205 - (size_t)writeBigEndianInt64s: (const uint64_t *)buffer count: (size_t)count;
1206 
1216 - (size_t)writeBigEndianFloats: (const float *)buffer count: (size_t)count;
1217 
1227 - (size_t)writeBigEndianDoubles: (const double *)buffer count: (size_t)count;
1228 
1234 - (void)writeLittleEndianInt16: (uint16_t)int16;
1235 
1241 - (void)writeLittleEndianInt32: (uint32_t)int32;
1242 
1248 - (void)writeLittleEndianInt64: (uint64_t)int64;
1249 
1255 - (void)writeLittleEndianFloat: (float)float_;
1256 
1262 - (void)writeLittleEndianDouble: (double)double_;
1263 
1273 - (size_t)writeLittleEndianInt16s: (const uint16_t *)buffer
1274  count: (size_t)count;
1275 
1285 - (size_t)writeLittleEndianInt32s: (const uint32_t *)buffer
1286  count: (size_t)count;
1287 
1297 - (size_t)writeLittleEndianInt64s: (const uint64_t *)buffer
1298  count: (size_t)count;
1299 
1309 - (size_t)writeLittleEndianFloats: (const float *)buffer count: (size_t)count;
1310 
1320 - (size_t)writeLittleEndianDoubles: (const double *)buffer count: (size_t)count;
1321 
1328 - (size_t)writeData: (OFData *)data;
1329 
1336 - (size_t)writeString: (OFString *)string;
1337 
1346 - (size_t)writeString: (OFString *)string
1347  encoding: (of_string_encoding_t)encoding;
1348 
1355 - (size_t)writeLine: (OFString *)string;
1356 
1365 - (size_t)writeLine: (OFString *)string
1366  encoding: (of_string_encoding_t)encoding;
1367 
1378 - (size_t)writeFormat: (OFConstantString *)format, ...;
1379 
1391 - (size_t)writeFormat: (OFConstantString *)format arguments: (va_list)arguments;
1392 
1393 #ifdef OF_HAVE_SOCKETS
1397 - (void)cancelAsyncRequests;
1398 #endif
1399 
1421 - (void)unreadFromBuffer: (const void *)buffer length: (size_t)length;
1422 
1428 - (void)close;
1429 
1442 - (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length;
1443 
1456 - (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length;
1457 
1468 - (bool)lowlevelIsAtEndOfStream;
1469 @end
1470 
1471 OF_ASSUME_NONNULL_END
OFData *_Nullable(^ of_stream_async_write_data_block_t)(OFData *_Nonnull data, size_t bytesWritten, id _Nullable exception)
A block which is called when data was written asynchronously to a stream.
Definition: OFStream.h:77
bool(^ of_stream_async_read_block_t)(size_t length, id _Nullable exception)
A block which is called when data was read asynchronously from a stream.
Definition: OFStream.h:49
OFString *_Nullable(^ of_stream_async_write_string_block_t)(OFString *_Nonnull string, size_t bytesWritten, id _Nullable exception)
A block which is called when a string was written asynchronously to a stream.
Definition: OFStream.h:92
bool(^ of_stream_async_read_line_block_t)(OFString *_Nullable line, id _Nullable exception)
A block which is called when a line was read asynchronously from a stream.
Definition: OFStream.h:62
of_string_encoding_t
The encoding of a string.
Definition: OFString.h:68
struct objc_object * id
A pointer to any object.
Definition: ObjFWRT.h:90
A class for storing constant strings using the @"" literal.
Definition: OFConstantString.h:42
A class for storing arbitrary data in an array.
Definition: OFData.h:40
The root class for all other classes inside ObjFW.
Definition: OFObject.h:520
A base class for different types of streams.
Definition: OFStream.h:191
A class for handling strings.
Definition: OFString.h:132
A protocol for the creation of copies.
Definition: OFObject.h:1187
Definition: OFStream.h:101