本文整理汇总了Java中io.netty.handler.codec.bytes.ByteArrayEncoder类的典型用法代码示例。如果您正苦于以下问题:Java ByteArrayEncoder类的具体用法?Java ByteArrayEncoder怎么用?Java ByteArrayEncoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ByteArrayEncoder类属于io.netty.handler.codec.bytes包,在下文中一共展示了ByteArrayEncoder类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
public void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(this.host, this.port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("connected server...");
ch.pipeline().addLast(new ByteArrayEncoder());
ch.pipeline().addLast(new ByteArrayDecoder());
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture cf = b.connect().sync();
cf.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
示例2: initChannel
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
final ChannelPipeline p = ch.pipeline();
final UUID uuid = UUID.randomUUID();
LOG.debug("KaaTcpServerInitializer Initializing Channel {} connection from {}:{}",
uuid, ch.remoteAddress().getAddress().toString(), ch.remoteAddress().getPort());
Attribute<UUID> uuidAttr = ch.attr(AbstractNettyServer.UUID_KEY);
uuidAttr.set(uuid);
p.addLast("binaryDecoder", new ByteArrayDecoder());
p.addLast("kaaTcpDecoder", getDecoder());
p.addLast("binaryEncoder", new ByteArrayEncoder());
p.addLast("kaaTcpEncoder", new KaaTcpEncoder());
p.addLast("mainHandler", getMainHandler(uuid));
p.addLast("kaaTcpExceptionHandler", new KaaTcpExceptionHandler());
}
示例3: start
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
@Override
public synchronized void start() {
bossGroup = new NioEventLoopGroup(); // (1)
workerGroup = new NioEventLoopGroup();
try {
b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ByteArrayDecoder());
ch.pipeline().addLast(new ByteArrayEncoder());
ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
ch.pipeline().addLast(new IdleStateHandler(heartTime, heartTime, heartTime, TimeUnit.SECONDS));
ch.pipeline().addLast(new DeliveryHandler(deliveryService));
}
})
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
// Bind and start to accept incoming connections.
b.bind(settingService.getDeliveryPort());
logger.info("socket: "+settingService.getDeliveryPort()+" starting....");
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
} catch (Exception e) {
e.printStackTrace();
}
}
示例4: newByteArrayEncoder
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
public static ChannelHandlerFactory newByteArrayEncoder(String protocol) {
if ("udp".equals(protocol)) {
return new ShareableChannelHandlerFactory(new DatagramPacketByteArrayEncoder());
} else {
return new ShareableChannelHandlerFactory(new ByteArrayEncoder());
}
}
示例5: open
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
public void open(EventLoopGroup eventLoopGroup) throws Exception {
if (openned.compareAndSet(false, true)) {
eventloopGroop = eventLoopGroup == null ? new NioEventLoopGroup()
: eventLoopGroup;
Bootstrap bootstrap = new Bootstrap();
final BlockingByteArrayClientHandler handler = new BlockingByteArrayClientHandler(
this);
this.clientHandler = handler;
bootstrap.group(eventloopGroop).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch)
throws Exception {
ChannelPipeline pipeline = ch.pipeline();
SSLEngine engine = SecureSocketSslContextFactory
.getClientContext().createSSLEngine();
engine.setUseClientMode(true);
pipeline.addLast("ssl", new SslHandler(engine));
pipeline.addLast("length-decoder",
new LengthFieldBasedFrameDecoder(
Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("bytearray-decoder",
new ByteArrayDecoder());
pipeline.addLast("length-encoder",
new LengthFieldPrepender(4));
pipeline.addLast("bytearray-encoder",
new ByteArrayEncoder());
pipeline.addLast("handler", handler);
}
});
channelFuture = bootstrap.connect(this.remoteHost, this.remotePort)
.sync();
}
}
示例6: initChannel
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
SSLEngine engine =
SecureSocketSslContextFactory.getServerContext().createSSLEngine();
engine.setUseClientMode(false);
pipeline.addLast("ssl", new SslHandler(engine));
pipeline.addLast("length-decoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast("bytearray-decoder", new ByteArrayDecoder());
pipeline.addLast("length-encoder", new LengthFieldPrepender(4));
pipeline.addLast("bytearray-encoder", new ByteArrayEncoder());
pipeline.addLast("handler", new SecureSocketServerhandler2());
}
示例7: GossipServerThread
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
public GossipServerThread() {
gossipBossGroup = new NioEventLoopGroup(GOSSIP_BOSS_THREADS);
gossipWorkerGroup = new NioEventLoopGroup(GOSSIP_WORKER_THREADS);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(gossipBossGroup, gossipWorkerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.config().setAllocator(UnpooledByteBufAllocator.DEFAULT);
ch.pipeline().addLast(new GossipMessageDecoder());
ch.pipeline().addLast("encoder", new ByteArrayEncoder());
ch.pipeline().addLast("decoder", new ByteArrayDecoder());
ch.pipeline().addLast(new GossipMessageHandler());
if(LOG.isTraceEnabled()) {
ch.pipeline().addLast(new LoggingHandler(LogLevel.TRACE));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOG.error("Cannot initialize gossip server.", cause);
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
f = b.bind(getIp(), getGossipPort()).sync();
} catch (InterruptedException ex) {
LOG.error("Gossip server interrupted.", ex);
}
}
示例8: GossipServerThread
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
public GossipServerThread() {
gossipBossGroup = new NioEventLoopGroup(GOSSIP_BOSS_THREADS);
gossipWorkerGroup = new NioEventLoopGroup(GOSSIP_WORKER_THREADS);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(gossipBossGroup, gossipWorkerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.config().setAllocator(UnpooledByteBufAllocator.DEFAULT);
ch.pipeline().addLast(new GossipMessageDecoder());
ch.pipeline().addLast("encoder", new ByteArrayEncoder());
ch.pipeline().addLast("decoder", new ByteArrayDecoder());
ch.pipeline().addLast(new GossipMessageHandler());
if (LOG.isTraceEnabled()) {
ch.pipeline().addLast(new LoggingHandler(LogLevel.TRACE));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOG.error("Cannot initialize gossip server.", cause);
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
// Bind and start to accept incoming connections.
f = b.bind(getIp(), getGossipPort()).sync();
} catch (InterruptedException ex) {
LOG.error("Gossip server interrupted.", ex);
}
}
示例9: initChannel
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
@Override
protected void initChannel(final SocketChannel ch) throws Exception {
ch.pipeline()
.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MAXFRAMELENGTH, 0, 4, 0, 4))
.addLast("bytesDecoder", new ByteArrayDecoder())
.addLast("frameEncoder", new LengthFieldPrepender(4))
.addLast("bytesEncoder", new ByteArrayEncoder())
.addLast("chunker", new ChunkedReadWriteHandler())
.addLast("handler", handlerFactory.createChannelInboundHandler());
}
示例10: initializeGossipClient
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
private void initializeGossipClient() {
LOG.trace("Initializing gossip client");
gossipWorkerGroup = new NioEventLoopGroup(GOSSIP_WORKER_THREADS);
Bootstrap b = new Bootstrap();
b.group(gossipWorkerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.config().setAllocator(UnpooledByteBufAllocator.DEFAULT);
ch.pipeline().addLast("encoder", new ByteArrayEncoder());
ch.pipeline().addLast("decoder", new ByteArrayDecoder());
ch.pipeline().addLast(new GossipExceptionHandler());
if(LOG.isTraceEnabled()) {
ch.pipeline().addLast(new LoggingHandler(LogLevel.TRACE));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOG.error("Cannot initialize gossip client.", cause);
ctx.close();
}
});
// Start the client.
ChannelFuture future = b.connect(getIp(), getGossipPort()).awaitUninterruptibly();
if(future.isCancelled()) {
gossipChannel = null;
} else if(!future.isSuccess()) {
gossipChannel = null;
retryGossipConnection();
} else {
gossipChannel = future.channel();
setStatus(MemberStatus.Alive);
updateMember();
}
}
示例11: initializeDataClient
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
private void initializeDataClient() {
LOG.trace("Initializing data client");
dataWorkerGroup = new NioEventLoopGroup(DATA_WORKER_THREADS);
Bootstrap b = new Bootstrap();
b.group(dataWorkerGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_SNDBUF, 262144)
.option(ChannelOption.SO_RCVBUF, 262144)
.option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.TCP_NODELAY, true)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.config().setAllocator(UnpooledByteBufAllocator.DEFAULT);
if(useSSL) {
ch.pipeline().addLast(sslContext.newHandler(ch.alloc(), ip, dataPort));
}
ch.pipeline().addLast("encoder", new ByteArrayEncoder());
ch.pipeline().addLast("decoder", new ByteArrayDecoder());
ch.pipeline().addLast(new DataExceptionHandler());
if(LOG.isTraceEnabled()) {
ch.pipeline().addLast(new LoggingHandler(LogLevel.TRACE));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOG.error("Cannot initialize data client.", cause);
ctx.close();
}
});
// Start the client.
ChannelFuture future = b.connect(getIp(), getDataPort()).awaitUninterruptibly();
if(future.isCancelled()) {
dataChannel = null;
} else if(!future.isSuccess()) {
dataChannel = null;
retryDataConnection();
} else {
dataChannel = future.channel();
try {
dataChannel.closeFuture().sync();
} catch (InterruptedException ex) {
LOG.debug("Interrupted waiting for client to shutdown.", ex);
}
}
}
示例12: initializeGossipClient
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
private void initializeGossipClient() {
LOG.trace("Initializing gossip client");
gossipWorkerGroup = new NioEventLoopGroup(GOSSIP_WORKER_THREADS);
Bootstrap b = new Bootstrap();
b.group(gossipWorkerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, timeout);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.config().setAllocator(UnpooledByteBufAllocator.DEFAULT);
ch.pipeline().addLast("encoder", new ByteArrayEncoder());
ch.pipeline().addLast("decoder", new ByteArrayDecoder());
ch.pipeline().addLast(new GossipExceptionHandler());
if (LOG.isTraceEnabled()) {
ch.pipeline().addLast(new LoggingHandler(LogLevel.TRACE));
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOG.error("Cannot initialize gossip client.", cause);
ctx.close();
}
});
// Start the client.
ChannelFuture future = b.connect(getIp(), getGossipPort()).awaitUninterruptibly();
if (future.isCancelled()) {
gossipChannel = null;
} else if (!future.isSuccess()) {
gossipChannel = null;
retryGossipConnection();
} else {
gossipChannel = future.channel();
setStatus(MemberStatus.Alive);
updateMember();
}
}
示例13: initChannel
import io.netty.handler.codec.bytes.ByteArrayEncoder; //导入依赖的package包/类
@Override
protected void initChannel(SocketChannel ch) throws Exception {
logger.debug("initChannel-start");
ProtocolDecoderService protocolDecoderService = null;
ProtocolEncoderService protocolEncoderService = null;
try{
protocolDecoderService = applicationContext.getBean(ProtocolDecoderService.class);
protocolEncoderService = applicationContext.getBean(ProtocolEncoderService.class);
}catch (Exception e){
protocolDecoderService = new DefaultProtocolDecoderService();
protocolEncoderService = new DefaultProtocolEncoderService();
}
logger.debug("initChannel->protocolDecoderService:"+protocolDecoderService);
logger.debug("initChannel->protocolEncoderService:"+protocolEncoderService);
ch.pipeline().addLast(ByteArrayDecoder,new ByteArrayDecoder());
ch.pipeline().addLast(ByteArrayEncoder,new ByteArrayEncoder());
ch.pipeline().addLast(LengthFieldBasedFrameDecoder,new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
ch.pipeline().addLast(ProtocolDecoderHandler,new ProtocolDecoderHandler(protocolDecoderService));
ch.pipeline().addLast(ProtocolEncoderHandler,new ProtocolEncoderHandler(protocolEncoderService));
ch.pipeline().addLast(SystemTimeOut,new IdleStateHandler(heartTime, heartTime, heartTime, TimeUnit.SECONDS));
ch.pipeline().addLast(SocketHandler,new SocketHandler(socketService));
logger.debug("initChannel-end");
}