public override void HandleFrame(Frame frame)
        {
            lock (_closingLock)
            {
                if (!m_closing)
                {
                    base.HandleFrame(frame);
                    return;
                }
            }

            if (!m_closeServerInitiated && (frame.Type == Constants.FrameMethod))
            {
                MethodBase method = Connection.Protocol.DecodeMethodFrom(frame.GetReader());
                if ((method.ProtocolClassId == m_closeClassId)
                    && (method.ProtocolMethodId == m_closeMethodId))
                {
                    base.HandleFrame(frame);
                    return;
                }

                if ((method.ProtocolClassId == m_closeOkClassId)
                    && (method.ProtocolMethodId == m_closeOkMethodId))
                {
                    // This is the reply (CloseOk) we were looking for
                    // Call any listener attached to this session
                    Handler();
                }
            }

            // Either a non-method frame, or not what we were looking
            // for. Ignore it - we're quiescing.
        }
        public override void HandleFrame(Frame frame)
        {
            if (frame.Type == Constants.FrameMethod)
            {
                MethodBase method = Connection.Protocol.DecodeMethodFrom(frame.GetReader());
                if ((method.ProtocolClassId == ChannelCloseOk.ClassId)
                    && (method.ProtocolMethodId == ChannelCloseOk.MethodId))
                {
                    // This is the reply we were looking for. Release
                    // the channel with the reason we were passed in
                    // our constructor.
                    Close(m_reason);
                }
                else if ((method.ProtocolClassId == ChannelClose.ClassId)
                         && (method.ProtocolMethodId == ChannelClose.MethodId))
                {
                    // We're already shutting down the channel, so
                    // just send back an ok.
                    Transmit(CreateChannelCloseOk());
                }
            }

            // Either a non-method frame, or not what we were looking
            // for. Ignore it - we're quiescing.
        }
Exemple #3
0
 public override void HandleFrame(Frame frame)
 {
     Command cmd = m_assembler.HandleFrame(frame);
     if (cmd != null)
     {
         OnCommandReceived(cmd);
     }
 }
        public Command HandleFrame(Frame f)
        {
            switch (m_state)
            {
                case AssemblyState.ExpectingMethod:
                {
                    if (f.Type != Constants.FrameMethod)
                    {
                        throw new UnexpectedFrameException(f);
                    }
                    m_command.Method = m_protocol.DecodeMethodFrom(f.GetReader());
                    m_state = m_command.Method.HasContent
                        ? AssemblyState.ExpectingContentHeader
                        : AssemblyState.Complete;
                    return CompletedCommand();
                }
                case AssemblyState.ExpectingContentHeader:
                {
                    if (f.Type != Constants.FrameHeader)
                    {
                        throw new UnexpectedFrameException(f);
                    }
                    NetworkBinaryReader reader = f.GetReader();
                    m_command.Header = m_protocol.DecodeContentHeaderFrom(reader);
                    m_remainingBodyBytes = m_command.Header.ReadFrom(reader);
                    UpdateContentBodyState();
                    return CompletedCommand();
                }
                case AssemblyState.ExpectingContentBody:
                {
                    if (f.Type != Constants.FrameBody)
                    {
                        throw new UnexpectedFrameException(f);
                    }
                    byte[] fragment = f.Payload;
                    m_command.AppendBodyFragment(fragment);
                    if ((ulong)fragment.Length > m_remainingBodyBytes)
                    {
                        throw new MalformedFrameException
                            (string.Format("Overlong content body received - {0} bytes remaining, {1} bytes received",
                                m_remainingBodyBytes,
                                fragment.Length));
                    }
                    m_remainingBodyBytes -= (ulong)fragment.Length;
                    UpdateContentBodyState();
                    return CompletedCommand();
                }
                case AssemblyState.Complete:
                default:
#if NETFX_CORE
                    Debug.WriteLine("Received frame in invalid state {0}; {1}", m_state, f);
#else
                    Trace.Fail(string.Format("Received frame in invalid state {0}; {1}", m_state, f));
#endif
                    return null;
            }
        }
        public static void CheckEmptyFrameSize() {
            Frame f = new Frame(CommonFraming.Constants.FrameBody, 0, m_emptyByteArray);
            MemoryStream stream = new MemoryStream();
            NetworkBinaryWriter writer = new NetworkBinaryWriter(stream);
            f.WriteTo(writer);
            long actualLength = stream.Length;

            if (EmptyFrameSize != actualLength) {
                string message =
                    string.Format("EmptyFrameSize is incorrect - defined as {0} where the computed value is in fact {1}.",
                                  EmptyFrameSize,
                                  actualLength);
                throw new ProtocolViolationException(message);
            }
        }
 public UnexpectedFrameException(Frame frame)
     : base("A frame of this type was not expected at this time")
 {
     m_frame = frame;
 }
 public void WriteFrame(Frame f)
 {
     m_frameHandler.WriteFrame(f);
     m_heartbeatWrite.Set();
 }
 public void WriteFrame(Frame frame)
 {
     lock (m_writer)
     {
         frame.WriteTo(m_writer);
         m_writer.Flush();
         //Console.WriteLine("OUTBOUND:");
         //DebugUtil.DumpProperties(frame, Console.Out, 2);
     }
 }
 public void WriteFrame(Frame frame)
 {
     lock (m_writer)
     {
         frame.WriteTo(m_writer);
         m_writer.Flush();
     }
 }
