Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream sockets #21

Merged
merged 4 commits into from Jul 16, 2012
Merged

Stream sockets #21

merged 4 commits into from Jul 16, 2012

Conversation

martinh
Copy link
Member

@martinh martinh commented Jul 15, 2012

This improves socket support in ViBufferedStream.

Implements a generic socket initializer that supports any type of
socket. Add convenience methods that connect to a UNIX stream socket, or
a INET/INET6 stream socket.
Schedules the stream in the current run loop in the default mode.
@martinh
Copy link
Member Author

martinh commented Jul 15, 2012

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
Copy link
Member

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
Copy link
Member

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

@Shadowfiend
Copy link
Member

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

martinh added a commit that referenced this pull request Jul 16, 2012
Improve socket support in ViBufferedStream.
@martinh martinh merged commit b956e99 into master Jul 16, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants