|
| 1 | +package io.jpower.kcp.example.rtt; |
| 2 | + |
| 3 | +import io.jpower.kcp.netty.UkcpChannel; |
| 4 | +import io.netty.buffer.ByteBuf; |
| 5 | +import io.netty.buffer.Unpooled; |
| 6 | +import io.netty.channel.ChannelHandlerContext; |
| 7 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import java.util.concurrent.Executors; |
| 12 | +import java.util.concurrent.ScheduledExecutorService; |
| 13 | +import java.util.concurrent.ScheduledFuture; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author <a href="mailto:[email protected]">szh</a> |
| 18 | + */ |
| 19 | +public class KcpRttClientHandler extends ChannelInboundHandlerAdapter { |
| 20 | + |
| 21 | + private static final Logger log = LoggerFactory.getLogger(KcpRttClientHandler.class); |
| 22 | + |
| 23 | + private final ByteBuf data; |
| 24 | + |
| 25 | + private int[] rtts; |
| 26 | + |
| 27 | + private volatile int count; |
| 28 | + |
| 29 | + private ScheduledExecutorService scheduleSrv; |
| 30 | + |
| 31 | + private ScheduledFuture<?> future = null; |
| 32 | + |
| 33 | + private final long startTime; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates a client-side handler. |
| 37 | + */ |
| 38 | + public KcpRttClientHandler(int count) { |
| 39 | + data = Unpooled.buffer(KcpRttClient.SIZE); |
| 40 | + for (int i = 0; i < data.capacity(); i++) { |
| 41 | + data.writeByte((byte) i); |
| 42 | + } |
| 43 | + |
| 44 | + rtts = new int[count]; |
| 45 | + for (int i = 0; i < rtts.length; i++) { |
| 46 | + rtts[i] = -1; |
| 47 | + } |
| 48 | + startTime = System.currentTimeMillis(); |
| 49 | + scheduleSrv = Executors.newSingleThreadScheduledExecutor(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void channelActive(ChannelHandlerContext ctx) { |
| 54 | + UkcpChannel kcpCh = (UkcpChannel) ctx.channel(); |
| 55 | + kcpCh.conv(KcpRttClient.CONV); // set conv |
| 56 | + |
| 57 | + future = scheduleSrv.scheduleWithFixedDelay(new Runnable() { |
| 58 | + @Override |
| 59 | + public void run() { |
| 60 | + ctx.write(rttMsg(++count)); |
| 61 | + if (count >= rtts.length) { |
| 62 | + // finish |
| 63 | + future.cancel(true); |
| 64 | + ctx.write(rttMsg(-1)); |
| 65 | + |
| 66 | + } |
| 67 | + ctx.flush(); |
| 68 | + } |
| 69 | + }, KcpRttClient.RTT_INTERVAL, KcpRttClient.RTT_INTERVAL, TimeUnit.MILLISECONDS); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * count+timestamp+dataLen+data |
| 74 | + * |
| 75 | + * @param count |
| 76 | + * @return |
| 77 | + */ |
| 78 | + public ByteBuf rttMsg(int count) { |
| 79 | + ByteBuf buf = Unpooled.buffer(10); |
| 80 | + buf.writeShort(count); |
| 81 | + buf.writeInt((int) (System.currentTimeMillis() - startTime)); |
| 82 | + int dataLen = data.readableBytes(); |
| 83 | + buf.writeShort(dataLen); |
| 84 | + buf.writeBytes(data, data.readerIndex(), dataLen); |
| 85 | + |
| 86 | + return buf; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
| 91 | + scheduleSrv.shutdown(); |
| 92 | + scheduleSrv.awaitTermination(3, TimeUnit.SECONDS); |
| 93 | + |
| 94 | + int sum = 0; |
| 95 | + for (int rtt : rtts) { |
| 96 | + sum += rtt; |
| 97 | + } |
| 98 | + log.info("average: {}", (sum / rtts.length)); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 103 | + ByteBuf buf = (ByteBuf) msg; |
| 104 | + int curCount = buf.readShort(); |
| 105 | + |
| 106 | + if (curCount == -1) { |
| 107 | + scheduleSrv.schedule(new Runnable() { |
| 108 | + @Override |
| 109 | + public void run() { |
| 110 | + ctx.close(); |
| 111 | + } |
| 112 | + }, 3, TimeUnit.SECONDS); |
| 113 | + } else { |
| 114 | + int idx = curCount - 1; |
| 115 | + long time = buf.readInt(); |
| 116 | + if (rtts[idx] != -1) { |
| 117 | + log.error("???"); |
| 118 | + } |
| 119 | + //log.info("rcv count {} {}", curCount, System.currentTimeMillis()); |
| 120 | + rtts[idx] = (int) (System.currentTimeMillis() - startTime - time); |
| 121 | + |
| 122 | + log.info("rtt {}: {}", curCount, rtts[idx]); |
| 123 | + } |
| 124 | + |
| 125 | + buf.release(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 130 | + // Close the connection when an exception is raised. |
| 131 | + log.error("exceptionCaught", cause); |
| 132 | + ctx.close(); |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments