2016-11-02 8 views
2

최근에 게임용 포트 매핑을 설정하려고했습니다. 이것은 플레이어 중 한 명을 호스트로 만드는 것입니다. 이를 위해서는 플레이어가 포트 7777을 열어야합니다.Unity .Net 2.0 UPNP 포트 매핑

이미 일치하는 항목이 있으면 간단하게 참여할 수 있습니다. 비어있는 성냥을 찾지 못하면 단순히 성냥을 만듭니다. 그리고 그것이 하나가되지 못하면 누군가가 그것을 만들 때까지 기다릴 것입니다.

그 때문에 포트를 포트 할 때 UPnP와 같은 것을 사용할 수 있어야합니다. 나는 또한 성냥 만들기를 진행하거나 간단히 기다리고 오류를 잡으려고 오류를 잡을 수 있어야합니다.

현재 .NET 2.0을 사용하는 Unity 게임 엔진에서 작업 중입니다. 이것은 Open.NAT가 호환되지 않기 때문에 매우 제한적입니다. 나는 Mono를 시도했다 .NAT 그러나 나는 그것을 작동시킬 수 없다.

아무에게도이 접근 방법에 대한 제안이 있습니까? 어떤 라이브러리를 사용하고 어쩌면 나를 시작할 수있는 코드 스 니펫을 제공 할 수도 있습니다.

감사합니다. TwoTen.

EDIT1 : 현재 Mono.NAT 코드는 다음과 같습니다

private void DeviceFound(object sender, DeviceEventArgs args) 
{ 
    Debug.Log("1"); 
    INatDevice device = args.Device; 
    Debug.Log("2"); 
    Mapping map = new Mapping(Protocol.Tcp, 6699, 6699); 
    Debug.Log("3"); 
    device.CreatePortMap(map); 
    Debug.Log("4"); 
    int test = device.GetAllMappings().Length; 
    Debug.Log(test); 
    foreach (Mapping portMap in device.GetAllMappings()) 
    { 
     Debug.Log("5"); 
     Debug.Log(portMap.ToString()); 
    } 
} 
private void DeviceLost(object sender, DeviceEventArgs args) 
{ 
    INatDevice device = args.Device; 
    Mapping map = new Mapping(Protocol.Tcp, 6699, 6699); 
    device.DeletePortMap(map); 
} 

나가라고 마지막 디버그 문이 나는 포트가 개방되지 않습니다 4 번이고 더 excetion 중 하나를 발생하지 않습니다. 내가 뭘 잘못하고 있니?

또한, 내 스타트 기능에서 나는이 전화 :

NatUtility.DeviceFound += DeviceFound; 
NatUtility.DeviceLost += DeviceLost; 
NatUtility.StartDiscovery(); 
+0

"Mono.NAT를 시도했지만 작동하지 않습니다"라는 코드를 게시하십시오. 이것은 나를 위해 작동합니다. 당신이하는 일을 볼 필요가 있습니다. 또한 작동하지 않는다는 것을 어떻게 알 수 있습니까? – Programmer

+0

내 코드를 포함하도록 게시물을 업데이트했습니다. – TwoTen

+0

'Debug.Log (test);와'Debug.Log ("4 ++"+ test);를 다시 실행 한 다음 질문을 다시 Log로 업데이트하십시오. 또한, Mono.NAT DLL은 어디에 두었습니까? – Programmer

답변

1

내가 어떻게 성공적으로에 대한 답을 발견했다. 나는 Mono를 떨어 뜨렸다 .NAT는 대신 Open으로 갔다 .NAT는 Unity와 호환되는 .NET 3.5로 포팅되었다! 라우터 포트를 등록하고 열립니다

Test().Wait(); 

:

private static Task Test() 
{ 
    var nat = new NatDiscoverer(); 
    var cts = new CancellationTokenSource(); 
    cts.CancelAfter(5000); 

    NatDevice device = null; 
    var sb = new StringBuilder(); 
    IPAddress ip = null; 

    return nat.DiscoverDeviceAsync(PortMapper.Upnp, cts) 
     .ContinueWith(task => 
     { 
      device = task.Result; 
      return device.GetExternalIPAsync(); 

     }) 
     .Unwrap() 
     .ContinueWith(task => 
     { 
      ip = task.Result; 
      sb.AppendFormat("\nYour IP: {0}", ip); 
      return device.CreatePortMapAsync(new Mapping(Protocol.Tcp, 7777, 7777, 0, "myGame Server (TCP)")); 
     }) 
     .Unwrap() 
     .ContinueWith(task => 
     { 
      return device.CreatePortMapAsync(
       new Mapping(Protocol.Udp, 7777, 7777, 0, "myGame Server (UDP)")); 
     }) 
     .Unwrap() 
     .ContinueWith(task => 
     { 
      sb.AppendFormat("\nAdded mapping: {0}:1700 -> 127.0.0.1:1600\n", ip); 
      sb.AppendFormat("\n+------+-------------------------------+--------------------------------+------------------------------------+-------------------------+"); 
      sb.AppendFormat("\n| PORT | PUBLIC (Reacheable)   | PRIVATE (Your computer)  | Description      |       |"); 
      sb.AppendFormat("\n+------+----------------------+--------+-----------------------+--------+------------------------------------+-------------------------+"); 
      sb.AppendFormat("\n|  | IP Address   | Port | IP Address   | Port |         | Expires     |"); 
      sb.AppendFormat("\n+------+----------------------+--------+-----------------------+--------+------------------------------------+-------------------------+"); 
      return device.GetAllMappingsAsync(); 
     }) 
     .Unwrap() 
     .ContinueWith(task => 
     { 
      foreach (var mapping in task.Result) 
      { 
       sb.AppendFormat("\n| {5} | {0,-20} | {1,6} | {2,-21} | {3,6} | {4,-35}|{6,25}|", 
        ip, mapping.PublicPort, mapping.PrivateIP, mapping.PrivatePort, mapping.Description, 
        mapping.Protocol == Protocol.Tcp ? "TCP" : "UDP", mapping.Expiration.ToLocalTime()); 
      } 
      sb.AppendFormat("\n+------+----------------------+--------+-----------------------+--------+------------------------------------+-------------------------+"); 
      sb.AppendFormat("\n[Removing TCP mapping] {0}:1700 -> 127.0.0.1:1600", ip); 
      return device.DeletePortMapAsync(new Mapping(Protocol.Tcp, 1600, 1700)); 
     }) 
     .Unwrap() 
     .ContinueWith(task => 
     { 
      sb.AppendFormat("\n[Done]"); 
      Debug.Log(sb.ToString()); 
     }); 
} 

내가해야 할 일을했을 모든 내 스타트 기능에서이 작업을 수행하는 것이 었습니다 :

마지막 코드는 I는 다음과 같습니다를 사용하여 종료 그것. 하나. 웹 사이트 canyouseeme.org는 항구가 열리는 이유를 위해 항구를 열지 못했습니다. 등록되었는지 확인하기 위해 라우터 설정으로 들어가서 UPnP를 찾았습니다. 내 응용 프로그램 목록이있었습니다. 아직 실제 테스트를 수행하고 있지는 않지만 일단 테스트가 완료되면 업데이트 할 예정입니다.

편집 : 유니티와 호환

Open.NAT 버전은 여기에서 발견 된 지금은 실제 테스트를 수행했습니다. 모든 것이 작동하고 사용자는 중계 서버 또는 단일성 중매 서버없이 연결할 수 있습니다.