ObjFW  Check-in [2e6119eb42]

Overview
Comment:Add - setBlocking and - enableKeepAlives for OFTCPSocket.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 2e6119eb42e285782f14c09107374b19c95b92147a7a1c5d40f176acc328ea7d
User & Date: js on 2008-12-19 22:04:35
Other Links: manifest | tags
Context
2008-12-19
22:30
Better variable names for initialization of some exceptions. check-in: 5ad7e24b78 user: js tags: trunk
22:04
Add - setBlocking and - enableKeepAlives for OFTCPSocket. check-in: 2e6119eb42 user: js tags: trunk
17:21
Also set CFLAGS as we also have .c files. check-in: fb2c2513d3 user: js tags: trunk
Changes

Modified src/OFExceptions.h from [c079798d1b] to [494e5c908d].

271
272
273
274
275
276
277










278
279
280
281
282
283
284
/**
 * An OFException indicating a write to the file failed.
 */
@interface OFWriteFailedException: OFReadOrWriteFailedException {}
/**
 * \return An error message for the exception as a C String
 */










- (const char*)cString;
@end

/**
 * An OFException indicating a socket is not connected or bound.
 */
@interface OFNotConnectedException: OFException {}







>
>
>
>
>
>
>
>
>
>







271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
 * An OFException indicating a write to the file failed.
 */
@interface OFWriteFailedException: OFReadOrWriteFailedException {}
/**
 * \return An error message for the exception as a C String
 */
- (const char*)cString;
@end

/**
 * An OFException indicating that setting an option failed.
 */
@interface OFSetOptionFailedException: OFException {}
/***
 * \return An error message for the exception as a C string.
 */
- (const char*)cString;
@end

/**
 * An OFException indicating a socket is not connected or bound.
 */
@interface OFNotConnectedException: OFException {}

Modified src/OFExceptions.m from [db7b077ee8] to [e0a392a175].

291
292
293
294
295
296
297













298
299
300
301
302
303
304
	if (has_items)
		asprintf(&string, "Failed to write %zu items of size %zu in "
		    "object of class %s!", req_items, req_size, [object name]);
	else
		asprintf(&string, "Failed to write %zu bytes in object of "
		    "class %s!", req_size, [object name]);














	return string;
}
@end

@implementation OFNotConnectedException
- (const char*)cString
{







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







291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
	if (has_items)
		asprintf(&string, "Failed to write %zu items of size %zu in "
		    "object of class %s!", req_items, req_size, [object name]);
	else
		asprintf(&string, "Failed to write %zu bytes in object of "
		    "class %s!", req_size, [object name]);

	return string;
}
@end

@implementation OFSetOptionFailedException
- (const char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Setting an option for an object of type type %s "
	    "failed!", [object name]);

	return string;
}
@end

@implementation OFNotConnectedException
- (const char*)cString
{

Modified src/OFTCPSocket.h from [59b06f95cb] to [e73bbae27a].

89
90
91
92
93
94
95










96
97
98
99
100
- listen;

/**
 * Accept an incoming connection.
 */
- (OFTCPSocket*)accept;











/**
 * Closes the socket.
 */
- close;
@end







>
>
>
>
>
>
>
>
>
>





89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
- listen;

/**
 * Accept an incoming connection.
 */
- (OFTCPSocket*)accept;

/**
 * Enables/disables non-blocking I/O.
 */
- setBlocking: (BOOL)enable;

/**
 * Enable or disable keep alives for the connection.
 */
- enableKeepAlives: (BOOL)enable;

/**
 * Closes the socket.
 */
- close;
@end

Modified src/OFTCPSocket.m from [014fa32cf3] to [7656e7376a].

11
12
13
14
15
16
17

18
19
20
21
22
23
24

#import "config.h"

#import <stdio.h>
#import <stdlib.h>
#import <string.h>
#import <unistd.h>


#import "OFTCPSocket.h"
#import "OFExceptions.h"

#ifndef _WIN32	/* FIXME */
@implementation OFTCPSocket
- init







>







11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

#import "config.h"

#import <stdio.h>
#import <stdlib.h>
#import <string.h>
#import <unistd.h>
#import <fcntl.h>

#import "OFTCPSocket.h"
#import "OFExceptions.h"

#ifndef _WIN32	/* FIXME */
@implementation OFTCPSocket
- init
257
258
259
260
261
262
263



























264
265
266
267
268
269
270
{
	if (sock < 0) 
		@throw [OFNotConnectedException newWithObject: self];

	return [self writeNBytes: strlen(str)
		      fromBuffer: (const uint8_t*)str];
}




























- close
{
	if (sock < 0) 
		@throw [OFNotConnectedException newWithObject: self];

	sock = -1;







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







258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{
	if (sock < 0) 
		@throw [OFNotConnectedException newWithObject: self];

	return [self writeNBytes: strlen(str)
		      fromBuffer: (const uint8_t*)str];
}

- setBlocking: (BOOL)enable
{
	int flags;

	if ((flags = fcntl(sock, F_GETFL)) < 0)
		@throw [OFSetOptionFailedException newWithObject: self];

	if (enable)
		flags &= ~O_NONBLOCK;
	else
		flags |= O_NONBLOCK;

	if (fcntl(sock, F_SETFL, flags) < 0)
		@throw [OFSetOptionFailedException newWithObject: self];

	return self;
}

- enableKeepAlives: (BOOL)enable
{
	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, &enable,
	    sizeof(enable)) != 0)
		@throw [OFSetOptionFailedException newWithObject: self];

	return self;
}

- close
{
	if (sock < 0) 
		@throw [OFNotConnectedException newWithObject: self];

	sock = -1;