In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is the read process of netty server". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what the read process of netty server is like"!
When the client sends data, exactly how netty performs the read task.
Still start with NioEventLoop, or the NioEventLoop.processSelectedKey method.
Private static void processSelectedKey (SelectionKey k, AbstractNioChannel ch) {
Final NioUnsafe unsafe = ch.unsafe ()
If (! k.isValid ()) {
/ / close the channel if the key is not valid anymore
Unsafe.close (unsafe.voidPromise ())
Return
}
Try {
Int readyOps = k.readyOps ()
/ / Also check for readOps of 0 to workaround possible JDK bug which may otherwise lead
/ / to a spin loop
If ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT))! = 0 | | readyOps = = 0) {
Unsafe.read ()
If (! ch.isOpen ()) {
/ / Connection already closed-no need to handle write.
Return
}
}
If ((readyOps & SelectionKey.OP_WRITE)! = 0) {
/ / Call forceFlush which will also take care of clear the OP_WRITE once there is nothing left to write
Ch.unsafe () .forceFlush ()
}
If ((readyOps & SelectionKey.OP_CONNECT)! = 0) {
/ / remove OP_CONNECT as otherwise Selector.select (..) Will always return without blocking
/ / See https://github.com/netty/netty/issues/924
Int ops = k.interestOps ()
Ops & = ~ SelectionKey.OP_CONNECT
K.interestOps (ops)
Unsafe.finishConnect ()
}
} catch (CancelledKeyException e) {
Unsafe.close (unsafe.voidPromise ())
}
}
The red font part, unsafe.read (), goes into AbstractNioByteChannel$NioByteUnsafe. To read data from io, you first need to allocate a ByteBuf. Here, two questions are involved: 1, where to allocate memory, 2, how much memory is allocated each time. First of all, the first question, first of all, we need to understand (the difference between allocate and allocateDirect2). The specific implementation in allocator.ioBuffer will determine whether the system memory can be allocated directly according to the platform. Second, you can take a look at the allocHandle.record method, which determines the amount of memory to be allocated later based on the amount of data currently read from the io. Then it reads the data from socket, writes it to bytebuf, and triggers a series of handler.
Public void read () {
Final ChannelConfig config = config ()
Final ChannelPipeline pipeline = pipeline ()
Final ByteBufAllocator allocator = config.getAllocator ()
Final int maxMessagesPerRead = config.getMaxMessagesPerRead ()
RecvByteBufAllocator.Handle allocHandle = this.allocHandle
If (allocHandle = = null) {
This.allocHandle = allocHandle = config.getRecvByteBufAllocator (). NewHandle ()
}
If (! config.isAutoRead ()) {
RemoveReadOp ()
}
ByteBuf byteBuf = null
Int messages = 0
Boolean close = false
Try {
Int byteBufCapacity = allocHandle.guess ()
Int totalReadAmount = 0
Do {
ByteBuf = allocator.ioBuffer (byteBufCapacity)
Int writable = byteBuf.writableBytes ()
Int localReadAmount = doReadBytes (byteBuf)
If (localReadAmount = Integer.MAX_VALUE-localReadAmount) {
/ / Avoid overflow.
TotalReadAmount = Integer.MAX_VALUE
Break
}
TotalReadAmount + = localReadAmount
If (localReadAmount < writable) {
/ / Read less than what the buffer can hold
/ / which might mean we drained the recv buffer completely.
Break
}
} while (+ + messages < maxMessagesPerRead)
Pipeline.fireChannelReadComplete ()
AllocHandle.record (totalReadAmount)
If (close) {
CloseOnRead (pipeline)
Close = false
}
} catch (Throwable t) {
HandleReadException (pipeline, byteBuf, t, close)
}
}
At this point, I believe you have a deeper understanding of "what is the read process of netty server". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.