David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 19 | #import "TSocketClient.h" |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 20 | #import "TObjective-C.h" |
Andrew McGeachie | b80024d | 2010-04-28 17:43:45 +0000 | [diff] [blame] | 21 | |
| 22 | #if !TARGET_OS_IPHONE |
| 23 | #import <CoreServices/CoreServices.h> |
| 24 | #else |
| 25 | #import <CFNetwork/CFNetwork.h> |
| 26 | #endif |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 27 | |
Jake Farrell | 04f8311 | 2013-03-02 22:51:55 -0500 | [diff] [blame] | 28 | @interface TSocketClient () |
| 29 | { |
| 30 | NSInputStream * inputStream; |
| 31 | NSOutputStream * outputStream; |
| 32 | } |
| 33 | @end |
| 34 | |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 35 | @implementation TSocketClient |
| 36 | |
| 37 | - (id) initWithHostname: (NSString *) hostname |
Roger Meier | 6b61601 | 2015-03-01 12:32:50 +0100 | [diff] [blame] | 38 | port: (UInt32) port |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 39 | { |
Jake Farrell | 04f8311 | 2013-03-02 22:51:55 -0500 | [diff] [blame] | 40 | inputStream = NULL; |
| 41 | outputStream = NULL; |
Andrew McGeachie | bbd55ad | 2009-07-24 15:58:07 +0000 | [diff] [blame] | 42 | CFReadStreamRef readStream = NULL; |
| 43 | CFWriteStreamRef writeStream = NULL; |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 44 | CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (bridge_stub CFStringRef)hostname, port, &readStream, &writeStream); |
Andrew McGeachie | bbd55ad | 2009-07-24 15:58:07 +0000 | [diff] [blame] | 45 | if (readStream && writeStream) { |
| 46 | CFReadStreamSetProperty(readStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); |
| 47 | CFWriteStreamSetProperty(writeStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue); |
| 48 | |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 49 | inputStream = (bridge_stub NSInputStream *)readStream; |
| 50 | [inputStream retain_stub]; |
Andrew McGeachie | bbd55ad | 2009-07-24 15:58:07 +0000 | [diff] [blame] | 51 | [inputStream setDelegate:self]; |
| 52 | [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; |
| 53 | [inputStream open]; |
| 54 | |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 55 | outputStream = (bridge_stub NSOutputStream *)writeStream; |
| 56 | [outputStream retain_stub]; |
Andrew McGeachie | bbd55ad | 2009-07-24 15:58:07 +0000 | [diff] [blame] | 57 | [outputStream setDelegate:self]; |
| 58 | [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; |
| 59 | [outputStream open]; |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 60 | CFRelease(readStream); |
| 61 | CFRelease(writeStream); |
Andrew McGeachie | bbd55ad | 2009-07-24 15:58:07 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | self = [super initWithInputStream: inputStream outputStream: outputStream]; |
| 65 | |
| 66 | return self; |
Mark Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Jake Farrell | 04f8311 | 2013-03-02 22:51:55 -0500 | [diff] [blame] | 69 | -(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 Slee | 77575e6 | 2007-09-24 19:24:53 +0000 | [diff] [blame] | 83 | |
| 84 | @end |
| 85 | |
| 86 | |
| 87 | |