ソースを参照

Merge pull request #498 from bassosimone/feature/api_refactoring

API refactoring
Miro 7 年 前
コミット
079b45c025

+ 10 - 4
ClientLibrary/PsiphonTunnel.go

@@ -51,7 +51,7 @@ var tunnel psiphonTunnel
 // Memory managed by PsiphonTunnel which is allocated in Start and freed in Stop
 var managedStartResult *C.char
 
-//export Start
+//export psiphon_tunnel_start
 //
 // ******************************* WARNING ********************************
 // The underlying memory referenced by the return value of Start is managed
@@ -110,10 +110,15 @@ var managedStartResult *C.char
 //     - https://github.com/Psiphon-Labs/psiphon-tunnel-core/blob/3d344194d21b250e0f18ededa4b4459a373b0690/MobileLibrary/Android/PsiphonTunnel/PsiphonTunnel.java#L371
 //   iOS:
 //     - https://github.com/Psiphon-Labs/psiphon-tunnel-core/blob/3d344194d21b250e0f18ededa4b4459a373b0690/MobileLibrary/iOS/PsiphonTunnel/PsiphonTunnel/PsiphonTunnel.m#L1105
-func Start(configJSON, embeddedServerEntryList, clientPlatform, networkID string, timeout int64) *C.char {
+func psiphon_tunnel_start(cConfigJSON, cEmbeddedServerEntryList, cClientPlatform, cNetworkID *C.char, timeout int64) *C.char {
 	// NOTE: all arguments which are still referenced once Start returns should be copied onto the Go heap
 	//       to ensure that they don't disappear later on and cause Go to crash.
 
+	configJSON := C.GoString(cConfigJSON)
+	embeddedServerEntryList := C.GoString(cEmbeddedServerEntryList)
+	clientPlatform := C.GoString(cClientPlatform)
+	networkID := C.GoString(cNetworkID)
+
 	// Load provided config
 
 	config, err := psiphon.LoadConfig([]byte(configJSON))
@@ -267,12 +272,13 @@ func Start(configJSON, embeddedServerEntryList, clientPlatform, networkID string
 	return managedStartResult
 }
 
-//export Stop
+//export psiphon_tunnel_stop
+//
 // Stop stops the controller if it is running and waits for it to clean up and exit.
 //
 // Stop should always be called after a successful call to Start to ensure the
 // controller is not left running.
-func Stop() {
+func psiphon_tunnel_stop() {
 	freeManagedStartResult()
 
 	if tunnel.stopController != nil {

+ 2 - 1
ClientLibrary/example/.gitignore

@@ -1 +1,2 @@
-main
+/main
+/server_list_compressed

+ 8 - 12
ClientLibrary/example/main.c

@@ -37,29 +37,25 @@ int main(int argc, char *argv[]) {
         printf("Using default config file: %s\n", default_config);
     }
 
-    char *file_contents = read_file(config);
-    if (!file_contents) {
+    char *psiphon_config = read_file(config);
+    if (!psiphon_config) {
         printf("Could not find config file: %s\n", config);
         return 1;
     }
 
-    GoString psiphon_config = {file_contents, strlen(file_contents)};
-
     // set server list
-    GoString serverList = {};
+    char *serverList = "";
 
     // set client platform
     char * const os = "OSName"; // "Android", "iOS", "Windows", etc.
     char * const os_version = "OSVersion"; // "4.0.4", "10.3", "10.0.10240", etc.
     char * const bundle_identifier = "com.example.exampleClientLibraryApp";
-    char * test_client_platform = (char *)malloc(sizeof(char) * (strlen(os) + strlen(os_version) + strlen(bundle_identifier) + 4)); // 4 for 3 underscores and null terminating character
+    char * client_platform = (char *)malloc(sizeof(char) * (strlen(os) + strlen(os_version) + strlen(bundle_identifier) + 4)); // 4 for 3 underscores and null terminating character
 
-    int n = sprintf(test_client_platform, "%s_%s_%s", os, os_version, bundle_identifier);
-    GoString client_platform = {test_client_platform, n};
+    int n = sprintf(client_platform, "%s_%s_%s", os, os_version, bundle_identifier);
 
     // set network ID
-    char * const test_network_id = "TEST";
-    GoString network_id = {test_network_id, strlen(test_network_id)};
+    char * const network_id = "TEST";
 
     // set timout
     long long timeout = 60;
@@ -67,13 +63,13 @@ int main(int argc, char *argv[]) {
     // connect 5 times
     for (int i = 0; i < 5; i++) {
         // start will return once Psiphon connects or does not connect for timeout seconds
-        char *result = Start(psiphon_config, serverList, client_platform, network_id, timeout);
+        char *result = psiphon_tunnel_start(psiphon_config, serverList, client_platform, network_id, timeout);
 
         // print results
         printf("Result: %s\n", result);
 
         // The underlying memory of `result` is managed by PsiphonTunnel and is freed in Stop
-        Stop();
+        psiphon_tunnel_stop();
     }
 }
 

+ 11 - 0
contributors/bassosimone.md

@@ -0,0 +1,11 @@
+2018-11-22
+
+I hereby agree to the terms of the "Psiphon Individual Contributor
+License Agreement", with MD5 checksum 83d54c85a43e0c0f416758779ea6740a.
+
+I furthermore declare that I am authorized and able to make this
+agreement and sign this declaration.
+
+Signed,
+
+Simone Basso https://github.com/bassosimone