HYP2P底层是用c语言实现的,这套p2p sdk中间件支持安卓,ios,windows平台。
在unity环境中要使用windows dll,或者安卓动态库so,ios lib库文件,首先需要根据sdk接口,生成一个C#类,导出需要使用到的接口函数。
先看HYP2P sdk的接口定义:
typedef void (*LOGFUN)(const char* str);
typedef void (*P2PCallback)(int session, int state );
typedef void (*P2PDataCallback)(int cmd, int subcmd, const void * pDataBuffer, int wDataSize);
//==================================================================
//用户app端和设备端都使用的api
//==================================================================
//login_domain - p2p服务器的域名或ip
//login_port - p2p服务器的端口号
//logfile - 本地日志文件的路径,utf8编码
P2PAPI_API int p2p_engine_init(const char* login_domain,int login_port, const char* logfile);
//p2p可选项定义:
#define P2P_OPTION_SINGLE_THREAD 0X0001 //单线程模式
//当前只有 P2P_OPTION_SINGLE_THREAD 一个选项,不推荐使用
P2PAPI_API void p2p_set_option(int opt);
P2PAPI_API void p2p_engine_destroy( );
//不推荐使用: 单线程模式下才需要在主循环中调用此函数
P2PAPI_API void p2p_engine_update( );
P2PAPI_API void setP2PLogCallback(LOGFUN f);
P2PAPI_API void setP2PCallBack(int nType, P2PCallback fn);
P2PAPI_API void setP2PDataCallback(int nType, P2PDataCallback fn);
// ipType=0 IPv4 or IPV6
// ipType=1 IPv4
// ipType=2 IPv6
P2PAPI_API int p2p_get_localip(char* ip, int buflen);
P2PAPI_API void p2p_set_localip(char* ip);
// level取值0-3,越大日志越详细
P2PAPI_API void p2p_set_log_level(int level);
P2PAPI_API int p2p_get_free_session();
P2PAPI_API int p2p_create_session(int session);
P2PAPI_API void p2p_close_session(int session);
#ifdef __cplusplus
P2PAPI_API void p2p_set_sink(int session, IP2PSessionSink* sink);
#endif
// return value: 0 is success, <0 is error
//加入p2p服务器,uid是自己的ID
P2PAPI_API int p2p_connect_server(int session, int64_t myuid,const char *secret);
P2PAPI_API int p2p_login_server(int session, int64_t uid);
// return value: <0 is error, 0 is timeout, >0 is success write byte count
//session
P2PAPI_API int p2p_send(int session, int channel, const void* buff, int len);
P2PAPI_API int p2p_sendtoserver(int session, unsigned short wMainCmdID, unsigned short wSubCmdID, const void * pData, unsigned short wDataSize);
//P2PAPI_API int p2p_lan_search(int session);
//
//1-p2p connnected
//2-relay
//0-p2p disconnnected
P2PAPI_API int p2p_get_state(int session);
//==================================================================
//用户app端使用的api
//==================================================================
// return value: <0 is error, >=0 is success
P2PAPI_API int p2p_connect_peer(int session, int64_t peerid);
//channel option
#define P2P_OPTION_TCP_MODEL 0X0001
#define P2P_OPTION_UDP_MODEL 0X0002
#define P2P_OPTION_UDP_WITH_PACKET_ORDER 0X0004 //不对udp包进行拆分
//channel的取值范围从0 — 100
P2PAPI_API int p2p_create_channel(int session,int channel,int channelOption);
P2PAPI_API void p2p_close_channel(int session,int channel);
// start a proxy to communicate with peer
// The data sent to the IP and port will be forwarded to ctx.peer_id
// ctx.func and ctx.func_param must be NULL
// return value:<0 is error, >=0 is success, proxyId is an output parameter
P2PAPI_API int p2p_start_proxy(int session, int port,int isApp);
P2PAPI_API void p2p_stop_proxy(int session);
//==================================================================
//设备端使用的api
//==================================================================
P2PAPI_API int p2p_set_device_port(int session, int port);
对应的c#类定义如下:
using System;
using System.Runtime.InteropServices;
using System.Text;
using XLua;
namespace P2PAPIDLL
{
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN || XLUA_GENERAL || (UNITY_WSA && !UNITY_EDITOR)
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int lua_CSFunction(IntPtr L);
#if GEN_CODE_MINIMIZE
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int CSharpWrapperCaller(IntPtr L, int funcidx, int top);
#endif
#else
public delegate int lua_CSFunction(IntPtr L);
#if GEN_CODE_MINIMIZE
public delegate int CSharpWrapperCaller(IntPtr L, int funcidx, int top);
#endif
#endif
public class P2PAPI
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void P2PLogCallback(string str);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void P2PCallback(int session, int state);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void P2PDataCallback(int cmd, int subcmd, IntPtr pDataBuffer, int wDataSize);
//tcp callback
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void TcpCallback(int state);
public const int P2P_SERVER_CONNECT = 0;
public const int P2P_SERVER_LOGIN = 1;
public const int P2P_HOLE_STATE = 2;
public const int P2P_CHANNEL_STATE = 3;
public const int P2P_PROXY_STATE = 4;
public const int P2P_DATA_SERVER = 0;
public const int P2P_DATA_P2P = 1;
// public const int P2P_DATA_CMD = 2;
#if (UNITY_IPHONE || UNITY_WEBGL || UNITY_SWITCH) && !UNITY_EDITOR
const string P2PDLL = "__Internal";
#else
const string P2PDLL = "haoyoup2p";
#endif
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public extern static void setP2PLogCallback(P2PLogCallback f);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public extern static void setP2PCallBack(int nType,P2PCallback f);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public extern static void setP2PDataCallback(int nType, P2PDataCallback f);
[DllImport(P2PDLL, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_engine_init(string login_domain,int login_port,string logfile);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void p2p_set_option(int opt);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void p2p_engine_update();
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void p2p_engine_destroy();
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_get_localip(byte[] ip, int len, int ipType);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void p2p_set_log_level(int level);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_get_free_session();
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_create_session(int session);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void p2p_close_session(int session);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void p2p_set_sink(int session, IntPtr sink);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_connect_server(int session, long uid, string secret);
[DllImport(P2PDLL, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_login_server(int session, long peerId);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_set_device_port(int session, int port);
// return value: <0 is error, >=0 is success
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_connect_peer(int session, long peerid);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_get_state(int session);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_send(int session, int channel, byte[] pDataBuffer, int wDataSize);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_create_channel(int session, int channel,int channelOption);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void p2p_close_channel(int session, int channel);
// start a proxy to communicate with peer
// The data sent to the IP and port will be forwarded to ctx.peer_id
// ctx.func and ctx.func_param must be NULL
// return value:<0 is error, >=0 is success, proxyId is an output parameter
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern int p2p_start_proxy(int session, int port);
[DllImport(P2PDLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void p2p_stop_proxy(int session);
}
}
安卓平台下,在unity项目的Plugins/Android/libs/armeabi-v7a目录下放入libhaoyoup2p.so
windows平台在Plugins\x86_64目录下放入haoyoup2p.dll
ios平台在Plugins\iOS目录下放入libhaoyoup2p.a
本文介绍了如何在Unity游戏中调用不同平台的动态库,如Windows的DLL和Android的SO库。通过创建C#类来封装SDK接口,并在相应平台的指定目录放置库文件,实现了HYP2P P2P SDK在Unity环境中的跨平台使用。
964

被折叠的 条评论
为什么被折叠?



