Пример #1
0
        protected virtual void HandlePacket(byte[] readBuffer, int length, CancellationTokenSource cancellationTokenSource)
        {
            ThrowClosed();

            var stop = ProtocolWatch.Start("rp-receive-request");

            var index = 0;

            var converterHeader = protocolConverter.Resolve <ConverterHeader>(readBuffer, ref index);
            var requestHeader   = protocolConverter.Resolve <RequestHeader>(readBuffer, ref index);

            if (converterHeader.VersionMajor != 1)
            {
                throw new ProtocolException(ErrorCode.VersionNotSupported);
            }


            if (SecurityInject != null)
            {
                var securityContext = new SecurityInjectContext()
                {
                    ConverterHeader = converterHeader,
                    RequestHeader   = requestHeader,
                    HeaderEndIndex  = index,
                    Data            = sendBuffer,
                    Length          = length
                };

                SecurityInject?.ReceiveSecurity(ResolverSession, securityContext);
                length = securityContext.Length;
            }

            var resolvedContexts = protocolConverter.Resolve(readBuffer, ref index, length);
            var resolvedContent  = new DataContent(resolvedContexts);

            if (DataInject != null)
            {
                var injectContext = new DataInjectContext()
                {
                    ConverterHeader = converterHeader,
                    RequestHeader   = requestHeader,
                    DataContent     = resolvedContent,
                };

                DataInject.ReceiveData(ResolverSession, injectContext);
            }

            stop();


            var requestId = requestHeader.RequestId;

            if (requestHeader.RequestType == RequestType.Response)
            {
                HandleResponse(resolvedContent, requestId, requestHeader.CanBeCached, index);
            }
            else if (requestHeader.RequestType == RequestType.Request)
            {
                HandleRequest(resolvedContent, requestId, index, cancellationTokenSource);
            }
            else
            {
                throw new ProtocolException(ErrorCode.BadRequest);
            }
        }
Пример #2
0
        protected virtual int PrepareSendData(byte[] sendBuffer, RequestHeader requestHeader, DataContent dataContent)
        {
            var index = 4;

            var converterHeader        = this.ConverterHeader;
            var converterHeaderContext = new DataContext()
            {
                Data     = converterHeader,
                DataFlag = DataFlag.Header,
            };

            var requestHeaderContext = new DataContext()
            {
                Data     = requestHeader,
                DataFlag = DataFlag.Header
            };

            DataContext[] dataContexts = null;
            if (DataInject != null)
            {
                var injectContext = new DataInjectContext()
                {
                    ConverterHeader = ConverterHeader,
                    RequestHeader   = requestHeader,
                    DataContent     = dataContent,
                };

                dataContexts                = DataInject.SendData(ResolverSession, injectContext)?.ToArray();
                converterHeader             = injectContext.ConverterHeader;
                converterHeaderContext.Data = converterHeader;
            }

            protocolConverter.Apply(sendBuffer, ref index, converterHeaderContext, requestHeaderContext);
            var headerEndIndex = index;

            if (dataContexts != null)
            {
                protocolConverter.Apply(sendBuffer, ref index, dataContexts);
            }

            protocolConverter.Apply(sendBuffer, ref index, dataContent.DataContexts);

            if (SecurityInject != null)
            {
                var securityContext = new SecurityInjectContext()
                {
                    ConverterHeader = converterHeader,
                    RequestHeader   = requestHeader,
                    HeaderEndIndex  = headerEndIndex,
                    Data            = sendBuffer,
                    Length          = index
                };

                SecurityInject?.SendSecurity(ResolverSession, securityContext);
                index = securityContext.Length;
            }

            sendBuffer.InsertLength(index - 4);

            return(index);
        }