/// <summary>
 /// This sends a network message with a message ID on the connection. This message is sent on channel zero, which by default is the reliable channel.
 /// </summary>
 /// <typeparam name="T">The message type to unregister.</typeparam>
 /// <param name="msg">The message to send.</param>
 /// <param name="channelId">The transport layer channel to send on.</param>
 /// <returns></returns>
 public virtual bool Send <T>(T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase
 {
     // pack message and send
     byte[] message = MessagePacker.Pack(msg);
     NetworkDiagnostics.OnSend(msg, channelId, message.Length, 1);
     return(SendBytes(message, channelId));
 }
Example #2
0
        public static void Send <T>(IEnumerable <INetworkConnection> connections, T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase
        {
            using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
            {
                // pack message into byte[] once
                MessagePacker.Pack(msg, writer);
                var segment = writer.ToArraySegment();
                int count   = 0;

                foreach (INetworkConnection conn in connections)
                {
                    if (conn is NetworkConnection networkConnection)
                    {
                        // send to all connections, but don't wait for them
                        _ = networkConnection.SendAsync(segment, channelId);
                    }
                    else
                    {
                        _ = conn.SendAsync(msg, channelId);
                    }
                    count++;
                }

                NetworkDiagnostics.OnSend(msg, channelId, segment.Count, count);
            }
        }
        /// <summary>
        /// Sends a message, but notify when it is delivered or lost
        /// </summary>
        /// <typeparam name="T">type of message to send</typeparam>
        /// <param name="msg">message to send</param>
        /// <param name="token">a arbitrary object that the sender will receive with their notification</param>
        public void SendNotify <T>(T msg, object token, byte channelId = (byte)Channels.Unreliable)
        {
            if (sendWindow.Count == WINDOW_SIZE)
            {
                NotifyLost?.Invoke(this, token);
                return;
            }

            using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
            {
                var notifyPacket = new NotifyPacket
                {
                    Sequence        = (ushort)sequencer.Next(),
                    ReceiveSequence = receiveSequence,
                    AckMask         = receiveMask
                };

                sendWindow.Enqueue(new PacketEnvelope
                {
                    Sequence = notifyPacket.Sequence,
                    Token    = token
                });

                MessagePacker.Pack(notifyPacket, writer);
                MessagePacker.Pack(msg, writer);
                NetworkDiagnostics.OnSend(msg, channelId, writer.Length, 1);
                SendAsync(writer.ToArraySegment(), channelId).Forget();
            }
        }
Example #4
0
 /// <summary>
 /// This sends a network message with a message ID on the connection. This message is sent on channel zero, which by default is the reliable channel.
 /// </summary>
 /// <typeparam name="T">The message type to unregister.</typeparam>
 /// <param name="msg">The message to send.</param>
 /// <param name="channelId">The transport layer channel to send on.</param>
 /// <returns></returns>
 public bool Send <T>(T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase
 {
     using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
     {
         // pack message and send allocation free
         MessagePacker.Pack(msg, writer);
         NetworkDiagnostics.OnSend(msg, channelId, writer.Position, 1);
         return(Send(writer.ToArraySegment(), channelId));
     }
 }
 /// <summary>
 /// This sends a network message to the connection. You can await it to check for errors
 /// </summary>
 /// <typeparam name="T">The message type</typeparam>
 /// <param name="msg">The message to send.</param>
 /// <param name="channelId">The transport layer channel to send on.</param>
 /// <returns></returns>
 public virtual UniTask SendAsync <T>(T msg, byte channelId = (byte)Channels.Reliable)
 {
     using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
     {
         // pack message and send allocation free
         MessagePacker.Pack(msg, writer);
         NetworkDiagnostics.OnSend(msg, channelId, writer.Length, 1);
         return(SendAsync(writer.ToArraySegment(), channelId));
     }
 }
Example #6
0
 /// <summary>Send a NetworkMessage to this connection over the given channel.</summary>
 public void Send <T>(T msg, int channelId = Channels.Reliable)
     where T : struct, NetworkMessage
 {
     using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
     {
         // pack message and send allocation free
         MessagePacking.Pack(msg, writer);
         NetworkDiagnostics.OnSend(msg, channelId, writer.Position, 1);
         Send(writer.ToArraySegment(), channelId);
     }
 }
        /// <summary>
        /// This sends a network message with a message ID on the connection. This message is sent on channel zero, which by default is the reliable channel.
        /// </summary>
        /// <typeparam name="T">The message type to unregister.</typeparam>
        /// <param name="msg">The message to send.</param>
        /// <param name="channelId">The transport layer channel to send on.</param>
        /// <returns></returns>
        public virtual bool Send <T>(T msg, int channelId = Channels.DefaultReliable) where T : IMessageBase
        {
            NetworkWriter writer = NetworkWriterPool.GetWriter();

            // pack message and send allocation free
            MessagePacker.Pack(msg, writer);
            NetworkDiagnostics.OnSend(msg, channelId, writer.Position, 1);
            bool result = Send(writer.ToArraySegment(), channelId);

            NetworkWriterPool.Recycle(writer);
            return(result);
        }
        public static void Send <T>(IEnumerable <INetworkConnection> connections, T msg, byte channelId = (byte)Channels.Reliable)
        {
            using (PooledNetworkWriter writer = NetworkWriterPool.GetWriter())
            {
                // pack message into byte[] once
                MessagePacker.Pack(msg, writer);
                var segment = writer.ToArraySegment();
                int count   = 0;

                foreach (INetworkConnection conn in connections)
                {
                    // send to all connections, but don't wait for them
                    conn.SendAsync(segment, channelId).Forget();
                    count++;
                }

                NetworkDiagnostics.OnSend(msg, channelId, segment.Count, count);
            }
        }