public bool TryGetTransportChannel(byte channel, out TransportChannel transportChannel)
 {
     if (channel >= m_Channels.Count)
     {
         transportChannel = null;
         return(false);
     }
     transportChannel = m_Channels[channel];
     return(transportChannel != null); //Null comparison is probably faster than doing a m_FreeChannels.Contains call
 }
        public bool TryGetTransportChannel(string channelName, out TransportChannel transportChannel)
        {
            if (m_ChannelsByName.TryGetValue(channelName, out byte channel))
            {
                transportChannel = m_Channels[channel];
                return(true);
            }

            transportChannel = null;
            return(false);
        }
        /// <summary>
        /// Channels are used in multiple cases such as when stalling(when sending messages) occurs on network transports the stalling is isolated to a specific channel and won't affect other areas of network messaging to maximize performance.
        /// Be aware registering and unregistering channels while the network is running may not take effect until the transport has been shutdown and reinitialized.
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="channelType"></param>
        /// <returns></returns>
        public byte RegisterChannel(string channelName, ChannelType channelType)
        {
            //Check if channeName is valid
            if (string.IsNullOrWhiteSpace(channelName))
            {
                Debug.LogError("Paramater channelName is null or whitespace.");
                return(INVALID_CHANNEL); //Return invalid channel
            }

            //Check if the channel name is taken
            if (m_ChannelsByName.ContainsKey(channelName))
            {
                Debug.LogError("Channel name '" + channelName + "' is already registered.");
                return(INVALID_CHANNEL); //Return invalid channel
            }

            //Check if there are channels left
            byte chosenChannelIndex;

            if (m_FreeChannels.Count > 0)
            {
                chosenChannelIndex = m_FreeChannels[0];
                m_FreeChannels.RemoveAt(0);
                m_Channels[chosenChannelIndex] = new TransportChannel()
                {
                    name = channelName, channel = chosenChannelIndex, channelType = channelType
                };
            }
            else if (m_Channels.Count == byte.MaxValue)
            {
                //Remember 0 is used for NETWORK_DEFAULT, 255 is invalid and whatever built-in channels being used by the Network Manager.
                Debug.LogError("No channels left to register. A maximum of 255 channels can be registered.");
                return(INVALID_CHANNEL); //Return invalid channel
            }
            else
            {
                chosenChannelIndex = (byte)m_Channels.Count;
                m_Channels.Add(new TransportChannel()
                {
                    name = channelName, channel = chosenChannelIndex, channelType = channelType
                });
            }

            //Warn about unsupported channel type
            if ((supportedChannelTypes & channelType) != channelType)
            {
                Debug.LogWarning("Channel Type '" + channelType + "' is not supported by this transport(" + this.GetType() + ") and will use an alternative channel type internally when a message with this channel type is used.");
            }

            //Do register
            m_ChannelsByName.Add(channelName, chosenChannelIndex);
            m_ChannelCount++;
            return(chosenChannelIndex);
        }
        public string GetChannelName(byte channel)
        {
            if (channel >= m_Channels.Count || channel == INVALID_CHANNEL)
            {
                throw new ArgumentException("Could not get channel name. Channel '" + channel + "' is not registered or invalid.", nameof(channel));
            }
            TransportChannel transportChannel = m_Channels[channel];

            if (transportChannel == null)
            {
                throw new ArgumentException("Could not get channel name. Channel '" + channel + "' is not registered or invalid.", nameof(channel));
            }
            return(transportChannel.name);
        }
        public ChannelType GetChannelType(byte channel)
        {
            if (channel >= m_Channels.Count || channel == INVALID_CHANNEL)
            {
                Debug.Log("a1");
                throw new ArgumentException("Could not get channel type. Channel '" + channel + "' is not registered or invalid.", nameof(channel));
            }

            TransportChannel transportChannel = m_Channels[channel];

            if (transportChannel == null)
            {
                Debug.Log("b2");
                throw new ArgumentException("Could not get channel type. Channel '" + channel + "' is not registered or invalid.", nameof(channel));
            }

            return(transportChannel.channelType);
        }