Example #1
0
            public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType)
            {
                if (stream == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("stream"));
                }

                WebContentFormat format = GetFormatForContentType(contentType);
                Message          message;

                switch (format)
                {
                case WebContentFormat.Json:
                    message = JsonMessageEncoder.ReadMessage(stream, maxSizeOfHeaders, contentType);
                    message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty);
                    break;

                case WebContentFormat.Xml:
                    message = TextMessageEncoder.ReadMessage(stream, maxSizeOfHeaders, contentType);
                    message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty);
                    break;

                case WebContentFormat.Raw:
                    message = RawMessageEncoder.ReadMessage(stream, maxSizeOfHeaders, contentType);
                    message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty);
                    break;

                default:
                    throw Fx.AssertAndThrow("This should never get hit because GetFormatForContentType shouldn't return a WebContentFormat other than Json, Xml, and Raw");
                }
                return(message);
            }
 public JsonBufferedMessageData(JsonMessageEncoder messageEncoder, int maxReaderPoolSize)
     : base(messageEncoder.RecycledStatePool)
 {
     this.messageEncoder = messageEncoder;
     readerPool          = new Pool <XmlDictionaryReader>(maxReaderPoolSize);
     onClose             = new OnXmlDictionaryReaderClose(OnXmlReaderClosed);
 }
Example #3
0
            public override ArraySegment <byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
            {
                if (message == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message"));
                }
                if (bufferManager == null)
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentNullException("bufferManager"), message);
                }
                if (maxMessageSize < 0)
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxMessageSize", maxMessageSize,
                                                                                        SR2.GetString(SR2.ValueMustBeNonNegative)), message);
                }
                if (messageOffset < 0 || messageOffset > maxMessageSize)
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentOutOfRangeException("messageOffset", messageOffset,
                                                                                        SR2.GetString(SR2.JsonValueMustBeInRange, 0, maxMessageSize)), message);
                }
                ThrowIfMismatchedMessageVersion(message);

                WebContentFormat messageFormat = ExtractFormatFromMessage(message);
                JavascriptCallbackResponseMessageProperty javascriptResponseMessageProperty;

                switch (messageFormat)
                {
                case WebContentFormat.Json:
                    return(JsonMessageEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset));

                case WebContentFormat.Xml:
                    if (message.Properties.TryGetValue <JavascriptCallbackResponseMessageProperty>(JavascriptCallbackResponseMessageProperty.Name, out javascriptResponseMessageProperty) &&
                        javascriptResponseMessageProperty != null &&
                        !String.IsNullOrEmpty(javascriptResponseMessageProperty.CallbackFunctionName))
                    {
                        throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR2.JavascriptCallbackNotsupported), message);
                    }
                    return(TextMessageEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset));

                case WebContentFormat.Raw:
                    if (message.Properties.TryGetValue <JavascriptCallbackResponseMessageProperty>(JavascriptCallbackResponseMessageProperty.Name, out javascriptResponseMessageProperty) &&
                        javascriptResponseMessageProperty != null &&
                        !String.IsNullOrEmpty(javascriptResponseMessageProperty.CallbackFunctionName))
                    {
                        throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR2.JavascriptCallbackNotsupported), message);
                    }
                    return(RawMessageEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset));

                default:
                    throw Fx.AssertAndThrow("This should never get hit because GetFormatForContentType shouldn't return a WebContentFormat other than Json, Xml, and Raw");
                }
            }
Example #4
0
            WebContentFormat GetFormatForContentType(string contentType)
            {
                WebContentFormat messageFormat;

                if (TryGetContentTypeMapping(contentType, out messageFormat) &&
                    (messageFormat != WebContentFormat.Default))
                {
                    if (DiagnosticUtility.ShouldTraceInformation)
                    {
                        if (string.IsNullOrEmpty(contentType))
                        {
                            contentType = "<null>";
                        }
                        TraceUtility.TraceEvent(TraceEventType.Information,
                                                TraceCode.RequestFormatSelectedFromContentTypeMapper,
                                                SR2.GetString(SR2.TraceCodeRequestFormatSelectedFromContentTypeMapper, messageFormat.ToString(), contentType));
                    }
                    return(messageFormat);
                }

                // Don't pass on null content types to IsContentTypeSupported methods -- they might throw.
                // If null content type isn't already mapped, return the default format of Raw.

                if (contentType == null)
                {
                    messageFormat = WebContentFormat.Raw;
                }
                else if (JsonMessageEncoder.IsContentTypeSupported(contentType))
                {
                    messageFormat = WebContentFormat.Json;
                }
                else if (TextMessageEncoder.IsContentTypeSupported(contentType))
                {
                    messageFormat = WebContentFormat.Xml;
                }
                else
                {
                    messageFormat = WebContentFormat.Raw;
                }

                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    TraceUtility.TraceEvent(TraceEventType.Information,
                                            TraceCode.RequestFormatSelectedByEncoderDefaults,
                                            SR2.GetString(SR2.TraceCodeRequestFormatSelectedByEncoderDefaults, messageFormat.ToString(), contentType));
                }

                return(messageFormat);
            }
