Add ping timeout
FossilOrigin-Name: c17c999968cef18b167eff8385bba52e95765ad6f5e912b78eab34e0af71f76d
This commit is contained in:
parent
0dd8cdc46a
commit
a145344ad2
3 changed files with 66 additions and 4 deletions
|
@ -82,6 +82,9 @@
|
|||
OFMutableDictionary *_channels;
|
||||
id <IRCConnectionDelegate> _delegate;
|
||||
of_string_encoding_t _fallbackEncoding;
|
||||
of_time_interval_t _pingInterval, _pingTimeout;
|
||||
OFString *_pingData;
|
||||
OFTimer *_pingTimer;
|
||||
}
|
||||
|
||||
@property (assign) Class socketClass;
|
||||
|
@ -91,6 +94,7 @@
|
|||
@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;
|
||||
|
|
|
@ -43,6 +43,7 @@
|
|||
@synthesize nickname = _nickname, username = _username, realname = _realname;
|
||||
@synthesize delegate = _delegate, socket = _socket;
|
||||
@synthesize fallbackEncoding = _fallbackEncoding;
|
||||
@synthesize pingInterval = _pingInterval, pingTimeout = _pingTimeout;
|
||||
|
||||
+ (instancetype)connection
|
||||
{
|
||||
|
@ -58,6 +59,8 @@
|
|||
_channels = [[OFMutableDictionary alloc] init];
|
||||
_port = 6667;
|
||||
_fallbackEncoding = OF_STRING_ENCODING_ISO_8859_1;
|
||||
_pingInterval = 120;
|
||||
_pingTimeout = 30;
|
||||
} @catch (id e) {
|
||||
[self release];
|
||||
@throw e;
|
||||
|
@ -74,6 +77,8 @@
|
|||
[_username release];
|
||||
[_realname release];
|
||||
[_channels release];
|
||||
[_pingData release];
|
||||
[_pingTimer release];
|
||||
|
||||
[super dealloc];
|
||||
}
|
||||
|
@ -249,6 +254,19 @@
|
|||
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 */
|
||||
|
@ -257,6 +275,11 @@
|
|||
@selector(connectionWasEstablished:)])
|
||||
[_delegate connectionWasEstablished: self];
|
||||
|
||||
[OFTimer scheduledTimerWithTimeInterval: _pingInterval
|
||||
target: self
|
||||
selector: @selector(IRC_sendPing)
|
||||
repeats: true];
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -527,6 +550,31 @@
|
|||
}
|
||||
}
|
||||
|
||||
- (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();
|
||||
|
@ -570,11 +618,14 @@
|
|||
return false;
|
||||
}
|
||||
|
||||
if ([_delegate respondsToSelector: @selector(connectionWasClosed:)]) {
|
||||
if ([_delegate respondsToSelector: @selector(connectionWasClosed:)])
|
||||
[_delegate connectionWasClosed: self];
|
||||
[_socket release];
|
||||
_socket = nil;
|
||||
}
|
||||
|
||||
[_pingTimer invalidate];
|
||||
|
||||
[_socket cancelAsyncRequests];
|
||||
[_socket release];
|
||||
_socket = nil;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue