using System.Runtime.InteropServices; using System.Text; using System.Net.Sockets; using System.Net; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; [StructLayout(LayoutKind.Sequential)] public struct securityConnection { [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public string ip; public int port; } public class AppVest { #if UNITY_STANDALONE_WIN //SDK初始化,成功返回0,失败返回-1 [DllImport("appvest")] public static extern int init(string accesskey, string uuid); [DllImport("appvest")] public static extern int getServerIPAndPort(ref securityConnection conn, string host, int port); //获取客户端真实IP [DllImport("appvest")] public static extern int getClientIP(StringBuilder buffer, int buf_size); #elif UNITY_ANDROID // 将jar文件放如Assets/Plugins/Android目录下 private const string packageName = "com.cf.msc.sdk.AppVest"; public static int init(string accesskey, string uuid) { AndroidJavaObject appvestObject = new AndroidJavaObject(packageName); return appvestObject.CallStatic("init", accesskey, uuid); } public static int getServerIPAndPort(ref securityConnection conn, string host, int port) { AndroidJavaObject appvestObject = new AndroidJavaObject(packageName); AndroidJavaObject connObject = appvestObject.CallStatic("getServerIPAndPort", host, port); AndroidJavaObject intObject = connObject.Call("getServerPort"); conn.port = intObject.Call("intValue"); conn.ip = connObject.Call("getServerIp"); if (conn.ip != "" && conn.port != -1) { return 0; } return -1; } public static int getClientIP(StringBuilder buffer, int buf_size) { AndroidJavaObject appvestObject = new AndroidJavaObject(packageName); string clientip = appvestObject.CallStatic("getClientIP"); buffer.Append(clientip); return 0; } #elif UNITY_IOS [DllImport("__Internal")] static extern int _Init(string key, string id); [DllImport("__Internal")] static extern string _GetServerIPAndPort(string host, int id); public static int init(string key, string id) { return _Init(key, id); } public static int getServerIPAndPort(ref securityConnection conn, string host, int port) { string currentip=_GetServerIPAndPort(host, port); if(!string.IsNullOrEmpty(currentip)) { int rc=LoadNetP(ref conn, currentip); if(rc==0) { return rc; } } return 1; } private static int LoadNetP(ref securityConnection conn, string host) { if (!string.IsNullOrEmpty(host)) { string[] str = host.Split(':'); string port = str[str.Length - 1]; int len = str[str.Length - 1].IndexOf("/"); if (len > 0) { port = str[str.Length - 1].Substring(0, len); } int curInt = System.Convert.ToInt32(port); string ip = str[str.Length - 2]; if (str.Length > 2) { ip = str[str.Length - 1].Substring(2); } conn.ip = ip; conn.port = curInt; return 0; } return 1; } #else public static int init(string accesskey, string uuid) { return 0; } public static int getServerIPAndPort(ref securityConnection conn, string host, int port) { conn.ip = "59.56.79.100"; conn.port = 80; return 0; } public static int getClientIP(StringBuilder buffer, int buf_size) { return 0; } #endif }