Example #5
0
        static void Main(string[] args)
        {
            CompositeLogger.Use();
            ILog log = Logger.Get <Program>();

            var messageEncoder = new JsonMessageEncoder()
            {
                CompressionEnabled = true
            };
            var messageDecoder = new JsonMessageDecoder()
            {
                CompressionEnabled = true
            };
            var encoder = new ActorMessageEncoder(messageEncoder);
            var decoder = new ActorMessageDecoder(messageDecoder);

            var configruation = new ActorConfiguration(encoder, decoder);

            configruation.Build();

            var master = new ActorMaster(configruation);

            master.DataReceived += (s, e) =>
            {
                var text = decoder.Decode <string>(e.Data, e.DataOffset, e.DataLength);
                log.DebugFormat(text);
            };
            master.Bootup();

            while (true)
            {
                try
                {
                    string text = Console.ReadLine();
                    if (text.ToLowerInvariant() == "quit" || text.ToLowerInvariant() == "exit")
                    {
                        break;
                    }

                    var message = encoder.Encode(text);
                    master.Send("chat", message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
Example #6
0
            public override bool IsContentTypeSupported(string contentType)
            {
                if (contentType == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType");
                }

                WebContentFormat messageFormat;

                if (TryGetContentTypeMapping(contentType, out messageFormat) &&
                    (messageFormat != WebContentFormat.Default))
                {
                    return(true);
                }

                return(RawMessageEncoder.IsContentTypeSupported(contentType) || JsonMessageEncoder.IsContentTypeSupported(contentType) || TextMessageEncoder.IsContentTypeSupported(contentType));
            }
Example #7
0
            public override void WriteMessage(Message message, Stream stream)
            {
                if (message == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message"));
                }
                if (stream == null)
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentNullException("stream"), message);
                }
                ThrowIfMismatchedMessageVersion(message);

                WebContentFormat messageFormat = ExtractFormatFromMessage(message);
                JavascriptCallbackResponseMessageProperty javascriptResponseMessageProperty;

                switch (messageFormat)
                {
                case WebContentFormat.Json:
                    JsonMessageEncoder.WriteMessage(message, stream);
                    break;

                case WebContentFormat.Xml:
                    if (message.Properties.TryGetValue <JavascriptCallbackResponseMessageProperty>(JavascriptCallbackResponseMessageProperty.Name, out javascriptResponseMessageProperty) &&
                        javascriptResponseMessageProperty != null &&
                        !String.IsNullOrEmpty(javascriptResponseMessageProperty.CallbackFunctionName))
                    {
                        throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR2.JavascriptCallbackNotsupported), message);
                    }
                    TextMessageEncoder.WriteMessage(message, stream);
                    break;

                case WebContentFormat.Raw:
                    if (message.Properties.TryGetValue <JavascriptCallbackResponseMessageProperty>(JavascriptCallbackResponseMessageProperty.Name, out javascriptResponseMessageProperty) &&
                        javascriptResponseMessageProperty != null &&
                        !String.IsNullOrEmpty(javascriptResponseMessageProperty.CallbackFunctionName))
                    {
                        throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR2.JavascriptCallbackNotsupported), message);
                    }
                    RawMessageEncoder.WriteMessage(message, stream);
                    break;

                default:
                    throw Fx.AssertAndThrow("This should never get hit because GetFormatForContentType shouldn't return a WebContentFormat other than Json, Xml, and Raw");
                }
            }
 public JsonBufferedMessageWriter(JsonMessageEncoder messageEncoder)
 {
     this.messageEncoder = messageEncoder;
 }
 public JsonMessageEncoderFactory(Encoding writeEncoding, int maxReadPoolSize, int maxWritePoolSize, XmlDictionaryReaderQuotas quotas, bool crossDomainScriptAccessEnabled)
 {
     messageEncoder = new JsonMessageEncoder(writeEncoding, maxReadPoolSize, maxWritePoolSize, quotas, crossDomainScriptAccessEnabled);
 }
 public AmqpServiceBusConfiguration()
 {
     MessageEncoder = new JsonMessageEncoder();
 }
 public JsonBufferedMessageWriter(JsonMessageEncoder messageEncoder)
 {
     this.messageEncoder = messageEncoder;
 }
 public JsonBufferedMessageData(JsonMessageEncoder messageEncoder, int maxReaderPoolSize)
     : base(messageEncoder.RecycledStatePool)
 {
     this.messageEncoder = messageEncoder;
     readerPool = new Pool<XmlDictionaryReader>(maxReaderPoolSize);
     onClose = new OnXmlDictionaryReaderClose(OnXmlReaderClosed);
 }
 public JsonMessageEncoderFactory(Encoding writeEncoding, int maxReadPoolSize, int maxWritePoolSize, XmlDictionaryReaderQuotas quotas, bool crossDomainScriptAccessEnabled)
 {
     messageEncoder = new JsonMessageEncoder(writeEncoding, maxReadPoolSize, maxWritePoolSize, quotas, crossDomainScriptAccessEnabled);
 }
Example #14
0
 public AmqpServiceBusConfiguration()
 {
     MessageEncoder = new JsonMessageEncoder();
 }