![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
Netty
amadeus_liu2
这个作者很懒,什么都没留下…
展开
-
netty源码:(59) AbstractNioMessageChannel之read方法
然后将SocketChannel作为参数,创建NioSocketChannel并添加到buf中,这个buf就是AbstractMessageChannel中调用doReadMessages方法时传过来的readBuf.当有客户端连接时,NioEventLoop中的processSelectedKey方法会被调用,这个方法会调用AbstracNioMessageChannel的read方法,AbstracNioMessageChannel的read方法接着往下执行,原创 2024-01-28 20:04:48 · 643 阅读 · 0 评论 -
netty源码:(58)NioEventLoop中处理IO事件和普通事件的时间比例是多少?
当ioRatio不为100时,首先通过System.nanoTime()获取IO事件的开始处理时间,然后调用processSelectedKeys方法处理IO时间,然后再计算IO事件执行了多长时间。当把ioRatio设置为100时,并不是只处理IO事件,而是处理完IO事件才处理普通任务,但不给普通任务限定执行时间。ioRatio的默认值为50。原创 2024-01-28 11:28:17 · 611 阅读 · 0 评论 -
netty源码:(52)AbstractBootstrap之doBind方法
initAndRegister方法会返回类型为ChannelFuture的对象regFuture,然后在这个ChannelFuture上注册了Listener,当regFuture返回时,注册的Listener的回调方法会被执行(拉姆达表达式里的代码),这里将通过doBind0方法完成ServerSocketChannel和端口的绑定。那么regFuture是怎么返回的呢?原创 2024-01-27 12:57:42 · 385 阅读 · 0 评论 -
netty源码:(48) ChannelHandlerContext的alloc方法得到的ByteBufAllocator类型是如何确定的?
也就是默认使用DirectByteBuf,如果设置了io.netty.noPreferDirect参数,则以这个参数为准,如果设置为true,则会使用HeapByteBuf。最终会返回UnpooledByteBufAllocator.DEFAULT或者PooledByteBufAllocator.DEFAULT.看是pooled还是unpooled,如果没有,则判断是否是安卓平台,是的话取unpooled,否则取pooled.首先获取io.netty.allocator.type(可按下图配置)原创 2024-01-26 11:49:50 · 388 阅读 · 0 评论 -
netty源码(47): 服务器端默认的backlog值是怎么得到的?
如果是Linux,就会读取/proc/sys/net/core/somaxconn里配置的值。这个值是在DefaultNioServerSocketChannelConfig。windows下默认是200,Linux下默认是128.可以看到它是从NetUtil类取的。原创 2024-01-26 10:58:28 · 324 阅读 · 0 评论 -
netty: MessageToMessageCodec的用法
三、定义netty TCP server 主类,在ChannelPipeline中加入上述两个handler。一、定义类,继承MessageToMessageCodec,重写encode和decode方法。二、定义一个简单的ChannelInboundHandlerAdapter。原创 2024-01-25 09:01:27 · 481 阅读 · 0 评论 -
netty: ByteToMessageCodec的用法
三、编写netty TCP服务器,并在ChannelPipeline中加入上述两个handler。一、创建ByteToMessageCodec的子类并重写encode和decode方法。二、编写一个简单的ChannelInboundHandlerAdapter。原创 2024-01-24 21:19:29 · 467 阅读 · 0 评论 -
netty:编写一个简单的Redis命令行工具
【代码】netty:编写一个简单的Redis命令行工具。原创 2024-01-24 14:37:17 · 360 阅读 · 0 评论 -
netty源码:(46) TailContext
TailContext在我们所添加的自己定义的所有ChannelInboundHandler(比如通过addLast方法)之后,是整个入栈消息处理的最后一环,也就是tail.TailContext是DefaultChannelPipeline中的一个内部类,它是一个ChannelInboundHandler。首先判断是不是ByteBuf对象,如果是,调用其release方法,如果不是,什么都不做,直接返回false.原创 2024-01-23 14:08:49 · 360 阅读 · 0 评论 -
netty: EventLoop执行普通任务
【代码】netty: EventLoop执行普通任务。原创 2024-01-20 08:50:12 · 334 阅读 · 0 评论 -
netty: Marshalling序列化示例
一、请求对象和响应对象,分别要实现Serializable接口。三、服务器端handler和启动类。原创 2023-12-30 19:27:06 · 440 阅读 · 0 评论 -
netty: LengthFieldBasedFrameDecoder的用法示例
二、服务器端handler。四、客户端handler。原创 2023-12-28 09:11:41 · 896 阅读 · 0 评论 -
netty源码:(40)ReplayingDecoder
第二种方式使用ReplayingDecoder,读取之前不需要判断ByteBuf中是否有足够的字节,具体的实现原理是:当要读的字节不够时,抛出一个错误,捕获这个错误的时候重置readerIndex,然后进行下一次尝试,实质上就是一种重试机制。在从in中读取数据(readLong)时,不需要判断所读取的字节是否够用,不会报错,会等到自己够用了才返回。上述代码用两种方式读取一条消息,消息分为消息头(定义消息体的长度)和消息体两部分。原创 2023-12-27 15:30:39 · 681 阅读 · 0 评论 -
netty源码:(38)ByteToMessageDecoder类
ByteToMessageDecoder是一个解码器,是一个ChannelInboundHandlerAdapter,它用来将ByteBuf中的字节流解析成另外的消息格式。它的核心方法是decode,ByteToMessageDecoder类不能被标记为@ChannelHandler.Sharable,因为ByteToMessageDecoder通常包含状态信息,共享的话会造成多线程问题。decode方法的in参数表示接收字节的来源,out参数表示节码之后输出的目的地。原创 2023-12-27 08:35:03 · 462 阅读 · 0 评论 -
netty源码:(30)channel的writeAndFlush和channelHandlerContext的writeAndFlush有什么区别?
调用channel的writeAndFlush,消息会被所有的ChannalOutboundHandler处理,而调用channelHandlerContext的writeAndFlush方法,消息只会被当前handler上游的ChannelOutboundHandler处理。从上图可以看出,它是从ChannelPipeline的尾部来开始调用writeAndFlush方法的,所以消息会流经所有的ChannelOutboundHandler.原创 2023-12-21 16:23:53 · 839 阅读 · 0 评论 -
netty源码:(29)ChannelInboundHandlerAdapter
因为channelRead中调用了ReferenceCountUtil.release(msg),所以,如果需要把消息传递给下一个ChannelInboundHandler进行处理,需要在channelRead0方法中调用ReferenceCountUtil.retain(msg);它实现的方法都有一个ChannelHandlerContext参数,它的方法都是直接调用ChannelHandlerContext参数对应的方法,该方法会调用下一个handler对应的方法。原创 2023-12-21 09:10:06 · 669 阅读 · 0 评论 -
netty源码:(28)ChannelPromise
它的setSuccess方法用来调用所有注册在其上的GenericFutureListener的operationComplete方法。可以看到GenericFutureListener中定义的operationComple方法被回调了。ChannelPromise有个默认的实现类:DefaultChannelPromise。ChannelPromise是ChannelFuture的子接口,它是可写入的。原创 2023-12-20 15:16:11 · 374 阅读 · 0 评论 -
netty源码:(24)EventExecutorChooserFactory类
DefaultEventExecutorChooserFactory,该类有个newChooser方法用来返回EventExecutorChooser。PowerOfTwoEventExecutorChooser和GenericEventExecutorChooser。原创 2023-12-19 09:33:21 · 377 阅读 · 0 评论 -
netty源码:(12)bossGroup和workGroup之间的关系
得到这个set之后就会遍历它,对每个元素调用accept方法来得到SocketChannel,然后将这个SocketChannel注册到workerGroup对应的Selector并设置感兴趣的事件为OP_READ、OP_WRITE.当客户端发送数据时,workerGroup对应的Selector就会感知,通过调用workerGroup的Selector的selectedKeys方法(调用select方法之后),就可以得到拿些连接有数据发过来,就可以进行读写了。原创 2023-12-12 09:57:13 · 116 阅读 · 0 评论 -
netty源码:(9)ChannelId
ChannelId代表了一个Channel的全局标识符。它提供了asShortText和asLongText两个方法来返回对应的字符串。原创 2023-12-08 12:55:05 · 251 阅读 · 0 评论 -
netty源码:(8)ChannelFactory和ReflectiveChannelFactory
ChannelFactory为接口,其定义了一个newChannel方法,用来返回Channel对象。ReflectiveChannelFactory实现了ChannelFactory接口,它通过反射的方式创建Channel对象。原创 2023-12-06 21:17:44 · 89 阅读 · 0 评论 -
netty源码:(6) Future接口
这个Future可以通过添加listener来实现任务完成时的回调,避免了阻塞调用(get). 同时,isSuccess方法真正返回任务是否完成,而jdk的isDone方法在完成、取消、异常的时候都返回true.它继承了jdk的Future,增加了一系列的方法。原创 2023-12-06 10:33:38 · 61 阅读 · 0 评论 -
netty源码:(4)ServerBootstrap
ServerBootstrap的channel方法用来设置channelFactory成员变量(在父类AbstractBootstrap里)AbstractBootstrap为ServerBootstrap的父类。ServerBootstrap的group方法用来给成员变量赋值,如下图。原创 2023-12-05 08:55:39 · 57 阅读 · 0 评论 -
netty源码:(3) ThreadPerTaskExecutor
这个类封装了一个ThreadFactory,同时提供了一个execute方法,用来把Runnable提交给ThreadFactory来执行。原创 2023-12-04 10:28:33 · 57 阅读 · 0 评论 -
netty源码:(1)NioEventLoopGroup
不加参数创建NioEventLoopGroup的话,会使用cpu核数*2作为bossGroup的线程数。原创 2023-12-01 17:07:53 · 243 阅读 · 0 评论 -
Netty复习:(2)IdleStateHandler的用法
当客户端连接之后,如果一段时间不发送数据,则服务器端会触发IdleStateEvent并被处理。三、定义server启动类,在pipeline中添加IdleStateHandler。二、定义Initializer。原创 2023-10-30 22:01:23 · 328 阅读 · 0 评论 -
Netty复习:(1)Http server: hello world
三、定义启动类,在pipeline中添加自定义的handler。二、创建自定义的handler。原创 2023-10-29 21:27:15 · 475 阅读 · 0 评论 -
Netty: ChannelInboundHandler和ChannelOutboundHandler的使用
一、需要注意的地方: 入站的消息会流经所有的添加的ChannelInboundHandler,出站的消息不一定会流经所有的ChannelOutboundHandler,通过在某个ChannelInboundHandler的ChannelHandlerContext的writeAndFlush方法写出的数据,并不会流经其后的ChannelOuntboundHandler,但如果是通过ctx.channel().writeAndFlush方法写出数据,则会流经所有的ChannelOutboundHandler.原创 2022-05-09 14:01:50 · 1858 阅读 · 0 评论 -
Netty:使用protobuf传递多种类型数据
1.定义消息.proto文件syntax="proto3";package test;option java_package="cn.edu.tju";option java_outer_classname="MyData";message Being{enum DataType{PersonType=0;CatType=1;DogType=2;}DataType data_type=1;oneof dataBody{Person person=2;Cat cat=3;D原创 2022-02-13 12:23:00 · 622 阅读 · 0 评论 -
Netty:创建WebSocket服务器和客户端
一、有点复杂的方式,自己处理协议升级、握手等信息1.创建ServerHandlerpackage cn.edu.tju;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.handler.codec.http.FullHttpRequest;import io.netty.handler.codec.http.FullH原创 2022-02-08 16:12:36 · 1977 阅读 · 0 评论 -
Netty: Udp服务器和客户端
1.引入相关依赖:<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.o原创 2022-02-07 15:53:57 · 875 阅读 · 0 评论 -
Netty: TCP服务器和客户端解决粘包问题
1.服务器端pom.xml<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apac原创 2022-01-29 16:29:38 · 2150 阅读 · 0 评论 -
Netty:使用protobuf发送和接受消息
1.下载protobuf32.编写.proto文件syntax="proto3";package cn.edu.tju.nt;option java_outer_classname = "SubscribeReqProto";message SubscribeReq{ int32 subReqID = 1 ; string userName = 2; string productName = 3; repeated string address = 4;原创 2022-01-15 20:19:21 · 652 阅读 · 0 评论 -
Netty:服务端给客户端发送消息后的异步回调
package cn.edu.tju.handler;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.Chann原创 2022-01-13 13:45:19 · 1164 阅读 · 0 评论 -
Netty: ByteBuf转String
package tju.SpringBootPostServer.controller;import java.io.UnsupportedEncodingException;import java.util.Date;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.util.CharsetUtil;public class TestMe { public static voi原创 2021-03-24 13:40:15 · 706 阅读 · 1 评论 -
Netty: 解码器要与ChannelHandler配合使用
解决粘包问题时,public class ChildChannelHandler extends ChannelInitializer{@Overrideprotected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(1024)); ch.pipeline().addLast(new StringDecoder()); //ch.pipe原创 2020-07-10 22:15:05 · 189 阅读 · 0 评论 -
Netty: Handler传递消息给下一个Handler
ctx.fireChannelRead(msg)原创 2020-07-10 14:27:54 · 2174 阅读 · 0 评论 -
Netty: NioEventLoopGroup
可通过.executorCount()获取线程数原创 2020-07-10 09:49:50 · 126 阅读 · 0 评论