Example #1
0
        /// <summary>
        /// Remove a callback from listening for payloads of a given type.
        /// </summary>
        /// <typeparam name="T">The payload type being listened for</typeparam>
        /// <param name="callback">The delegate to remove</param>
        public void RemoveCallback <T>(FlarePayloadCallback <T> callback) where T : INetworkPayload
        {
            if (isInvoking)
            {
                registrationQueue.Enqueue(() => Remove(callback));
            }
            else
            {
                Remove(callback);
            }

            void Remove <P>(FlarePayloadCallback <P> c) where P : INetworkPayload
            {
                var tag = NetworkTagAttribute.GetTag(typeof(P));

                // Remove the callback from the dictionary
                if (tag != null)
                {
                    if (payloadCallbacks.TryGetValue(tag.Value, out var value))
                    {
                        value.Remove(c);
                    }
                    else
                    {
                        NetworkLogger.Log($"Payload type [{typeof(P).Name}] has no callbacks to remove!", LogCategory.PayloadCallbacks, LogLevel.Warning);
                    }
                }
                else
                {
                    NetworkLogger.Log("Cannot remove callbacks for types with no NetworkTag!", LogCategory.PayloadCallbacks, LogLevel.Warning);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Add a callback to listen for payloads of a given type.
        /// </summary>
        /// <typeparam name="T">The payload type to listen for</typeparam>
        /// <param name="callback">The delegate to call</param>
        public void AddCallback <T>(FlarePayloadCallback <T> callback) where T : INetworkPayload
        {
            if (isInvoking)
            {
                registrationQueue.Enqueue(() => Add(callback));
            }
            else
            {
                Add(callback);
            }

            void Add <P>(FlarePayloadCallback <P> c) where P : INetworkPayload
            {
                var type = typeof(P);
                var tag  = NetworkTagAttribute.GetTag(type);

                if (tag != null)
                {
                    // Add the callback to the dictionary
                    if (!payloadCallbacks.TryGetValue(tag.Value, out var value))
                    {
                        // Create callback entry if it doesn't exist
                        value = new Callback(type);
                        payloadCallbacks.Add(tag.Value, value);
                    }

                    value.Add(c);
                }
                else
                {
                    NetworkLogger.Log("Cannot add callbacks for types with no NetworkTag!", LogCategory.PayloadCallbacks, LogLevel.Message);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Remove all registered callbacks for a given payload type.
        /// </summary>
        /// <typeparam name="T">The type to remove callbacks for</typeparam>
        public void ClearCallbacks <T>() where T : INetworkPayload
        {
            if (isInvoking)
            {
                registrationQueue.Enqueue(() => Clear <T>());
            }
            else
            {
                Clear <T>();
            }

            void Clear <P>() where P : INetworkPayload
            {
                var tag = NetworkTagAttribute.GetTag(typeof(P));

                if (tag != null)
                {
                    if (payloadCallbacks.ContainsKey(tag.Value))
                    {
                        payloadCallbacks.Remove(tag.Value);
                        NetworkLogger.Log($"Cleared callbacks for payload type [{typeof(P).Name}]", LogCategory.PayloadCallbacks);
                    }
                    else
                    {
                        NetworkLogger.Log($"Payload type [{typeof(P).Name}] has no callbacks to clear!", LogCategory.PayloadCallbacks, LogLevel.Warning);
                    }
                }
                else
                {
                    NetworkLogger.Log("Cannot clear callbacks for types with no NetworkTag!", LogCategory.PayloadCallbacks, LogLevel.Warning);
                }
            }
        }
Example #4
0
        /// <summary>
        /// Manually push a new payload into the queue.
        /// </summary>
        /// <typeparam name="T">Payload type</typeparam>
        /// <param name="payload">The payload to add</param>
        internal void PushPayload <T>(T payload) where T : INetworkPayload
        {
            var tag = NetworkTagAttribute.GetTag(typeof(T));

            if (tag != null && payloadCallbacks.TryGetValue(tag.Value, out var c))
            {
                pollQueue.Enqueue(new MessagePayload {
                    Value = payload, Callback = c
                });
            }
        }
Example #5
0
        private void SendMessage <T>(T value, byte channel, params Peer[] peers) where T : INetworkPayload
        {
            var tag = NetworkTagAttribute.GetTag(typeof(T));

            if (tag != null)
            {
                using (Message m = new Message(tag.Value))
                {
                    m.Process(ref value);
                    SendMessage(m, tag.PacketFlags, channel, peers);
                }
            }
            else
            {
                NetworkLogger.Log("Cannot send a NetworkPayload with no NetworkTag!", LogCategory.PayloadProcessing, LogLevel.Warning);
            }
        }
Example #6
0
        public override void SendMessage <T>(T value, byte channel = 0, params IClient[] clients)
        {
            var tag = NetworkTagAttribute.GetTag(typeof(T));

            if (tag != null)
            {
                using (Message m = new Message(tag.Value))
                {
                    m.Process(ref value);
                    SendMessage(m, tag.PacketFlags, channel, clients);
                }
            }
            else
            {
                NetworkLogger.Log("Cannot send a NetworkPayload with no NetworkTag!", LogCategory.PayloadProcessing, LogLevel.Warning);
            }
        }
Example #7
0
        public virtual void SendMessage <T>(T value, byte channel = 0) where T : INetworkPayload
        {
            var tag = NetworkTagAttribute.GetTag(typeof(T));

            if (tag != null)
            {
                using (Message m = new Message(tag.Value))
                {
                    m.Process(ref value);
                    SendMessage(m, tag.PacketFlags, channel);
                }
            }
            else
            {
                NetworkLogger.Log("Cannot send a NetworkPayload with no NetworkTag!", LogCategory.PayloadProcessing);
            }
        }