-
-
Save sunilsharma08/2120d3761f826ef25d6f7d15458e3b86 to your computer and use it in GitHub Desktop.
Revisions
-
hotpaw2 revised this gist
Mar 11, 2017 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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) } } -
hotpaw2 revised this gist
Mar 8, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ // MyURLStreamTask.swift // Demonstrates using an NSURLSessionStreamTask to implement a bidirectional TCP socket connection // // by [email protected] 2017-03-07 // distribution: BSD 2-clause -
hotpaw2 revised this gist
Mar 8, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 // // by [email protected] 2017-03-07 // distribution: BSD 2-clause -
hotpaw2 revised this gist
Mar 8, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 // // by [email protected] 2017-03-07 // distribution: BSD 2-clause -
hotpaw2 revised this gist
Mar 8, 2017 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) -
hotpaw2 created this gist
Mar 8, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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