ObjFW
Loading...
Searching...
No Matches
OFStream.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2024 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
32OF_ASSUME_NONNULL_BEGIN
33
36@class OFStream;
37@class OFData;
38
39#if defined(OF_HAVE_SOCKETS) && defined(OF_HAVE_BLOCKS)
49typedef bool (^OFStreamAsyncReadBlock)(size_t length, id _Nullable exception);
50
61typedef bool (^OFStreamAsyncReadLineBlock)(OFString *_Nullable line,
62 id _Nullable exception);
63
75typedef OFData *_Nullable (^OFStreamAsyncWriteDataBlock)(size_t bytesWritten,
76 id _Nullable exception);
77
90 size_t bytesWritten, id _Nullable exception);
91#endif
92
98@protocol OFStreamDelegate <OFObject>
99@optional
110- (bool)stream: (OFStream *)stream
111 didReadIntoBuffer: (void *)buffer
112 length: (size_t)length
113 exception: (nullable id)exception;
114
125- (bool)stream: (OFStream *)stream
126 didReadLine: (nullable OFString *)line
127 exception: (nullable id)exception;
128
141- (nullable OFData *)stream: (OFStream *)stream
142 didWriteData: (OFData *)data
143 bytesWritten: (size_t)bytesWritten
144 exception: (nullable id)exception;
145
159- (nullable OFString *)stream: (OFStream *)stream
160 didWriteString: (OFString *)string
161 encoding: (OFStringEncoding)encoding
162 bytesWritten: (size_t)bytesWritten
163 exception: (nullable id)exception;
164@end
165
187{
188 bool _canBlock;
189 id _Nullable _delegate;
190#ifndef OF_SEEKABLE_STREAM_M
191@private
192#endif
193 char *_Nullable _readBuffer, *_Nullable _readBufferMemory;
194 char *_Nullable _writeBuffer;
195 size_t _readBufferLength, _writeBufferLength;
196 bool _buffersWrites, _waitingForDelimiter;
197 OF_RESERVE_IVARS(OFStream, 4)
198}
199
203@property (readonly, nonatomic, getter=isAtEndOfStream) bool atEndOfStream;
208@property (nonatomic) bool buffersWrites;
213@property (readonly, nonatomic) bool hasDataInReadBuffer;
224@property (nonatomic) bool canBlock;
232@property OF_NULLABLE_PROPERTY (assign, nonatomic)
233 id <OFStreamDelegate> delegate;
253- (size_t)readIntoBuffer: (void *)buffer length: (size_t)length;
254
274 - (void)readIntoBuffer: (void *)buffer exactLength: (size_t)length;
275
276#ifdef OF_HAVE_SOCKETS
297- (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length;
298
320- (void)asyncReadIntoBuffer: (void *)buffer
321 length: (size_t)length
322 runLoopMode: (OFRunLoopMode)runLoopMode;
323
340- (void)asyncReadIntoBuffer: (void *)buffer exactLength: (size_t)length;
341
359- (void)asyncReadIntoBuffer: (void *)buffer
360 exactLength: (size_t)length
361 runLoopMode: (OFRunLoopMode)runLoopMode;
362
363# ifdef OF_HAVE_BLOCKS
389- (void)asyncReadIntoBuffer: (void *)buffer
390 length: (size_t)length
391 block: (OFStreamAsyncReadBlock)block;
392
419- (void)asyncReadIntoBuffer: (void *)buffer
420 length: (size_t)length
421 runLoopMode: (OFRunLoopMode)runLoopMode
422 block: (OFStreamAsyncReadBlock)block;
423
445- (void)asyncReadIntoBuffer: (void *)buffer
446 exactLength: (size_t)length
447 block: (OFStreamAsyncReadBlock)block;
448
471- (void)asyncReadIntoBuffer: (void *)buffer
472 exactLength: (size_t)length
473 runLoopMode: (OFRunLoopMode)runLoopMode
474 block: (OFStreamAsyncReadBlock)block;
475# endif
476#endif
477
490- (uint8_t)readInt8;
491
504- (uint16_t)readBigEndianInt16;
505
518- (uint32_t)readBigEndianInt32;
519
532- (uint64_t)readBigEndianInt64;
533
546- (float)readBigEndianFloat;
547
560- (double)readBigEndianDouble;
561
574- (uint16_t)readLittleEndianInt16;
575
588- (uint32_t)readLittleEndianInt32;
589
602- (uint64_t)readLittleEndianInt64;
603
616- (float)readLittleEndianFloat;
617
630- (double)readLittleEndianDouble;
631
646- (OFData *)readDataWithCount: (size_t)count;
647
663- (OFData *)readDataWithItemSize: (size_t)itemSize count: (size_t)count;
664
673- (OFData *)readDataUntilEndOfStream;
674
695- (OFString *)readStringWithLength: (size_t)length;
696
718- (OFString *)readStringWithLength: (size_t)length
719 encoding: (OFStringEncoding)encoding;
720
731- (nullable OFString *)readLine;
732
745- (nullable OFString *)readLineWithEncoding: (OFStringEncoding)encoding;
746
747#ifdef OF_HAVE_SOCKETS
755- (void)asyncReadLine;
756
766- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding;
767
778- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding
779 runLoopMode: (OFRunLoopMode)runLoopMode;
780
781# ifdef OF_HAVE_BLOCKS
795- (void)asyncReadLineWithBlock: (OFStreamAsyncReadLineBlock)block;
796
811- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding
812 block: (OFStreamAsyncReadLineBlock)block;
813
829- (void)asyncReadLineWithEncoding: (OFStringEncoding)encoding
830 runLoopMode: (OFRunLoopMode)runLoopMode
831 block: (OFStreamAsyncReadLineBlock)block;
832# endif
833#endif
834
846- (nullable OFString *)tryReadLine;
847
861- (nullable OFString *)tryReadLineWithEncoding: (OFStringEncoding)encoding;
862
875- (nullable OFString *)readUntilDelimiter: (OFString *)delimiter;
876
890- (nullable OFString *)readUntilDelimiter: (OFString *)delimiter
891 encoding: (OFStringEncoding)encoding;
892
906- (nullable OFString *)tryReadUntilDelimiter: (OFString *)delimiter;
907
922- (nullable OFString *)tryReadUntilDelimiter: (OFString *)delimiter
923 encoding: (OFStringEncoding)encoding;
924
932- (bool)flushWriteBuffer;
933
949- (void)writeBuffer: (const void *)buffer length: (size_t)length;
950
951#ifdef OF_HAVE_SOCKETS
960- (void)asyncWriteData: (OFData *)data;
961
971- (void)asyncWriteData: (OFData *)data
972 runLoopMode: (OFRunLoopMode)runLoopMode;
973
982- (void)asyncWriteString: (OFString *)string;
983
995- (void)asyncWriteString: (OFString *)string
996 encoding: (OFStringEncoding)encoding;
997
1010- (void)asyncWriteString: (OFString *)string
1011 encoding: (OFStringEncoding)encoding
1012 runLoopMode: (OFRunLoopMode)runLoopMode;
1013
1014# ifdef OF_HAVE_BLOCKS
1026- (void)asyncWriteData: (OFData *)data
1027 block: (OFStreamAsyncWriteDataBlock)block;
1028
1041- (void)asyncWriteData: (OFData *)data
1042 runLoopMode: (OFRunLoopMode)runLoopMode
1043 block: (OFStreamAsyncWriteDataBlock)block;
1044
1056- (void)asyncWriteString: (OFString *)string
1057 block: (OFStreamAsyncWriteStringBlock)block;
1058
1073- (void)asyncWriteString: (OFString *)string
1074 encoding: (OFStringEncoding)encoding
1075 block: (OFStreamAsyncWriteStringBlock)block;
1076
1092- (void)asyncWriteString: (OFString *)string
1093 encoding: (OFStringEncoding)encoding
1094 runLoopMode: (OFRunLoopMode)runLoopMode
1095 block: (OFStreamAsyncWriteStringBlock)block;
1096# endif
1097#endif
1098
1108- (void)writeInt8: (uint8_t)int8;
1109
1119- (void)writeBigEndianInt16: (uint16_t)int16;
1120
1130- (void)writeBigEndianInt32: (uint32_t)int32;
1131
1141- (void)writeBigEndianInt64: (uint64_t)int64;
1142
1152- (void)writeBigEndianFloat: (float)float_;
1153
1163- (void)writeBigEndianDouble: (double)double_;
1164
1174- (void)writeLittleEndianInt16: (uint16_t)int16;
1175
1185- (void)writeLittleEndianInt32: (uint32_t)int32;
1186
1196- (void)writeLittleEndianInt64: (uint64_t)int64;
1197
1207- (void)writeLittleEndianFloat: (float)float_;
1208
1218- (void)writeLittleEndianDouble: (double)double_;
1219
1229- (void)writeData: (OFData *)data;
1230
1240- (void)writeString: (OFString *)string;
1241
1253- (void)writeString: (OFString *)string encoding: (OFStringEncoding)encoding;
1254
1264- (void)writeLine: (OFString *)string;
1265
1277- (void)writeLine: (OFString *)string encoding: (OFStringEncoding)encoding;
1278
1293- (void)writeFormat: (OFConstantString *)format, ...;
1294
1310- (void)writeFormat: (OFConstantString *)format arguments: (va_list)arguments;
1311
1312#ifdef OF_HAVE_SOCKETS
1316- (void)cancelAsyncRequests;
1317#endif
1318
1340- (void)unreadFromBuffer: (const void *)buffer length: (size_t)length;
1341
1349- (void)close;
1350
1365- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length;
1366
1381- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length;
1382
1393- (bool)lowlevelIsAtEndOfStream;
1394@end
1395
1396OF_ASSUME_NONNULL_END
OFData *(^ OFStreamAsyncWriteDataBlock)(size_t bytesWritten, id exception)
A block which is called when data was written asynchronously to a stream.
Definition OFStream.h:75
bool(^ OFStreamAsyncReadBlock)(size_t length, id exception)
A block which is called when data was read asynchronously from a stream.
Definition OFStream.h:49
bool(^ OFStreamAsyncReadLineBlock)(OFString *line, id exception)
A block which is called when a line was read asynchronously from a stream.
Definition OFStream.h:61
OFString *(^ OFStreamAsyncWriteStringBlock)(size_t bytesWritten, id exception)
A block which is called when a string was written asynchronously to a stream.
Definition OFStream.h:89
OFStringEncoding
The encoding of a string.
Definition OFString.h:61
A class for storing constant strings using the @"" literal.
Definition OFConstantString.h:38
A class for storing arbitrary data in an array.
Definition OFData.h:42
The root class for all other classes inside ObjFW.
Definition OFObject.h:688
A base class for different types of streams.
Definition OFStream.h:188
A class for handling strings.
Definition OFString.h:135
A protocol for the creation of copies.
Definition OFObject.h:1346