ObjFW  Check-in [a01c94d9dd]

Overview
Comment:Enhance exceptions; fix some FIXMEs in OFTCPSocket.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: a01c94d9dd4b73ce572c1b9fa9b9b05cd828d94dd33b4e7064eea58b72369368
User & Date: js on 2008-12-10 18:00:59
Other Links: manifest | tags
Context
2008-12-10
20:53
Fix FIXMEs in OFString; new exception. check-in: 4f7378978c user: js tags: trunk
18:00
Enhance exceptions; fix some FIXMEs in OFTCPSocket. check-in: a01c94d9dd user: js tags: trunk
17:54
When closing an OFTCPSocket, set it's internal socket to -1. check-in: e9f870cfc0 user: js tags: trunk
Changes

Modified src/OFExceptions.h from [c0dcb82286] to [077a4ec303].

36
37
38
39
40
41
42





43
44
45
46
47
48
49
 * \param obj The object which caused the exception
 * \return An initialized OFException
 */
- initWithObject: (id)obj;

- free;






/**
 * \return An error message for the exception as a C String
 */
- (char*)cString;
@end

/**







>
>
>
>
>







36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 * \param obj The object which caused the exception
 * \return An initialized OFException
 */
- initWithObject: (id)obj;

- free;

/**
 * \return The object that caused the exception
 */
- (id)object;

/**
 * \return An error message for the exception as a C String
 */
- (char*)cString;
@end

/**
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
@end

/**
 * An OFException indicating the given value is out of range. 
 */
@interface OFOutOfRangeException: OFException {}
/**
 * Creates a new out of range exception.
 *
 * \param obj The object which caused the exception
 * \return A new out of range exception
 */
+ newWithObject: (id)obj;

/**
 * Initializes an already allocated out of range exception.
 *
 * \param obj The object which caused the exception
 * \return An initialized out of range exception
 */
- initWithObject: (id)obj;
@end

/**
 * An OFException indicating the file couldn't be opened.
 */
@interface OFOpenFileFailedException: OFException
{







<
<
|
<

<
|
<
<
<
<
<
<
<







130
131
132
133
134
135
136


137

138

139







140
141
142
143
144
145
146
@end

/**
 * An OFException indicating the given value is out of range. 
 */
@interface OFOutOfRangeException: OFException {}
/**


 * \return An error message for the exception as a C String

 */

- (char*)cString;







@end

/**
 * An OFException indicating the file couldn't be opened.
 */
@interface OFOpenFileFailedException: OFException
{
253
254
255
256
257
258
259
260
261





















/**
 * An OFException indicating a write to the file failed.
 */
@interface OFWriteFailedException: OFReadOrWriteFailedException {}
/**
 * \return An error message for the exception as a C String
 */
- (char*)cString;
@end






























>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
 * An OFException indicating a write to the file failed.
 */
@interface OFWriteFailedException: OFReadOrWriteFailedException {}
/**
 * \return An error message for the exception as a C String
 */
- (char*)cString;
@end

/**
 * An OFException indicating a socket is not connected or bound.
 */
@interface OFNotConnectedException: OFException {}
/**
 * \return An error message for the exception as a C string.
 */
- (char*)cString;
@end

/**
 * An OFException indicating an attempt to connect or bind an already connected
 * or bound socket 
 */
@interface OFAlreadyConnectedException: OFException {}
/**
 * \return An error message for the exception as a C string.
 */
- (char*)cString;
@end

Modified src/OFExceptions.m from [4ac6f2f10b] to [47e102896c].

47
48
49
50
51
52
53





54
55
56
57
58
59
60
- free
{
	if (string != NULL)
		free(string);

	return [super free];
}






- (char*)cString
{
	return string;
}
@end








>
>
>
>
>







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
- free
{
	if (string != NULL)
		free(string);

	return [super free];
}

- (id)object
{
	return object;
}

- (char*)cString
{
	return string;
}
@end

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Could not allocate %zu bytes for object of "
	    "class %s!\n", req_size, object != nil ? [object name] : "(null)");

	return string;
}

- (size_t)requestedSize
{
	return req_size;







|
|







81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Could not allocate %zu bytes for object of class "
	    "%s!", req_size, object != nil ? [object name] : "(null)");

	return string;
}

- (size_t)requestedSize
{
	return req_size;
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
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Memory at %p was not allocated as part of "
	    "object of class\n"
	    "ERROR: %s!\n"
	    "ERROR: -> Not changing memory allocation!\n"
	    "ERROR: (Hint: It is also possible that you tried to free the same "
	    "memory twice!)\n", pointer, [object name]);

	return string;
}

- (void*)pointer
{
	return pointer;
}
@end

@implementation OFOutOfRangeException
+ newWithObject: (id)obj
{
	return [[self alloc] initWithObject: obj];
}

- initWithObject: (id)obj
{
	return (self = [super initWithObject: obj]);
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Value out of range in object of class %s!\n",
	    object != nil ? [object name] : "(null)");

	return string;
}
@end

