Example #1
0
        public byte[] EncodeMessage(Message message)
        {
            string json = null;

            switch (message.type)
            {
            case MessageTypes.Invocation:
            case MessageTypes.StreamInvocation:
                // While message contains all informations already, the spec states that no additional field are allowed in messages
                //  So we are creating 'specialized' messages here to send to the server.
                json = this.Encoder.EncodeAsText <InvocationMessage>(new InvocationMessage()
                {
                    type         = message.type,
                    invocationId = message.invocationId,
                    nonblocking  = message.nonblocking,
                    target       = message.target,
                    arguments    = message.arguments
                });
                break;

            case MessageTypes.CancelInvocation:
                json = this.Encoder.EncodeAsText <CancelInvocationMessage>(new CancelInvocationMessage()
                {
                    invocationId = message.invocationId
                });
                break;
            }

            return(!string.IsNullOrEmpty(json) ? JsonProtocol.WithSeparator(json) : null);
        }
Example #2
0
        public BufferSegment EncodeMessage(Message message)
        {
            string json = null;

            // While message contains all informations already, the spec states that no additional field are allowed in messages
            //  So we are creating 'specialized' messages here to send to the server.
            switch (message.type)
            {
            case MessageTypes.StreamItem:
                json = this.Encoder.EncodeAsText <StreamItemMessage>(new StreamItemMessage()
                {
                    type         = message.type,
                    invocationId = message.invocationId,
                    item         = message.item
                });
                break;

            case MessageTypes.Completion:
                if (!string.IsNullOrEmpty(message.error))
                {
                    json = this.Encoder.EncodeAsText <CompletionWithError>(new CompletionWithError()
                    {
                        type         = MessageTypes.Completion,
                        invocationId = message.invocationId,
                        error        = message.error
                    });
                }
                else if (message.result != null)
                {
                    json = this.Encoder.EncodeAsText <CompletionWithResult>(new CompletionWithResult()
                    {
                        type         = MessageTypes.Completion,
                        invocationId = message.invocationId,
                        result       = message.result
                    });
                }
                else
                {
                    json = this.Encoder.EncodeAsText <Completion>(new Completion()
                    {
                        type         = MessageTypes.Completion,
                        invocationId = message.invocationId
                    });
                }
                break;

            case MessageTypes.Invocation:
            case MessageTypes.StreamInvocation:
                if (message.streamIds != null)
                {
                    json = this.Encoder.EncodeAsText <UploadInvocationMessage>(new UploadInvocationMessage()
                    {
                        type         = message.type,
                        invocationId = message.invocationId,
                        nonblocking  = message.nonblocking,
                        target       = message.target,
                        arguments    = message.arguments,
                        streamIds    = message.streamIds
                    });
                }
                else
                {
                    json = this.Encoder.EncodeAsText <InvocationMessage>(new InvocationMessage()
                    {
                        type         = message.type,
                        invocationId = message.invocationId,
                        nonblocking  = message.nonblocking,
                        target       = message.target,
                        arguments    = message.arguments
                    });
                }
                break;

            case MessageTypes.CancelInvocation:
                json = this.Encoder.EncodeAsText <CancelInvocationMessage>(new CancelInvocationMessage()
                {
                    invocationId = message.invocationId
                });
                break;

            case MessageTypes.Ping:
                json = this.Encoder.EncodeAsText <PingMessage>(new PingMessage());
                break;
            }

            if (HTTPManager.Logger.Level == Logger.Loglevels.All)
            {
                HTTPManager.Logger.Verbose("JsonProtocol", "EncodeMessage - json: " + json);
            }

            return(!string.IsNullOrEmpty(json) ? JsonProtocol.WithSeparator(json) : BufferSegment.Empty);
        }