Differences From Artifact [66ad6e1874]:
- File src/OFStream.m — part of check-in [cf93ca4b21] at 2018-09-24 22:14:08 on branch trunk — OFStream: Allow specifying the run loop mode (user: js, size: 39992) [annotate] [blame] [check-ins using]
To Artifact [a08f06103a]:
- File
src/OFStream.m
— part of check-in
[d16ad96cbd]
at
2018-12-07 01:33:47
on branch trunk
— OFStream: Use a delegate for async operations
The target / selector approach had several drawbacks:
* It was inconvenient to use, as for every read or write, a target,
selector and context would need to be specified.
* It lacked any kind of type-safety and would not even warn about using
a callback method with the wrong number of parameters.
* It encouraged using a different callback method for each read or
write call, which results in code that is hard to follow and also
slower (as it needs to recreate the async operation with a new
callback every time). (user: js, size: 38772) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
59 60 61 62 63 64 65 | #import "OFWriteFailedException.h" #import "of_asprintf.h" #define MIN_READ_SIZE 512 @implementation OFStream | | | 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #import "OFWriteFailedException.h" #import "of_asprintf.h" #define MIN_READ_SIZE 512 @implementation OFStream @synthesize of_waitingForDelimiter = _waitingForDelimiter, delegate = _delegate; #if defined(SIGPIPE) && defined(SIG_IGN) + (void)initialize { if (self == [OFStream class]) signal(SIGPIPE, SIG_IGN); } |
︙ | ︙ | |||
193 194 195 196 197 198 199 | length: length - readLength]; } } #ifdef OF_HAVE_SOCKETS - (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length | < < < | < < < < < < < | < < < < | < < < < < < < | < | 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 240 241 242 243 244 245 246 | length: length - readLength]; } } #ifdef OF_HAVE_SOCKETS - (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length { [self asyncReadIntoBuffer: buffer length: length runLoopMode: of_run_loop_mode_default]; } - (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length runLoopMode: (of_run_loop_mode_t)runLoopMode { OFStream <OFReadyForReadingObserving> *stream = (OFStream <OFReadyForReadingObserving> *)self; [OFRunLoop of_addAsyncReadForStream: stream buffer: buffer length: length mode: runLoopMode delegate: _delegate]; } - (void)asyncReadIntoBuffer: (void *)buffer exactLength: (size_t)length { [self asyncReadIntoBuffer: buffer exactLength: length runLoopMode: of_run_loop_mode_default]; } - (void)asyncReadIntoBuffer: (void *)buffer exactLength: (size_t)length runLoopMode: (of_run_loop_mode_t)runLoopMode { OFStream <OFReadyForReadingObserving> *stream = (OFStream <OFReadyForReadingObserving> *)self; [OFRunLoop of_addAsyncReadForStream: stream buffer: buffer exactLength: length mode: runLoopMode delegate: _delegate]; } # ifdef OF_HAVE_BLOCKS - (void)asyncReadIntoBuffer: (void *)buffer length: (size_t)length block: (of_stream_async_read_block_t)block { |
︙ | ︙ | |||
884 885 886 887 888 889 890 | if ([self isAtEndOfStream]) return nil; return line; } #ifdef OF_HAVE_SOCKETS | | < < | < < < < < < | < < < < < < < | < | 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 | if ([self isAtEndOfStream]) return nil; return line; } #ifdef OF_HAVE_SOCKETS - (void)asyncReadLine { [self asyncReadLineWithEncoding: OF_STRING_ENCODING_UTF_8 runLoopMode: of_run_loop_mode_default]; } - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding { [self asyncReadLineWithEncoding: encoding runLoopMode: of_run_loop_mode_default]; } - (void)asyncReadLineWithEncoding: (of_string_encoding_t)encoding runLoopMode: (of_run_loop_mode_t)runLoopMode { OFStream <OFReadyForReadingObserving> *stream = (OFStream <OFReadyForReadingObserving> *)self; [OFRunLoop of_addAsyncReadLineForStream: stream encoding: encoding mode: runLoopMode delegate: _delegate]; } # ifdef OF_HAVE_BLOCKS - (void)asyncReadLineWithBlock: (of_stream_async_read_line_block_t)block { [self asyncReadLineWithEncoding: OF_STRING_ENCODING_UTF_8 runLoopMode: of_run_loop_mode_default |
︙ | ︙ | |||
1202 1203 1204 1205 1206 1207 1208 | return length; } } #ifdef OF_HAVE_SOCKETS - (void)asyncWriteBuffer: (const void *)buffer length: (size_t)length | < < < | < < < < < < < | < | 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 | return length; } } #ifdef OF_HAVE_SOCKETS - (void)asyncWriteBuffer: (const void *)buffer length: (size_t)length { [self asyncWriteBuffer: buffer length: length runLoopMode: of_run_loop_mode_default]; } - (void)asyncWriteBuffer: (const void *)buffer length: (size_t)length runLoopMode: (of_run_loop_mode_t)runLoopMode { OFStream <OFReadyForWritingObserving> *stream = (OFStream <OFReadyForWritingObserving> *)self; [OFRunLoop of_addAsyncWriteForStream: stream buffer: buffer length: length mode: runLoopMode delegate: _delegate]; } # ifdef OF_HAVE_BLOCKS - (void)asyncWriteBuffer: (const void *)buffer length: (size_t)length block: (of_stream_async_write_block_t)block { |
︙ | ︙ |