using FishNet.Broadcast; using FishNet.Managing; using FishNet.Managing.Logging; using FishNet.Managing.Transporting; using FishNet.Transporting; using System; using System.Collections.Generic; using UnityEngine; namespace FishNet.Connection { public partial class NetworkConnection { #region Private. /// /// PacketBundles to send to this connection. An entry will be made for each channel. /// private List _toClientBundles = new List(); /// /// True if this object has been dirtied. /// private bool _serverDirtied; #endregion /// /// Initializes this script. /// private void InitializeBuffer() { for (byte i = 0; i < TransportManager.CHANNEL_COUNT; i++) { int mtu = NetworkManager.TransportManager.GetLowestMTU(i); _toClientBundles.Add(new PacketBundle(NetworkManager, mtu)); } } /// /// Sends a broadcast to this connection. /// /// Type of broadcast to send. /// Broadcast data being sent; for example: an instance of your broadcast type. /// True if the client must be authenticated for this broadcast to send. /// Channel to send on. public void Broadcast(T message, bool requireAuthenticated = true, Channel channel = Channel.Reliable) where T : struct, IBroadcast { if (!IsActive) NetworkManager.LogError($"Connection is not valid, cannot send broadcast."); else NetworkManager.ServerManager.Broadcast(this, message, requireAuthenticated, channel); } /// /// Sends data from the server to a client. /// /// True to force data into a new buffer. internal void SendToClient(byte channel, ArraySegment segment, bool forceNewBuffer = false) { //Cannot send data when disconnecting. if (Disconnecting) return; if (!IsActive) { NetworkManager.LogWarning($"Data cannot be sent to connection {ClientId} because it is not active."); return; } //If channel is out of bounds then default to the first channel. if (channel >= _toClientBundles.Count) channel = 0; _toClientBundles[channel].Write(segment, forceNewBuffer); ServerDirty(); } /// /// Returns a PacketBundle for a channel. ResetPackets must be called afterwards. /// /// /// True if PacketBundle is valid on the index and contains data. internal bool GetPacketBundle(int channel, out PacketBundle packetBundle) { return PacketBundle.GetPacketBundle(channel, _toClientBundles, out packetBundle); } /// /// Indicates the server has data to send to this connection. /// private void ServerDirty() { bool wasDirty = _serverDirtied; _serverDirtied = true; //If not yet dirty then tell transport manager this is dirty. if (!wasDirty) NetworkManager.TransportManager.ServerDirty(this); } /// /// Resets that there is data to send. /// internal void ResetServerDirty() { _serverDirtied = false; } } }