From 2d705754fddd4541e911545c5c5e9c3fbdd6e5e8 Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Wed, 17 Oct 2012 20:18:30 +0000 Subject: [PATCH] Use async I/O. FossilOrigin-Name: e7f34ce8d03e83a75b0da0471aafe0e84e139f0b2e4f5dde571bd6b5bdd07cbd --- src/IRCChannel.m | 2 ++ src/IRCConnection.h | 2 +- src/IRCConnection.m | 62 +++++++++++++++++++++++++++++---------------- 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/IRCChannel.m b/src/IRCChannel.m index f68f36e..1414525 100644 --- a/src/IRCChannel.m +++ b/src/IRCChannel.m @@ -20,6 +20,8 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#import + #import "IRCChannel.h" @implementation IRCChannel diff --git a/src/IRCConnection.h b/src/IRCConnection.h index 1f2c95d..2ec1e29 100644 --- a/src/IRCConnection.h +++ b/src/IRCConnection.h @@ -110,6 +110,6 @@ fromChannel: (IRCChannel*)channel withReason: (OFString*)reason; - (void)changeNicknameTo: (OFString*)nickname; -- (void)process; +- (void)processLine: (OFString*)line; - (void)handleConnection; @end diff --git a/src/IRCConnection.m b/src/IRCConnection.m index 4de7fc2..ac4c892 100644 --- a/src/IRCConnection.m +++ b/src/IRCConnection.m @@ -30,6 +30,8 @@ #import +#import + #import "IRCConnection.h" #import "IRCUser.h" #import "IRCChannel.h" @@ -163,31 +165,12 @@ [self sendLineWithFormat: @"NICK %@", nickname_]; } -- (void)process +- (void)processLine: (OFString*)line { OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; - OFString *line; OFArray *split; OFString *action = nil; - if (sock.atEndOfStream) { - if ([delegate respondsToSelector: - @selector(connectionWasClosed:)]) - [delegate connectionWasClosed: self]; - - return; - } - - @try { - line = [sock tryReadLine]; - } @catch (OFInvalidEncodingException *e) { - line = [sock tryReadLineWithEncoding: - OF_STRING_ENCODING_WINDOWS_1252]; - } - - if (line == nil) - return; - if ([delegate respondsToSelector: @selector(connection:didReceiveLine:)]) [delegate connection: self @@ -502,10 +485,45 @@ } } +- (BOOL)connection: (OFTCPSocket*)connection + didReceiveISO88591Line: (OFString*)line + exception: (OFException*)exception +{ + if (line != nil) { + [self processLine: line]; + [sock asyncReadLineWithTarget: self + selector: @selector(connection: + didReceiveLine: + exception:)]; + } + + return NO; +} + +- (BOOL)connection: (OFTCPSocket*)connection + didReceiveLine: (OFString*)line + exception: (OFException*)exception +{ + if (line != nil) { + [self processLine: line]; + return YES; + } + + if ([exception isKindOfClass: [OFInvalidEncodingException class]]) + [sock asyncReadLineWithEncoding: OF_STRING_ENCODING_ISO_8859_1 + target: self + selector: @selector(connection: + didReceiveISO88591Line: + exception:)]; + + return NO; +} + - (void)handleConnection { - while (![sock isAtEndOfStream]) - [self process]; + [sock asyncReadLineWithTarget: self + selector: @selector(connection:didReceiveLine: + exception:)]; } - (void)dealloc