示例#1
0
        /// <summary>
        /// Sends a packet over the network
        /// </summary>
        /// <param name="packet">The <see cref="Packet"/> instance that is to be sent</param>
        /// <param name="reliable">Whether the transmission is reliable (slow) or unreliable (fast)</param>
        /// <returns>Whether message can be sent or not</returns>
        public bool Send(Packet packet, bool reliable = false)
        {
            if (m_Network == null || ConnectionIds == null || ConnectionIds.Count == 0)
            {
                return(false);
            }

            List <ConnectionId> recipients = new List <ConnectionId>();

            if (packet.Recipients.Length != 0)
            {
                recipients = ConnectionIds.Select(x => x)
                             .Where(x => packet.Recipients.Contains(x.id))
                             .ToList();
            }
            else
            {
                recipients = ConnectionIds.ToList();
            }

            var bytes = packet.Serialize();

            foreach (var cid in recipients)
            {
                m_Network.SendData(cid, bytes, 0, bytes.Length, reliable);
            }

            return(true);
        }