From 0a7fdd1b4d03f68995a649575e9019112f687aca Mon Sep 17 00:00:00 2001 From: jaysunxiao Date: Sun, 16 Jan 2022 15:56:29 +0800 Subject: [PATCH] =?UTF-8?q?feat[net]:=20=E5=A2=9E=E5=8A=A0protobuf?= =?UTF-8?q?=E7=9A=84=E7=BD=91=E5=85=B3=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jprotobuf/JProtobufGatewayServer.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufGatewayServer.java diff --git a/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufGatewayServer.java b/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufGatewayServer.java new file mode 100644 index 00000000..6a9fe5d5 --- /dev/null +++ b/net/src/main/java/com/zfoo/net/core/jprotobuf/JProtobufGatewayServer.java @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2020 The zfoo Authors + * Licensed 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 com.zfoo.net.core.jprotobuf; + +import com.zfoo.net.core.AbstractServer; +import com.zfoo.net.handler.GatewayRouteHandler; +import com.zfoo.net.handler.codec.jprotobuf.JProtobufTcpCodecHandler; +import com.zfoo.net.handler.idle.ServerIdleHandler; +import com.zfoo.net.session.model.Session; +import com.zfoo.protocol.IPacket; +import com.zfoo.util.net.HostAndPort; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.timeout.IdleStateHandler; +import org.springframework.lang.Nullable; + +import java.util.function.BiFunction; + +/** + * @author jaysunxiao + * @version 3.0 + */ +public class JProtobufGatewayServer extends AbstractServer { + + + private final BiFunction packetFilter; + + public JProtobufGatewayServer(HostAndPort host, @Nullable BiFunction packetFilter) { + super(host); + this.packetFilter = packetFilter; + } + + @Override + public ChannelInitializer channelChannelInitializer() { + return new ChannelHandlerInitializer(packetFilter); + } + + + private static class ChannelHandlerInitializer extends ChannelInitializer { + + private final BiFunction packetFilter; + + public ChannelHandlerInitializer(BiFunction packetFilter) { + this.packetFilter = packetFilter; + } + + @Override + protected void initChannel(SocketChannel channel) { + channel.pipeline().addLast(new IdleStateHandler(0, 0, 180)); + channel.pipeline().addLast(new ServerIdleHandler()); + channel.pipeline().addLast(new JProtobufTcpCodecHandler()); + channel.pipeline().addLast(new GatewayRouteHandler(packetFilter)); + } + } + +}