internal static void PassthroughSend(uint targetId, uint sourceId, ushort messageType, int channelId, BitReader reader, uint?networkId = null, ushort?orderId = null) { if (netManager.isHost && targetId == netManager.NetworkConfig.NetworkTransport.HostDummyId) { //Host trying to send data to it's own client return; } using (BitWriter writer = BitWriter.Get()) { writer.WriteGenericMessageHeader(messageType, networkId != null, networkId.GetValueOrDefault(), orderId.GetValueOrDefault(), true, null, sourceId); #if !DISABLE_CRYPTOGRAPHY if (netManager.NetworkConfig.EncryptedChannelsHashSet.Contains(MessageManager.reverseChannels[channelId])) { writer.WriteByteArray(CryptographyHelper.Encrypt(reader.ReadByteArray(), netManager.connectedClients[targetId].AesKey)); } else #endif writer.WriteByteArray(reader.ReadByteArray()); writer.Finalize(ref FinalMessageBuffer); byte error; int byteCount = (int)writer.GetFinalizeSize(); NetworkProfiler.addBytesSent((uint)byteCount); netManager.NetworkConfig.NetworkTransport.QueueMessageForSending(targetId, ref FinalMessageBuffer, byteCount, channelId, false, out error); } }
//RETURNS THE CLIENTIDS WHICH WAS NOT BEING OBSERVED internal static ref List <uint> Send(string messageType, string channelName, BitWriter messageWriter, uint clientIdToIgnore, uint?fromNetId, uint?networkId = null, ushort?orderId = null) { failedObservers.Clear(); if (netManager.NetworkConfig.EncryptedChannels.Contains(channelName)) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("Cannot send messages over encrypted channel to multiple clients"); } return(ref failedObservers); } using (BitWriter writer = BitWriter.Get()) { writer.WriteGenericMessageHeader(MessageManager.messageTypes[messageType], networkId != null, networkId.GetValueOrDefault(), orderId.GetValueOrDefault(), false, 0, 0); writer.WriteWriter(messageWriter); int channel = MessageManager.channels[channelName]; foreach (KeyValuePair <uint, NetworkedClient> pair in netManager.connectedClients) { if (pair.Key == clientIdToIgnore) { continue; } uint targetClientId = pair.Key; if (netManager.isHost && targetClientId == netManager.NetworkConfig.NetworkTransport.HostDummyId) { //Don't invoke the message on our own machine. Instant stack overflow. continue; } else if (targetClientId == netManager.NetworkConfig.NetworkTransport.HostDummyId) { //Client trying to send data to host targetClientId = netManager.NetworkConfig.NetworkTransport.ServerNetId; } //If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we continue if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(pair.Key)) { failedObservers.Add(pair.Key); continue; } writer.Finalize(ref FinalMessageBuffer); NetworkProfiler.StartEvent(TickType.Send, (uint)messageWriter.GetFinalizeSize(), channelName, messageType); byte error; int byteCount = (int)writer.GetFinalizeSize(); NetworkProfiler.addBytesSent((uint)byteCount); netManager.NetworkConfig.NetworkTransport.QueueMessageForSending(targetClientId, ref FinalMessageBuffer, byteCount, channel, false, out error); NetworkProfiler.EndEvent(); } return(ref failedObservers); } }
//RETURNS IF IT SUCCEDED OR FAILED BECAUSE OF NON-OBSERVER. ANY OTHER FAIL WILL RETURN TRUE internal static bool Send(uint clientId, string messageType, string channelName, BitWriter messageWriter, uint?fromNetId, uint?networkId = null, ushort?orderId = null, bool skipQueue = false) { uint targetClientId = clientId; if (netManager.isHost && targetClientId == netManager.NetworkConfig.NetworkTransport.HostDummyId) { //Don't invoke the message on our own machine. Instant stack overflow. return(true); } else if (targetClientId == netManager.NetworkConfig.NetworkTransport.HostDummyId) { //Client trying to send data to host targetClientId = netManager.NetworkConfig.NetworkTransport.ServerNetId; } //If we respect the observers, and the message is targeted (networkId != null) and the targetedNetworkId isnt observing the receiver. Then we return if (netManager.isServer && fromNetId != null && !SpawnManager.spawnedObjects[fromNetId.Value].observers.Contains(clientId)) { return(false); } bool isPassthrough = (!netManager.isServer && clientId != netManager.NetworkConfig.NetworkTransport.ServerNetId && netManager.NetworkConfig.AllowPassthroughMessages); if (isPassthrough && !netManager.NetworkConfig.PassthroughMessageHashSet.Contains(MessageManager.messageTypes[messageType])) { if (LogHelper.CurrentLogLevel <= LogLevel.Normal) { LogHelper.LogWarning("The The MessageType " + messageType + " is not registered as an allowed passthrough message type"); } return(true); } using (BitWriter writer = BitWriter.Get()) { writer.WriteGenericMessageHeader(MessageManager.messageTypes[messageType], networkId != null, networkId.GetValueOrDefault(), orderId.GetValueOrDefault(), isPassthrough, clientId, null); #if !DISABLE_CRYPTOGRAPHY if (netManager.NetworkConfig.EncryptedChannelsHashSet.Contains(channelName)) { //This is an encrypted message. byte[] encrypted; if (netManager.isServer) { encrypted = CryptographyHelper.Encrypt(messageWriter.Finalize(), netManager.connectedClients[clientId].AesKey); } else { encrypted = CryptographyHelper.Encrypt(messageWriter.Finalize(), netManager.clientAesKey); } writer.WriteByteArray(encrypted); } else #endif writer.WriteWriter(messageWriter); if (isPassthrough) { targetClientId = netManager.NetworkConfig.NetworkTransport.ServerNetId; } writer.Finalize(ref FinalMessageBuffer); NetworkProfiler.StartEvent(TickType.Send, (uint)messageWriter.GetFinalizeSize(), channelName, messageType); byte error; int byteCount = (int)writer.GetFinalizeSize(); NetworkProfiler.addBytesSent((uint)byteCount); if (skipQueue) { netManager.NetworkConfig.NetworkTransport.QueueMessageForSending(targetClientId, ref FinalMessageBuffer, byteCount, MessageManager.channels[channelName], true, out error); } else { netManager.NetworkConfig.NetworkTransport.QueueMessageForSending(targetClientId, ref FinalMessageBuffer, byteCount, MessageManager.channels[channelName], false, out error); } NetworkProfiler.EndEvent(); return(true); } }