ObjFW  Check-in [2b6a12065e]

Overview
Comment:Separate error methods for async method delegates
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2b6a12065e9c85e8427cd7084595edca17a6ac36242c87f3305b75a38a27cb89
User & Date: js on 2018-12-08 16:53:30
Other Links: manifest | tags
Context
2018-12-08
17:05
OFTLSSocketDelegate: Inherit OFTCPSocketDelegate check-in: d3a4cda21a user: js tags: trunk
16:53
Separate error methods for async method delegates check-in: 2b6a12065e user: js tags: trunk
16:05
OFTCPSocket: Use a delegate for async operations check-in: 27153bf8cf user: js tags: trunk
Changes

Modified src/OFHTTPClient.m from [66db22090b] to [a601f5af31].

543
544
545
546
547
548
549
550
551









552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
		[self raiseException: e];
		ret = false;
	}

	return ret;
}

-	  (void)stream: (OF_KINDOF(OFStream *))sock
  didFailWithException: (id)exception









{
	if ([exception isKindOfClass: [OFWriteFailedException class]] &&
	    ([exception errNo] == ECONNRESET || [exception errNo] == EPIPE)) {
		/* In case a keep-alive connection timed out */
		[self closeAndReconnect];
		return;
	}

	if ([exception isKindOfClass: [OFInvalidEncodingException class]])
		exception = [OFInvalidServerReplyException exception];

	[self raiseException: exception];
}

- (size_t)stream: (OF_KINDOF(OFStream *))sock
  didWriteBuffer: (const void **)request
	  length: (size_t)length
{







|
|
>
>
>
>
>
>
>
>
>








<
<
<







543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568



569
570
571
572
573
574
575
		[self raiseException: e];
		ret = false;
	}

	return ret;
}

-		(void)stream: (OF_KINDOF(OFStream *))sock
  didFailToReadWithException: (id)exception
{
	if ([exception isKindOfClass: [OFInvalidEncodingException class]])
		exception = [OFInvalidServerReplyException exception];

	[self raiseException: exception];
}

-		 (void)stream: (OF_KINDOF(OFStream *))sock
  didFailToWriteWithException: (id)exception
{
	if ([exception isKindOfClass: [OFWriteFailedException class]] &&
	    ([exception errNo] == ECONNRESET || [exception errNo] == EPIPE)) {
		/* In case a keep-alive connection timed out */
		[self closeAndReconnect];
		return;
	}




	[self raiseException: exception];
}

- (size_t)stream: (OF_KINDOF(OFStream *))sock
  didWriteBuffer: (const void **)request
	  length: (size_t)length
{

Modified src/OFHTTPServer.m from [abf11296e3] to [814464cd03].

796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812

	[(OFTCPSocket *)acceptedSocket setDelegate: connection];
	[acceptedSocket asyncReadLine];

	return true;
}

-	  (void)stream: (OF_KINDOF(OFStream *))stream
  didFailWithException: (id)exception
{
	if ([_delegate respondsToSelector:
	    @selector(server:didReceiveExceptionOnListeningSocket:)])
		if ([_delegate			  server: self
		    didReceiveExceptionOnListeningSocket: exception])
			[stream asyncAccept];
}
@end







|
|








796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812

	[(OFTCPSocket *)acceptedSocket setDelegate: connection];
	[acceptedSocket asyncReadLine];

	return true;
}

-		  (void)stream: (OF_KINDOF(OFStream *))stream
  didFailToAcceptWithException: (id)exception
{
	if ([_delegate respondsToSelector:
	    @selector(server:didReceiveExceptionOnListeningSocket:)])
		if ([_delegate			  server: self
		    didReceiveExceptionOnListeningSocket: exception])
			[stream asyncAccept];
}
@end

Modified src/OFRunLoop.m from [ab8e75f5ed] to [1cb22455c7].

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
				return false;

			return [_delegate stream: object
			       didReadIntoBuffer: _buffer
					  length: length];
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailWithException:)])
				[_delegate	  stream: object
				    didFailWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}







|
|
|







335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
				return false;

			return [_delegate stream: object
			       didReadIntoBuffer: _buffer
					  length: length];
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailToReadWithException:)])
				[_delegate		stream: object
				    didFailToReadWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
					length: _readLength])
				return false;

			_readLength = 0;
			return true;
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailWithException:)])
				[_delegate	  stream: object
				    didFailWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}







|
|
|







399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
					length: _readLength])
				return false;

			_readLength = 0;
			return true;
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailToReadWithException:)])
				[_delegate		stream: object
				    didFailToReadWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
			    @selector(stream:didReadLine:)])
				return false;

			return [_delegate stream: object
				     didReadLine: line];
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailWithException:)])
				[_delegate	  stream: object
				    didFailWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}







|
|
|







450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
			    @selector(stream:didReadLine:)])
				return false;

			return [_delegate stream: object
				     didReadLine: line];
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailToReadWithException:)])
				[_delegate		stream: object
				    didFailToReadWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
			if (_length == 0)
				return false;

			_writtenLength = 0;
			return true;
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailWithException:)])
				[_delegate	  stream: object
				    didFailWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}







|
|
|







517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
			if (_length == 0)
				return false;

			_writtenLength = 0;
			return true;
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailToWriteWithException:)])
				[_delegate		 stream: object
				    didFailToWriteWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
			    @selector(socket:didAcceptSocket:)])
				return false;

			return [_delegate socket: object
				 didAcceptSocket: acceptedSocket];
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailWithException:)])
				[_delegate	  stream: object
				    didFailWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}







|
|
|







587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
			    @selector(socket:didAcceptSocket:)])
				return false;

			return [_delegate socket: object
				 didAcceptSocket: acceptedSocket];
		} else {
			if ([_delegate respondsToSelector:
			    @selector(socket:didFailToAcceptWithException:)])
				[_delegate		  socket: object
				    didFailToAcceptWithException: exception];

			return false;
		}
# ifdef OF_HAVE_BLOCKS
	}
# endif
}

Modified src/OFStream.h from [c566a77887] to [ea19b2f833].

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
 * @protocol OFStreamDelegate OFStream.h ObjFW/OFStream.h
 *
 * A delegate for OFStream.
 */
@protocol OFStreamDelegate <OFObject>
@optional
/*!
 * @brief This method is called when data was read asynchronously from the
 *	  stream.
 *
 * @param stream The stream on which data was read
 * @param buffer A buffer with the data that has been read
 * @param length The length of the data that has been read
 * @return A bool whether the read should be repeated
 */
-      (bool)stream: (OF_KINDOF(OFStream *))stream
  didReadIntoBuffer: (void *)buffer
	     length: (size_t)length;

/*!
 * @brief This method is called when a line was read asynchronously from the
 *	  stream.
 *
 * @param stream The stream on which a line was read
 * @param line The line which has been read or `nil` when the end of stream
 *	       occurred
 * @return A bool whether the read should be repeated
 */
- (bool)stream: (OF_KINDOF(OFStream *))stream
   didReadLine: (nullable OFString *)line;

/*!
 * @brief This method is called when data was written asynchronously to the
 *	  stream.
 *
 * @param stream The stream to which data was written
 * @param buffer A pointer to the buffer which was written to the stream. This
 *		 can be changed to point to a different buffer to be used on the
 *		 next write.
 * @param length The length of the buffer that has been written
 * @return The length to repeat the write with or 0 if it should not repeat.
 *	   The buffer may be changed, so that every time a new buffer and
 *	   length can be specified
 */
- (size_t)stream: (OF_KINDOF(OFStream *))stream
  didWriteBuffer: (const void *_Nonnull *_Nonnull)buffer
	  length: (size_t)length;

/*!
 * @brief This method is called when an exception occurred during an
 *	  asynchronous operation on the stream.










 *
 * @param stream The stream for which an exception occurred
 * @param exception The exception which occurred for the stream
 */
