jx_client_neibu/wb_unity_pro/Assets/Scripts/AppVest.cs

135 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<int>("init", accesskey, uuid);
}
public static int getServerIPAndPort(ref securityConnection conn, string host, int port)
{
AndroidJavaObject appvestObject = new AndroidJavaObject(packageName);
AndroidJavaObject connObject = appvestObject.CallStatic<AndroidJavaObject>("getServerIPAndPort", host, port);
AndroidJavaObject intObject = connObject.Call<AndroidJavaObject>("getServerPort");
conn.port = intObject.Call<int>("intValue");
conn.ip = connObject.Call<string>("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<string>("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 = "0.0.0.0";
conn.port = 80;
return 0;
}
public static int getClientIP(StringBuilder buffer, int buf_size) {
return 0;
}
#endif
}