Unity IPv6 Socket 支持

Unity IPv6  几点注意事项:

1.unity版本: 4.7.2f1(此为我用的版本)

2.服务器IP地址 需要通过iOS原生代码 getaddrinfo 做转换

3.socket.LocalEndPoint  socket.RemoteEndPoint (IPv6环境  访问这两个变量会报异常,需要做处理)

4.IOS9.2及以后的版本才支持IPv6(这个一定要注意)

Apple如何审核支持IPV6-Only?

首先第一点:这里说的支持IPV6-Only网络,其实就是说让应用在 IPv6 DNS64/NAT64 网络环境下仍然能够正常运行。但是考虑到我们目前的实际网络环境仍然是IPV4网络,所以应用需要能够同时保证IPV4和IPV6环境下的可用性。从这点来说,苹果不会去扫描IPV4的专有API来拒绝审核通过,因为IPV4的API和IPV6的API调用都会同时存在于代码中。

其次第二点:Apple官方声明iOS9开始向IPV6支持过渡,在iOS9.2+支持IPV4地址合成IPV6地址。其提供的Reachability库在iOS8系统下,当从IPV4切换到IPV6网络,或者从IPV6网络切换到IPV4,是无法监控到网络状态的变化。也有一些开发者针对这些Bug询问Apple的审核部门,给予的答复是只需要在苹果最新的系统上保证IPV6的兼容性即可。

最后第三点:只要应用的主流程支持IPV6,通过苹果审核即可。对于不支持IPV6的模块,考虑到我们现实IPV6网络的部署还需要一段时间,短时间内不会影响我们用户的使用。但随着4G网络IPV6的部署,这部分模块还是需要逐渐安排人力进行支持。

IOS IPv6   官方说明文档:https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html#//apple_ref/doc/uid/TP40010220-CH213-SW1

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

你可能感兴趣的:(Unity IPv6 Socket 支持)