Search Unity

NAT Traversal with UNET

Discussion in 'UNet' started by thegreatzebadiah, Mar 17, 2016.

Thread Status:
Not open for further replies.
  1. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    NAT-Traversal-for-UNET

    This an update to my previously posted NATPunchthroughClient

    Overview
    An example implementation of NAT Traversal combined with Unity's UNet HLAPI. Attempts to establish direct connections between players using a combination of NAT Punchthrough and automatic port forwarding. Falls back to UNET's relay servers only when absolutely necessary. When initially connecting the first connection to succeed is used, but if a direct connection succeeds after a relay connection is already established the direct connection will replace the relay connection.

    *Make sure to take a look at the README.*

    But Why?
    The primary motivation for creating this was so that I could be less dependent on Unity's relay servers. For one, they are bandwidth restricted, and they seem to introduce some pretty high latency. Plus it's just one more point of failure that us lowly developers have absolutely no control over. This solution should mean that connecting through Unity's relay servers is almost never necessary.

    Requirements
    Unity 5.2 or above
    RakNet and Open.NAT dll's are included for windows but you'll have to compiler your own for Mac / Linux

    Known Issues
    RakNet doesn't work with web sockets, so no web builds.
    Needs lots of testing

    Download
    NAT Traversal for UNET

    Source
    NAT Traversal for UNET

    I'm putting this out there for anyone to use. I'd really appreciate any feedback / testing that the community can provide. Another set of eyes always helps and I'm really not positive I've got everything set up 100% correctly. If you download the project and can't get it working please report back here and make sure to mention the router models involved. RakNet is a pretty mature library so if something goes wrong it's probably a problem in my code and probably something easily fixed, or you just happen to have one of the few router setups that punchthrough doesn't work on.

    You are welcome (and encouraged) to submit pull requests to the repo if you find yourself adding to or extending the functionality in useful ways.

    Thank you!
    https://www.patreon.com/Zebadiah
     
    Last edited: Mar 17, 2016
  2. GrymmyD

    GrymmyD

    Joined:
    Dec 2, 2015
    Posts:
    42
    Hey man - I'm totally with ya here, I would love this functionality. I spent a couple hours attempting to set up the example but didn't realize it was only for 5.4 (downloaded the beta today). I did successfully set up the RakNet server on AWS, etc, and stored that AMI off so I can resume playing with it later (I have to spend today working on my game, but I am highly interested in seeing where this is going and can definitely be a guinea pig to test this out over the next week or so).

    I would love it if a Unity representative could answer the following question:

    Is it legit to continue utilization of the matchmaking/discovery portion of the multiplayer unity services, but opt to punchthrough instead of using the relay (as I believe this example does), and thus incur costs? It would still require us to license Unity Pro to get the CCU we want, right? And we'd certainly want to fallback to using the relay if necessary... So it's not like we get our lunch entirely for free...
     
  3. GalacticGlum

    GalacticGlum

    Joined:
    Jul 30, 2015
    Posts:
    31
    Did the older NAT Punchthrough work with Unity 5.3.4? I'm on 5.3.4 and don't feel like testing my luck!
     
  4. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    Sorry no, both versions require 5.4. I'm working on fixing that right now. I just need to replicate the functionality of NetworkServerSimple which isn't available before 5.4
     
    GrymmyD likes this.
  5. GalacticGlum

    GalacticGlum

    Joined:
    Jul 30, 2015
    Posts:
    31
    Ahh, what does the "NetworkServerSimple" do?
     
  6. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    Just to let everyone know I just pushed an update so that 5.4 is no longer required. I've tested with 5.2.0f2 and 5.4.0b3. If anyone can confirm whether or not 5.3 works that would be awesome.

    Basically the same things that NetworkServer does except...simpler. All I really know is that I can use it to listen for connections on different ports and have the connections added to NetworkServer as they connect.
     
    GrymmyD likes this.
  7. GalacticGlum

    GalacticGlum

    Joined:
    Jul 30, 2015
    Posts:
    31
    I'll test it on 5.3.4f as soon as I get my project up and running (it keeps crashing).
     
    GrymmyD likes this.
Thread Status:
Not open for further replies.