11/*
2- * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016
2+ * Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
33 * Jonathan Schleifer <js@heap.zone>
44 * Copyright (c) 2011, Florian Zeitz <florob@babelmonkeys.de>
55 * Copyright (c) 2011, Jos Kuijpers <jos@kuijpersvof.nl>
@@ -96,40 +96,29 @@ - (void)SSL_startTLSWithExpectedHost: (OFString *)host
9696- (void )SSL_super_close ;
9797@end
9898
99- @interface SSLSocket_ConnectContext : OFObject
99+ @interface SSLSocket_ConnectContext : OFObject <OFTLSSocketDelegate>
100100{
101101 OFString *_host;
102102 uint16_t _port;
103- id _target;
104- SEL _selector;
105- id _context;
103+ id <OFTLSSocketDelegate> _delegate;
106104}
107105
108106- (instancetype )initWithHost : (OFString *)host
109107 port : (uint16_t )port
110- target : (id )target
111- selector : (SEL )selector
112- context : (id )context ;
113- - (void )socketDidConnect : (SSLSocket *)sock
114- context : (id )context
115- exception : (id )exception ;
108+ delegate : (id <OFTLSSocketDelegate>)delegate ;
116109@end
117110
118111@implementation SSLSocket_ConnectContext
119112- (instancetype )initWithHost : (OFString *)host
120113 port : (uint16_t )port
121- target : (id )target
122- selector : (SEL )selector
123- context : (id )context
114+ delegate : (id <OFTLSSocketDelegate>)delegate
124115{
125116 self = [super init ];
126117
127118 @try {
128119 _host = [host copy ];
129120 _port = port;
130- _target = [target retain ];
131- _selector = selector;
132- _context = [context retain ];
121+ _delegate = [delegate retain ];
133122 } @catch (id e) {
134123 [self release ];
135124 @throw e;
@@ -140,37 +129,46 @@ - (instancetype)initWithHost: (OFString *)host
140129
141130- (void )dealloc
142131{
143- [_host release ];
144- [_target release ];
145- [_context release ];
132+ [_delegate release ];
146133
147134 [super dealloc ];
148135}
149136
150- - (void )socketDidConnect : (SSLSocket * )sock
151- context : (id ) context
152- exception : (id ) exception
137+ - (void )socket : (OF_KINDOF(OFTCPSocket *) )sock
138+ didConnectToHost : (OFString *) host
139+ port : (uint16_t ) port
153140{
154- void (*func)(id , SEL , OFTCPSocket *, id , id ) =
155- (void (*)(id , SEL , OFTCPSocket *, id , id ))
156- [_target methodForSelector: _selector];
157-
158- if (exception == nil ) {
159- @try {
160- [sock SSL_startTLSWithExpectedHost: _host
161- port: _port];
162- } @catch (id e) {
163- func (_target, _selector, sock, _context, e);
164- return ;
165- }
141+ @try {
142+ [sock SSL_startTLSWithExpectedHost: _host
143+ port: _port];
144+ } @catch (id e) {
145+ [_delegate socket: sock
146+ didFailToConnectWithException: e
147+ host: host
148+ port: port];
149+ return ;
166150 }
167151
168- func (_target, _selector, sock, _context, exception);
152+ [_delegate socket: sock
153+ didConnectToHost: host
154+ port: port];
155+ }
156+
157+ - (void )socket : (OF_KINDOF(OFTCPSocket *))sock
158+ didFailToConnectWithException : (id )exception
159+ host : (OFString *)host
160+ port : (uint16_t )port
161+ {
162+ return [_delegate socket: sock
163+ didFailToConnectWithException: exception
164+ host: host
165+ port: port];
169166}
170167@end
171168
172169@implementation SSLSocket
173- @synthesize delegate = _delegate, certificateFile = _certificateFile;
170+ @dynamic delegate;
171+ @synthesize certificateFile = _certificateFile;
174172@synthesize privateKeyFile = _privateKeyFile;
175173@synthesize privateKeyPassphrase = _privateKeyPassphrase;
176174@synthesize certificateVerificationEnabled = _certificateVerificationEnabled;
@@ -359,26 +357,17 @@ - (void)startTLSWithExpectedHost: (OFString *)host
359357- (void )asyncConnectToHost : (OFString *)host
360358 port : (uint16_t )port
361359 runLoopMode : (of_run_loop_mode_t )runLoopMode
362- target : (id )target
363- selector : (SEL )selector
364- context : (id )userContext
365360{
366361 void *pool = objc_autoreleasePoolPush ();
367362 SSLSocket_ConnectContext *context;
368363
369364 context = [[[SSLSocket_ConnectContext alloc ]
370365 initWithHost: host
371366 port: port
372- target: target
373- selector: selector
374- context: userContext] autorelease ];
367+ delegate: _delegate] autorelease ];
375368 [super asyncConnectToHost: host
376369 port: port
377- runLoopMode: runLoopMode
378- target: context
379- selector: @selector (socketDidConnect:context:
380- exception: )
381- context: nil ];
370+ runLoopMode: runLoopMode];
382371
383372 objc_autoreleasePoolPop (pool);
384373}
0 commit comments