Exemple #10
0
 public abstract void HandleFrame(Frame frame);
        public void Transmit(int channelNumber, ConnectionBase connection)
        {
            Frame frame = new Frame(CommonFraming.Constants.FrameMethod, channelNumber);
            NetworkBinaryWriter writer = frame.GetWriter();
            writer.Write((ushort) m_method.ProtocolClassId);
            writer.Write((ushort) m_method.ProtocolMethodId);
            MethodArgumentWriter argWriter = new MethodArgumentWriter(writer);
            m_method.WriteArgumentsTo(argWriter);
            argWriter.Flush();
            connection.WriteFrame(frame);

            if (m_method.HasContent) {
                byte[] body = Body;

                frame = new Frame(CommonFraming.Constants.FrameHeader, channelNumber);
                writer = frame.GetWriter();
                writer.Write((ushort) m_header.ProtocolClassId);
                m_header.WriteTo(writer, (ulong) body.Length);
                connection.WriteFrame(frame);

                int frameMax = (int) Math.Min(int.MaxValue, connection.FrameMax);
                int bodyPayloadMax = (frameMax == 0)
                    ? body.Length
                    : frameMax - EmptyFrameSize;
                for (int offset = 0; offset < body.Length; offset += bodyPayloadMax) {
                    int remaining = body.Length - offset;

                    frame = new Frame(CommonFraming.Constants.FrameBody, channelNumber);
                    writer = frame.GetWriter();
                    writer.Write(body, offset,
                                 (remaining < bodyPayloadMax) ? remaining : bodyPayloadMax);
                    connection.WriteFrame(frame);
                }
            }
        }
 public void TransmitAsSingleFrame(int channelNumber, Connection connection)
 {
     var frame = new Frame(Constants.FrameMethod, channelNumber);
     NetworkBinaryWriter writer = frame.GetWriter();
     writer.Write((ushort)Method.ProtocolClassId);
     writer.Write((ushort)Method.ProtocolMethodId);
     var argWriter = new MethodArgumentWriter(writer);
     Method.WriteArgumentsTo(argWriter);
     argWriter.Flush();
     connection.WriteFrame(frame);
 }
 public void WriteFrame(Frame frame)
 {
     lock (m_writer)
     {
         m_socket.Client.Poll(m_writeableStateTimeout, SelectMode.SelectWrite);
         frame.WriteTo(m_writer);
         m_writer.Flush();
     }
 }
        public override void HandleFrame(Frame frame)
        {
            if (frame.Type == CommonFraming.Constants.FrameMethod) {
                MethodBase method = Connection.Protocol.DecodeMethodFrom(frame.GetReader());
                if ((method.ProtocolClassId == m_replyClassId)
                    && (method.ProtocolMethodId == m_replyMethodId))
                {
                    // This is the reply we were looking for. Release
                    // the channel with the reason we were passed in
                    // our constructor.
                    Close(m_reason);
                    return;
                }
            }

            // Either a non-method frame, or not what we were looking
            // for. Ignore it - we're quiescing.
        }
 public void WriteFrame(Frame f)
 {
     m_frameHandler.WriteFrame(f);
 }