|
1 | 1 | package org.pyj;
|
2 | 2 |
|
3 |
| -import org.pyj.http.annotation.NettyHttpHandler; |
| 3 | +import io.netty.bootstrap.ServerBootstrap; |
| 4 | +import io.netty.channel.ChannelFuture; |
| 5 | +import io.netty.channel.ChannelInitializer; |
| 6 | +import io.netty.channel.ChannelOption; |
| 7 | +import io.netty.channel.ChannelPipeline; |
| 8 | +import io.netty.channel.EventLoopGroup; |
| 9 | +import io.netty.channel.WriteBufferWaterMark; |
| 10 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 11 | +import io.netty.channel.socket.nio.NioServerSocketChannel; |
| 12 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 13 | +import io.netty.handler.codec.http.HttpObjectAggregator; |
| 14 | +import io.netty.handler.codec.http.HttpServerCodec; |
| 15 | +import io.netty.handler.logging.LogLevel; |
| 16 | +import io.netty.handler.logging.LoggingHandler; |
| 17 | +import java.net.InetAddress; |
| 18 | +import java.net.InetSocketAddress; |
| 19 | +import java.net.UnknownHostException; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import lombok.extern.slf4j.Slf4j; |
4 | 23 | import org.pyj.exception.IllegalPathDuplicatedException;
|
| 24 | +import org.pyj.http.annotation.NettyHttpHandler; |
5 | 25 | import org.pyj.http.handler.IFunctionHandler;
|
6 | 26 | import org.pyj.http.path.Path;
|
7 |
| -import lombok.extern.slf4j.Slf4j; |
8 | 27 | import org.springframework.beans.TypeConverter;
|
9 | 28 | import org.springframework.beans.TypeMismatchException;
|
10 | 29 | import org.springframework.beans.factory.BeanFactory;
|
11 |
| -import org.springframework.beans.factory.annotation.Autowired; |
12 | 30 | import org.springframework.beans.factory.config.BeanExpressionContext;
|
13 | 31 | import org.springframework.beans.factory.config.BeanExpressionResolver;
|
14 | 32 | import org.springframework.beans.factory.support.AbstractBeanFactory;
|
15 | 33 | import org.springframework.context.ApplicationContext;
|
16 | 34 | import org.springframework.core.annotation.AnnotatedElementUtils;
|
17 |
| -import org.springframework.core.env.Environment; |
18 | 35 | import org.yeauty.annotation.ServerEndpoint;
|
19 | 36 | import org.yeauty.exception.DeploymentException;
|
20 | 37 | import org.yeauty.pojo.PojoEndpointServer;
|
21 | 38 | import org.yeauty.pojo.PojoMethodMapping;
|
22 | 39 | import org.yeauty.standard.ServerEndpointConfig;
|
23 | 40 |
|
24 |
| -import java.util.HashMap; |
25 |
| -import java.util.Map; |
26 |
| - |
27 | 41 | /**
|
28 | 42 | * @author pengyongjian
|
29 | 43 | * @Description:
|
|
32 | 46 | @Slf4j
|
33 | 47 | public class ServerEndpointExporter extends org.yeauty.standard.ServerEndpointExporter {
|
34 | 48 |
|
35 |
| - @Autowired |
36 |
| - Environment environment; |
37 |
| - |
38 | 49 | private AbstractBeanFactory beanFactory;
|
39 | 50 |
|
40 |
| - @Autowired |
41 |
| - private WebsocketServer websocketServer; |
42 |
| - |
43 | 51 | @Override
|
44 | 52 | public void afterSingletonsInstantiated() {
|
45 | 53 | registerEndpoint();
|
@@ -89,15 +97,67 @@ private void registerEndpoint() {
|
89 | 97 | // 创建http处理业务类对象
|
90 | 98 | HttpServerHandler httpServerHandler = new HttpServerHandler(pojoEndpointServer, serverEndpointConfig,
|
91 | 99 | functionHandlerMap, webSocketServerHandler);
|
92 |
| - // 将http处理业务类对象注入到netty的web容器中 |
93 |
| - websocketServer.setHttpServerHandler(httpServerHandler); |
94 | 100 | // netty的web容器的启动
|
95 |
| - init(serverEndpointConfig.getPort()); |
| 101 | + init(serverEndpointConfig.getPort(), httpServerHandler); |
96 | 102 | }
|
97 | 103 |
|
98 |
| - private void init(int port) { |
| 104 | + private void init(int port, HttpServerHandler httpServerHandler) { |
99 | 105 | try {
|
100 |
| - websocketServer.init(); |
| 106 | + ServerEndpointConfig config = httpServerHandler.getConfig(); |
| 107 | + EventLoopGroup boss = new NioEventLoopGroup(config.getBossLoopGroupThreads()); |
| 108 | + EventLoopGroup worker = new NioEventLoopGroup(config.getWorkerLoopGroupThreads()); |
| 109 | + ServerBootstrap bootstrap = new ServerBootstrap(); |
| 110 | + bootstrap.group(boss, worker) |
| 111 | + .channel(NioServerSocketChannel.class) |
| 112 | + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeoutMillis()) |
| 113 | + .option(ChannelOption.SO_BACKLOG, config.getSoBacklog()) |
| 114 | + .childOption(ChannelOption.WRITE_SPIN_COUNT, config.getWriteSpinCount()) |
| 115 | + .childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(config.getWriteBufferLowWaterMark(), config.getWriteBufferHighWaterMark())) |
| 116 | + .childOption(ChannelOption.TCP_NODELAY, config.isTcpNodelay()) |
| 117 | + .childOption(ChannelOption.SO_KEEPALIVE, config.isSoKeepalive()) |
| 118 | + .childOption(ChannelOption.SO_LINGER, config.getSoLinger()) |
| 119 | + .childOption(ChannelOption.ALLOW_HALF_CLOSURE, config.isAllowHalfClosure()) |
| 120 | + .handler(new LoggingHandler(LogLevel.DEBUG)) |
| 121 | + .childHandler(new ChannelInitializer<NioSocketChannel>() { |
| 122 | + @Override |
| 123 | + protected void initChannel(NioSocketChannel ch) throws Exception { |
| 124 | + ChannelPipeline pipeline = ch.pipeline(); |
| 125 | + pipeline.addLast(new HttpServerCodec()); |
| 126 | + pipeline.addLast(new HttpObjectAggregator(65536)); |
| 127 | + pipeline.addLast(httpServerHandler); |
| 128 | + } |
| 129 | + }); |
| 130 | + |
| 131 | + if (config.getSoRcvbuf() != -1) { |
| 132 | + bootstrap.childOption(ChannelOption.SO_RCVBUF, config.getSoRcvbuf()); |
| 133 | + } |
| 134 | + |
| 135 | + if (config.getSoSndbuf() != -1) { |
| 136 | + bootstrap.childOption(ChannelOption.SO_SNDBUF, config.getSoSndbuf()); |
| 137 | + } |
| 138 | + |
| 139 | + ChannelFuture channelFuture; |
| 140 | + if ("0.0.0.0".equals(config.getHost())) { |
| 141 | + channelFuture = bootstrap.bind(config.getPort()); |
| 142 | + } else { |
| 143 | + try { |
| 144 | + channelFuture = bootstrap.bind(new InetSocketAddress(InetAddress.getByName(config.getHost()), config.getPort())); |
| 145 | + } catch (UnknownHostException e) { |
| 146 | + channelFuture = bootstrap.bind(config.getHost(), config.getPort()); |
| 147 | + e.printStackTrace(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + channelFuture.addListener(future -> { |
| 152 | + if (!future.isSuccess()){ |
| 153 | + future.cause().printStackTrace(); |
| 154 | + } |
| 155 | + }); |
| 156 | + |
| 157 | + Runtime.getRuntime().addShutdownHook(new Thread(() -> { |
| 158 | + boss.shutdownGracefully().syncUninterruptibly(); |
| 159 | + worker.shutdownGracefully().syncUninterruptibly(); |
| 160 | + })); |
101 | 161 | logger.info("=====Netty WebSocket started on port:" + port + " =====");
|
102 | 162 | } catch (Exception e) {
|
103 | 163 | logger.error("websocket init fail", e);
|
|
0 commit comments