Unity IPv6 ?幾點注意事項:
1.unity版本: 4.7.2f1(此為我用的版本)
2.服務(wù)器IP地址 需要通過iOS原生代碼?getaddrinfo 做轉(zhuǎn)換
3.socket.LocalEndPoint ?socket.RemoteEndPoint (IPv6環(huán)境 ?訪問這兩個變量會報異常晒杈,需要做處理)
4.IOS9.2及以后的版本才支持IPv6(這個一定要注意)
Apple如何審核支持IPV6-Only?
首先第一點:這里說的支持IPV6-Only網(wǎng)絡(luò),其實就是說讓應(yīng)用在 IPv6 DNS64/NAT64 網(wǎng)絡(luò)環(huán)境下仍然能夠正常運行鲫剿。但是考慮到我們目前的實際網(wǎng)絡(luò)環(huán)境仍然是IPV4網(wǎng)絡(luò),所以應(yīng)用需要能夠同時保證IPV4和IPV6環(huán)境下的可用性。從這點來說,蘋果不會去掃描IPV4的專有API來拒絕審核通過斟赚,因為IPV4的API和IPV6的API調(diào)用都會同時存在于代碼中。
其次第二點:Apple官方聲明iOS9開始向IPV6支持過渡差油,在iOS9.2+支持IPV4地址合成IPV6地址拗军。其提供的Reachability庫在iOS8系統(tǒng)下,當(dāng)從IPV4切換到IPV6網(wǎng)絡(luò)蓄喇,或者從IPV6網(wǎng)絡(luò)切換到IPV4发侵,是無法監(jiān)控到網(wǎng)絡(luò)狀態(tài)的變化。也有一些開發(fā)者針對這些Bug詢問Apple的審核部門妆偏,給予的答復(fù)是只需要在蘋果最新的系統(tǒng)上保證IPV6的兼容性即可刃鳄。
最后第三點:只要應(yīng)用的主流程支持IPV6,通過蘋果審核即可钱骂。對于不支持IPV6的模塊叔锐,考慮到我們現(xiàn)實IPV6網(wǎng)絡(luò)的部署還需要一段時間挪鹏,短時間內(nèi)不會影響我們用戶的使用。但隨著4G網(wǎng)絡(luò)IPV6的部署愉烙,這部分模塊還是需要逐漸安排人力進行支持讨盒。
IOS代碼 ?.h 文件?(新建iOSipv6.h)
// ?iOSipv6.h
// ?Unity-iPhone
// ?Created by luzufei on 2017/3/2.
#ifndef iOSipv6_h
#define iOSipv6_h
@interface BundleId : NSObject
+(const char*)getIPv6:(const char*)mHost withPort:(const char* )mPort;
@end
#endif /* iOSipv6_h */
IOS代碼 .m文件?(新建iOSipv6.m)
//
// ?iOSipv6.m
// ?Unity-iPhone
// ?Created by luzufei on 2017/3/2.
#import "iOSipv6.h"
#include
#include
#include
#include
@implementation BundleId
#define MakeStringCopy( _x_ ) ( _x_ != NULL && [_x_ isKindOfClass:[NSString class]] ) ? strdup( [_x_ UTF8String] ) : NULL
const char* getIPv6(const char *mHost,const char *mPort)
{
if( nil == mHost )
return NULL;
const char *newChar = "No";
struct addrinfo* res0;
struct addrinfo hints;
struct addrinfo* res;
int n, s;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_DEFAULT;
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if((n=getaddrinfo(mHost, "http", &hints, &res0))!=0)
{
printf("getaddrinfo error: %s\n",gai_strerror(n));
return NULL;
}
struct sockaddr_in6* addr6;
struct sockaddr_in* addr;
NSString * NewStr = NULL;
char ipbuf[32];
s = -1;
for(res = res0; res; res = res->ai_next)
{
if (res->ai_family == AF_INET6)
{
addr6 =( struct sockaddr_in6*)res->ai_addr;
newChar = inet_ntop(AF_INET6, &addr6->sin6_addr, ipbuf, sizeof(ipbuf));
NSString * TempA = [[NSString alloc] initWithCString:(const char*)newChar
encoding:NSASCIIStringEncoding];
NSString * TempB = [NSString stringWithUTF8String:"&&ipv6"];
NewStr = [TempA stringByAppendingString: TempB];
printf("%s\n", newChar);
}
else
{
addr =( struct sockaddr_in*)res->ai_addr;
newChar = inet_ntop(AF_INET, &addr->sin_addr, ipbuf, sizeof(ipbuf));
NSString * TempA = [[NSString alloc] initWithCString:(const char*)newChar
encoding:NSASCIIStringEncoding];
NSString * TempB = [NSString stringWithUTF8String:"&&ipv4"];
NewStr = [TempA stringByAppendingString: TempB];
printf("%s\n", newChar);
}
break;
}
freeaddrinfo(res0);
printf("getaddrinfo OK");
NSString * mIPaddr = NewStr;
return MakeStringCopy(mIPaddr);
}
@end
Unity C#代碼
首先需要添加頭文件
#if UNITY_IPHONE
using System.Runtime.InteropServices;
#endif
定義接口
#if UNITY_IPHONE && !UNITY_EDITOR
[DllImport("__Internal")]
private static extern string getIPv6(string host, string port);
#endif
封裝接口
public static string GetIPv6(string host, string port)
{
#if UNITY_IPHONE && !UNITY_EDITOR
string ipv6 = getIPv6(host, port);
return ipv6;
#else
return host + "&&ipv4";
#endif
}
void getIPType(string serverIp, string serverPorts, out string newServerIp, out AddressFamily mIPType)
{
mIPType = AddressFamily.InterNetwork;
newServerIp = serverIp;
try
{
string mIpv6 = GetIPv6(serverIp, serverPorts);
if (!string.IsNullOrEmpty(mIpv6))
{
string[] m_StrTemp = System.Text.RegularExpressions.Regex.Split(mIpv6, "&&");
if (m_StrTemp != null && m_StrTemp.Length >= 2)
{
string IPType = m_StrTemp[1];
if (IPType == "ipv6")
{
newServerIp = m_StrTemp[0];
mIPType = AddressFamily.InterNetworkV6;
}
}
}
}
catch (Exception e)
{
DebugUtils.LogError("ipv6: " + e.Message);
}
}
Socket連接處添加的代碼(此為部分代碼片段,其余部分和ipv4相同)
#region ipv6
string newServerIp = "";
getIPType(server, port, out newServerIp, out mIpAddressFamily);
if (!string.IsNullOrEmpty(newServerIp))
{
server = newServerIp;
}
mSocket = new Socket(mIpAddressFamily, SocketType.Stream, ProtocolType.Tcp);
mIPs = Dns.GetHostAddresses(server);
mPort = int.Parse(port);
#endregion