public override Task WriteAsync(IChannelHandlerContext context, object message)
        {
            var sendContext = message as SendContext;

            Debug.Assert(sendContext != null);
            var coreMessage = sendContext.Message as ICoreMessage;

            Debug.Assert(coreMessage != null);

            var buffer = context.Allocator.Buffer();

            try
            {
                CoreMessageEncoder.Encode(coreMessage, buffer);

                return(base.WriteAsync(context, new UdpMessage
                {
                    Flag = 43981,
                    Content = buffer,
                    EndPoint = sendContext.UdpEndPoint
                }));
            }
            catch (Exception ex)
            {
                buffer.Release();
                ex.Rethrow();
                throw;
            }
        }
Beispiel #2
0
        public override Task WriteAsync(IChannelHandlerContext context, object message)
        {
            var sendContext = message as SendContext;
            var coreMessage = (ICoreMessage)sendContext?.Message;

            var buffer = context.Allocator.Buffer();

            try
            {
                CoreMessageEncoder.Encode(coreMessage, buffer);

                if (sendContext == null)
                {
                    return(Task.CompletedTask);
                }

                return(base.WriteAsync(context, new UdpMessage
                {
                    Flag = 43981,
                    Content = buffer,
                    EndPoint = sendContext.UdpEndPoint
                }));
            }
            catch (Exception ex)
            {
                buffer.Release();
                ex.Rethrow();
                throw;
            }
        }
Beispiel #3
0
        protected override void Encode(IChannelHandlerContext context, SendContext message, List <object> output)
        {
            var buffer = message.Message as IByteBuffer;

            if (buffer == null)
            {
                throw new ProudException($"{nameof(SendContextEncoder)} can only handle {nameof(IByteBuffer)}");
            }

            try
            {
                var          data        = buffer.ToArray();
                ICoreMessage coreMessage = new RmiMessage(data);
                var          session     = context.Channel.GetAttribute(ChannelAttributes.Session).Get();
                if (session == null)
                {
                    return;
                }

                var server = context.Channel.GetAttribute(ChannelAttributes.Server).Get();

                if (data.Length > server.Configuration.MessageMaxLength)
                {
                    throw new ProudException("Message is longer than max messagelength!");
                }
                //else if (data.Length > server.Configuration.MaxUncompressedMessageLength &&
                //    coreMessage.GetType() != typeof(CompressedMessage))
                //    message.SendOptions.Compress = true;

                if (message.SendOptions.Compress)
                {
                    data        = CoreMessageEncoder.Encode(coreMessage);
                    coreMessage = new CompressedMessage(data.Length, data.CompressZLib());
                }

                if (message.SendOptions.Encrypt)
                {
                    data = CoreMessageEncoder.Encode(coreMessage);
                    using (var src = new MemoryStream(data))
                        using (var dst = new MemoryStream())
                        {
                            session.Crypt?.Encrypt(context.Allocator, EncryptMode.Secure, src, dst, true);
                            data = dst.ToArray();
                        }
                    coreMessage = new EncryptedReliableMessage(data, EncryptMode.Secure);
                }

                output.Add(coreMessage);
            }
            finally
            {
                buffer.Release();
            }
        }
        protected override void Encode(IChannelHandlerContext context, SendContext message, List <object> output)
        {
            if (!(message.Message is IByteBuffer buffer))
            {
                throw new ProudException($"{nameof(SendContextEncoder)} can only handle {nameof(IByteBuffer)}");
            }

            try
            {
                var          data        = buffer.GetIoBuffer().ToArray();
                ICoreMessage coreMessage = new RmiMessage(data);

                if (message.SendOptions.Compress)
                {
                    data = CoreMessageEncoder.Encode(coreMessage);
                    if (data.Length > 500)
                    {
                        coreMessage = new CompressedMessage(data.Length, data.CompressZLib());
                    }
                }

                if (message.SendOptions.Encrypt)
                {
                    data = CoreMessageEncoder.Encode(coreMessage);
                    var session = context.Channel.GetAttribute(ChannelAttributes.Session).Get();
                    using (var src = new MemoryStream(data))
                        using (var dst = new MemoryStream())
                        {
                            session.Crypt.Encrypt(context.Allocator, EncryptMode.Secure, src, dst, true);
                            data = dst.ToArray();
                        }

                    coreMessage = new EncryptedReliableMessage(data, EncryptMode.Secure);
                }

                output.Add(coreMessage);
            }
            finally
            {
                buffer.Release();
            }
        }