Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raknet Dedicate Server - Help

Discussion in 'Multiplayer' started by digitalwm, Sep 30, 2009.

  1. digitalwm

    digitalwm

    Joined:
    Sep 21, 2009
    Posts:
    12
    Hi everybody,

    I am digging for 3 days on how to create a dedicated server for Unity games. I have tried SmartFox but seems to complex for what I want and I thought that I can don that in Raknet directly.

    All I want is to have a number of RPC registered in server and also in Clients, and call them through the server. So when from a Client I will call "X" RPC it will be invoked in the server and on all other clients, except the caller.

    I have tried studding "MasterServer" concept (Nothing helpful - except that I see a connection.
    I have tried from RAKNET downloaded samples and compiled the RPC examples, but in this case the UNITY clients don't connect.

    Any help, links or ideas will be welcomed.
     
  2. oxl

    oxl

    Joined:
    Nov 21, 2008
    Posts:
    325
    Hi,
    a Master Server is usually for finding other game servers on the net, so you can skip this at the moment.

    If you are using pure RakNet on the server side, you should use pure RakNet on the client side , too. (Though I don't know via .dll or the c# port somewhere).

    If you want to use built in Unity networking, you might take a look at the network examples in the resource section.

    Sending RPCs via Unitys built in network stuff is rather easy to implement.

    --
    oxl
     
  3. digitalwm

    digitalwm

    Joined:
    Sep 21, 2009
    Posts:
    12
    Hi,

    I agree and know that Master Server is not for dedicated server, but was a point of inspiration (I hoped so).

    And about sending RPC via Unity I used them and worked very nice. The issue is that I need to build a more reliable server, than building a unity app as server. And I looked for raknet samples that will cope with unity.

    Another solution for me would be to build my own server with my own network layer. But I can't find anywhere the protocol that Unity is using for networking. And by that I mean not only how datagrams are composed but what is the flow of a connection. Like in FTP (who opens conection, what reply is expected, what should then be sent and so on).
     
  4. oxl

    oxl

    Joined:
    Nov 21, 2008
    Posts:
    325
    I would love to see Unitys RakNet implementation exposed for us, too, but :cry: ...

    Btw, has anyone ever used the RakNet C# port within Unity ?

    --
    oxl
     
  5. cj-currie

    cj-currie

    Joined:
    Nov 27, 2008
    Posts:
    336
    Likewise I wonder: how would one approach a custom networking layer? The networking guys in my studio don't know where to start, either.
     
  6. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Well downloading raknet directly won't work with unity. They have a custom build based on an older version. Second, you need to program in Xcode and c++ or make a build that compiles on both. You also will need to structure your own masterserver around what ever version of raknet you choose to use. Of course this is only the begining.
     
  7. digitalwm

    digitalwm

    Joined:
    Sep 21, 2009
    Posts:
    12
    Hi zumwalt, can you please help us, and provide the link where we can find the older version of racknet that works with unity, and maybe some samples how to add on it. I saw your posts around this forum and I saw you might have the most experience with it.

    Currently I am not interested in building a Master Server, but just to get into the networking (the dedicate server side)

    Thanks.

    PS: I will send you some beer in exchange of information :D
     
  8. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Sure, let me break out my MacBook pro and grab the only know build that works with Unity's calls. I should have the XCode Carbon Bundle code for building up the library and I think I built it also for VS 2003 on that same machine, once I grab that and post it, I'll go back and look through Jenkins archives for the original beta source.

    EDIT:
    Ok here is a simple XCode Project with the correct version of RakNet to get you started, just unzip it, open the project, click on build/go and then open up the Console window so you can visually see the connection. I'll post some screen shots of information in a minute, but this is to simple, the key will be for you to translate their enumeration for the packets then decipher the rpc elements. All I have dug out of them so far is the enumerators, I haven't received any information from them about the serialized class that they are using to pass across the information to the server that is then passed around to the clients. That is uber top secret material. You will have to buy the source of Unity to get that uber material. All hush hush internal stuff.

    This will, however, let you use their Network.Connect and send, so enjoy.
     

    Attached Files:

  9. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Oxl, yes, I have done a C# port of RakNet however it won't work with Unity, and I haven't heard back from the wonderful individual I was working on, last I heard from him was well over a month ago. He did an awesome job keeping me in the loop on working through a crash issue, then he got uber busy internally and had passed it on to another tech, but it literally died at that point, with him busy and the tech non-responsive, I just take it that they are fully concentrating on 2.6 and will get back with me after it is released, that or it really has died.

    Anyway, for this to work, after you click on build/go, you simply create a C# script file in Unity, call it what ever you want, add it to an ego, and click play, watch the XCode console to see the physical connection happen. The rest is an open book.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TestConnector : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         Network.Connect("127.0.0.1",10000);
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.    
    15.     }
    16. }
    17.  
    FYI, I have this also working in a cluster, I have only tested 2 remote servers, one in France, one in Italy then with 3 local servers including code that has multiple connections from one client to multiple servers and multiple clients across servers talking to multiple clients, it all works, but this is the bare bone skeleton, not the aforementioned changes.
     
  10. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    And here is the screen print of what you should see on the console when you run it, you will see that ID_NEW_INCOMMING_CONNECTION followed by a K. The K came from Unity during the call to connect, that is what was in the packet. FYI you will need to use RakNet for the send and receive, until they add or IF they add in the pass through I requested in the "wish list" to allow us to send custom packets back to have them be handled, I use my own interface using this skeleton framework as it is. Otherwise you will see the error which is image number 2.

    I have actually offered to help them with this on many levels, but the "thanks but no thanks, we got this covered and nailed" was the response I got *shrugs* look through the wish list and vote on my networking wishes :)
     

    Attached Files:

  11. digitalwm

    digitalwm

    Joined:
    Sep 21, 2009
    Posts:
    12
    Thanks for all the information gathered in one point, so what I understood from you is that Unity have it's own add-on on the raknet layer, so anything that we would try to do we would need those "secret" information. All clear for me. So for now I will try a different approach.

    Does anyone knows if i use RPC connection from Unity-Unity and the server would contain no objects just the scripts for server and the update would be occasional, would still the server be limited to somewhere 32 connections?
     
  12. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Highly doubtful. The less information you are planning to send between the clients, the more connections you can support. The default server startup has 1000 connections defined, you can change this value when you start the server. The formula is really straight forward to figure out your load.

    Logic:
    < 32 players, lots of info
    > 32 players, limit info as much as possible

    Keep player count max to 64
    Another way to look at it:
    <32 players, prediction should be ok at a low number in time
    >32 players <64 players, prediction should be set to a higher number in time because of delays and packet loss increases

    Don't do > 64 players without your own backbone.
     
  13. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    *scratches head*
    Ok, so get this, I was tinkering more with my wrapper trying to get more speed out of it and trying to do some packet tracking with it last night until about 3am (wife didn't like me much, lying in bed with MacBook Pro chugging on C++ RakNet 3.7 code) When it suddenly dawned on me, why deal with wrapping this thing up since Raknet is unmanaged library anyway.

    And I was thinking more of your issue and trying to find a cleaner way to handle this which gives more power to C# that what it currently has, which would then also allow the latest and greatest version of RakNet to be usable with Unity. I am not one for having to re-invent the wheel on every new release (and obviously neither is Unity since the core is based on 3.0beta)

    So after only 3 hours sleep, and now going on 2 days working on this, I was able to switch Raknet 3.7 to a managed C++ library and static library, the static won't be of any use without a wrapper, but the dll... well, all of the core changes I made allowed me to do something I wasn't sure was possible without pinvoking or marshalling, I was able to simply add a reference to the managed debug library.. see image below:
    (yes, this is a managed C++ library of RakNet 3.7 in VS 2005 C# windows forms project without a wrapper or using dllimport)
     

    Attached Files:

  14. digitalwm

    digitalwm

    Joined:
    Sep 21, 2009
    Posts:
    12
    :)
    Nice for keeping the discussion open, I would really like to get this topic on a permanent going, on how you and maybe me and other can develop a possibility to implement a dedicated server for Unity.

    So if I got this right you managed to gather the Raknet into a library that can be used when developing server application?

    Question? Have you ever tried to use a Network sniffer to dig the UDP data grams between two Unity (Server / Client). Because I've seen that there are specs on Raknet page for the data gram structure.

    Cheers Dan.
     
  15. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Not yet, I have been using WireShark to help with packet identification / assembly / disassembly, trying to figure out the datastructure use so that I can come up with a structure that matches theirs without having to keep bugging them for the integration. One thing you have to keep in mind is that they have some sort of generalized serializer that takes any object and serializes it, then send it as a bitstream on a broadcast channel. The channels seem to only be limited to what player ID's match that channel, so that your broadcast only goes to people within the same range. (probably not explaining that right, everyone around here absolutely knows I can't explain things right, I just know how to 'do' things right)

    http://www.wireshark.org/

    I'll have another screen shot to post here in a little bit, I'll keep it in this thread so that since it is all specific to what you are wanting to do with a stand alone raknet server and Unity using native raknet with built in Unity, that is the goal. The problem, as some know and some don't, Jenkins changes the packet structure from major release to major release (well .X is major) so 3.0beta was not compatible with 3.1, etc, like 3.5 isn't compatible with 3.7.

    My thought is, if I can keep outside of the "box" of packet issues, all I have to do is deal with the serialization routine and the RPC structure in the code, going through every release to see what has changed on the BYTE level, thats all that matters to me. In about 30 minutes or less I will post a new shot showing the manifest of public methods showing in the browser tree in C# object browser, this is getting mighty interesting on my machine right now.
     
  16. digitalwm

    digitalwm

    Joined:
    Sep 21, 2009
    Posts:
    12
    Hi looking forward for your research (tomorrow) sleep required. Currently I am working on releasing a Iphone App, but after that I will do some Network Sniffing and digging for the protocol.

    The idea is that if you know what you are sending and what you are receiving, you can ignore the data layer for now, and just figure out the protocol and logistics flow. Because I guess when logging into a server and call a RPC function, there are some flags sent and received before sending serialized data. And serialization what I know is done in the same way in all languages.
     
  17. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    My original was a port using SWIG based on the 3.0beta build, but for some reason Unity crashes hard against that c# library build, not sure why. I have about 200 more files go comb through right now so the entire conversion process will take me a few more hours, going to take a break here in about an hour. All that matters is the bitstream information and the serializer. Even if I never figure out their structure, it will be fine since this new library will be a managed library and I am presuming right now based on current testing that this should work on both Unity Pro and Unity Indy without the need for the C++ piece at all. Once I am done with the conversion, I'll post a link to the built debug version of the library so that someone with Indy can test it. I'll even put up a Unity Package that can be used as the client and a C# project that can be used as the server.

    The key element here is "if" this works with Mono, which so far I don't see why not, mono works fine with managed assemblies, it is the unmanaged ones it has issues with. I'll edit this post when my machine gets through compiling this lastest build with that screen shot I promised.

    Edit: as promised, current screen shot of making the classes public in the managed version of the C++ build. Once I get through all of the base classes, I am going to expose all of the structures that are needed and figure out how to expose the templates.
     

    Attached Files:

  18. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Just tested the incomplete port managed C++ library built in VS 2005 c# within Parallels, copied the DLL to the project on my MacBook Pro under the Assets folder directly, Unity had no errors on the import, then I added a using statement to the top of one of my CS files to see if Unity would puke or not, no errors, so I consider this first step a successful step. At least Unity 2.5 on Snow Leopard, MacBook Pro doesn't crap out like the SWIG version did. Going to see what it will take to start up the client connector using this new library with regular calls and no dllimport commands. Will post a shot of that hopefully tomorrow. Taking a break from this.
     

    Attached Files:

  19. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    All classes in headers are now exposed, now I am working on the hard part, or trying to figure it out for it to work correctly, getting the inner methods exposed per class that is public and getting all structures exposed so that they can be created via C# by simple "new" commands.. here is a short list of exposed classes. Not sure at the moment how to proceed on this next part, doing research now.
     

    Attached Files:

  20. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Sorry about no recent post on this thread, life and all, but at the moment, I am still trying to optimize the internal methods and the calls to the garbage collector. Straight up, Rakknet shift to managed from unmanaged poses some challenges, class definitions can be shifted from private to public and from unmanaged classes to managed classes, but you can't simply add the garbage collector directive to the classes, that said, this lead to a new path of development in the core code for me, one that lead down the path of adding additional new classes that handle the garbage collection and create pointers to the correct methods and structures. Not fun especially since my developement team consists of just myself and my Chihuahua and as you can imagine, a small hyper dog doesn't typically get much coding done but does a great job at grabbing the mouse and running until the chord tightens and snaps back.

    This project is going to turn into a pointer reference nightmare, but as long as the pointers do not get garbage collected or go out of scope, this should work out just fine.
     
  21. bloodtiger10

    bloodtiger10

    Joined:
    Nov 9, 2008
    Posts:
    619
    if you need any help I might be able to since I am interested in this. just pm me if you do because I have used c# inside and outside of unity and know c++. :D
     
  22. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Thanks for the offer, I tweaked something with one of the classes last night that is causing me some linking issues, once I get that fixed back to where it will compile again, I'll shoot you a link to the code I have so far, maybe you can help me find a better way to handle the static methods from the RakNetFactory, that is what I am working on now for phase 2. I have this split into 8 phases.

    Almost forgot to mention, I do have an alternative RakNet library that only works for windows been working with support to figure out why it won't work on the Mac.

    edit: ah ha fixed my problem back, got it to compile now both as cdecl and stdcall calling convention, still have to check a few more things out though for this phase and redo some methods, but can still disconnect it from my vault so you can grab the source to play with it if you want, I use sourcegear vault so right now it is linked
     
  23. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    Interesting work zumwalt, keep it up!
     
  24. bloodtiger10

    bloodtiger10

    Joined:
    Nov 9, 2008
    Posts:
    619
  25. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    PM sent with link to download project in current state, I am working on this and the SWIG version in parallel. Let me know if you get the file ok, it is 52 meg file zip.
     
  26. bloodtiger10

    bloodtiger10

    Joined:
    Nov 9, 2008
    Posts:
    619
  27. hogus

    hogus

    Joined:
    Jul 9, 2009
    Posts:
    145
    Hi, this is a very interesting thread.

    I've been developing a game-state and entity manager (Indie license for now), but I've abstracted the Networking layer.

    I'd love to see if I can plug this in once it's in a working state...
     
  28. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    I plan on posting up the package once I have the RakNetworkFactory working, of all of the classes, there are about a dozen or so that are the ones that you will actually use, the rest invoke each other when needed. Around phase 6 I plan on integrating SQLLite along with the LightWeightDatabase that is built into the code.

    The SWIG version is for compatibility with the Raknet 3.0 core the only problem will be the packet ID to send back and forth between the internal RakNet and the external server base.
     
  29. tomcat

    tomcat

    Joined:
    Nov 16, 2008
    Posts:
    66
    great job zumwalt. I have been struggling with the unreliability of the unity server and hope that the raknet package would help a little if you get it sorted.

    tomcat
     
  30. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    I have gotten a little farther on this issue, just taking longer than expected.
     
  31. soffa1

    soffa1

    Joined:
    Dec 3, 2009
    Posts:
    1
    Hi everyone :)

    How about dedicated servers (created with RekNet sources and running on linux) on Unity 2.6.1?

    Is it still hard do bring Unity-RekNet and original RekNet-Server together?
    Or is it much easier now?
    Is the code zumwalt provided easy to refaktor? (atm. i don't have a Mac but this should be the smallest problem).

    it would great to know, if there is a solution (with Unity-RekNet) to connect to a dedicated server.

    Cheers
     
  32. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Well, since with each version of RakNet, they are not backward compatible from major verion to major version, Unity's internal version is a sub of beta 3.0, the most recent version of RakNet is not compatible with Unity. With the tons of work I have put into this thing and all around lack of desire from Unity on either the master server help or the RakNet help, I have pretty much redacted this back to a personal project until I am happy with some of the newer features I am building into it, so you are pretty much stuck with using the built in version or start from scratch with something else. If you opt for the second, I recommend Photon if you have something serious and want between Mac and PC, if you want strictly PC, get RakNet pure and write something yourself to chatter between client and server.

    If you have the free version, you will probably want to go with something that is framework 2.0 / 1.1 compliant since you will be limited from the Plugins folder with C++ extensions.

    FYI, you can get RakNet source and compile as a carbon bundel into an .a file and compile using Visual Studio into a .dll and place both of them in your Plugins folder and use them with dllimport and be cross platform. Everything I was doing at the time was to make this easier for the Indy version rather than the Pro version, but now that Indy is free, there are tons of other options out there.

    Maybe once I am done, I will build up some demo's with it and maybe sell the library or sell the source also, haven't decided yet. Since all of my helping was aparently nothing more than lurking about, I have changed my approach on a lot of things around here.

    I am sure one of the forum mods has some code around here they can post to help get you pointed in the right direction on this until such time as the managed library I am making is ready to work with the free version. If you own pro, just grab the source from Jenkins and compile it up and use dllimport to get started.

    Jenkins website:
    http://www.jenkinssoftware.com/

    Exit Games Photon:
    http://www.exitgames.com/product/photon