Giter VIP home page Giter VIP logo

Comments (4)

alexeyxo avatar alexeyxo commented on August 16, 2024

CodedInputStream - this implementation "streams" in the official PB (Streaming).
CodedInputStream correctly separates message in byte-stream.
I always use CodedInputStream.

from protobuf-swift.

alexeyxo avatar alexeyxo commented on August 16, 2024

When you use NSInput/OutputStream, will be used in CodedInput/OutputStream. This is helper methods.

from protobuf-swift.

petersesztak avatar petersesztak commented on August 16, 2024

Perfect !

from protobuf-swift.

petersesztak avatar petersesztak commented on August 16, 2024

The standard way to build up a TPC connection we use:
NSStream.getStreamsToHostWithName(host, port: port, inputStream: &inputStream, outputStream: &outputStream)

Question: how to do that with CodedInputStream and CodedOutputStream streams as NSInputStream and NSOutputStream are privates in your implementation?
(e.g.
private var input:NSInputStream!
private var output:NSOutputStream!
)

Background: normally we use "inputStream!.delegate = ..., inputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode) ..., func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {... pattern to handle TCP events (like ErrorOccurred, OpenCompleted, HasBytesAvailable, HasSpaceAvailable, etc).

Do I have to use another approach in case of CodedInputStream, CodedOutputStream?
Best Regards,
Péter

more detailed implementation of our standard TCP streams as follows:

func connect(host: String, port: Int) {

    self.host = host
    self.port = port

    NSStream.getStreamsToHostWithName(host, port: port, inputStream: &inputStream, outputStream: &outputStream)

    if inputStream != nil && outputStream != nil {

        // Set delegate
        inputStream!.delegate = self
        outputStream!.delegate = self

        // Schedule
        inputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)
        outputStream!.scheduleInRunLoop(.mainRunLoop(), forMode: NSDefaultRunLoopMode)

        println("Start open()")

        // Open!
        inputStream!.open()
        outputStream!.open()
    }
}

// streams event handling:
func stream(aStream: NSStream, handleEvent eventCode: NSStreamEvent) {
if aStream === inputStream {
switch eventCode {
case NSStreamEvent.ErrorOccurred:
println("input: ErrorOccurred: (aStream.streamError?.description)")
break
case NSStreamEvent.EndEncountered:
println("input: EndEncountered")
break
case NSStreamEvent.None:
println("input: None")
break
case NSStreamEvent.OpenCompleted:
println("input: OpenCompleted")
break
case NSStreamEvent.HasBytesAvailable:
println("input: HasBytesAvailable")

             // Here you can `read()` from `inputStream`
            if ( aStream == inputStream){
            var buffer = [UInt8](count: 4096, repeatedValue: 0)
                            while (inputStream!.hasBytesAvailable){
                                var len = inputStream!.read(&buffer, maxLength: buffer.count)
                               // ...
                                }
                            }
            }
            break

        case NSStreamEvent.HasSpaceAvailable:
            println("input: HasSpaceAvailable")
            break
        case NSStreamEvent.allZeros:
            println("input: allZeros")
            break

        default:
            println("input: default break")
            break

        }
    }
    else if aStream === outputStream {
        switch eventCode {
        case NSStreamEvent.ErrorOccurred:
            println("output: ErrorOccurred: \(aStream.streamError?.description)")
            break
        case NSStreamEvent.EndEncountered:
            println("output: OpenCompleted")
            break
        case NSStreamEvent.None:
           println("output: None")
            break
        case NSStreamEvent.OpenCompleted:
            println("output: OpenCompleted")
            break
        case NSStreamEvent.HasBytesAvailable:
            println("output: HasBytesAvailable")
            break
        case NSStreamEvent.HasSpaceAvailable:
            // Here you can write() to `outputStream`
            break

        case NSStreamEvent.allZeros:
            println("output: allZeros")
            break

        default:
            println("output: default break")
            break
        }
    }
} 

from protobuf-swift.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.