-	  (void)stream: (OF_KINDOF(OFStream *))stream
  didFailWithException: (id)exception;
@end

/*!
 * @class OFStream OFStream.h ObjFW/OFStream.h
 *
 * @brief A base class for different types of streams.
 *







|












|











|

















|
>
>
>
>
>
>
>
>
>
>




|
|







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
 * @protocol OFStreamDelegate OFStream.h ObjFW/OFStream.h
 *
 * A delegate for OFStream.
 */
@protocol OFStreamDelegate <OFObject>
@optional
/*!
 * @brief This method is called when data was read asynchronously from a
 *	  stream.
 *
 * @param stream The stream on which data was read
 * @param buffer A buffer with the data that has been read
 * @param length The length of the data that has been read
 * @return A bool whether the read should be repeated
 */
-      (bool)stream: (OF_KINDOF(OFStream *))stream
  didReadIntoBuffer: (void *)buffer
	     length: (size_t)length;

/*!
 * @brief This method is called when a line was read asynchronously from a
 *	  stream.
 *
 * @param stream The stream on which a line was read
 * @param line The line which has been read or `nil` when the end of stream
 *	       occurred
 * @return A bool whether the read should be repeated
 */
- (bool)stream: (OF_KINDOF(OFStream *))stream
   didReadLine: (nullable OFString *)line;

/*!
 * @brief This method is called when data was written asynchronously to a
 *	  stream.
 *
 * @param stream The stream to which data was written
 * @param buffer A pointer to the buffer which was written to the stream. This
 *		 can be changed to point to a different buffer to be used on the
 *		 next write.
 * @param length The length of the buffer that has been written
 * @return The length to repeat the write with or 0 if it should not repeat.
 *	   The buffer may be changed, so that every time a new buffer and
 *	   length can be specified
 */
- (size_t)stream: (OF_KINDOF(OFStream *))stream
  didWriteBuffer: (const void *_Nonnull *_Nonnull)buffer
	  length: (size_t)length;

/*!
 * @brief This method is called when an exception occurred during an
 *	  asynchronous read on a stream.
 *
 * @param stream The stream for which an exception occurred
 * @param exception The exception which occurred for the stream
 */
-		(void)stream: (OF_KINDOF(OFStream *))stream
  didFailToReadWithException: (id)exception;

/*!
 * @brief This method is called when an exception occurred during an
 *	  asynchronous write on a stream.
 *
 * @param stream The stream for which an exception occurred
 * @param exception The exception which occurred for the stream
 */
-		 (void)stream: (OF_KINDOF(OFStream *))stream
  didFailToWriteWithException: (id)exception;
@end

/*!
 * @class OFStream OFStream.h ObjFW/OFStream.h
 *
 * @brief A base class for different types of streams.
 *

Modified src/OFTCPSocket.h from [4f0f6c4d0d] to [077e4589a5].

74
75
76
77
78
79
80
























81
82
83
84
85
86
87
 *
 * @param socket The socket which accepted the connection
 * @param acceptedSocket The socket which has been accepted
 * @return A bool whether to accept the next incoming connection
 */
-    (bool)socket: (OF_KINDOF(OFTCPSocket *))socket
  didAcceptSocket: (OF_KINDOF(OFTCPSocket *))acceptedSocket;
























@end

/*!
 * @class OFTCPSocket OFTCPSocket.h ObjFW/OFTCPSocket.h
 *
 * @brief A class which provides methods to create and use TCP sockets.
 *







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
 *
 * @param socket The socket which accepted the connection
 * @param acceptedSocket The socket which has been accepted
 * @return A bool whether to accept the next incoming connection
 */
-    (bool)socket: (OF_KINDOF(OFTCPSocket *))socket
  didAcceptSocket: (OF_KINDOF(OFTCPSocket *))acceptedSocket;

