Exemple #1
0
        /// <summary>
        /// Sends a packet of data through this connection mode.
        /// </summary>
        /// <param name="data"></param>
        public async Task Send(byte[] data)
        {
            try
            {
                Logger.Log(Logger.Level.Info, "Attempting to send packet using the underlying connection instance");

                // Make sure the packet is valid
                if (data == null || data.Length == 0)
                {
                    throw new ArgumentNullException(nameof(data));
                }

                Logger.Log(Logger.Level.Debug, $"\tUnserialized Length: {data.Length}");

                // Using the overridden method (hopefully), serialize
                var outgoing = SerializePacket(data)
                               ?? throw new Exception("Serializing the packet resulted in a null value.");

                Logger.Log(Logger.Level.Debug, $"\tSerialized Length: {outgoing.Length}");

                // Send the data using the socket wrapper
                await ClientInstance.Send(outgoing);

                Logger.Log(Logger.Level.Info, $"Sent {outgoing.Length} bytes successfully");
            }
            catch (Exception ex)
            {
                Logger.Log(Logger.Level.Error, $"Sending failed.\n\n{ex.Message}");
                throw;
            }
        }