1. Preface
The previous section described the first key startup class of Netty, some of the operations initiated by the class, and the channel-fixed handler execution of the server, and talked about whether the connect or Bind method ultimately calls the channel-related method, This section begins with a description of the channel. The concept of channel setup is very much, and it is important to put a class diagram of NIO's client channel first.
2. Key Concepts 2.1 Channel
Channel is a data channel that is directly related to the operating system layer, either by Java, or by extending the C + + functionality itself through the native method, but regardless of the class, there is a basic definition. The following are the main interface methods defined in the channel:
ID (): Gets the identity of the channel
EventLoop (): Gets the thread pool registered by the channel
Parent (): Gets the Channel,nio of the channel has no parent channel, which is generally null
Config (): Gets the configuration of the channel
IsOpen (): Whether the channel is open
Isregistered (): Whether the channel is registered in the thread pool
IsActive (): Whether the channel is available
Metadata (): meta-data for the channel
LocalAddress (): Local bound address port for the channel
Remoteaddress (): The address port of the peer of the channel connection
Closefuture (): The future triggered when the channel is closed
IsWritable (): Whether the channel is currently writable, only the IO thread will handle the writable state
Bytesbeforeunwritable (): How many bytes the channel can write
Unsafe (): To obtain this channel's unsafe operation object, for the channel read and write, generally do not directly operate the channel, but to the unsafe object processing, channel itself usually only do the query state, get the relevant field content operation.
Alloc (): Gets the allocated buffer
Read (): Perform a read operation
Write (): write operation
The above method we see an unfamiliar unsafe object, this is a more important concept, understand the position of the class in the entire structure of the role, for understanding the framework has a greater help. Unsafe is defined directly inside the channel interface, which means that the interface is bound to the channel, which is also explained by the above method. The channel itself does not do the corresponding work directly to the unsafe method call. is the interface definition for unsafe:
Recvbufallochandle (): Gets the handler processed after reading the channel data
LocalAddress (): Local address port
Remoteaddress (): Remote address port
Register (): Register channel to thread pool
Bind (): Server bound to Port
Connect (): client connects to remote port
Disconnect (): Disconnected
Close (): Close channel
Closeforcibly (): Forced shutdown
Deregister (): Remove the registration of the thread pool
BeginRead (): Preparing to read data
Write (): Write Data
Flush (): Forced flush
Voidpromise (): Special Promise
Outboundbuffer (): Gets the buffer operation class for the output data
See above a series of interfaces to be able to understand that the actual operation of the channel is unsafe class, but is directly operating the unsafe class? For example, when binding the port is actually called the Channel.bind method Ah, actually here also involves other concepts, around a circle to operate. Unsafe look at the name should also understand that the channel operation of the class is a thread non-security class, so generally through the netty itself structure design, to ensure that the thread isolation, can be assured to use. Of course, if you do not define an IO mode, basically use now Netty package good will not have any problem, if you build wheels, this will be extra attention.
2.2 Channelpipeline
In front of the pipeline, this section will talk about the role of this class. Before you look at the interface definition, focus on where the class is located throughout the structure. Open the Abstractchannel and take a closer look at this kind of abstract channel class, and you'll reap the rewards. Most of the channel's main operating methods are done through pipeline, such as: Bind,connect,close,deregister,flush,read,write and so on. Is it strange? Not what we said above is handled by unsafe. But this is not contradictory, unsafe is the most basic processing, we will have a series of business layers to deal with, such as bind when the parameters of the socket to handler processing, so the channel will be related operations entrusted to pipeline processing, Pipeline after a series of operations, finally called the related action of unsafe, eventually back to the channel. Pipeline translation is pipeline, it feels more like pipeline operation here.
Now look at the basic definition of pipeline and don't feel abrupt.
There are many methods of pipline, but they are divided into two main categories: 1. Register handler;2. Use handler; Is the method of using handler, there are many ways to register handler, not introduced here. Channelpipeline does not have so many implementation classes, the basic is the defaultchannelpipeline, so directly to the class part of the method parsing.
1. What exactly did the method of registering handler do, take the AddLast () method used in the example as an example:
Public final Channelpipeline AddLast (eventexecutorgroup Group, String name, Channelhandler handler) {final Abs Tractchannelhandlercontext NewCtx; Synchronized (this) {checkmultiplicity (handler); NEWCTX = Newcontext (group, FilterName (name, handler), handler); AddLast0 (NEWCTX); If the registered is false it means, the channel was not a registered on an eventloop yet. In this case we add the context to the pipeline and add a task that would call//channelhandler.handleradded (...) Once the channel is registered. if (!registered) {newctx.setaddpending (); Callhandlercallbacklater (NewCtx, true); return this; } eventexecutor Executor = Newctx.executor (); if (!executor.ineventloop ()) {newctx.setaddpending (); Executor.execute (New Runnable () {@Override public void Run () {callHandlerAdded0 (NEWCTX); } }); return this; }} callHandlerAdded0 (NEWCTX); return this; }
To understand this code there is a content to note, tail and head, as well as Handlercontext (this is a handler-related concept, not described here). Tail and head is pipeline hold handler head node and tail knot point, see addlast time should understand, handler is chained structure string up, in front also said, handler is the responsibility chain mode, there are sequencing. This approach is to put handler at the end of the chain of responsibility. In the middle there is a process, will be handler with context packaging, this is not the focus of this section, after handler chapter again introduced.
2. The next step is pipeline's focus on how the channel is operated, or how the channel is operated.
Public final channelfuture bind (socketaddress localaddress) { return tail.bind (localaddress); }
Basic channel operations are done through the default tail, which are bind,connect,close,deregister,flush,read,write. Tail is a handlercontext, here will involve some handlercontext content, briefly say: In the previous pipeline add handler, the generation of context,context constitute a chain structure, It knows what his front and back handler are. The others do not elaborate, ultimately through the tail of this handler constantly early on it before an out type of handler, finally find head, see Headcontext class you will understand that the class acquires the channel's unsafe object, all operations are done by the object , so the whole link is connected. Generated by the channel Channelpipeline and unsafe objects, all operations to pipeline,pipeline from the tail has been searched to head, and finally by the head of the unsafe method to obtain the channel, the final related operations.
There is a kind of method here also introduced, involved in the handlercontext responsibility chain operation process, not detailed.
Public final Channelpipeline firechannelactive () { abstractchannelhandlercontext.invokechannelactive (head); return this; }
This is called from the head.
public void Channelactive (Channelhandlercontext ctx) throws Exception { ctx.firechannelactive (); Readifisautoread (); }
Then it calls its own firechannelactive method, looking backwards from head, and Next's in type method, which pushes the entire chain of responsibilities. So pipeline's Firechannelactive () method is the starting method that pushes the invocation of the responsibility chain.
3. PostScript
The channel has a total of three important concepts:
1.Channel does not do things itself, the event is given to Channelpipeline.
2.ChannelPipeline itself does not do what, its main is to control the handler chain, from the tail query to head holding unsafe objects, control channel connection, read, write, The direct triggering event chain method firexxx from head to tail is provided.
3.Unsafe is the final position of the channel operation.
Finally, attach a diagram:
Channel of Netty Core concept (5)