del[net]: remove jprotobuf

This commit is contained in:
godotg committed 2023-12-10 23:13:41 +08:00
1 parent d0e83c6e1e
commit f1cb975715
28 files changed
+12 -783

No files matched your search

@@ -1,48 +0,0 @@
/*
* 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.core.HostAndPort;
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.Session;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.lang.Nullable;
import java.util.function.BiFunction;
/**
* @author godotg
*/
public class JProtobufGatewayServer extends AbstractServer<SocketChannel> {
private final BiFunction<Session, Object, Boolean> packetFilter;
public JProtobufGatewayServer(HostAndPort host, @Nullable BiFunction<Session, Object, Boolean> packetFilter) {
super(host);
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));
}
}
@@ -1,40 +0,0 @@
/*
* 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.AbstractClient;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.handler.ClientRouteHandler;
import com.zfoo.net.handler.codec.jprotobuf.JProtobufTcpCodecHandler;
import com.zfoo.net.handler.idle.ClientIdleHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
/**
* @author godotg
*/
public class JProtobufTcpClient extends AbstractClient<SocketChannel> {
public JProtobufTcpClient(HostAndPort host) {
super(host);
}
@Override
protected void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new IdleStateHandler(0, 0, 60));
channel.pipeline().addLast(new ClientIdleHandler());
channel.pipeline().addLast(new JProtobufTcpCodecHandler());
channel.pipeline().addLast(new ClientRouteHandler());
}
}
@@ -1,40 +0,0 @@
/*
* 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.core.HostAndPort;
import com.zfoo.net.handler.ServerRouteHandler;
import com.zfoo.net.handler.codec.jprotobuf.JProtobufTcpCodecHandler;
import com.zfoo.net.handler.idle.ServerIdleHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;
/**
* @author godotg
*/
public class JProtobufTcpServer extends AbstractServer<SocketChannel> {
public JProtobufTcpServer(HostAndPort host) {
super(host);
}
@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 ServerRouteHandler());
}
}
@@ -1,97 +0,0 @@
/*
* 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.handler.codec.jprotobuf;
import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.zfoo.net.packet.DecodedPacketInfo;
import com.zfoo.net.packet.EncodedPacketInfo;
import com.zfoo.net.packet.PacketService;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.buffer.ByteBufUtils;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;
import java.io.IOException;
import java.util.List;
/**
* header(4byte) + protocolId(2byte) + packet
* header = body(bytes.length) + protocolId.length(2byte)
*
* @author godotg
*/
public class JProtobufTcpCodecHandler extends ByteToMessageCodec<EncodedPacketInfo> {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws IOException {
// 不够读一个int
if (in.readableBytes() <= PacketService.PACKET_HEAD_LENGTH) {
return;
}
in.markReaderIndex();
var length = in.readInt();
// 如果长度非法,则抛出异常断开连接,按照自己的使用场景指定合适的长度,防止客户端发送超大包占用带宽
if (length < 0 || length > IOUtils.BYTES_PER_MB) {
throw new IllegalArgumentException(StringUtils.format("illegal packet [length:{}]", length));
}
// ByteBuf里的数据太小
if (in.readableBytes() < length) {
in.resetReaderIndex();
return;
}
var sliceByteBuf = in.readSlice(length);
var packetInfo = read(sliceByteBuf);
out.add(packetInfo);
}
@Override
protected void encode(ChannelHandlerContext ctx, EncodedPacketInfo packetInfo, ByteBuf out) throws IOException {
write(out, packetInfo.getPacket(), packetInfo.getAttachment());
}
public static DecodedPacketInfo read(ByteBuf buffer) throws IOException {
var protocolId = ByteBufUtils.readShort(buffer);
var protocolRegistration = ProtocolManager.getProtocol(protocolId);
var protocolClass = protocolRegistration.protocolConstructor().getDeclaringClass();
var protobufCodec = ProtobufProxy.create(protocolClass);
var bytes = ByteBufUtils.readAllBytes(buffer);
var packet = protobufCodec.decode(bytes);
return DecodedPacketInfo.valueOf(packet, null);
}
public void write(ByteBuf buffer, Object packet, Object attachment) throws IOException {
// 写入protobuf协议
@SuppressWarnings("unchecked")
var protobufCodec = (Codec<Object>) ProtobufProxy.create(packet.getClass());
byte[] bytes = protobufCodec.encode(packet);
// header(4byte) + protocolId(2byte)
buffer.writeInt(bytes.length + 2);
var protocolId = ProtocolManager.protocolId(packet.getClass());
// 写入协议号
ByteBufUtils.writeShort(buffer, protocolId);
buffer.writeBytes(bytes);
}
}
@@ -13,8 +13,6 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.anno.Protocol;
import org.slf4j.helpers.FormattingTuple;
@@ -23,15 +21,11 @@ import org.slf4j.helpers.MessageFormatter;
/**
* @author godotg
*/
@ProtobufClass
@Protocol(id = 101)
public class Error {
@Protobuf(order = 1)
private int module;
@Protobuf(order = 2)
private int errorCode;
@Protobuf(order = 3)
private String errorMessage;
@Override
@@ -13,13 +13,11 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@ProtobufClass
@Protocol(id = 102)
public class Heartbeat {
@@ -13,7 +13,6 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.anno.Protocol;
@@ -22,7 +21,6 @@ import com.zfoo.protocol.anno.Protocol;
*
* @author godotg
*/
@ProtobufClass
@Protocol(id = 100)
public class Message {
@@ -13,13 +13,11 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@ProtobufClass
@Protocol(id = 103)
public class Ping {
@@ -13,21 +13,17 @@
package com.zfoo.net.packet.common;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@ProtobufClass
@Protocol(id = 104)
public class Pong {
/**
* 服务器当前的时间戳
*/
@Protobuf(order = 1)
private long time;
public static Pong valueOf(long time) {
@@ -1,36 +0,0 @@
/*
* 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.client;
import com.zfoo.net.packet.jprotobuf.JProtobufHelloResponse;
import com.zfoo.net.anno.PacketReceiver;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @author godotg
*/
@Component
public class JProtobufTcpClientController {
private static final Logger logger = LoggerFactory.getLogger(JProtobufTcpClientController.class);
@PacketReceiver
public void atJProtobufHelloResponse(Session session, JProtobufHelloResponse ask) {
logger.info("receive [packet:{}] from server", JsonUtils.object2String(ask));
}
}
@@ -1,52 +0,0 @@
/*
* 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.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.jprotobuf.JProtobufTcpClient;
import com.zfoo.net.packet.jprotobuf.JProtobufHelloRequest;
import com.zfoo.protocol.util.ThreadUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author godotg
*/
@Ignore
public class JProtobufTcpClientTest {
private static final Logger logger = LoggerFactory.getLogger(JProtobufTcpClientTest.class);
@Test
public void startClient() throws Exception {
var context = new ClassPathXmlApplicationContext("config.xml");
var client = new JProtobufTcpClient(HostAndPort.valueOf("127.0.0.1:9000"));
var session = client.start();
for (int i = 0; i < 1000; i++) {
var ask = new JProtobufHelloRequest();
ask.setMessage("Hello, this is jprotobuf client!");
NetContext.getRouter().send(session, ask);
ThreadUtils.sleep(1000);
}
ThreadUtils.sleep(Long.MAX_VALUE);
}
}
@@ -1,43 +0,0 @@
/*
* 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.server;
import com.zfoo.net.NetContext;
import com.zfoo.net.packet.jprotobuf.JProtobufHelloRequest;
import com.zfoo.net.packet.jprotobuf.JProtobufHelloResponse;
import com.zfoo.net.anno.PacketReceiver;
import com.zfoo.net.session.Session;
import com.zfoo.protocol.util.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* @author godotg
*/
@Component
public class JProtobufTcpController {
private static final Logger logger = LoggerFactory.getLogger(JProtobufTcpController.class);
@PacketReceiver
public void atJProtobufHelloRequest(Session session, JProtobufHelloRequest ask) {
logger.info("receive [packet:{}] from client", JsonUtils.object2String(ask));
var answer = new JProtobufHelloResponse();
answer.setMessage("Hello, this is the jprotobuf tcp server!");
NetContext.getRouter().send(session, answer);
}
}
@@ -1,39 +0,0 @@
/*
* 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.server;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.jprotobuf.JProtobufTcpServer;
import com.zfoo.protocol.util.ThreadUtils;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author godotg
*/
@Ignore
public class JProtobufTcpServerTest {
@Test
public void startServer() {
var context = new ClassPathXmlApplicationContext("config.xml");
var server = new JProtobufTcpServer(HostAndPort.valueOf("127.0.0.1:9000"));
server.start();
ThreadUtils.sleep(Long.MAX_VALUE);
}
}
@@ -15,7 +15,6 @@ package com.zfoo.net.core.json.client;
import com.zfoo.net.NetContext;
import com.zfoo.net.core.HostAndPort;
import com.zfoo.net.core.jprotobuf.server.JProtobufTcpController;
import com.zfoo.net.core.json.JsonWebsocketClient;
import com.zfoo.net.packet.json.JsonHelloRequest;
import com.zfoo.net.packet.json.JsonHelloResponse;
@@ -55,8 +55,6 @@ public class TcpServerTest {
/*
serverPacketController
gatewayProviderController
JProtobufTcpClientController
JProtobufTcpController
providerController
tcpClientController
tcpServerController
@@ -1,40 +0,0 @@
/*
* 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.packet.jprotobuf;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@Protocol(id = 1500)
public class JProtobufHelloRequest {
@Protobuf(order = 1)
private String message;
public static JProtobufHelloRequest valueOf(String message) {
var request = new JProtobufHelloRequest();
request.message = message;
return request;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
@@ -1,34 +0,0 @@
/*
* 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.packet.jprotobuf;
import com.baidu.bjf.remoting.protobuf.annotation.Protobuf;
import com.zfoo.protocol.anno.Protocol;
/**
* @author godotg
*/
@Protocol(id = 1501)
public class JProtobufHelloResponse {
@Protobuf(order = 1)
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
-3
View File
@@ -65,9 +65,6 @@
<protocol id="1400" location="com.zfoo.net.packet.websocket.WebsocketHelloRequest"/>
<protocol id="1401" location="com.zfoo.net.packet.websocket.WebsocketHelloResponse"/>
<protocol id="1500" location="com.zfoo.net.packet.jprotobuf.JProtobufHelloRequest"/>
<protocol id="1501" location="com.zfoo.net.packet.jprotobuf.JProtobufHelloResponse"/>
<protocol id="1600" location="com.zfoo.net.packet.json.JsonHelloRequest"/>
<protocol id="1601" location="com.zfoo.net.packet.json.JsonHelloResponse"/>