Skip to content

Instantly share code, notes, and snippets.

@linktoming
Forked from rjungemann/Communicator.h
Created February 1, 2016 08:51
Show Gist options
  • Save linktoming/536955be6b8ec545a3e3 to your computer and use it in GitHub Desktop.
Save linktoming/536955be6b8ec545a3e3 to your computer and use it in GitHub Desktop.

Revisions

  1. @rjungemann rjungemann revised this gist Jun 21, 2010. 3 changed files with 4 additions and 21 deletions.
    9 changes: 0 additions & 9 deletions Communicator.h
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,5 @@
    //
    // Communicator.h
    // StreamExample
    //
    // Created by Roger Jungemann on 6/20/10.
    // Copyright 2010 __MyCompanyName__. All rights reserved.
    //

    #import <Foundation/Foundation.h>


    @interface Communicator : NSObject <NSStreamDelegate> {
    @public

    8 changes: 0 additions & 8 deletions Communicator.m
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,3 @@
    //
    // Communicator.m
    // StreamExample
    //
    // Created by Roger Jungemann on 6/20/10.
    // Copyright 2010 __MyCompanyName__. All rights reserved.
    //

    #import "Communicator.h"

    CFReadStreamRef readStream;
    8 changes: 4 additions & 4 deletions StreamExample.m
    Original file line number Diff line number Diff line change
    @@ -3,17 +3,17 @@
    #import "Communicator.h"

    int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Communicator *c = [[Communicator alloc] init];
    Communicator *c = [[Communicator alloc] init];

    c->host = @"http://127.0.0.1";
    c->port = 6789;

    [c setup];
    [c open];

    [pool drain];
    [pool drain];

    return 0;
    return 0;
    }
  2. @rjungemann rjungemann created this gist Jun 21, 2010.
    26 changes: 26 additions & 0 deletions Communicator.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    //
    // Communicator.h
    // StreamExample
    //
    // Created by Roger Jungemann on 6/20/10.
    // Copyright 2010 __MyCompanyName__. All rights reserved.
    //

    #import <Foundation/Foundation.h>


    @interface Communicator : NSObject <NSStreamDelegate> {
    @public

    NSString *host;
    int port;
    }

    - (void)setup;
    - (void)open;
    - (void)close;
    - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event;
    - (void)readIn:(NSString *)s;
    - (void)writeOut:(NSString *)s;

    @end
    131 changes: 131 additions & 0 deletions Communicator.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,131 @@
    //
    // Communicator.m
    // StreamExample
    //
    // Created by Roger Jungemann on 6/20/10.
    // Copyright 2010 __MyCompanyName__. All rights reserved.
    //

    #import "Communicator.h"

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;

    NSInputStream *inputStream;
    NSOutputStream *outputStream;

    @implementation Communicator

    - (void)setup {
    NSURL *url = [NSURL URLWithString:host];

    NSLog(@"Setting up connection to %@ : %i", [url absoluteString], port);

    CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, (CFStringRef)[url host], port, &readStream, &writeStream);

    if(!CFWriteStreamOpen(writeStream)) {
    NSLog(@"Error, writeStream not open");

    return;
    }
    [self open];

    NSLog(@"Status of outputStream: %i", [outputStream streamStatus]);

    return;
    }

    - (void)open {
    NSLog(@"Opening streams.");

    inputStream = (NSInputStream *)readStream;
    outputStream = (NSOutputStream *)writeStream;

    [inputStream retain];
    [outputStream retain];

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];
    }

    - (void)close {
    NSLog(@"Closing streams.");

    [inputStream close];
    [outputStream close];

    [inputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream setDelegate:nil];
    [outputStream setDelegate:nil];

    [inputStream release];
    [outputStream release];

    inputStream = nil;
    outputStream = nil;
    }

    - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)event {
    NSLog(@"Stream triggered.");

    switch(event) {
    case NSStreamEventHasSpaceAvailable: {
    if(stream == outputStream) {
    NSLog(@"outputStream is ready.");
    }
    break;
    }
    case NSStreamEventHasBytesAvailable: {
    if(stream == inputStream) {
    NSLog(@"inputStream is ready.");

    uint8_t buf[1024];
    unsigned int len = 0;

    len = [inputStream read:buf maxLength:1024];

    if(len > 0) {
    NSMutableData* data=[[NSMutableData alloc] initWithLength:0];

    [data appendBytes: (const void *)buf length:len];

    NSString *s = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    [self readIn:s];

    [data release];
    }
    }
    break;
    }
    default: {
    NSLog(@"Stream is sending an Event: %i", event);

    break;
    }
    }
    }

    - (void)readIn:(NSString *)s {
    NSLog(@"Reading in the following:");
    NSLog(@"%@", s);
    }

    - (void)writeOut:(NSString *)s {
    uint8_t *buf = (uint8_t *)[s UTF8String];

    [outputStream write:buf maxLength:strlen((char *)buf)];

    NSLog(@"Writing out the following:");
    NSLog(@"%@", s);
    }

    @end
    19 changes: 19 additions & 0 deletions StreamExample.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    #import <Foundation/Foundation.h>

    #import "Communicator.h"

    int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Communicator *c = [[Communicator alloc] init];

    c->host = @"http://127.0.0.1";
    c->port = 6789;

    [c setup];
    [c open];

    [pool drain];

    return 0;
    }