Writing own network layer on top of Unity network layer

Hi there,

I’ve been having a pretty bad week-end because Unity’s been crashing on me about 20 or so times (a few of those crashes filed as bug reports). I think I figured out one particular problem I need a solution for:

Since I need fine-grained control on what I’m sending to whom (different game sessions taking place on one server, some player participating, others not), I’m trying to create a networking layer on top of the Unity networking API.

I would like to be as close as possible to the original networking approach, so I basically have a class JCsNetwork which has a method “RPC” which takes a NetworkView, RPC-Name and params object[ ] args. This method then delegates to RPCToClients which does the actually sending out.

On the server, this works smoothly (as far as I can see - RPCToClients is directly called as a method). However, when this method is called on the client, it needs to RPC the args to the server so the server can do the distribution (because I only have the NetworkPlayers on the server). So, on the client, RPCToClients is called through an RPC, passing the parameters that RPC has received.

Let me illustrate that with actual code:

public void RPC(NetworkView nv, string rpcName, RPCMode mode, params object[] args) {
        if (Network.isClient) {
            networkView.RPC("RPCToClients", RPCMode.Server, nv.viewID, rpcName, args);
        } else {
            NetworkMessageInfo nmInfo = new NetworkMessageInfo();
            RPCToClients(nv.viewID, rpcName, args, nmInfo);
        }
    }
}

This is a bit simplified - in particular, I have removed handling of the RPCMode (it’s converted to int and then passed to RPCToClients).