@implementation OFOpenFileFailedException







|
<
<
|
|
|











<
<
<
<
<
<
<
<
<
<





|







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
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Memory at %p was not allocated as part of object "


	    "of class %s, thus the memory allocation was not changed! It is "
	    "also possible that there was an attempt to free the same memory "
	    "twice.", pointer, [object name]);

	return string;
}

- (void*)pointer
{
	return pointer;
}
@end

@implementation OFOutOfRangeException










- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Value out of range in object of class %s!",
	    object != nil ? [object name] : "(null)");

	return string;
}
@end

@implementation OFOpenFileFailedException
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "ERROR: Failed to open file %s with mode %s "
	    "in object of class %s!\n", path, mode, [self name]);

	return string;
}

- (char*)path
{
	return path;







|
|







179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
}

- (char*)cString
{
	if (string != NULL)
		return string;

	asprintf(&string, "Failed to open file %s with mode %s in object of "
	    "class %s!", path, mode, [self name]);

	return string;
}

- (char*)path
{
	return path;
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263


























264
265
266
267

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

	asprintf(&string, "ERROR: Failed to read %zu items of size %zu in "
	    "object of class %s!\n", req_items, req_size, [object name]);

	return string;
}
@end

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

	asprintf(&string, "ERROR: Failed to write %zu items of size %zu in "
	    "object of class %s!\n", req_items, req_size, [object name]);



























	return string;
}
@end







|
|











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




235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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

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

	asprintf(&string, "Failed to read %zu items of size %zu in object of "
	    "class %s!", req_items, req_size, [object name]);

	return string;
}
@end

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

	asprintf(&string, "Failed to write %zu items of size %zu in object of "
	    "class %s!", req_items, req_size, [object name]);

	return string;
}
@end

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

	asprintf(&string, "The socket of type %s is not connected or bound!",
	    [object name]);

	return string;
}
@end

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

	asprintf(&string, "The socket of type %s is already connected or bound "
	    "and thus can't be connected or bound again!", [object name]);

	return string;
}
@end

Modified src/OFTCPSocket.m from [594dc2652b] to [8a2ef1e3cd].

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
	char portstr[6];

	if (!port) {
		/* FIXME: Throw exception */
		return nil;
	}

	if (sock >= 0) {
		/* FIXME: Throw exception */
		return nil;
	}

	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	snprintf(portstr, 6, "%d", port);








|
|
<
<







62
63
64
65
66
67
68
69
70


71
72
73
74
75
76
77
	char portstr[6];

	if (!port) {
		/* FIXME: Throw exception */
		return nil;
	}

	if (sock >= 0)
		@throw [OFAlreadyConnectedException newWithObject: self];



	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	snprintf(portstr, 6, "%d", port);

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
	char portstr[6];

	if (!port) {
		/* FIXME: Throw exception */
		return nil;
	}

	if (sock >= 0) {
		/* FIXME: Throw exception */
		return nil;
	}

	if ((sock = socket(family, SOCK_STREAM, 0)) < 0) {
		/* FIXME: Throw exception */
		return nil;
	}

	memset(&hints, 0, sizeof(struct addrinfo));







|
|
<
<







112
113
114
115
116
117
118
119
120


121
122
123
124
125
126
127
	char portstr[6];

	if (!port) {
		/* FIXME: Throw exception */
		return nil;
	}

	if (sock >= 0)
		@throw [OFAlreadyConnectedException newWithObject: self];



	if ((sock = socket(family, SOCK_STREAM, 0)) < 0) {
		/* FIXME: Throw exception */
		return nil;
	}

	memset(&hints, 0, sizeof(struct addrinfo));
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	freeaddrinfo(res);

	return self;
}

- listenWithBackLog: (int)backlog
{
	if (sock < 0) {
		/* FIXME: Throw exception */
		return nil;
	}

	if (listen(sock, backlog) < 0 ) {
		/* FIXME: Throw exception */
		return nil;
	}

	return self;
}

- listen
{
	if (sock < 0) {
		/* FIXME: Throw exception */
		return nil;
	}

	if (listen(sock, 5) < 0 ) {
		/* FIXME: Throw exception */
		return nil;
	}

	return self;







|
|
<
<











|
|
<
<







144
145
146
147
148
149
150
151
152


153
154
155
156
157
158
159
160
161
162
163
164
165


166
167
168
169
170
171
172
	freeaddrinfo(res);

	return self;
}

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



	if (listen(sock, backlog) < 0 ) {
		/* FIXME: Throw exception */
		return nil;
	}

	return self;
}

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



	if (listen(sock, 5) < 0 ) {
		/* FIXME: Throw exception */
		return nil;
	}

	return self;
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
247
248
249
250
251
252
253
254
255
256
257
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
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (uint8_t*)buf
{
	ssize_t ret;

	if (sock < 0) {
		/* FIXME: Throw exception */
		return 0;
	}

	if ((ret = recv(sock, buf, size, 0)) < 0) {
		/* FIXME: Throw exception */
		return 0;
	}

	/* This is safe, as we already checked < 0 */
	return ret;
}

