Advertisement
Guest User

Untitled

a guest
May 29th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. // RAW TSTING
  2. static int mtu = 9999;
  3. static String address = "192.168.1.16";
  4. static int port = 19132;
  5. static int decrease = 2;
  6.  
  7. public static void main(String[] args) {
  8. // Create boss group for shut down
  9. InetSocketAddress destination = new InetSocketAddress(address, port);
  10. EventLoopGroup group = new NioEventLoopGroup();
  11. try {
  12. // Store status in an object as it must be final
  13. final BooleanContainer status = new BooleanContainer(true);
  14.  
  15. // Creaete bootstrap and set the options
  16. final Bootstrap b = new Bootstrap().group(group).channel(NioDatagramChannel.class);
  17. b.option(ChannelOption.SO_RCVBUF, mtu).option(ChannelOption.SO_SNDBUF, mtu)
  18. .handler(new SimpleChannelInboundHandler<DatagramPacket>() {
  19.  
  20. @Override
  21. protected void messageReceived(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
  22. // Message was received
  23. ByteBuf buf = msg.content();
  24. short id = (short) (buf.readByte() & 0xFF);
  25.  
  26. // Make sure it was the correct packet
  27. if (id == 0x06) {
  28. buf.readBytes(new byte[25]); // These are not
  29. // used in this
  30. // program, only
  31. // the last 2
  32. // bytes (The
  33. // MTU)
  34. System.out.println("Received response! (MTU: " + buf.readShort() + ")");
  35. b.option(ChannelOption.SO_SNDBUF, mtu).option(ChannelOption.SO_RCVBUF, mtu);
  36.  
  37. // Stop loop and let them know it finished
  38. // correctly
  39. status.value = false;
  40. System.out.println("Connected!");
  41. } else if (id == 0x1A) {
  42. // Stop loop but let them know it was the wrong
  43. // protocol
  44. System.out.println("Wrong protocol!");
  45. status.value = false;
  46. } else {
  47. System.out.println("Unknown ID! " + id);
  48. }
  49. }
  50. });
  51.  
  52. // Bind, get channel, and start the send loop
  53. Channel c = b.bind(0).sync().channel();
  54. while (status.value) {
  55. // Send 0x05 with the specified MTU and protocol
  56. UnconnectedConnectionRequestOne r = new UnconnectedConnectionRequestOne();
  57. r.mtuSize = (short) mtu;
  58. r.protocol = 7;
  59. r.encode();
  60.  
  61. // Set send buffer to the new mtu and send
  62. b.option(ChannelOption.SO_SNDBUF, (int) r.mtuSize);
  63. c.writeAndFlush(new DatagramPacket(r.buffer(), destination)).sync();
  64.  
  65. // Print message, decrease mtu, and wait .5 seconds to give the
  66. // server some time to response
  67. System.out.println("Sent packet with MTU: " + r.mtuSize);
  68. mtu -= 100L;
  69. Thread.sleep(500L);
  70. }
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. group.shutdownGracefully();
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement