5. ChannelPipeline Netty analysis of the source and ChannelHanler

Introduction: ChannelPipeline ChannelHandler and achieve a similar Spring interceptor, data propagation in the pipeline, each ChannelHandler processing section of interest.

一、ChannelPipeline

  ChannelPipeline is ChannelHandler container ChannelHandler responsible for the management and event interception and scheduling.

1. ChannelPipeline event processing

1. The read operation (InBound): NioEventLoop call ChannelPipeline of fireChannelRead (..) method, the message is transmitted to the ChannelPipeline. Channelhandler process flow: HeadHandler -> ChannelHandler1 -> .. -> ChannelHandlerN -> TailHandler

2. Write operation (OutBound): calls the write method of ChannelHandlerContext send a message handler after treatment, it was eventually added to the message and send buffer waiting to be flushed. ChannelHandler process flow: TailHandler -> ChannelHandlerN -> ... -> ChannelHandler1 -> HeadHandler

 

Inbound and OutBound mentioned here, as the name suggests, refers to Inbound in, e.g. read, accept these IO operations are carried out to the inner direction; OutBound is to be noted, for example, connect, write, flush these are the direction to the external IO operations ongoing.

 2. ChannelPipeline source code analysis

  ChannelPipeline ChannelHandler management is actually a container, it maintains an internal list and iterators a ChannelHandler, you can easily CRUD ChannelHandler.

We look addBefore method, check here has a handler does not allow multiple ChannelPipeline share, handlerName can not be repeated, the benchmark handler can not be null, the last before the new handler on the handler reference, last handlerAdded trigger event.

public  Final the ChannelPipeline AddBefore (String baseName, String name, of ChannelHandler Handler) {
     return AddBefore ( null , baseName, name, Handler); 
} 

public  Final the ChannelPipeline AddBefore (EventExecutorGroup Group, String baseName, String name, of ChannelHandler Handler) {
     Final AbstractChannelHandlerContext newCtx;
     Final AbstractChannelHandlerContext CTX;
     the synchronized ( the this ) {
         // Channdler plurality ChannelPipeline allowed shared 
        checkMultiplicity (Handler);
         // 1. If the name is empty generates a name, 2.name allowed to repeat 
        name =filterName (name, Handler);
         // according to basic queries to be inserted prior to which Handler, and if not, then throw an exception 
        ctx = getContextOrDie (baseName);
         // create DefaultChannelHandlerContext objects 
        newCtx = newContext (Group, name, Handler); 
        
        / / add it in front of ctx, is simple linked list insertion 
        addBefore0 (ctx, newCtx); 

        // when success registered yet, add a handlerAdded task, called when the registration is successful 
        iF (! registered) { 
            newCtx.setAddPending ( ); 
            callHandlerCallbackLater (newCtx, to true );
             return  the this ; 
        } 
        
        // if the current thread is not EventLoop thread, into the execution queue handlerAdded EventLoop
        EventExecutor executor = newCtx.executor();
        if (!executor.inEventLoop()) {
            callHandlerAddedInEventLoop(newCtx, executor);
            return this;
        }
    }
    // 执行handlerAdded
    callHandlerAdded0(newCtx);
    return this;
}

private static void addBefore0(AbstractChannelHandlerContext ctx, AbstractChannelHandlerContext newCtx) {
    newCtx.prev = ctx.prev;
    newCtx.next = ctx;
    ctx.prev.next = newCtx;
    ctx.prev = newCtx;
}

二、ChannelHandler

  ChannelHandler similar to Spring interceptors, responsible for the event or IO IO operations to intercept and processing, it can selectively intercept and handle events of interest, and can pass through the transfer termination events.

  ChannelHandler implementation class a lot, here mainly talk about ChannelHandlerAdapter.

  For most ChannelHandler, it will be of interest to selectively intercept the event, if the direct realization ChannelHandler, you need to write too many methods and what does not. The 2ChannelHandlerAdapter implements all methods ChannelHandler, but all implementations are pass-through, we just need to inherit ChannelHandlerAdapter, you can override the events that interest you.

 

Guess you like

Origin www.cnblogs.com/lovezmc/p/11547896.html