public override void OnPreAdd(IoFilterChain parent, String name, INextFilter nextFilter)
 {
     if (_count == 0)
         _filter.Init();
     _count++;
     _filter.OnPreAdd(parent, name, nextFilter);
 }
 public override void OnPostRemove(IoFilterChain parent, String name, INextFilter nextFilter)
 {
     _filter.OnPostRemove(parent, name, nextFilter);
     _count--;
     if (_count == 0)
         _filter.Destroy();
 }
Пример #3
0
 public IoFilterEvent(INextFilter nextFilter, IoEventType eventType, IoSession session, Object parameter)
     : base(eventType, session, parameter)
 {
     if (nextFilter == null)
         throw new ArgumentNullException("nextFilter");
     _nextFilter = nextFilter;
 }
Пример #4
0
        /// <inheritdoc/>
        public override void MessageReceived(INextFilter nextFilter, IoSession session, Object message)
        {
            //if (log.IsDebugEnabled)
            //    log.DebugFormat("Processing a MESSAGE_RECEIVED for session {0}", session.Id);

            IoBuffer input = message as IoBuffer;
            if (input == null)
            {
                nextFilter.MessageReceived(session, message);
                return;
            }

            IProtocolDecoder decoder = _factory.GetDecoder(session);
            IProtocolDecoderOutput decoderOutput = GetDecoderOut(session, nextFilter);

            // Loop until we don't have anymore byte in the buffer,
            // or until the decoder throws an unrecoverable exception or
            // can't decoder a message, because there are not enough
            // data in the buffer
            while (input.HasRemaining)
            {
                Int32 oldPos = input.Position;
                try
                {
                    // TODO may not need lock on UDP
                    lock (session)
                    {
                        // Call the decoder with the read bytes
                        decoder.Decode(session, input, decoderOutput);
                    }

                    // Finish decoding if no exception was thrown.
                    decoderOutput.Flush(nextFilter, session);
                }
                catch (Exception ex)
                {
                    ProtocolDecoderException pde = ex as ProtocolDecoderException;
                    if (pde == null)
                        pde = new ProtocolDecoderException(null, ex);
                    if (pde.Hexdump == null)
                    {
                        // Generate a message hex dump
                        Int32 curPos = input.Position;
                        input.Position = oldPos;
                        pde.Hexdump = input.GetHexDump();
                        input.Position = curPos;
                    }

                    decoderOutput.Flush(nextFilter, session);
                    nextFilter.ExceptionCaught(session, pde);

                    // Retry only if the type of the caught exception is
                    // recoverable and the buffer position has changed.
                    // We check buffer position additionally to prevent an
                    // infinite loop.
                    if (!(ex is RecoverableProtocolDecoderException) || input.Position == oldPos)
                        break;
                }
            }
        }
Пример #5
0
 /// <inheritdoc/>
 public override void MessageReceived(INextFilter nextFilter, IoSession session, Object message)
 {
     if (IsBlocked(session))
         BlockSession(session);
     else
         // forward if not blocked
         base.MessageReceived(nextFilter, session, message);
 }
