Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
* please note that tRPC source code is licensed under the Apache 2.0 License,
* A copy of the Apache 2.0 License can be found in the LICENSE file.
*/
package com.tencent.trpc.transport.netty;
import com.google.common.base.Preconditions;
import com.tencent.trpc.core.common.config.ProtocolConfig;
import com.tencent.trpc.core.exception.TransportException;
import com.tencent.trpc.core.rpc.Request;
import com.tencent.trpc.core.rpc.RequestMeta;
import com.tencent.trpc.core.rpc.Response;
import com.tencent.trpc.core.transport.codec.ChannelBuffer;
import com.tencent.trpc.core.transport.codec.Codec;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import java.net.InetSocketAddress;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Codec adapter both for tcp & udp
*/
public class NettyCodecAdapter {
private static final Logger LOGGER = LoggerFactory.getLogger(NettyCodecAdapter.class);
private final ChannelHandler encoder;
private final ChannelHandler decoder;
private final Codec codec;
private final ProtocolConfig config;
private NettyCodecAdapter(Codec codec, ProtocolConfig config, boolean isTcp) {
this.codec = codec;
this.config = config;
this.encoder = (isTcp ? new TcpEncoder0() : new UdpEncoder0());
this.decoder = (isTcp ? (config.getBatchDecoder() ? new TcpDecoder0() : new TcpDecoder1()) : new UdpDecoder0());
}
public static NettyCodecAdapter createTcpCodecAdapter(Codec codec, ProtocolConfig config) {
return new NettyCodecAdapter(codec, config, true);
}
public static NettyCodecAdapter createUdpCodecAdapter(Codec codec, ProtocolConfig config) {
return new NettyCodecAdapter(codec, config, false);
}
public ChannelHandler getEncoder() {
return encoder;
}
public ChannelHandler getDecoder() {
return decoder;
}
private class TcpEncoder0 extends MessageToByteEncoder