diff --git a/integrations/apple/example_app/OSX/Example_OSX_App.xcodeproj/xcuserdata/Joseph.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/integrations/apple/example_app/OSX/Example_OSX_App.xcodeproj/xcuserdata/Joseph.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 7ceefb0..eec5470 100644 --- a/integrations/apple/example_app/OSX/Example_OSX_App.xcodeproj/xcuserdata/Joseph.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/integrations/apple/example_app/OSX/Example_OSX_App.xcodeproj/xcuserdata/Joseph.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -10,27 +10,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Example_OSX_App/ViewController.swift" - timestampString = "489623400.349295" + timestampString = "490574990.533534" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "78" - endingLineNumber = "78" - landmarkName = "UI_Connect(_:)" - landmarkType = "5"> - - - - @@ -42,43 +26,11 @@ ignoreCount = "0" continueAfterRunningActions = "No" filePath = "Example_OSX_App/ViewController.swift" - timestampString = "489623686.632265" + timestampString = "490574990.533534" startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" - startingLineNumber = "137" - endingLineNumber = "137" - landmarkName = "UI_Bind(_:)" - landmarkType = "5"> - - - - - - - - @@ -94,9 +46,7 @@ startingColumnNumber = "9223372036854775807" endingColumnNumber = "9223372036854775807" startingLineNumber = "50" - endingLineNumber = "50" - landmarkName = "zts_socket(SOCKET_SIG)" - landmarkType = "7"> + endingLineNumber = "50"> diff --git a/integrations/apple/example_app/OSX/Example_OSX_App/Base.lproj/Main.storyboard b/integrations/apple/example_app/OSX/Example_OSX_App/Base.lproj/Main.storyboard index 0a9b861..316a5de 100644 --- a/integrations/apple/example_app/OSX/Example_OSX_App/Base.lproj/Main.storyboard +++ b/integrations/apple/example_app/OSX/Example_OSX_App/Base.lproj/Main.storyboard @@ -1,8 +1,8 @@ - + - + diff --git a/integrations/apple/example_app/OSX/Example_OSX_App/ViewController.swift b/integrations/apple/example_app/OSX/Example_OSX_App/ViewController.swift index 9b29101..659c37c 100644 --- a/integrations/apple/example_app/OSX/Example_OSX_App/ViewController.swift +++ b/integrations/apple/example_app/OSX/Example_OSX_App/ViewController.swift @@ -71,9 +71,10 @@ class ViewController: NSViewController { } } - // Connect to remote host on ZeroTier virtual network - @IBAction func UI_Connect(sender: AnyObject) { - + // CONNECT + var connect_thread : NSThread! + func attempt_connect() + { // TCP if(selectedProtocol == SOCK_STREAM) { @@ -103,8 +104,18 @@ class ViewController: NSViewController { } } - // Bind a ZeroTier socket - @IBAction func UI_Bind(sender: AnyObject) { + // Connect to remote host on ZeroTier virtual network + @IBAction func UI_Connect(sender: AnyObject) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { + self.connect_thread = NSThread(target:self, selector:"attempt_connect", object:nil) + self.connect_thread.start() + }); + } + + // BIND + var bind_thread : NSThread! + func attempt_bind() + { // TCP if(selectedProtocol == SOCK_STREAM) { @@ -146,6 +157,14 @@ class ViewController: NSViewController { } } + // Bind a ZeroTier socket + @IBAction func UI_Bind(sender: AnyObject) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { + self.bind_thread = NSThread(target:self, selector:"attempt_bind", object:nil) + self.bind_thread.start() + }); + } + // TX @IBOutlet weak var btnSend: NSButton! @IBAction func UI_SendData(sender: AnyObject) { @@ -170,10 +189,6 @@ class ViewController: NSViewController { if(selectedProtocol == SOCK_STREAM) { var buffer = [UInt8](count: 100, repeatedValue: 0) - let str = "GET / HTTP/1.0\r\n\r\n" - //let str = "Welcome to the machine" - print("strlen = %d\n", str.characters.count) - let encodedDataArray = [UInt8](str.utf8) read(accepted_sock, &buffer, 1024); print(buffer) @@ -185,6 +200,39 @@ class ViewController: NSViewController { } } + // Watch for incoming data + var rx_thread : NSThread! + func update_rx() { + while(true) + { + sleep(1) + // TCP + if(selectedProtocol == SOCK_STREAM) + { + var len = 32 + var buffer = [UInt8](count: len, repeatedValue: 0) + let n = read(accepted_sock, &buffer, len); + if(n > 0) + { + if let str = String(data: NSData(bytes: &buffer, length: len), encoding: NSUTF8StringEncoding) { + dispatch_async(dispatch_get_main_queue()) { + self.txtRX.stringValue = str + } + } else { + print("not a valid UTF-8 sequence") + } + } + } + // UDP + if(selectedProtocol == SOCK_DGRAM) + { + // recvfrom + } + } + } + + + // Built-in SOCKS5 Proxy Server Test func test_client_proxy_nsstream() { // For HTTP request @@ -216,16 +264,15 @@ class ViewController: NSViewController { outputStream?.write(encodedDataArray, maxLength: encodedDataArray.count) // RX - //sleep(5) - //inputStream?.read(&buffer, maxLength: 100) - //print("buffer = \(buffer)\n") + sleep(5) + inputStream?.read(&buffer, maxLength: 100) + print("buffer = \(buffer)\n") } var service_thread : NSThread! func ztnc_start_service() { // If you plan on using SOCKS Proxy, you don't need to initialize the RPC //start_service("/Users/Joseph/utest3") - // If you plan on using direct calls via RPC start_service_and_rpc("/Users/Joseph/utest3","565799d8f65063e5"); } @@ -244,6 +291,12 @@ class ViewController: NSViewController { self.service_thread = NSThread(target:self, selector:"ztnc_start_service", object:nil) self.service_thread.start() }); + + // Update UI on RX of data + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { + self.rx_thread = NSThread(target:self, selector:"update_rx", object:nil) + self.rx_thread.start() + }); } override var representedObject: AnyObject? { diff --git a/integrations/apple/example_app/iOS/Example_iOS_App/Base.lproj/Main.storyboard b/integrations/apple/example_app/iOS/Example_iOS_App/Base.lproj/Main.storyboard index b2b1fbe..ef01e0a 100644 --- a/integrations/apple/example_app/iOS/Example_iOS_App/Base.lproj/Main.storyboard +++ b/integrations/apple/example_app/iOS/Example_iOS_App/Base.lproj/Main.storyboard @@ -32,8 +32,7 @@ - - + diff --git a/integrations/apple/example_app/iOS/Example_iOS_App/ViewController.swift b/integrations/apple/example_app/iOS/Example_iOS_App/ViewController.swift index 79454ed..7e31c80 100644 --- a/integrations/apple/example_app/iOS/Example_iOS_App/ViewController.swift +++ b/integrations/apple/example_app/iOS/Example_iOS_App/ViewController.swift @@ -18,12 +18,11 @@ class ViewController: UIViewController { @IBOutlet weak var txtAddr: UITextField! @IBOutlet weak var txtPort: UITextField! - @IBOutlet weak var txtTX: UITextField! @IBOutlet weak var txtRX: UITextField! - @IBOutlet weak var btnTX: UIButton! - + @IBOutlet weak var btnConnect: UIButton! + @IBOutlet weak var btnBind: UIButton! @IBOutlet weak var btnRX: UIButton! @IBAction func UI_RX(sender: AnyObject) { @@ -33,12 +32,6 @@ class ViewController: UIViewController { if(selectedProtocol == SOCK_STREAM) { var buffer = [UInt8](count: 100, repeatedValue: 0) - let str = "GET / HTTP/1.0\r\n\r\n" - //let str = "Welcome to the machine" - print("strlen = %d\n", str.characters.count) - let encodedDataArray = [UInt8](str.utf8) - - // read(accepted_sock, UnsafeMutablePointer([txtTX.stringValue]), 128); read(accepted_sock, &buffer, 100); print(buffer) @@ -51,7 +44,6 @@ class ViewController: UIViewController { } - @IBAction func UI_TX(sender: AnyObject) { // Use ordinary read/write calls on ZeroTier socket @@ -93,9 +85,12 @@ class ViewController: UIViewController { break; } } - - @IBOutlet weak var btnConnect: UIButton! - @IBAction func UI_Connect(sender: AnyObject) { + + + // CONNECT + var connect_thread : NSThread! + func attempt_connect() + { // TCP if(selectedProtocol == SOCK_STREAM) { @@ -125,9 +120,18 @@ class ViewController: UIViewController { } } + // Connect to remote host on ZeroTier virtual network + @IBAction func UI_Connect(sender: AnyObject) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { + self.connect_thread = NSThread(target:self, selector:"attempt_connect", object:nil) + self.connect_thread.start() + }); + } - @IBOutlet weak var btnBind: UIButton! - @IBAction func UI_Bind(sender: AnyObject) { + // BIND + var bind_thread : NSThread! + func attempt_bind() + { // TCP if(selectedProtocol == SOCK_STREAM) { @@ -161,8 +165,52 @@ class ViewController: UIViewController { } print("accepted connection") } + + // UDP + if(selectedProtocol == SOCK_DGRAM) + { + + } + } + + // Bind a ZeroTier socket + @IBAction func UI_Bind(sender: AnyObject) { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { + self.bind_thread = NSThread(target:self, selector:"attempt_bind", object:nil) + self.bind_thread.start() + }); } + // Watch for incoming data + var rx_thread : NSThread! + func update_rx() { + while(true) + { + sleep(1) + // TCP + if(selectedProtocol == SOCK_STREAM) + { + var len = 32 + var buffer = [UInt8](count: len, repeatedValue: 0) + let n = read(accepted_sock, &buffer, len); + if(n > 0) + { + if let str = String(data: NSData(bytes: &buffer, length: len), encoding: NSUTF8StringEncoding) { + dispatch_async(dispatch_get_main_queue()) { + self.txtRX.text = str + } + } else { + print("not a valid UTF-8 sequence") + } + } + } + // UDP + if(selectedProtocol == SOCK_DGRAM) + { + // recvfrom + } + } + } // ZeroTier service thread var service_thread : NSThread! @@ -176,8 +224,11 @@ class ViewController: UIViewController { txtNWID.text = "565799d8f65063e5" txtTX.text = "welcome to the machine" - txtAddr.text = "10.9.9.203" + txtAddr.text = "0.0.0.0" + serverAddr = "0.0.0.0" txtPort.text = "8080" + serverPort = 8080 + selectedProtocol = SOCK_STREAM // ZeroTier Service thread @@ -185,7 +236,13 @@ class ViewController: UIViewController { self.service_thread = NSThread(target:self, selector:"ztnc_start_service", object:nil) self.service_thread.start() }); - + + // UI RX update + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), { + self.rx_thread = NSThread(target:self, selector:"update_rx", object:nil) + self.rx_thread.start() + }); + // Do any additional setup after loading the view, typically from a nib. } diff --git a/src/SDK_Debug.c b/src/SDK_Debug.c index 4e27fc5..7375fcf 100644 --- a/src/SDK_Debug.c +++ b/src/SDK_Debug.c @@ -98,20 +98,20 @@ void dwr(int level, const char *fmt, ... ) #elif defined(__APPLE__) pid_t tid = pthread_mach_thread_np(pthread_self()); #endif - //#if defined(SDK_DEBUG_LOG_TO_FILE) - //if(!debug_logfile) { // Try to get logfile from env - // debug_logfile = getenv("ZT_SDK_LOGFILE"); - //} + #if defined(SDK_DEBUG_LOG_TO_FILE) + if(!debug_logfile) { // Try to get logfile from env + debug_logfile = getenv("ZT_SDK_LOGFILE"); + } - //if(debug_logfile) { - FILE *file = fopen("/Users/Joseph/code/unity.log","a"); + if(debug_logfile) { + FILE *file = fopen(debug_logfile,"a"); fprintf(file, "%s [tid=%7d] ", timestring, tid); vfprintf(file, fmt, ap); fclose(file); va_end(ap); - //} + } - //#endif + #endif va_start(ap, fmt); fprintf(stderr, "%s [tid=%7d] ", timestring, tid); vfprintf(stderr, fmt, ap);