ObjFW  Diff

Differences From Artifact [0f7c2e2900]:

To Artifact [fe6d461e93]:


54
55
56
57
58
59
60
61
62


63
64
65
66
67
68
69
54
55
56
57
58
59
60


61
62
63
64
65
66
67
68
69







-
-
+
+








	sock = INVALID_SOCKET;
	saddr = NULL;

	return self;
}

- connectToService: (OFString*)service
	    onNode: (OFString*)node
- (void)connectToService: (OFString*)service
		  onNode: (OFString*)node
{
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

#ifdef HAVE_THREADSAFE_GETADDRINFO
	struct addrinfo hints, *res, *res0;

184
185
186
187
188
189
190
191

192
193

194
195
196
197



198
199
200
201
202
203
204
184
185
186
187
188
189
190

191


192




193
194
195
196
197
198
199
200
201
202







-
+
-
-
+
-
-
-
-
+
+
+







	}
#endif

	if (sock == INVALID_SOCKET)
		@throw [OFConnectionFailedException newWithClass: isa
							    node: node
							 service: service];

}
	return self;
}


- bindService: (OFString*)service
       onNode: (OFString*)node
   withFamily: (int)family
- (void)bindService: (OFString*)service
	     onNode: (OFString*)node
	 withFamily: (int)family
{
	if (sock != INVALID_SOCKET)
		@throw [OFAlreadyConnectedException newWithClass: isa];

#ifndef HAVE_THREADSAFE_GETADDRINFO
	if (family != AF_INET)
		@throw [OFBindFailedException newWithClass: isa
302
303
304
305
306
307
308
309

310
311

312
313

314
315
316
317
318
319
320
321

322
323

324
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
300
301
302
303
304
305
306

307


308


309
310
311
312
313
314
315
316

317


318


319
320
321
322
323
324
325
326


327
328
329
330
331
332
333







-
+
-
-
+
-
-
+







-
+
-
-
+
-
-
+







-
-







		sock = INVALID_SOCKET;
		@throw [OFBindFailedException newWithClass: isa
						      node: node
						   service: service
						    family: family];
	}
#endif

}
	return self;
}


- listenWithBackLog: (int)backlog
- (void)listenWithBackLog: (int)backlog
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	if (listen(sock, backlog) == -1)
		@throw [OFListenFailedException newWithClass: isa
						     backLog: backlog];

}
	return self;
}


- listen
- (void)listen
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	if (listen(sock, 5) == -1)
		@throw [OFListenFailedException newWithClass: isa
						     backLog: 5];

	return self;
}

- (OFTCPSocket*)accept
{
	OFTCPSocket *newsock;
	struct sockaddr *addr;
	socklen_t addrlen;
359
360
361
362
363
364
365
366

367
368
369
370
371
372
373
374
375
376
377
378
379
380
351
352
353
354
355
356
357

358
359
360
361
362
363


364
365
366
367
368
369
370







-
+





-
-







	newsock->sock = s;
	newsock->saddr = addr;
	newsock->saddr_len = addrlen;

	return newsock;
}

- enableKeepAlives: (BOOL)enable
- (void)setKeepAlivesEnabled: (BOOL)enable
{
	int v = enable;

	if (setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&v, sizeof(v)))
		@throw [OFSetOptionFailedException newWithClass: isa];

	return self;
}

- (OFString*)remoteAddress
{
	if (saddr == NULL || saddr_len == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416


417
418
419
420
421
422
423
424

425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
378
379
380
381
382
383
384



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408


409
410

411
412
413
414
415
416
417
418
419
420
421


422
423
424
425
426
427
428
429
430
431







-
-
-



















+
+



-
-


-
+










-
-










			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: node];
	} @finally {
		[self freeMemory: node];
	}

	/* Get rid of a warning, never reached anyway */
	assert(0);
#else
	char *node;

# ifdef OF_THREADS
	[mutex lock];

	@try {
# endif
		node = inet_ntoa(((struct sockaddr_in*)saddr)->sin_addr);

		if (node == NULL)
			@throw [OFAddressTranslationFailedException
			    newWithClass: isa];

		return [OFString stringWithCString: node];
# ifdef OF_THREADS
	} @finally {
		[mutex unlock];
	}
# endif
#endif

	/* Get rid of a warning, never reached anyway */
	assert(0);
# endif
#endif
}

- close
- (void)close
{
	if (sock == INVALID_SOCKET)
		@throw [OFNotConnectedException newWithClass: isa];

	close(sock);
	sock = INVALID_SOCKET;

	[self freeMemory: saddr];
	saddr = NULL;
	saddr_len = 0;

	return self;
}

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

	[super dealloc];
}
@end