Ejemplo n.º 1
0
                protected override Message DecodeMessage(byte[] buffer, ref int offset, ref int size, ref bool isAtEof, TimeSpan timeout)
                {
                    while (!isAtEof && size > 0)
                    {
                        int bytesRead = decoder.Decode(buffer, offset, size);
                        if (bytesRead > 0)
                        {
                            if (EnvelopeBuffer != null)
                            {
                                if (!object.ReferenceEquals(buffer, EnvelopeBuffer))
                                {
                                    System.Buffer.BlockCopy(buffer, offset, EnvelopeBuffer, EnvelopeOffset, bytesRead);
                                }
                                EnvelopeOffset += bytesRead;
                            }

                            offset += bytesRead;
                            size   -= bytesRead;
                        }

                        switch (decoder.CurrentState)
                        {
                        case ServerSessionDecoder.State.EnvelopeStart:
                            int envelopeSize = decoder.EnvelopeSize;
                            if (envelopeSize > maxBufferSize)
                            {
                                base.SendFault(FramingEncodingString.MaxMessageSizeExceededFault, timeout);

                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                          MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(maxBufferSize));
                            }
                            EnvelopeBuffer = bufferManager.TakeBuffer(envelopeSize);
                            EnvelopeOffset = 0;
                            EnvelopeSize   = envelopeSize;
                            break;

                        case ServerSessionDecoder.State.EnvelopeEnd:
                            if (EnvelopeBuffer != null)
                            {
                                Message message = null;

                                try
                                {
                                    message = messageEncoder.ReadMessage(new ArraySegment <byte>(EnvelopeBuffer, 0, EnvelopeSize), bufferManager, contentType);
                                }
                                catch (XmlException xmlException)
                                {
                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                                              new ProtocolException(SR.Format(SR.MessageXmlProtocolError), xmlException));
                                }

                                EnvelopeBuffer = null;
                                return(message);
                            }
                            break;

                        case ServerSessionDecoder.State.End:
                            isAtEof = true;
                            break;
                        }
                    }

                    return(null);
                }
 protected override int Decode(byte[] buffer, int offset, int size)
 {
     return(decoder.Decode(buffer, offset, size));
 }