Пример #1
0
        /// <summary>
        /// Send a <see cref="NetObject"/>.
        /// </summary>
        /// <param name="netobj">The NetObject to send. Cannot be null.</param>
        /// <param name="socket">The client to send the data to.</param>
        /// <exception cref="NullReferenceException"></exception>
        public void Send(Socket socket, bool deleteOnSend = true)
        {
            if (socket == null)
            {
                return;
            }

            Task.Run(() =>
            {
                Type type = this.GetType();

                if (!_GenericConstructors.TryGetValue(type, out ConstructorInfo ctor))
                {
                    ctor = typeof(NetData <>).MakeGenericType(type).GetConstructor(new [] { type });
                    _GenericConstructors.TryAdd(type, ctor);
                }

                OnSend?.BeginInvoke(this, type, socket, null, null);

                ((ISendible)ctor.Invoke(new object[] { this })).Send(socket);
                if (deleteOnSend)
                {
                    this.Delete();
                }
            });
        }
Пример #2
0
        /// <summary>
        /// Sends packet to the destination.
        /// </summary>
        public void Send(NetworkMessage msg)
        {
            if (OnSend != null)
            {
                OnSend.BeginInvoke(msg, null, null);
            }

            pipe.Write(msg.GetData(), 0, msg.Length);
        }