Skip to content

Commit 2d24e66

Browse files
Eric WilliamsonEric Williamson
authored andcommitted
Add the API helper and a NSStream helper for making TCP connections easy
1 parent dda0239 commit 2d24e66

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

IR Remote/APIHelper.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// APIHelper.h
3+
// IR Remote
4+
//
5+
// Created by Eric Williamson on 4/18/13.
6+
// Copyright (c) 2013 Eric Williamson. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface APIHelper : NSObject <NSStreamDelegate>
12+
13+
-(void) connectToServerUsingCFStream:(NSString *) urlStr portNo: (uint) portNo;
14+
-(void) writeToServer:(const uint8_t *) buf;
15+
16+
@end

IR Remote/APIHelper.m

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//
2+
// APIHelper.m
3+
// IR Remote
4+
//
5+
// Created by Eric Williamson on 4/18/13.
6+
// Copyright (c) 2013 Eric Williamson. All rights reserved.
7+
//
8+
9+
#import "APIHelper.h"
10+
11+
@implementation APIHelper
12+
13+
NSMutableData *data;
14+
15+
NSInputStream *iStream;
16+
NSOutputStream *oStream;
17+
18+
CFReadStreamRef readStream = NULL;
19+
CFWriteStreamRef writeStream = NULL;
20+
21+
-(void) connectToServerUsingCFStream:(NSString *) urlStr portNo: (uint) portNo {
22+
23+
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
24+
(__bridge CFStringRef) urlStr,
25+
portNo,
26+
&readStream,
27+
&writeStream);
28+
29+
if (readStream && writeStream) {
30+
CFReadStreamSetProperty(readStream,
31+
kCFStreamPropertyShouldCloseNativeSocket,
32+
kCFBooleanTrue);
33+
CFWriteStreamSetProperty(writeStream,
34+
kCFStreamPropertyShouldCloseNativeSocket,
35+
kCFBooleanTrue);
36+
37+
iStream = (__bridge NSInputStream *)readStream;
38+
[iStream setDelegate:self];
39+
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
40+
forMode:NSDefaultRunLoopMode];
41+
[iStream open];
42+
43+
oStream = (__bridge NSOutputStream *)writeStream;
44+
[oStream setDelegate:self];
45+
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
46+
forMode:NSDefaultRunLoopMode];
47+
[oStream open];
48+
}
49+
}
50+
51+
-(void) writeToServer:(const uint8_t *) buf {
52+
[oStream write:buf maxLength:strlen((char*)buf)];
53+
}
54+
55+
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
56+
57+
switch(eventCode) {
58+
case NSStreamEventHasBytesAvailable:
59+
{
60+
if (data == nil) {
61+
data = [[NSMutableData alloc] init];
62+
}
63+
uint8_t buf[1024];
64+
unsigned int len = 0;
65+
len = [(NSInputStream *)stream read:buf maxLength:1024];
66+
if(len) {
67+
[data appendBytes:(const void *)buf length:len];
68+
int bytesRead;
69+
bytesRead += len;
70+
} else {
71+
NSLog(@"No data.");
72+
}
73+
74+
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
75+
NSLog(str);
76+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"From server"
77+
message:str
78+
delegate:self
79+
cancelButtonTitle:@"OK"
80+
otherButtonTitles:nil];
81+
[alert show];
82+
83+
data = nil;
84+
} break;
85+
}
86+
87+
88+
}
89+
90+
@end
91+

IR Remote/NSStream+Helper.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// NSStream+Helper.h
3+
// IR Remote
4+
//
5+
// Created by Eric Williamson on 4/18/13.
6+
// Copyright (c) 2013 Eric Williamson. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
@interface NSStream (Helper)
12+
13+
+ (void)getStreamsToHostNamed:(NSString *)hostName
14+
port:(NSInteger)port
15+
inputStream:(NSInputStream **)inputStreamPtr
16+
outputStream:(NSOutputStream **)outputStreamPtr;
17+
18+
@end

IR Remote/NSStream+Helper.m

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// NSStream+Helper.m
3+
// IR Remote
4+
//
5+
// Created by Eric Williamson on 4/18/13.
6+
// Copyright (c) 2013 Eric Williamson. All rights reserved.
7+
//
8+
9+
#import "NSStream+Helper.h"
10+
11+
@implementation NSStream_Helper
12+
13+
+ (void)getStreamsToHostNamed:(NSString *)hostName
14+
port:(NSInteger)port
15+
inputStream:(NSInputStream **)inputStreamPtr
16+
outputStream:(NSOutputStream **)outputStreamPtr
17+
{
18+
CFReadStreamRef readStream;
19+
CFWriteStreamRef writeStream;
20+
21+
assert(hostName != nil);
22+
assert( (port > 0) && (port < 65536) );
23+
assert( (inputStreamPtr != NULL) || (outputStreamPtr != NULL) );
24+
25+
readStream = NULL;
26+
writeStream = NULL;
27+
28+
CFStreamCreatePairWithSocketToHost(
29+
NULL,
30+
(__bridge CFStringRef) hostName,
31+
port,
32+
((inputStreamPtr != nil) ? &readStream : NULL),
33+
((outputStreamPtr != nil) ? &writeStream : NULL)
34+
);
35+
36+
if (inputStreamPtr != NULL) {
37+
*inputStreamPtr = CFBridgingRelease(readStream);
38+
}
39+
if (outputStreamPtr != NULL) {
40+
*outputStreamPtr = CFBridgingRelease(writeStream);
41+
}
42+
}
43+
44+
@end

0 commit comments

Comments
 (0)