netty 4.x源码分析
服务端需要经过socket、bind、accept、read、write等步骤,客户端需要经过socket、connect、read、write等步骤,后续此系列文章会对每一个步骤如何发生进行分析。
- netty4源码分析-线程的创建
- netty4源码分析-socket
- netty4源码分析-bind
- netty4源码分析-connect
- netty4源码分析-accept
- netty4源码分析-write
- netty4源码分析-flush
- netty4源码分析-read
- 通信总结
以下是服务端和客户端的测试代码,后面的分析以这份代码为基础,目前使用的Netty的版本是4.0.4.Final及以上
服务端: /* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package io.netty.example.echo; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; /** * Echoes back any received data from a client. */ public class EchoServer { private final int port; public EchoServer(int port) { this.port = port; } public void run() throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( //new LoggingHandler(LogLevel.INFO), new EchoServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(port).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port; if (args.length > 0) { port = Integer.parseInt(args[0]); } else { port = 8080; } new EchoServer(port).run(); } }
/* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package io.netty.example.echo; import io.netty.channel.ChannelHandler.Sharable; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import java.util.logging.Level; import java.util.logging.Logger; /** * Handler implementation for the echo server. */ @Sharable public class EchoServerHandler extends ChannelInboundHandlerAdapter { private static final Logger logger = Logger.getLogger( EchoServerHandler.class.getName()); @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ctx.write(msg); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // Close the connection when an exception is raised. logger.log(Level.WARNING, "Unexpected exception from downstream.", cause); ctx.close(); } }
客户端: /* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package io.netty.example.echo; import io.netty.bootstrap.Bootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; /** * Sends one message when a connection is open and echoes back any received * data to the server. Simply put, the echo client initiates the ping-pong * traffic between the echo client and server by sending the first message to * the server. */ public class EchoClient { private final String host; private final int port; private final int firstMessageSize; public EchoClient(String host, int port, int firstMessageSize) { this.host = host; this.port = port; this.firstMessageSize = firstMessageSize; } public void run() throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( //new LoggingHandler(LogLevel.INFO), new EchoClientHandler(firstMessageSize)); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } } public static void main(String[] args) throws Exception { // Print usage if no argument is specified. if (args.length < 2 || args.length > 3) { System.err.println( "Usage: " + EchoClient.class.getSimpleName() + " <host> <port> [<first message size>]"); return; } // Parse options. final String host = args[0]; final int port = Integer.parseInt(args[1]); final int firstMessageSize; if (args.length == 3) { firstMessageSize = Integer.parseInt(args[2]); } else { firstMessageSize = 256; } new EchoClient(host, port, firstMessageSize).run(); } }
/* * Copyright 2012 The Netty Project * * The Netty Project licenses this file to you under the Apache License, * version 2.0 (the "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ package io.netty.example.echo; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import java.util.logging.Level; import java.util.logging.Logger; /** * Handler implementation for the echo client. It initiates the ping-pong * traffic between the echo client and server by sending the first message to * the server. */ public class EchoClientHandler extends ChannelInboundHandlerAdapter { private static final Logger logger = Logger.getLogger( EchoClientHandler.class.getName()); private final ByteBuf firstMessage; /** * Creates a client-side handler. */ public EchoClientHandler(int firstMessageSize) { if (firstMessageSize <= 0) { throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize); } firstMessage = Unpooled.buffer(firstMessageSize); for (int i = 0; i < firstMessage.capacity(); i ++) { firstMessage.writeByte((byte) i); } } @Override public void channelActive(ChannelHandlerContext ctx) { ctx.writeAndFlush(firstMessage); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ctx.write(msg); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // Close the connection when an exception is raised. logger.log(Level.WARNING, "Unexpected exception from downstream.", cause); ctx.close(); } }
相关推荐
Netty 的源码分析主要包括以下几个方面: 1. **I/O 模型**:理解 Netty 如何利用 NIO 实现非阻塞 I/O,包括 Channel、Selector 和 Socket 的工作原理。 2. **Pipeline 实现**:分析 Handler 的添加、移除和调用链的...
本系列文章是对Netty3.x版本源码的详细解读,旨在帮助读者深入理解Netty内部的工作机制以及其设计理念。Netty源码阅读的目的通常有两个:一是因为工作中使用到了Netty,希望通过阅读源码来更加深入地了解它;二是...
Netty3.x 版本是其早期的一个版本,虽然现在最新的版本已经发展到了4.x,但3.x版本仍然具有学习价值,特别是对于理解Netty的基本原理和设计模式。 首先,让我们来看看`pom.xml`文件。这是Maven项目对象模型的配置...
源码分析对于理解Netty的工作原理、优化性能以及定制化开发非常有帮助。 Netty 的核心特性包括: 1. **异步事件驱动**:Netty 使用非阻塞I/O(NIO)模型,通过事件循环(EventLoop)和通道(Channel)来处理网络...
│ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...
│ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...
│ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...
│ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...
│ 01 spring源码分析.mp4 │ 02 spring源码分析.mp4 │ 03 spring源码分析03.mp4 │ 04 spring源码分析04.mp4 │ 05 spring源码分析05.mp4 │ 06 spring源码分析06_1.mp4 │ 07 spring源码分析06_2.mp4 │ ...
JDK 5(用于3.x)或6(用于4.0+)足以运行基于Netty的应用程序。 分支机构看 所有版本的开发都在名称与<majorVersion>.相同的每个分支中进行。 例如,3.9和4.0的开发分别位于和。 与JDK 9结合使用 Netty可以在模块...
源码分析可以帮助开发者理解其非阻塞 I/O 模型、线程模型、事件循环(EventLoop)和通道(Channel)等核心概念。同时,提供的“所有jar包”包含了运行 Netty 应用所需的依赖,这些依赖可能包括编解码器、SSL/TLS ...
Netty 是一个高性能、异步...通过分析和学习这些示例,你可以掌握Netty在实际应用中的技巧,加深对网络编程的理解,提高开发效率。同时,对于自定义协议和RPC模块的学习,也能帮助你更好地应对各种复杂通信场景的需求。
源码分析篇 :triangular_ruler: DDD领域驱动设计 :electric_plug: 中间件开发 :ghost: JavaAgent全链路监控 :artist_palette: 架构框架搭建 转载分享 建立本开源项目的初衷是基于个人学习与工作中对 Java 相关技术栈...
7. **源码分析与调试** 阅读并理解 Netty 源码可以帮助我们更好地掌握其工作原理。Netty 的代码结构清晰,模块化设计使得我们可以针对具体需求进行定制。在实际项目中,可以使用 IDE 的调试功能逐步跟踪请求处理...
学习 Netty 的资源通常包括源码分析、官方文档、示例项目以及社区提供的教程。压缩包中的“代码”部分可能包含了 Netty 的示例代码或者已经实现的协议处理器,这对于理解框架的工作原理非常有帮助。而“文档”部分...
源码分析可以从以下几个方面入手: 1. **模块划分**:Elasticsearch 源码包括了客户端、服务器端、核心库、插件等多个模块,了解这些模块的职责有助于深入理解代码。 2. **网络通信**:基于 Netty 实现,负责节点间...
源码分析可以帮助开发者深入理解其内部机制,提升开发和优化服务的能力。 1. **设计模式与架构** - **服务提供者(Provider)**:提供服务的模块,暴露服务接口供消费者调用。 - **服务消费者(Consumer)**:...
在描述中提到的"代码完整"意味着这是一个可能包含了整个项目源码的压缩包,包括服务器端的Java代码、JSP页面以及可能的数据库脚本和其他资源配置文件。开发者或研究者可以通过分析这些代码来学习手机网络游戏的开发...
- 网狐6603版的服务器端可能基于高性能的网络框架,如Netty或Tornado,负责处理并发连接,提供稳定的游戏服务。 - 服务器端代码会涉及用户认证、房间创建、游戏匹配、数据同步等功能模块,确保玩家之间的交互顺畅...