- (uint8_t*)readNBytes: (size_t)size
{
	uint8_t *ret;

	if (sock < 0) {
		/* FIXME: Throw exception */
		return NULL;
	}

	ret = [self getMemWithSize: size];

	@try {
		[self readNBytes: size
		      intoBuffer: ret];
	} @catch (id exception) {
		[self freeMem: ret];
		@throw exception;
	}

	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	ssize_t ret;

	if (sock < 0) {
		/* FIXME: Throw exception */
		return 0;
	}

	if ((ret = send(sock, buf, size, 0)) < 0) {
		/* FIXME: Throw exception */
		return 0;
	}

	/* This is safe, as we already checked < 0 */
	return ret;
}

- (size_t)writeCString: (const char*)str
{
	if (sock < 0) {
		/* FIXME: Throw exception */
		return 0;
	}

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

- close
{
	if (sock < 0) {
		/* FIXME: Throw exception */
		return nil;
	}

	sock = -1;

	if (saddr != NULL)
		[self freeMem: saddr];
	saddr_len = 0;

	return self;
}
@end







|
|
<
<














|
|
<
<



















|
|
<
<












|
|
<
<







|
|
<
<










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
247


248
249
250
251
252
253
254
255
256
257
258
259
260
261


262
263
264
265
266
267
268
269
270


271
272
273
274
275
276
277
278
279
280
}

- (size_t)readNBytes: (size_t)size
	  intoBuffer: (uint8_t*)buf
{
	ssize_t ret;

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



	if ((ret = recv(sock, buf, size, 0)) < 0) {
		/* FIXME: Throw exception */
		return 0;
	}

	/* This is safe, as we already checked < 0 */
	return ret;
}

- (uint8_t*)readNBytes: (size_t)size
{
	uint8_t *ret;

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



	ret = [self getMemWithSize: size];

	@try {
		[self readNBytes: size
		      intoBuffer: ret];
	} @catch (id exception) {
		[self freeMem: ret];
		@throw exception;
	}

	return ret;
}

- (size_t)writeNBytes: (size_t)size
	   fromBuffer: (const uint8_t*)buf
{
	ssize_t ret;

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



	if ((ret = send(sock, buf, size, 0)) < 0) {
		/* FIXME: Throw exception */
		return 0;
	}

	/* This is safe, as we already checked < 0 */
	return ret;
}

- (size_t)writeCString: (const char*)str
{
	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;

	if (saddr != NULL)
		[self freeMem: saddr];
	saddr_len = 0;

	return self;
}
@end

Modified tests/OFArray/OFArray.m from [2327b3b399] to [6fb90c3139].

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#define CATCH_EXCEPTION(code, exception)		\
	caught = NO;					\
	@try {						\
		code;					\
	} @catch (exception *e) {			\
		caught = YES;				\
		puts("CAUGHT! Error string was:");	\
		fputs([e cString], stdout);		\
		puts("Resuming...");			\
	}						\
	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}








|







21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#define CATCH_EXCEPTION(code, exception)		\
	caught = NO;					\
	@try {						\
		code;					\
	} @catch (exception *e) {			\
		caught = YES;				\
		puts("CAUGHT! Error string was:");	\
		puts([e cString]);			\
		puts("Resuming...");			\
	}						\
	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}

Modified tests/OFObject/OFObject.m from [efc895790f] to [3c3dc8da7e].

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#define CATCH_EXCEPTION(code, exception)		\
	caught = NO;					\
	@try {						\
		code;					\
	} @catch (exception *e) {			\
		caught = YES;				\
		puts("CAUGHT! Error string was:");	\
		fputs([e cString], stdout);		\
		puts("Resuming...");			\
	}						\
	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}








|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#define CATCH_EXCEPTION(code, exception)		\
	caught = NO;					\
	@try {						\
		code;					\
	} @catch (exception *e) {			\
		caught = YES;				\
		puts("CAUGHT! Error string was:");	\
		puts([e cString]);			\
		puts("Resuming...");			\
	}						\
	if (!caught) {					\
		puts("NOT CAUGHT!");			\
		return 1;				\
	}

Modified tests/OFTCPSocket/OFTCPSocket.m from [5359cde91c] to [af4aa7dc3f].

100
101
102
103
104
105
106

107
108
109
110
		}

		[accepted free];
		[client close];
		[server close];
	} @catch(OFException *e) {
		printf("EXCEPTION: %s\n", [e cString]);

	}

	return 0;
}







>




100
101
102
103
104
105
106
107
108
109
110
111
		}

		[accepted free];
		[client close];
		[server close];
	} @catch(OFException *e) {
		printf("EXCEPTION: %s\n", [e cString]);
		return 1;
	}

	return 0;
}