예제 #1
0
        public bool Write(IoBuffer output, DuplexMessage message)
        {
            var writeOk = false;

            try
            {
                using (var scope = ObjectHost.Host.BeginLifetimeScope())
                {
                    var filters       = new List <IMessageFilter>();
                    var dataStack     = new StackMessageDataContaner();
                    var filterFactory = scope.Resolve <MessageFilterFactory>();
                    filters.Add(filterFactory.CreateFilter(
                                    message.Header.FilterCode[0], message.Header.FilterType & MessageFilterType.Compression));
                    filters.Add(filterFactory.CreateFilter(
                                    message.Header.FilterCode[0], message.Header.FilterType & MessageFilterType.Crypto));
                    var identifierBinary = message.Header.Identifier.FromHex();
                    dataStack.Push(identifierBinary);
                    dataStack.Push(message.GetContentBinary());
                    FilterResult result = new FilterResult {
                        OK = true
                    };
                    foreach (var filter in filters)
                    {
                        result = filter.Out(message.Header, dataStack);
                        if (!result.OK)
                        {
                            break;
                        }
                    }
                    if (result.OK)
                    {
                        var body = dataStack.Take();
                        output.PutInt32(49 + body.Length);
                        output.Put(message.Header.Version.ToByte());
                        output.PutInt16(Convert.ToInt16(message.Header.CommandCode));
                        output.Put(message.Header.ErrorCode.ToByte());
                        output.Put(message.Header.MessageType.ToByte());
                        output.Put(identifierBinary);
                        output.Put(message.Header.MessageID.FromBase64());
                        output.Put(message.Header.FilterType.ToByte());
                        output.Put(message.Header.FilterCode);
                        output.Put(message.Header.SerializeMode.ToByte());
                        output.Flip();
                        var header = output.GetRemainingArray();
                        dataStack.Push(body);
                        dataStack.Push(header);
                        result = filterFactory
                                 .CreateFilter(message.Header.FilterCode[0],
                                               message.Header.FilterType & MessageFilterType.Checksum)
                                 .Out(message.Header, dataStack);
                        writeOk = result.OK;
                        if (writeOk)
                        {
                            output.Put(dataStack.Take());
                            output.Put(body);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ErrorCode.SysError.ToString(), ex);
            }
            return(writeOk);
        }
예제 #2
0
        public DuplexMessage Read(IoBuffer input)
        {
            DuplexMessage message = null;

            byte[] identifier = null;
            byte[] messageId  = null;
            try
            {
                using (var scope = ObjectHost.Host.BeginLifetimeScope())
                {
                    // skip 4 bytes length
                    input.Skip(4);
                    var filters     = new List <IMessageFilter>();
                    var version     = input.Get().ToEnum <MessageVersion>();
                    var commandCode = input.GetInt16().ToEnum <CommandCode>();
                    var errorCode   = input.Get().ToEnum <ErrorCode>();
                    var messageType = input.Get().ToEnum <MessageType>();
                    identifier = input.GetArray(16);
                    messageId  = input.GetArray(16);
                    var filterType    = input.Get().ToEnum <MessageFilterType>();
                    var filterCode    = new byte[] { input.Get(), input.Get() };
                    var serializeMode = input.Get().ToEnum <SerializeMode>();
                    input.Position = 0;
                    var header = input.GetArray(45);

                    var checksum = input.GetArray(4);
                    var body     = input.GetRemainingArray();

                    //make data stack like
                    //- checksum
                    //- header without checksum
                    //- body
                    //- identifier
                    var dataStack = new StackMessageDataContaner();
                    dataStack.Push(identifier);
                    dataStack.Push(body);
                    dataStack.Push(header);
                    dataStack.Push(checksum);

                    FilterResult result = new FilterResult {
                        OK = true
                    };
                    var filterFactory = scope.Resolve <MessageFilterFactory>();
                    filters.Add(filterFactory.CreateFilter(filterCode[0], filterType & MessageFilterType.Checksum));
                    filters.Add(filterFactory.CreateFilter(filterCode[0], filterType & MessageFilterType.Crypto));
                    filters.Add(filterFactory.CreateFilter(filterCode[0], filterType & MessageFilterType.Compression));

                    foreach (var filter in filters)
                    {
                        result = filter.In(dataStack);
                        if (!result.OK)
                        {
                            break;
                        }
                    }

                    if (result.OK)
                    {
                        message = DuplexMessage.CreateMessage(
                            new MessageHeader(
                                messageId.ToBase64(),
                                version,
                                identifier.ToHex(),
                                filterCode,
                                filterType,
                                errorCode == ErrorCode.NoError ? MessageState.Success : MessageState.Fail,
                                errorCode,
                                serializeMode,
                                commandCode,
                                messageType), dataStack.Take());
                    }
                    else
                    {
                        message = DuplexMessage.CreateMessage(
                            new MessageHeader(
                                messageId.ToBase64(),
                                version,
                                identifier.ToHex(),
                                filterCode,
                                MessageFilterType.Checksum,
                                MessageState.Fail,
                                result.Error,
                                SerializeMode.None,
                                commandCode,
                                messageType), null);
                    }
                }
            }
            catch (Exception ex)
            {
                //dont do anything, may be let requestor retry.
                Log.Error(ErrorCode.SysError.ToString(), ex);
            }
            return(message);
        }