Skip to content

Instantly share code, notes, and snippets.

@sunilsharma08
Forked from hotpaw2/MyURLStreamTask.swift
Created April 22, 2019 05:42
Show Gist options
  • Select an option

  • Save sunilsharma08/2120d3761f826ef25d6f7d15458e3b86 to your computer and use it in GitHub Desktop.

Select an option

Save sunilsharma08/2120d3761f826ef25d6f7d15458e3b86 to your computer and use it in GitHub Desktop.

Revisions

  1. @hotpaw2 hotpaw2 revised this gist Mar 11, 2017. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions MyURLStreamTask.swift
    Original file line number Diff line number Diff line change
    @@ -48,8 +48,8 @@ class MyURLStreamTask {
    guard socketRunning else { return }
    requestedBytes = bytes
    let minBytes = 1
    let maxBytes = bytes // 32768
    let timeout = TimeInterval(60.0)
    let maxBytes = bytes
    let timeout = TimeInterval(2.0)
    task?.readData(ofMinLength: minBytes, // read from socket
    maxLength: maxBytes,
    timeout: timeout, completionHandler:
    @@ -65,6 +65,7 @@ class MyURLStreamTask {
    }
    self.requestedBytes -= byteCount
    if self.requestedBytes > 0 {
    // call on main thread for low bandwidth needs
    self.myRead(bytes: self.requestedBytes)
    }
    }
  2. @hotpaw2 hotpaw2 revised this gist Mar 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MyURLStreamTask.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // MyURLStreamTask.swift
    // Demonstrates using a NSURLSessionStreamTask to implement a bidirectional TCP socket connection
    // Demonstrates using an NSURLSessionStreamTask to implement a bidirectional TCP socket connection
    //
    // by [email protected] 2017-03-07
    // distribution: BSD 2-clause
  3. @hotpaw2 hotpaw2 revised this gist Mar 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MyURLStreamTask.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // MyURLStreamTask.swift
    // Demonstrates using a NSURLSessionStreamTask to implement a bidirectional TCP connection
    // Demonstrates using a NSURLSessionStreamTask to implement a bidirectional TCP socket connection
    //
    // by [email protected] 2017-03-07
    // distribution: BSD 2-clause
  4. @hotpaw2 hotpaw2 revised this gist Mar 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MyURLStreamTask.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    // MyURLStreamTask.swift
    // Uses a URLSessionStreamTask to implement a bidirectional TCP connection using a NSURLSession
    // Demonstrates using a NSURLSessionStreamTask to implement a bidirectional TCP connection
    //
    // by [email protected] 2017-03-07
    // distribution: BSD 2-clause
  5. @hotpaw2 hotpaw2 revised this gist Mar 8, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions MyURLStreamTask.swift
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,8 @@
    // distribution: BSD 2-clause
    //

    import Foundation

    class MyURLStreamTask {

    let session = URLSession(configuration: .default)
  6. @hotpaw2 hotpaw2 created this gist Mar 8, 2017.
    85 changes: 85 additions & 0 deletions MyURLStreamTask.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,85 @@
    // MyURLStreamTask.swift
    // Uses a URLSessionStreamTask to implement a bidirectional TCP connection using a NSURLSession
    //
    // by [email protected] 2017-03-07
    // distribution: BSD 2-clause
    //

    class MyURLStreamTask {

    let session = URLSession(configuration: .default)
    var hostIPString = "127.0.0.1" // or set to whatever
    let port = 80 // as needed
    var task : URLSessionStreamTask? = nil
    var socketRunning = false
    var status = String()

    func startNetworkStreams() {
    // create a URLSessionStreamTask
    task = session.streamTask(withHostName: hostIPString, port: port)
    if task != nil {
    self.task?.resume() // start task running
    socketRunning = true
    status = ""
    }
    }

    var writeCounter = 0
    func sendCommand(_ flag : Int, dataBuffer : [Byte], len : Int ) { // write data to socket
    guard socketRunning else { return }
    let data = Data(bytes: dataBuffer, count: len)
    let timeout = TimeInterval(2.0)
    task?.write(data, timeout: timeout, completionHandler:
    { (err: Error?) -> Void in
    if err == nil {
    self.writeCounter += 1
    } else {
    self.status = err!.localizedDescription // display somewhere when error handling
    self.close()
    socketRunning = true
    }
    } )
    }

    var requestedBytes = 0
    func myRead(bytes : Int) { // call myRead periodically as needed
    guard socketRunning else { return }
    requestedBytes = bytes
    let minBytes = 1
    let maxBytes = bytes // 32768
    let timeout = TimeInterval(60.0)
    task?.readData(ofMinLength: minBytes, // read from socket
    maxLength: maxBytes,
    timeout: timeout, completionHandler:
    { (data: Data?, flag: Bool, err: Error?) -> Void in
    if err == nil {
    if let d = data {
    let byteCount = d.count
    var _ = d.withUnsafeBytes{ (buffer: UnsafePointer<UInt8>) -> Int in
    //
    // copy data buffer or call a delegate return function here
    //
    return 0
    }
    self.requestedBytes -= byteCount
    if self.requestedBytes > 0 {
    self.myRead(bytes: self.requestedBytes)
    }
    }
    } else {
    self.status = err!.localizedDescription // for error reporting
    self.close()
    socketRunning = true
    } } )
    }

    func close() {
    if task != nil && socketRunning {
    task!.closeRead()
    task!.closeWrite()
    }
    socketRunning = false
    }
    } // end of class MyURLStreamTask

    // eof