ObjFW  Check-in [1de551cb5f]

Overview
Comment:Add support for reusing OFStreams after close

Right now, this is only useful for OFTCPSocket, as this is the only
class so far not establishing the stream in the init method. However,
this adds the general infrastructure to allow reuse to all subclasses of
OFStream.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1de551cb5f25425492682c462e01d8ad17c6caa4f59a3aa9b7c650cbb28b18d5
User & Date: js on 2016-06-07 22:56:28
Other Links: manifest | tags
Context
2016-06-08
23:05
Don't use ARM EHABI if __ARM_DWARF_EH__ is defined check-in: d55f62409f user: js tags: trunk
2016-06-07
22:56
Add support for reusing OFStreams after close check-in: 1de551cb5f user: js tags: trunk
2016-06-06
20:57
macros.h: Define OF_MIPS64_ASM on MIPS64 check-in: a2a47a8757 user: js tags: trunk
Changes

Modified src/OFFile.m from [a34749abe1] to [70abf75d98].

291
292
293
294
295
296
297


298
299
300
301
302
303
304
305
306
307

- (void)close
{
	if (_fd != -1)
		close(_fd);

	_fd = -1;


}

- (void)dealloc
{
	if (_fd != -1)
		close(_fd);

	[super dealloc];
}
@end







>
>










291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309

- (void)close
{
	if (_fd != -1)
		close(_fd);

	_fd = -1;

	[super close];
}

- (void)dealloc
{
	if (_fd != -1)
		close(_fd);

	[super dealloc];
}
@end

Modified src/OFHTTPClient.m from [cf407bada1] to [2ea850eecb].

251
252
253
254
255
256
257


258
259
260
261
262
263
264
	return ([super hasDataInReadBuffer] || [_socket hasDataInReadBuffer]);
}

- (void)close
{
	[_socket release];
	_socket = nil;


}
@end

@implementation OFHTTPClient
@synthesize delegate = _delegate;
@synthesize insecureRedirectsAllowed = _insecureRedirectsAllowed;








>
>







251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
	return ([super hasDataInReadBuffer] || [_socket hasDataInReadBuffer]);
}

- (void)close
{
	[_socket release];
	_socket = nil;

	[super close];
}
@end

@implementation OFHTTPClient
@synthesize delegate = _delegate;
@synthesize insecureRedirectsAllowed = _insecureRedirectsAllowed;

Modified src/OFHTTPServer.m from [b9d140020a] to [28126036a1].

301
302
303
304
305
306
307


308
309
310
311
312
313
314
			    didReceiveExceptionForResponse: self
						   request: _request
						 exception: e];
	}

	[_socket release];
	_socket = nil;


}

- (int)fileDescriptorForWriting
{
	if (_socket == nil)
		return -1;








>
>







301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
			    didReceiveExceptionForResponse: self
						   request: _request
						 exception: e];
	}

	[_socket release];
	_socket = nil;

	[super close];
}

- (int)fileDescriptorForWriting
{
	if (_socket == nil)
		return -1;

Modified src/OFProcess.m from [e8f32c52e1] to [273ab6b21b].

590
591
592
593
594
595
596
597


598
		CloseHandle(_process);
	}

	_process = INVALID_HANDLE_VALUE;
	_readPipe[0] = NULL;
	_writePipe[1] = NULL;
#endif
}


@end







|
>
>

590
591
592
593
594
595
596
597
598
599
600
		CloseHandle(_process);
	}

	_process = INVALID_HANDLE_VALUE;
	_readPipe[0] = NULL;
	_writePipe[1] = NULL;
#endif

	[super close];
}
@end

Modified src/OFStdIOStream.m from [53b11ea420] to [3491f0262a].

173
174
175
176
177
178
179


180
181
182
183
184
185
186

- (void)close
{
	if (_fd != -1)
		close(_fd);

	_fd = -1;


}

- autorelease
{
	return self;
}








>
>







173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

- (void)close
{
	if (_fd != -1)
		close(_fd);

	_fd = -1;

	[super close];
}

- autorelease
{
	return self;
}

Modified src/OFStream.h from [38cc11b62a] to [c3708c076a].

1117
1118
1119
1120
1121
1122
1123


1124
1125
1126
1127
1128
1129
1130
 * @param length The length of the buffer to unread
 */
- (void)unreadFromBuffer: (const void*)buffer
		  length: (size_t)length;

/*!
 * @brief Closes the stream.


 */
- (void)close;

/*!
 * @brief Performs a lowlevel read.
 *
 * @warning Do not call this directly!







>
>







1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
 * @param length The length of the buffer to unread
 */
- (void)unreadFromBuffer: (const void*)buffer
		  length: (size_t)length;

/*!
 * @brief Closes the stream.
 *
 * @note If you override this, make sure to call `[super close]`!
 */