Then, there’s:

    [RPC()]
    public void RPCToClients(NetworkViewID viewID, string rpcName, int rpcMode, object[] args, NetworkMessageInfo info) {
...

Now what seems to happen is that when I RPC to that, my args are somehow converted to a byte-array instead of the original parameter-list. When I then put this into the RPCs that go to the clients (in a loop over the network players that are in that particular game session), I get BOOM - a crash of the editor (I’ve had a few other problems, too - seems to me this is “weekend of Unity crashes” for me).

As far as I can tell from my logs, this crash is right at:

nv.RPC(rpcName, player, args);

Where nv is the NetworkView (which I’m getting through the NetworkViewID), rpcName obviously is the name of the RPC, player is the NetworkPlayer (I’m looping over), and - that seems to be the thing that’s making the editor crash: args is “something in System.Byte[ ]”. It should be the object-array of my original parameters. Maybe I should note that there were ints sent, some of which are negative.

The question now is: Is there any (convenient) way of creating a generic RPC that could take any number of the typical RPC parameters (bool, int, Vector3, NetworkView etc.), so I can pass this along into another RPC? I mean, something more elegant than encoding all parameters into some string (or byte[ ] which should work in 2.0.2) and than decoding it on the server?

I somehow need a way to model “RPCMode.All” and “RPCMode.Others” so that clients can send stuff through my networking layer. I’d like to just replace networkView.RPC(…) with JCsNetwork.RPC(networkView, …). Which would be very nice from the “software-designer’s point of view”.

I’ve also tried:

    [RPC()]
    public void RPCToClients(NetworkViewID viewID, string rpcName, int rpcMode, NetworkPlayer sender, params object[] args) {
...

But I’m still getting this nasty Byte[ ] in args (which is not what I’ve sent!)

Anyone with any ideas? Larus??? :wink:

Still sunny regards :wink:
Jashan

Somewhat related to this - could anybody tell me what this means:

NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) System.Object:stelemref (object,intptr,object)

I’m getting this on

… Bug id is 19122 …

Hi Jashan,

I’m having the craziest crashing problems here so I totally know how your weekend was … I’m there now!. My game is crashing the IDE frequently, like 1 in 3. Interesting enough it’s not crashing the browser, although every once in a while it refuses to connect (but does not crash) and I have to close and re-open the browser to get it going.

I’m using Net.Sockets to connect to a server and I have no idea how to track down whats causing the crash other than stripping the code down line by line (which will literally take days). We’ve ran all of the code on Windows in Visual Studio successfully, but once it was moved over to Unity it’s imploding all over the place.

When I first launch the game in the IDE, it will crash. Re-launching the IDE brings it to life and everything is cool for the next 3-4 runs. Then after that every 3-4 it’ll crash.

Are there any tools to help determine why these crashes are happening? Or if the data is getting corrupted etc.? Did you see anything like this during your crash weekend?

I’m desperately looking for pointers since I’m new to Macs and I’m lacking my usual PC-based arsenal for solving this kind of stuff.

On that note, has anyone else encountered crazy crashes like this? I wish I could post up a link or some code, but it’s under NDA until it goes live :frowning: grrrr

Thanks for any help or pointers and I hope you manage to sort out your problems too,
Jeff.

Hi Jeff,

sorry to hear that… what I’m doing is using log4net to put out significant logfiles, and then simply check where the logfile ends (and if that doesn’t help, add more debug code). Obviously, a real step-debugger could simplify all of our lifes very significantly on these issues… Hope that’ll make it into one of the 2.x-releases of Unity, that sure is a rather involved “project” :wink:

I have to say, though, that in the case of one bug I had with Unity 2.0.1, that didn’t get me anywhere. However, what I did find out then was that the bug was not at all related to networking. It was fixed with 2.0.2… Thank UT :wink:

If, in any way possible, it would be great if you could reproduce the problem in “the simplest possible case” and send UT a bug report with that “simply project”. Since you’re in this NDA-situation, recreating the problem “from scratch” will probably be the only way to file a proper bug report to UT anyways, and it might help you a lot to find a workaround, too. I have to admit, though, that I didn’t have time to create a “just the error”-project for the current issue I was having (might still give it a try, but I simply don’t have much time and need to focus on getting my game finished).

I guess one could say “Editor crashing” is always a bad Unity bug. At the same time, however, sometimes there’s a “little issue” that makes this happen which really is a bug in the game. One “typical problem” are endless recursions… One thing I’m running into frequently are “funny” NullReferenceExceptions, which I think is due to the way the Unity API is designed (I think it’s a very nice design - it just takes a while to get used to it)… those usually don’t cause crashes, though. But especially, when you have code that worked outside of Unity, I’d try looking into that direction…

You might gain some insights on the reason for the crash through checking the console… Personally, I’ve started always opening Unity from the terminal (one reason for that is that I like having multiple instances of Unity for network testing), so I can simply look at the output to the terminal. You might see some “last words” before the crash, which might help…

Well, I wish you good luck :wink:

Jashan

Thankyou a lot for the reply, Jashan. I really appreciate it.

Hmmm … running Unity from the console - haven’t tried that yet. Log4Net. Right. I’ll grab that too!

It’s tough when it’s a straight crash with no explanation and, as you said, I need to try and rebuild a simplified version that can repro it.

I actually found that I could reproduce the crash every time by saving the game just before I run it, so that got me totally baffled!

Anyways, thanks again for the help. I’ll give it another bash … it looks like the north pole here in Ottawa today - been the most snowfall on record this winter - it’s really started my day in a ‘I just want to go back to bed’ kind of way so I’m not sure how productivity is going to be at the office! :wink:

Jeff.

Ok … console says (yippeee! I found the console lol) that odd stuff is afoot …

05/03/08 9:57:17 AM [0x0-0x22022].com.unity3d.UnityEditor[171] at SocketAsyncResult.Complete () <0x001bf>
05/03/08 9:57:17 AM [0x0-0x22022].com.unity3d.UnityEditor[171] at Worker.Receive () <0x00016>
05/03/08 9:57:17 AM [0x0-0x22022].com.unity3d.UnityEditor[171] at (wrapper delegate-invoke) System.MulticastDelegate.invoke_void () <0xffffffff>
05/03/08 9:57:17 AM [0x0-0x22022].com.unity3d.UnityEditor[171] at (wrapper runtime-invoke) System.Net.Configuration.NetSectionGroup.runtime_invoke_void (object,intptr,intptr,intptr) <0xffffffff> 05/03/08 9:57:17 AM [0x0-0x22022].com.unity3d.UnityEditor[171] Receiving unhandled NULL exception
05/03/08 9:57:17 AM [0x0-0x22022].com.unity3d.UnityEditor[171] *** Launching bug reporter due to crashbug.

Just like you said … it looks like some kind of NULL exception. Now I just gotta figure out why :S

Thanks! Helpful stuff :slight_smile:

Hi Jeff,

yeah, the weather’s kind of crazy here in Munich, too… last week was “convertible time” - felt really like spring. Today, I woke up and it was winter again… really weird…

If you want to, you can check out the log4net-Package that I’ve created to be used with Unity. See my posting on this in the Scripting forum:

http://forum.unity3d.com/viewtopic.php?t=8964

Sunny regards (at least the sun is shining, even if it’s cold :wink: ),
Jashan

Thanks again, mister!

I’ve got your logger setup (VERY useful thanks!) but still having trouble tracking down exactly what is crashing it out…

Ah well, I’ll keep finding other jobs to do until I feel brave enough to rebuild the whole C# lib line by line!

Slightly sunny with patches of cloud and a 60% chance of precipitation regards,
Jeff.

Sorry for hijacking your thread, btw … just thought I’d let you know that I found the offending line of code.

System.Console.WriteLine

Originally I went through and changed all my System.Console.WriteLine calls to Debug.Log instead, but one slipped through and caused me a nightmare!

The call was being made as the script was destroyed and kaboom!

Very odd… now I know for the future :slight_smile:

Thanks for all your help,
Jeff.

Oh, never mind… Threads are like living beings: They tend to change :wink:

I’m glad you found the offending code!!! Funny that System.Console.WriteLine would cause the editor to crash, but I guess that’s just life…

I’m really glad log4net is working without a problem… If I didn’t have that tool at hand… I’d be just lost :wink:

Sunny regards,
Jashan