- Home /
Mysterious OnSerializeNetworkView calls with an invalid NetworkMessageInfo.sender
Hey guys,
I'm making an online game with a client-server relationship (clients must reach each other through the server).
I have a NetworkView on each client, observing their Player scripts. I'm pretty darn sure they're set up properly, with all the ID's and owners as they should be. The clients allocate view IDs for their own NetworkViews, and thus they are the 'owner' of the NetworkView.
However, my server's OnSerializeNetworkView method is getting called constantly, and the BitStream has 'isWriting' set to true. As far as I know, the server should never have to write to the stream, because the only NetworkView it has that it actually 'owns' is one that has State Synchronization off and the Observed property set to None (because it's used for RPCs only).
The strange thing is, when the clients are supposed to be sending data over their network views, the server's stream has 'isReading' set to true. The only time 'isWriting' is set to true is when I get these weird calls that I can't explain.
I used Debug.Log to find the NetworkPlayer of the players sending these calls. I run tests with the server and 2 clients; the server's NetworkPlayer is 0, the clients are 1 and 2.
When I use Debug.Log on the NetworkMessageInfo.sender of these strange calls that have 'isWriting' set to true, I get -1.
I don't understand how I'm getting these OnSerializeNetworkView() calls with a NetworkPlayer that doesn't even exist sending them.
For example, if I ran this simple code on my server:
void OnSerializeNetworkView(BitStream stream,NetworkMessageInfo info)
{
if (stream.isReading)
{
Debug.Log("Reading with a sender of " + info.sender);
}
if (stream.isWriting)
{
Debug.Log("Writing with a sender of " + info.sender);
}
}
...then the only time the info.sender value is -1 would be when the stream is writing. The value would always be 1 or 2 when it's reading.
I just don't understand why I'm getting these calls. My server should never be writing to any of the streams, right? I've read multiple times while searching around that only the owner of the NetworkView can write to the stream, and all the others read from it. I'm sure, from many tests and Debug.Log() calls, that the clients own the NetworkViews and their ID's, not the server. So how come the server is writing to these strange, mystery calls?
Any help would be greatly appreciated, and if you need more information, please let me know.
Thank you!
I have a same problem... if you solve that probem, Would you Help me? Thank...!
reffers to self. If you want to for instance check wether the sender of an RPC is yourself, you can check for wether the the
sender.ToString() == "-1"
That's odd...why would the server be receiving calls from itself? Also, why not just use the NetworkPlayer of the server (which is 0 in my case), ins$$anonymous$$d of -1?
On any local mashine (`Network.player`) the "id" is always = -1
. The server, on the server is -1. The server on the clients is always 0. Other clients on each singular client don't exist.
Network.connections
On the server contains all the client,
On the clients contains only the server.
The server could be recieving calls from itself, by using RPC$$anonymous$$ode.All
, which includes the local client.
Ah, I see. Thank you for the explanation :) How could RPCs from itself trigger the OnSerializeNetworkView() method?
Answer by Jamora · Jul 25, 2013 at 04:47 PM
When a NetworkView observes a Component, the OnSerializeNetworkView of the Component is called Network.sendRate (default: 15) times per second. Conversely, if a networkView does not observe a Component, its OnSerializeNetworkView is not called.
Only the owner of the object can have isWriting true. The reason sender == -1 when isWriting == true, is because you don't have a sender when writing something, because technically you can't be writing and sending at the same time. Consider this question, "I'm writing this letter, who is its sender?"
Back to the ownership issue... You can check who the owner of the networkview is by DebugLogging networkView.owner. If inside if(stream.isWriting) netviewView.isMine debugs as false, you need to submit a bug report.