下面列出了 io.netty.handler.codec.bytes.ByteArrayEncoder #io.netty.handler.traffic.ChannelTrafficShapingHandler 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Channel getContextLocalChannel(ClientSession clientSession) {
Channel channel = null;
if (!oneNetClientContextConfig.isLocalPool()) {
channel = OneNetClient.createChannel(oneNetClientContextConfig.getLocalhost(),
oneNetClientContextConfig.getPort(),
new LocalChannelInitializer(this, clientSession));
} else {
try {
//log.info(String.format("%s channel borrowed. ,Local Pool active %d , idle %d",this.getOneNetClientContextConfig().getContextName(),localPool.getNumActive(),localPool.getNumIdle()));
channel = localPool.borrowObject();
} catch (Exception e) {
throw new RuntimeException("Can't borrow object from pool. - " + e.getMessage());
}
ChannelHandler handler = channel.pipeline().get(LocalChannelInitializer.LOCAL_RESPONSE_HANDLER);
((LocalInboudHandler) handler).setClientSession(clientSession);
ChannelTrafficShapingHandler channelTrafficShapingHandler =
(ChannelTrafficShapingHandler) channel.pipeline().get(LocalChannelInitializer.CHANNEL_TRAFFIC_HANDLER);
channelTrafficShapingHandler.setReadLimit(kBps * OneNetCommonConstants.KByte);
channelTrafficShapingHandler.setWriteLimit(kBps * OneNetCommonConstants.KByte);
log.debug(localPool.getNumActive() + "-" + localPool.getNumIdle());
}
return channel;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ClientSession clientSession =
oneNetServerContext.getOneNetConnectionManager().getAvailableSession(
oneNetServerContext.getOneNetServerContextConfig().getContextName());
if (!clientSession.isActive()) {
ch.close();
log.info(String.format("Can't found %s's client session.",oneNetServerContext.getOneNetServerContextConfig().getContextName()));
} else {
OneNetSession oneNetSession = oneNetServerContext.createSession(ch, clientSession);
int bytePreSecond = oneNetServerContext.getOneNetServerContextConfig().getKBps() * OneNetCommonConstants.KByte;
log.debug("Session create:"+oneNetSession.toString());
ch.pipeline()
.addLast(new ChannelTrafficShapingHandler(bytePreSecond,
bytePreSecond))
.addLast(new InternetChannelInboundHandler(oneNetServerContext, oneNetSession))
.addLast(new ByteArrayEncoder());
}
}
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
if (GameConfiguration.getInstance().getBoolean("server.limit.bandwidth")) {
long bandwidthLimit = GameConfiguration.getInstance().getLong("server.limit.bandwidth.amount");
pipeline.addLast("trafficShapingHandler", new ChannelTrafficShapingHandler(bandwidthLimit, bandwidthLimit));
}
pipeline.addLast("gameEncoder", new NetworkEncoder());
pipeline.addLast("gameDecoder", new NetworkDecoder());
pipeline.addLast("handler", new ConnectionHandler(this.nettyServer));
pipeline.addLast("idleStateHandler", new IdleStateHandler(60, 0, 0));
pipeline.addLast("idleHandler", new IdleConnectionHandler());
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
int bytesPreSecond = 0;
if (clientSession != null) {
bytesPreSecond = oneNetClientContext.getKBps() * OneNetCommonConstants.KByte;
}
p.addLast(new WriteTimeoutHandler(5))
.addLast(CHANNEL_TRAFFIC_HANDLER, new ChannelTrafficShapingHandler(bytesPreSecond,
bytesPreSecond))
.addLast(LOCAL_RESPONSE_HANDLER, new LocalInboudHandler(oneNetClientContext, clientSession))
.addLast(new ByteArrayEncoder());
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf in = (ByteBuf) msg;
int length = in.readableBytes();
byte[] currentData = new byte[length];
in.readBytes(currentData, 0, length);
in.resetReaderIndex();
super.channelRead(ctx, msg);
if (httpSession != null) {
httpSession.getClientSession().getClientChannel().writeAndFlush(new DataPackage(
httpSession.getContextName(),
httpSession.getSessionId(),
currentData,
oneNetServerContext.getOneNetServerContextConfig().isZip(),
oneNetServerContext.getOneNetServerContextConfig().isAes()));
int kBps = oneNetServerContext.getOneNetServerContextConfig().getKBps() * OneNetCommonConstants.KByte;
ChannelTrafficShapingHandler trafficShapingHandler = ((ChannelTrafficShapingHandler) ctx.pipeline().get(HttpChannelInitializer.trafficHandler));
trafficShapingHandler.setWriteLimit(kBps);
trafficShapingHandler.setReadLimit(kBps);
} else {
log.info(String.format("Can't find client session for http context %s.",
hostName));
ctx.close();
}
in.release();
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
int bytePreSecond = 5 * OneNetCommonConstants.KByte;
ch.pipeline()
.addLast(trafficHandler, new ChannelTrafficShapingHandler(bytePreSecond,
bytePreSecond))
.addLast(new HttpChannelInboundHandler())
.addLast(new ByteArrayEncoder());
}