ObjIRC  Check-in [c17c999968]

Overview
Comment:Add ping timeout
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: c17c999968cef18b167eff8385bba52e95765ad6f5e912b78eab34e0af71f76d
User & Date: js on 2017-01-22 23:01:50
Other Links: manifest | tags
Context
2017-05-08
00:54
Adjust to recent ObjFW changes check-in: 5dbb32c633 user: js tags: trunk
2017-01-22
23:01
Add ping timeout check-in: c17c999968 user: js tags: trunk
20:49
IRCConnection: Make fallback encoding configurable check-in: 0ca6e4f04d user: js tags: trunk
Changes

Modified src/IRCConnection.h from [c6f6102a91] to [2a183ce5dc].

78
79
80
81
82
83
84



85
86
87
88
89
90
91
92
93

94
95
96
97
98
99
100
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104







+
+
+









+







	OF_KINDOF(OFTCPSocket) *_socket;
	OFString *_server;
	uint16_t _port;
	OFString *_nickname, *_username, *_realname;
	OFMutableDictionary *_channels;
	id <IRCConnectionDelegate> _delegate;
	of_string_encoding_t _fallbackEncoding;
	of_time_interval_t _pingInterval, _pingTimeout;
	OFString *_pingData;
	OFTimer *_pingTimer;
}

@property (assign) Class socketClass;
@property (copy) OFString *server;
@property uint16_t port;
@property (copy) OFString *nickname, *username, *realname;
@property (assign) id <IRCConnectionDelegate> delegate;
@property (readonly, retain) OFTCPSocket *socket;
@property of_string_encoding_t fallbackEncoding;
@property of_time_interval_t pingInterval, pingTimeout;

+ (instancetype)connection;
- (void)sendLine: (OFString*)line;
- (void)sendLineWithFormat: (OFConstantString*)line, ...;
- (void)connect;
- (void)disconnect;
- (void)disconnectWithReason: (OFString*)reason;

Modified src/IRCConnection.m from [475d90a93d] to [84167a7c12].

39
40
41
42
43
44
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76


77
78
79
80
81
82
83
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88







+















+
+
















+
+








@implementation IRCConnection
@synthesize socketClass = _socketClass;
@synthesize server = _server, port = _port;
@synthesize nickname = _nickname, username = _username, realname = _realname;
@synthesize delegate = _delegate, socket = _socket;
@synthesize fallbackEncoding = _fallbackEncoding;
@synthesize pingInterval = _pingInterval, pingTimeout = _pingTimeout;

+ (instancetype)connection
{
	return [[[self alloc] init] autorelease];
}

- init
{
	self = [super init];

	@try {
		_socketClass = [OFTCPSocket class];
		_channels = [[OFMutableDictionary alloc] init];
		_port = 6667;
		_fallbackEncoding = OF_STRING_ENCODING_ISO_8859_1;
		_pingInterval = 120;
		_pingTimeout = 30;
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (void)dealloc
{
	[_socket release];
	[_server release];
	[_nickname release];
	[_username release];
	[_realname release];
	[_channels release];
	[_pingData release];
	[_pingTimer release];

	[super dealloc];
}

- (void)connect
{
	void *pool = objc_autoreleasePoolPush();
244
245
246
247
248
249
250













251
252
253
254
255
256
257
258





259
260
261
262
263
264
265
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







+
+
+
+
+
+
+
+
+
+
+
+
+








+
+
+
+
+







		OFMutableString *s = [[line mutableCopy] autorelease];
		[s replaceCharactersInRange: of_range(0, 4)
				 withString: @"PONG"];
		[self sendLine: s];

		return;
	}

	/* PONG */
	if ([components count] == 4 &&
	    [[components objectAtIndex: 1] isEqual: @"PONG"] &&
	    [[components objectAtIndex: 3] isEqual: _pingData]) {
		[_pingTimer invalidate];

		[_pingData release];
		[_pingTimer release];

		_pingData = nil;
		_pingTimer = nil;
	}

	action = [[components objectAtIndex: 1] uppercaseString];

	/* Connected */
	if ([action isEqual: @"001"] && [components count] >= 4) {
		if ([_delegate respondsToSelector:
		    @selector(connectionWasEstablished:)])
			[_delegate connectionWasEstablished: self];

		[OFTimer scheduledTimerWithTimeInterval: _pingInterval
						 target: self
					       selector: @selector(IRC_sendPing)
						repeats: true];

		return;
	}

	/* JOIN */
	if ([action isEqual: @"JOIN"] && [components count] == 3) {
		OFString *who = [components objectAtIndex: 0];
522
523
524
525
526
527
528

























529
530
531
532
533
534
535
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







				     didReceiveNotice: notice
						 user: user];
		}

		return;
	}
}

- (void)IRC_sendPing
{
	[_pingData release];
	[_pingTimer release];

	_pingData = [[OFString alloc] initWithFormat: @":%d", rand()];
	[_socket writeFormat: @"PING %@\r\n", _pingData];

	_pingTimer = [[OFTimer
	    scheduledTimerWithTimeInterval: _pingTimeout
				    target: self
				  selector: @selector(IRC_pingTimeout)
				   repeats: false] retain];
}

- (void)IRC_pingTimeout
{
	if ([_delegate respondsToSelector: @selector(connectionWasClosed:)])
		[_delegate connectionWasClosed: self];

	[_socket cancelAsyncRequests];
	[_socket release];
	_socket = nil;
}

- (void)processLine: (OFString*)line
{
	void *pool = objc_autoreleasePoolPush();

	[self IRC_processLine: line];

566
567
568
569
570
571
572
573

574




575
576


577
578
579
580
581
582
583
584
614
615
616
617
618
619
620

621
622
623
624
625
626


627
628

629
630
631
632
633
634
635







-
+

+
+
+
+
-
-
+
+
-







				       target: self
				     selector: @selector(socket:
						   didReceiveWronglyEncodedLine:
						   exception:)];
		return false;
	}

	if ([_delegate respondsToSelector: @selector(connectionWasClosed:)]) {
	if ([_delegate respondsToSelector: @selector(connectionWasClosed:)])
		[_delegate connectionWasClosed: self];

	[_pingTimer invalidate];

	[_socket cancelAsyncRequests];
		[_socket release];
		_socket = nil;
	[_socket release];
	_socket = nil;
	}

	return false;
}

- (void)handleConnection
{
	[_socket asyncReadLineWithTarget: self

Modified tests/tests.m from [9f9c8d8d61] to [ce0aa7ef08].

134
135
136
137
138
139
140







141
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148







+
+
+
+
+
+
+


-	   (void)connection: (IRCConnection*)connection
  didReceiveNamesForChannel: (OFString*)channel
{
	of_log(@"Users in %@: %@", channel,
	    [connection usersInChannel: channel]);
}

- (void)connectionWasClosed: (IRCConnection*)connection
{
	of_log(@"Disconnected!");

	[OFApplication terminate];
}
@end