blob: b0bac74c6dcdc058730e7bf5ed7af2e8513e513f [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Mark Slee77575e62007-09-24 19:24:53 +000019#import "TSocketClient.h"
Jake Farrell9689d892011-12-06 01:07:17 +000020#import "TObjective-C.h"
Andrew McGeachieb80024d2010-04-28 17:43:45 +000021
22#if !TARGET_OS_IPHONE
23#import <CoreServices/CoreServices.h>
24#else
25#import <CFNetwork/CFNetwork.h>
26#endif
Mark Slee77575e62007-09-24 19:24:53 +000027
Jake Farrell04f83112013-03-02 22:51:55 -050028@interface TSocketClient ()
29{
30 NSInputStream * inputStream;
31 NSOutputStream * outputStream;
32}
33@end
34
Mark Slee77575e62007-09-24 19:24:53 +000035@implementation TSocketClient
36
37- (id) initWithHostname: (NSString *) hostname
Roger Meier6b616012015-03-01 12:32:50 +010038 port: (UInt32) port
Mark Slee77575e62007-09-24 19:24:53 +000039{
Jake Farrell04f83112013-03-02 22:51:55 -050040 inputStream = NULL;
41 outputStream = NULL;
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000042 CFReadStreamRef readStream = NULL;
43 CFWriteStreamRef writeStream = NULL;
Jake Farrell9689d892011-12-06 01:07:17 +000044 CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (bridge_stub CFStringRef)hostname, port, &readStream, &writeStream);
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000045 if (readStream && writeStream) {
46 CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
47 CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
48
Jake Farrell9689d892011-12-06 01:07:17 +000049 inputStream = (bridge_stub NSInputStream *)readStream;
50 [inputStream retain_stub];
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000051 [inputStream setDelegate:self];
52 [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
53 [inputStream open];
54
Jake Farrell9689d892011-12-06 01:07:17 +000055 outputStream = (bridge_stub NSOutputStream *)writeStream;
56 [outputStream retain_stub];
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000057 [outputStream setDelegate:self];
58 [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
59 [outputStream open];
Jake Farrell9689d892011-12-06 01:07:17 +000060 CFRelease(readStream);
61 CFRelease(writeStream);
Andrew McGeachiebbd55ad2009-07-24 15:58:07 +000062 }
63
64 self = [super initWithInputStream: inputStream outputStream: outputStream];
65
66 return self;
Mark Slee77575e62007-09-24 19:24:53 +000067}
68
Jake Farrell04f83112013-03-02 22:51:55 -050069-(void)dealloc
70{
71 [inputStream close];
72 [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
73 [inputStream setDelegate:nil];
74 [inputStream release_stub];
75
76 [outputStream close];
77 [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
78 [outputStream setDelegate:nil];
79 [outputStream release_stub];
80 [super dealloc_stub];
81}
82
Mark Slee77575e62007-09-24 19:24:53 +000083
84@end
85
86
87