예제 #1
0
        private void OnReceiveDataFromServer(int channelId, byte[] bytesReceived, int numBytesReceived)
        {
            Profiler.BeginSample("OnReceiveDataFromServer");
            using (var memoryStream = new MemoryStream(bytesReceived, 0, numBytesReceived))
            {
                using (var reader = new BinaryReader(memoryStream))
                {
                    var     messageTypeAsByte = reader.ReadByte();
                    RpcInfo rpcInfo;

                    if (messageTypeAsByte == NetLib.StateSynchronizationMessageId)
                    {
                        OnReceiveDeltaGameStateFromServer(reader, bytesReceived, numBytesReceived);
                    }
                    else if (NetLib.rpcInfoById.TryGetValue(messageTypeAsByte, out rpcInfo))
                    {
                        Profiler.BeginSample("Deserialize & Execute RPC");
                        var rpcArguments = NetworkSerializationUtils.DeserializeRpcCallArguments(rpcInfo, reader);
                        NetLib.ExecuteRpc(rpcInfo.Id, null, ObjectContainingRpcs, rpcArguments);
                        Profiler.EndSample();
                    }
                    else
                    {
                        throw new NotImplementedException("Unknown message type: " + messageTypeAsByte);
                    }
                }
            }
            Profiler.EndSample();
        }
예제 #2
0
        private void OnReceiveDataFromClient(int connectionId, int channelId, byte[] bytesReceived, int numBytesReceived)
        {
            using (var memoryStream = new MemoryStream(bytesReceived, 0, numBytesReceived))
            {
                using (var reader = new BinaryReader(memoryStream))
                {
                    var messageTypeAsByte = reader.ReadByte();

                    RpcInfo rpcInfo;

                    if (messageTypeAsByte == NetLib.StateSynchronizationMessageId)
                    {
                        throw new System.NotImplementedException("Servers don't support receiving state synchronization messages.");
                    }
                    else if (NetLib.rpcInfoById.TryGetValue(messageTypeAsByte, out rpcInfo))
                    {
                        var rpcArguments = NetworkSerializationUtils.DeserializeRpcCallArguments(rpcInfo, reader);

                        CurrentRpcSenderConnectionId = connectionId;

                        if (rpcInfo.Name != "ServerOnReceiveClientGameStateAck")
                        {
                            NetLib.ExecuteRpc(rpcInfo.Id, ObjectContainingRpcs, null, rpcArguments);
                        }
                        else
                        {
                            NetLib.ExecuteRpc(rpcInfo.Id, this, null, rpcArguments);
                        }
                    }
                    else
                    {
                        throw new System.NotImplementedException("Unknown message type: " + messageTypeAsByte);
                    }
                }
            }
        }