ObjFW  Diff

Differences From Artifact [594dc2652b]:

To Artifact [8a2ef1e3cd]:


62
63
64
65
66
67
68
69
70


71
72
73
74
75
76
77
78
79
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) {
		/* FIXME: Throw exception */
	if (sock >= 0)
		@throw [OFAlreadyConnectedException newWithObject: self];
		return nil;
	}

	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
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) {
		/* FIXME: Throw exception */
	if (sock >= 0)
		@throw [OFAlreadyConnectedException newWithObject: self];
		return nil;
	}

	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
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) {
		/* FIXME: Throw exception */
	if (sock < 0)
		@throw [OFNotConnectedException newWithObject: self];
		return nil;
	}

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

	return self;
}

- listen
{
	if (sock < 0) {
		/* FIXME: Throw exception */
	if (sock < 0)
		@throw [OFNotConnectedException newWithObject: self];
		return nil;
	}

	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
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) {
		/* FIXME: Throw exception */
	if (sock < 0) 
		@throw [OFNotConnectedException newWithObject: self];
		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 */
	if (sock < 0) 
		@throw [OFNotConnectedException newWithObject: self];
		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 */
	if (sock < 0) 
		@throw [OFNotConnectedException newWithObject: self];
		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 */
	if (sock < 0) 
		@throw [OFNotConnectedException newWithObject: self];
		return 0;
	}

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

- close
{
	if (sock < 0) {
		/* FIXME: Throw exception */
	if (sock < 0) 
		@throw [OFNotConnectedException newWithObject: self];
		return nil;
	}

	sock = -1;

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

	return self;
}
@end