vicoapp / vico

Mac Programmers Text Editor
http://www.vicoapp.com/
658 stars 89 forks source link

Stream sockets #21

Closed martinh closed 12 years ago

martinh commented 12 years ago

This improves socket support in ViBufferedStream.

martinh commented 12 years ago

Now you can do stuff like this:

(class SockTest is NSObject
    (- (void)parse:(id)data is
        (set string (((NSString alloc) initWithData:data encoding:NSUTF8StringEncoding) autorelease))
        (NSLog "got data: #{string}"))

    ; stream delegate method
    (- (void)stream:(id)stream handleEvent:(int)event is
        (NSLog "got event #{event}")
        (case event
            (NSStreamEventHasBytesAvailable (self parse:(stream data)))
            (NSStreamEventHasSpaceAvailable (stream shutdownWrite))
            (NSStreamEventErrorOccurred (stream close))
            (NSStreamEventEndEncountered (stream close)))) )

(set $stream (ViBufferedStream streamWithHost:"www.google.com" port:"80"))
(set $delegate (SockTest new))
($stream setDelegate:$delegate)
($stream writeString:"GET / HTTP/1.0\r\n\r\n")
($stream schedule)
(NSLog "scheduled stream")
Shadowfiend commented 12 years ago

So when I wrote my close functions for NSStreams I did:

      [stream removeFromRunLoop:[NSRunLoop currentRunLoop]
                        forMode:NSDefaultRunLoopMode];

Is this not necessary or is it something that should probably be added to ViBufferedStream's close function?

Shadowfiend commented 12 years ago

Never mind, just spotted the comment about CFSocketInvalidate pulling things out of the run loop.

Shadowfiend commented 12 years ago

Ok, so I just switched out the NS*Stream stuff in vico-ensime for ViBufferedStream and everything is running smooth as butter. Looks great!