- (void)close;

/*!
 * @brief Performs a lowlevel read.
 *
 * @warning Do not call this directly!

Modified src/OFStream.m from [b4ba3031a1] to [48141a71fe].

1534
1535
1536
1537
1538
1539
1540



1541




1542



1543
	[self freeMemory: _readBufferMemory];
	_readBuffer = _readBufferMemory = readBuffer;
	_readBufferLength += length;
}

- (void)close
{



	OF_UNRECOGNIZED_SELECTOR




}



@end







>
>
>
|
>
>
>
>
|
>
>
>

1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
	[self freeMemory: _readBufferMemory];
	_readBuffer = _readBufferMemory = readBuffer;
	_readBufferLength += length;
}

- (void)close
{
	[self freeMemory: _readBufferMemory];
	_readBuffer = _readBufferMemory = NULL;
	_readBufferLength = 0;

	[self freeMemory: _writeBuffer];
	_writeBuffer = NULL;
	_writeBufferLength = 0;
	_writeBuffered = false;

	_waitingForDelimiter = false;
	_blocking = false;
}
@end

Modified src/OFStreamSocket.m from [3021941e31] to [77a66a4df1].

169
170
171
172
173
174
175


176
177
178
179
180
181
182
183
184
185
	if (_socket == INVALID_SOCKET)
		@throw [OFNotOpenException exceptionWithObject: self];

	close(_socket);
	_socket = INVALID_SOCKET;

	_atEndOfStream = false;


}

- (void)dealloc
{
	if (_socket != INVALID_SOCKET)
		[self close];

	[super dealloc];
}
@end







>
>










169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
	if (_socket == INVALID_SOCKET)
		@throw [OFNotOpenException exceptionWithObject: self];

	close(_socket);
	_socket = INVALID_SOCKET;

	_atEndOfStream = false;

	[super close];
}

- (void)dealloc
{
	if (_socket != INVALID_SOCKET)
		[self close];

	[super dealloc];
}
@end

Modified src/OFTCPSocket.m from [15a3d3213a] to [2735f938b2].

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
	uint16_t destinationPort = port;
	of_resolver_result_t **results, **iter;
	int errNo = 0;

	if (_socket != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException exceptionWithSocket: self];

	_listening = false;

	/* Make sure to clear the read buffer in case the socket is reused */
	[self freeMemory: _readBuffer];
	_readBuffer = NULL;
	_readBufferLength = 0;

	if (_SOCKS5Host != nil) {
		/* Connect to the SOCKS5 proxy instead */
		host = _SOCKS5Host;
		port = _SOCKS5Port;
	}

	results = of_resolve_host(host, port, SOCK_STREAM);







<
<
<
<
<
<
<







252
253
254
255
256
257
258







259
260
261
262
263
264
265
	uint16_t destinationPort = port;
	of_resolver_result_t **results, **iter;
	int errNo = 0;

	if (_socket != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException exceptionWithSocket: self];








	if (_SOCKS5Host != nil) {
		/* Connect to the SOCKS5 proxy instead */
		host = _SOCKS5Host;
		port = _SOCKS5Port;
	}

	results = of_resolve_host(host, port, SOCK_STREAM);
662
663
664
665
666
667
668















669
		@throw [OFGetOptionFailedException
		    exceptionWithStream: self
				  errNo: of_socket_errno()];

	return v;
}
#endif















@end







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

655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
		@throw [OFGetOptionFailedException
		    exceptionWithStream: self
				  errNo: of_socket_errno()];

	return v;
}
#endif

- (void)close
{
	_listening = false;

	[self freeMemory: _address];
	_address = NULL;
	_addressLength = 0;

#ifdef OF_WII
	_port = 0;
#endif

	[super close];
}
@end

Modified src/OFTarArchiveEntry.m from [eff1f9c576] to [9d5bd4c896].

150
151
152
153
154
155
156


157
158
159
160
161
162
163
{
	return ([super hasDataInReadBuffer] || [_stream hasDataInReadBuffer]);
}

- (void)close
{
	_atEndOfStream = true;


}

- (void)OF_skip
{
	char buffer[512];

	while (_toRead >= 512) {







>
>







150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
{
	return ([super hasDataInReadBuffer] || [_stream hasDataInReadBuffer]);
}

- (void)close
{
	_atEndOfStream = true;

	[super close];
}

- (void)OF_skip
{
	char buffer[512];

	while (_toRead >= 512) {

Modified src/OFZIPArchive.m from [f2af10f3e4] to [c12f6e9a97].

563
564
565
566
567
568
569
570


571

	return ret;
}

- (void)close
{
	_closed = true;
}


@end







|
>
>

563
564
565
566
567
568
569
570
571
572
573

	return ret;
}

- (void)close
{
	_closed = true;

	[super close];
}
@end