Пример #6
0
 /// <inheritdoc/>
 public override void FilterWrite(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
 {
     IoBuffer buf = writeRequest.Message as IoBuffer;
     if (buf == null)
         throw new ArgumentException("This filter should only buffer IoBuffer objects");
     else
         Write(session, buf);
 }
Пример #7
0
 /// <inheritdoc/>
 public override void SessionIdle(INextFilter nextFilter, IoSession session, IdleStatus status)
 {
     if (IsBlocked(session))
         BlockSession(session);
     else
         // forward if not blocked
         base.SessionIdle(nextFilter, session, status);
 }
Пример #8
0
 /// <inheritdoc/>
 public override void SessionClosed(INextFilter nextFilter, IoSession session)
 {
     if (IsBlocked(session))
         BlockSession(session);
     else
         // forward if not blocked
         base.SessionClosed(nextFilter, session);
 }
Пример #9
0
 /// <inheritdoc/>
 public override void MessageSent(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
 {
     if (IsBlocked(session))
         BlockSession(session);
     else
         // forward if not blocked
         base.MessageSent(nextFilter, session, writeRequest);
 }
        public override void SessionCreated(INextFilter nextFilter, IoSession session)
        {
            foreach (KeyValuePair<String, Object> pair in _attributes)
            {
                session.SetAttribute(pair.Key, pair.Value);
            }

            base.SessionCreated(nextFilter, session);
        }
 public override void SessionCreated(INextFilter nextFilter, IoSession session)
 {
     if (!IsConnectionOk(session))
     {
         Debug.WriteLine("Connections coming in too fast; closing.");
         session.Close(true);
     }
     base.SessionCreated(nextFilter, session);
 }
Пример #12
0
 /// <inheritdoc/>
 public override void SessionCreated(INextFilter nextFilter, IoSession session)
 {
     if (!IsConnectionOk(session))
     {
         if (log.IsWarnEnabled)
             log.Warn("Connections coming in too fast; closing.");
         session.Close(true);
     }
     base.SessionCreated(nextFilter, session);
 }
Пример #13
0
        public override void OnPreAdd(IoFilterChain parent, String name, INextFilter nextFilter)
        {
            if (parent.Contains<SslFilter>())
                throw new InvalidOperationException("Only one SSL filter is permitted in a chain.");

            IoSession session = parent.Session;
            session.SetAttribute(NEXT_FILTER, nextFilter);
            // Create a SSL handler and start handshake.
            SslHandler handler = new SslHandler(this, session);
            session.SetAttribute(SSL_HANDLER, handler);
        }
Пример #14
0
 public override void ExceptionCaught(INextFilter nextFilter, IoSession session, Exception cause)
 {
     Log.Error(ErrorCode.UnhandledExceptionCaught, cause);
     var counter = session.GetAttribute<int>(KeyName.SESSION_ERROR_COUNTER);
     if (counter >= MAX_UNCAUGHT_SESSION_ERROR_COUNT) session.Close(true);
     else
     {
         session.SetAttribute(KeyName.SESSION_ERROR_COUNTER, ++counter);
         base.ExceptionCaught(nextFilter, session, cause);
     }
 }
Пример #15
0
 /// <inheritdoc/>
 public override void SessionOpened(INextFilter nextFilter, IoSession session)
 {
     if ((_eventTypes & IoEventType.SessionOpened) == IoEventType.SessionOpened)
     {
         IoFilterEvent ioe = new IoFilterEvent(nextFilter, IoEventType.SessionOpened, session, null);
         FireEvent(ioe);
     }
     else
     {
         base.SessionOpened(nextFilter, session);
     }
 }
Пример #16
0
 public override void SessionIdle(INextFilter nextFilter, IoSession session, IdleStatus status)
 {
     if ((_eventTypes & IoEventType.SessionIdle) == IoEventType.SessionIdle)
     {
         IoFilterEvent ioe = new IoFilterEvent(nextFilter, IoEventType.SessionIdle, session, status);
         FireEvent(ioe);
     }
     else
     {
         base.SessionIdle(nextFilter, session, status);
     }
 }
Пример #17
0
 public override void SessionClosed(INextFilter nextFilter, IoSession session)
 {
     SslHandler handler = GetSslSessionHandler(session);
     try
     {
         // release resources
         handler.Destroy();
     }
     finally
     {
         // notify closed session
         base.SessionClosed(nextFilter, session);
     }
 }
Пример #18
0
        /// <inheritdoc/>
        public override void FilterWrite(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
        {
            IoEvent ioe = new IoEvent(IoEventType.Write, session, writeRequest);
            if (_queueHandler.Accept(this, ioe))
            {
                nextFilter.FilterWrite(session, writeRequest);
                IWriteFuture writeFuture = writeRequest.Future;
                if (writeFuture == null)
                    return;

                // We can track the write request only when it has a future.
                _queueHandler.Offered(this, ioe);
                writeFuture.Complete += (s, e) => _queueHandler.Polled(this, ioe);
            }
        }
Пример #19
0
            public override void FilterWrite(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
            {
                _counter++;

                IoBuffer buf = writeRequest.Message as IoBuffer;
                if (buf == null)
                    throw new AssertFailedException("Wrong message type");
                if (_counter == 3)
                {
                    Assert.AreEqual(1, buf.Limit);
                    Assert.AreEqual(0, buf.Get());
                }
                else
                {
                    Assert.AreEqual(10, buf.Limit);
                }
            }
Пример #20
0
 public override void SessionCreated(INextFilter nextFilter, IoSession session)
 {
     Metric.Gauge("01.message.read.count", () => session.Service.Statistics.ReadMessages, Unit.Requests);
     Metric.Gauge("02.message.read.bytes", () => session.Service.Statistics.ReadBytes / 1024 / 1024, Unit.MegaBytes);
     Metric.Gauge("03.message.read.throughput", () => session.Service.Statistics.ReadMessagesThroughput, Unit.Requests);
     Metric.Gauge("04.message.read.throughput.max", () => session.Service.Statistics.LargestReadMessagesThroughput, Unit.Requests);
     Metric.Gauge("05.message.read.bytes.throughput", () => session.Service.Statistics.ReadBytesThroughput / 1024, Unit.KiloBytes);
     Metric.Gauge("06.message.read.bytes.throughput.max", () => session.Service.Statistics.LargestReadBytesThroughput / 1024, Unit.KiloBytes);
     Metric.Gauge("07.message.write.count", () => session.Service.Statistics.WrittenMessages, Unit.Results);
     Metric.Gauge("08.message.write.bytes", () => session.Service.Statistics.WrittenBytes / 1024 / 1024, Unit.MegaBytes);
     Metric.Gauge("09.message.write.throughput", () => session.Service.Statistics.WrittenMessagesThroughput, Unit.Results);
     Metric.Gauge("10.message.write.throughput.max", () => session.Service.Statistics.LargestWrittenMessagesThroughput, Unit.Requests);
     Metric.Gauge("11.message.write.bytes.throughput", () => session.Service.Statistics.WrittenBytesThroughput / 1024, Unit.KiloBytes);
     Metric.Gauge("12.message.write.bytes.throughput.max", () => session.Service.Statistics.LargestWrittenBytesThroughput / 1024, Unit.KiloBytes);
     Metric.Gauge("13.message.write.queue.count", () => session.Service.Statistics.ScheduledWriteMessages, Unit.Results);
     Metric.Gauge("14.message.write.queue.bytes", () => session.Service.Statistics.ScheduledWriteBytes / 1024, Unit.KiloBytes);
     base.SessionCreated(nextFilter, session);
 }
        public override void FilterWrite(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
        {
            var buf = writeRequest.Message as IoBuffer;
            if (buf != null && buf.Remaining != 0)
            {
                var length = buf.Remaining + Magic.Length;
                if (length < 0)
                    throw new ArgumentOutOfRangeException("length", "length is less than zero");

                var @out = IoBuffer.Allocate(length);
                Magic.WriteMagic(@out);
                @out.Put(buf);
                @out.Flip();
                base.FilterWrite(
                    nextFilter, session, new ProtocolCodecFilter.EncodedWriteRequest(@out, null, writeRequest.Destination));
            }
            else
                base.FilterWrite(nextFilter, session, writeRequest);
        }
Пример #22
0
        public override void MessageReceived(INextFilter nextFilter, IoSession session, object message)
        {
            List<String> phrases = session.GetAttribute<List<String>>("phrases");

            if (null == phrases)
            {
                phrases = new List<String>();
                session.SetAttribute("phrases", phrases);
            }

            phrases.Add((String)message);

            if (phrases.Count == 3)
            {
                session.RemoveAttribute("phrases");

                base.MessageReceived(nextFilter, session, new Haiku(phrases
                        .ToArray()));
            }
        }
Пример #23
0
        /// <inheritdoc/>
        public override void FilterWrite(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
        {
            if (_manipulateWrites)
            {
                // manipulate bytes
                IoBuffer buf = writeRequest.Message as IoBuffer;
                if (buf != null)
                {
                    ManipulateIoBuffer(session, buf);
                    IoBuffer buffer = InsertBytesToNewIoBuffer(session, buf);
                    if (buffer != null)
                    {
                        writeRequest = new DefaultWriteRequest(buffer, writeRequest.Future);
                    }
                    // manipulate PDU
                }
                else
                {
                    if (_duplicatePduProbability > _rng.Next())
                    {
                        nextFilter.FilterWrite(session, writeRequest);
                    }

                    if (_resendPduLasterProbability > _rng.Next())
                    {
                        // store it somewhere and trigger a write execution for
                        // later
                        // TODO
                    }
                    if (_removePduProbability > _rng.Next())
                    {
                        return;
                    }
                }
            }

            base.FilterWrite(nextFilter, session, writeRequest);
        }
Пример #24
0
        /// <inheritdoc/>
        public override void MessageReceived(INextFilter nextFilter, IoSession session, Object message)
        {
            if (_manipulateReads)
            {
                IoBuffer buf = message as IoBuffer;
                if (buf != null)
                {
                    // manipulate bytes
                    ManipulateIoBuffer(session, buf);
                    IoBuffer buffer = InsertBytesToNewIoBuffer(session, buf);
                    if (buffer != null)
                    {
                        message = buffer;
                    }
                }
                else
                {
                    // manipulate PDU
                    // TODO
                }
            }

            base.MessageReceived(nextFilter, session, message);
        }
Пример #25
0
 /// <inheritdoc/>
 public virtual void MessageSent(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
 {
     nextFilter.MessageSent(session, writeRequest);
 }
Пример #26
0
 /// <inheritdoc/>
 public virtual void MessageReceived(INextFilter nextFilter, IoSession session, Object message)
 {
     nextFilter.MessageReceived(session, message);
 }
Пример #27
0
 /// <inheritdoc/>
 public virtual void InputClosed(INextFilter nextFilter, IoSession session)
 {
     nextFilter.InputClosed(session);
 }
Пример #28
0
 /// <inheritdoc/>
 public virtual void ExceptionCaught(INextFilter nextFilter, IoSession session, Exception cause)
 {
     nextFilter.ExceptionCaught(session, cause);
 }
Пример #29
0
 public ProtocolEncoderOutputImpl(IoSession session, INextFilter nextFilter, IWriteRequest writeRequest)
 {
     _session     = session;
     _nextFilter  = nextFilter;
     _destination = writeRequest.Destination;
 }
Пример #30
0
 /// <inheritdoc/>
 public override void OnPostRemove(IoFilterChain parent, String name, INextFilter nextFilter)
 {
     // Clean everything
     DisposeCodec(parent.Session);
 }
Пример #31
0
 /// <inheritdoc/>
 public override void SessionClosed(INextFilter nextFilter, IoSession session)
 {
     Free(session);
     base.SessionClosed(nextFilter, session);
 }
Пример #32
0
        public override void MessageSent(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
        {
            IVoltronPacket voltronPacket = writeRequest.OriginalRequest.Message as IVoltronPacket;

            if (voltronPacket != null)
            {
                var voltronBuffer = IoBuffer.Allocate(512);
                voltronBuffer.Order      = ByteOrder.BigEndian;
                voltronBuffer.AutoExpand = true;
                voltronPacket.Serialize(voltronBuffer, Context);
                voltronBuffer.Flip();

                var byteArray = new byte[voltronBuffer.Remaining];
                voltronBuffer.Get(byteArray, 0, voltronBuffer.Remaining);

                PacketLogger.OnPacket(new Packet
                {
                    Data      = byteArray,
                    Type      = PacketType.VOLTRON,
                    SubType   = voltronPacket.GetPacketType().GetPacketCode(),
                    Direction = PacketDirection.OUTPUT
                });
                nextFilter.MessageSent(session, writeRequest);
                return;
            }

            IAriesPacket ariesPacket = writeRequest.OriginalRequest.Message as IAriesPacket;

            if (ariesPacket != null)
            {
                IoBuffer ariesBuffer = IoBuffer.Allocate(128);
                ariesBuffer.AutoExpand = true;
                ariesBuffer.Order      = ByteOrder.LittleEndian;
                ariesPacket.Serialize(ariesBuffer, Context);
                ariesBuffer.Flip();

                var byteArray = new byte[ariesBuffer.Remaining];
                ariesBuffer.Get(byteArray, 0, ariesBuffer.Remaining);

                PacketLogger.OnPacket(new Packet
                {
                    Data      = byteArray,
                    Type      = PacketType.ARIES,
                    SubType   = ariesPacket.GetPacketType().GetPacketCode(),
                    Direction = PacketDirection.OUTPUT
                });
                nextFilter.MessageSent(session, writeRequest);
                return;
            }

            IoBuffer buffer = writeRequest.Message as IoBuffer;

            if (buffer == null)
            {
                nextFilter.MessageSent(session, writeRequest);
                return;
            }

            TryParseAriesFrame(buffer, PacketDirection.OUTPUT);
            nextFilter.MessageSent(session, writeRequest);
        }
Пример #33
0
 /// <inheritdoc/>
 public override void MessageSent(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
 {
     if (!(writeRequest is EncryptedWriteRequest encryptedWriteRequest))
     {
         // ignore extra buffers used for handshaking
     }
Пример #34
0
 /// <inheritdoc/>
 public override void ExceptionCaught(INextFilter nextFilter, IoSession session, Exception cause)
 {
     base.ExceptionCaught(nextFilter, session, cause);
 }
Пример #35
0
        /// <inheritdoc/>
        public override void FilterWrite(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
        {
            SslHandler handler = GetSslSessionHandler(session);

            handler.ScheduleFilterWrite(nextFilter, writeRequest);
        }
Пример #36
0
        /// <inheritdoc/>
        public override void OnPostAdd(IoFilterChain parent, String name, INextFilter nextFilter)
        {
            SslHandler handler = GetSslSessionHandler(parent.Session);

            handler.Handshake(nextFilter);
        }
Пример #37
0
 /// <inheritdoc/>
 public virtual void FilterClose(INextFilter nextFilter, IoSession session)
 {
     nextFilter.FilterClose(session);
 }
Пример #38
0
        /// <inheritdoc/>
        public override void MessageReceived(INextFilter nextFilter, IoSession session, Object message)
        {
            //if (log.IsDebugEnabled)
            //    log.DebugFormat("Processing a MESSAGE_RECEIVED for session {0}", session.Id);

            IoBuffer input = message as IoBuffer;

            if (input == null)
            {
                nextFilter.MessageReceived(session, message);
                return;
            }

            IProtocolDecoder       decoder       = _factory.GetDecoder(session);
            IProtocolDecoderOutput decoderOutput = GetDecoderOut(session, nextFilter);

            // Loop until we don't have anymore byte in the buffer,
            // or until the decoder throws an unrecoverable exception or
            // can't decoder a message, because there are not enough
            // data in the buffer
            while (input.HasRemaining)
            {
                Int32 oldPos = input.Position;
                try
                {
                    _semaphore.WaitOne();

                    // Call the decoder with the read bytes
                    decoder.Decode(session, input, decoderOutput);

                    // Finish decoding if no exception was thrown.
                    decoderOutput.Flush(nextFilter, session);
                }
                catch (Exception ex)
                {
                    ProtocolDecoderException pde = ex as ProtocolDecoderException;
                    if (pde == null)
                    {
                        pde = new ProtocolDecoderException(null, ex);
                    }
                    if (pde.Hexdump == null)
                    {
                        // Generate a message hex dump
                        Int32 curPos = input.Position;
                        input.Position = oldPos;
                        pde.Hexdump    = input.GetHexDump();
                        input.Position = curPos;
                    }

                    decoderOutput.Flush(nextFilter, session);
                    nextFilter.ExceptionCaught(session, pde);

                    // Retry only if the type of the caught exception is
                    // recoverable and the buffer position has changed.
                    // We check buffer position additionally to prevent an
                    // infinite loop.
                    if (!(ex is RecoverableProtocolDecoderException) || input.Position == oldPos)
                    {
                        break;
                    }
                }
                finally
                {
                    _semaphore.Release();
                }
            }
        }
Пример #39
0
 /// <inheritdoc/>
 public virtual void FilterWrite(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
 {
     nextFilter.FilterWrite(session, writeRequest);
 }
Пример #40
0
        /// <inheritdoc/>
        public override void FilterWrite(INextFilter nextFilter, IoSession session, IWriteRequest writeRequest)
        {
            Object message = writeRequest.Message;

            // Bypass the encoding if the message is contained in a IoBuffer,
            // as it has already been encoded before
            if (message is IoBuffer || message is IFileRegion)
            {
                nextFilter.FilterWrite(session, writeRequest);
                return;
            }

            // Get the encoder in the session
            IProtocolEncoder       encoder    = _factory.GetEncoder(session);
            IProtocolEncoderOutput encoderOut = GetEncoderOut(session, nextFilter, writeRequest);

            if (encoder == null)
            {
                throw new ProtocolEncoderException("The encoder is null for the session " + session);
            }
            if (encoderOut == null)
            {
                throw new ProtocolEncoderException("The encoderOut is null for the session " + session);
            }

            try
            {
                encoder.Encode(session, message, encoderOut);
                AbstractProtocolEncoderOutput ape = encoderOut as AbstractProtocolEncoderOutput;
                if (ape != null)
                {
                    // Send it directly
                    IQueue <Object> bufferQueue = ape.MessageQueue;
                    // Write all the encoded messages now
                    while (!bufferQueue.IsEmpty)
                    {
                        Object encodedMessage = bufferQueue.Dequeue();

                        if (encodedMessage == null)
                        {
                            break;
                        }

                        // Flush only when the buffer has remaining.
                        IoBuffer buf = encodedMessage as IoBuffer;
                        if (buf == null || buf.HasRemaining)
                        {
                            IWriteRequest encodedWriteRequest = new EncodedWriteRequest(encodedMessage, null, writeRequest.Destination);
                            nextFilter.FilterWrite(session, encodedWriteRequest);
                        }
                    }
                }

                // Call the next filter
                nextFilter.FilterWrite(session, new MessageWriteRequest(writeRequest));
            }
            catch (Exception ex)
            {
                ProtocolEncoderException pee = ex as ProtocolEncoderException;
                if (pee == null)
                {
                    pee = new ProtocolEncoderException(null, ex);
                }
                throw pee;
            }
        }