/*!
 * @brief This method is called when an exception occurred during an
 *	  asynchronous connect.
 *
 * @param socket The socket for which an exception occurred
 * @param exception The exception which occurred for the stream
 * @param host The host to which the connection failed
 * @param port The port on the host to which the connection failed
 */
-		   (void)socket: (OF_KINDOF(OFTCPSocket *))socket
  didFailToConnectWithException: (id)exception
			   host: (OFString *)host
			   port: (uint16_t)port;

/*!
 * @brief This method is called when an exception occurred during an
 *	  asynchronous accept.
 *
 * @param socket The socket for which an exception occurred
 * @param exception The exception which occurred for the stream
 */
-		  (void)socket: (OF_KINDOF(OFTCPSocket *))socket
  didFailToAcceptWithException: (id)exception;
@end

/*!
 * @class OFTCPSocket OFTCPSocket.h ObjFW/OFTCPSocket.h
 *
 * @brief A class which provides methods to create and use TCP sockets.
 *

Modified src/OFTCPSocket.m from [8cf85678b9] to [855c7eb790].

216
217
218
219
220
221
222
223
224
225
226


227
228
229
230
231
232
233
		if (_exception == nil) {
			if ([_delegate respondsToSelector:
			    @selector(socket:didConnectToHost:port:)])
				[_delegate    socket: _socket
				    didConnectToHost: _host
						port: _port];
		} else {
			if ([_delegate respondsToSelector:
			    @selector(stream:didFailWithException:)])
				[_delegate	  stream: _socket
				    didFailWithException: _exception];


		}
#ifdef OF_HAVE_BLOCKS
	}
#endif
}

- (void)socketDidConnect: (OFTCPSocket *)sock







|
|
|
|
>
>







216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
		if (_exception == nil) {
			if ([_delegate respondsToSelector:
			    @selector(socket:didConnectToHost:port:)])
				[_delegate    socket: _socket
				    didConnectToHost: _host
						port: _port];
		} else {
			if ([_delegate respondsToSelector: @selector(socket:
			    didFailToConnectWithException:host:port:)])
				[_delegate		   socket: _socket
				    didFailToConnectWithException: _exception
							     host: _host
							     port: _port];
		}
#ifdef OF_HAVE_BLOCKS
	}
#endif
}

- (void)socketDidConnect: (OFTCPSocket *)sock
550
551
552
553
554
555
556
557
558







559
560
561
562
563
564
565
		return 0;
	default:
		assert(0);
		return 0;
	}
}

-	  (void)stream: (OF_KINDOF(OFStream *))sock
  didFailWithException: (id)exception







{
	_exception = [exception retain];
	[self didConnect];
}
@end

@implementation OFTCPSocket_ConnectDelegate







|
|
>
>
>
>
>
>
>







552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
		return 0;
	default:
		assert(0);
		return 0;
	}
}

-		(void)stream: (OF_KINDOF(OFStream *))sock
  didFailToReadWithException: (id)exception
{
	_exception = [exception retain];
	[self didConnect];
}

-		 (void)stream: (OF_KINDOF(OFStream *))sock
  didFailToWriteWithException: (id)exception
{
	_exception = [exception retain];
	[self didConnect];
}
@end

@implementation OFTCPSocket_ConnectDelegate
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
-     (void)socket: (OF_KINDOF(OFTCPSocket *))sock
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
{
	_done = true;
}

-	  (void)stream: (OF_KINDOF(OFStream *))stream
  didFailWithException: (id)exception
{
	_done = true;
	_exception = [exception retain];
}
@end

@implementation OFTCPSocket







|
|







582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
-     (void)socket: (OF_KINDOF(OFTCPSocket *))sock
  didConnectToHost: (OFString *)host
	      port: (uint16_t)port
{
	_done = true;
}

-		   (void)socket: (OF_KINDOF(OFTCPSocket *))sock
  didFailToConnectWithException: (id)exception
{
	_done = true;
	_exception = [exception retain];
}
@end

@implementation OFTCPSocket