Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1.  
  2. Save New Duplicate & Edit Just Text
  3. package io.netty.handler.codec;
  4.  
  5. import de.melonbungee.javainteger.MelonBungee;
  6. import io.netty.buffer.ByteBuf;
  7. import io.netty.buffer.ByteBufAllocator;
  8. import io.netty.buffer.Unpooled;
  9. import io.netty.channel.ChannelFuture;
  10. import io.netty.channel.ChannelHandlerContext;
  11. import io.netty.channel.ChannelOutboundHandlerAdapter;
  12. import io.netty.channel.ChannelPromise;
  13. import io.netty.handler.codec.EncoderException;
  14. import io.netty.util.ReferenceCountUtil;
  15. import io.netty.util.ReferenceCounted;
  16. import io.netty.util.internal.TypeParameterMatcher;
  17.  
  18. public abstract class MessageToByteEncoder<I>
  19. extends ChannelOutboundHandlerAdapter {
  20. private final TypeParameterMatcher matcher;
  21. private final boolean preferDirect;
  22. private MelonBungee MelonBungee;
  23.  
  24. protected MessageToByteEncoder() {
  25. this(true);
  26. }
  27.  
  28. protected MessageToByteEncoder(Class<? extends I> outboundMessageType) {
  29. this(outboundMessageType, true);
  30. }
  31.  
  32. protected MessageToByteEncoder(boolean preferDirect) {
  33. this.matcher = TypeParameterMatcher.find(this, MessageToByteEncoder.class, "I");
  34. this.preferDirect = preferDirect;
  35. }
  36.  
  37. protected MessageToByteEncoder(Class<? extends I> outboundMessageType, boolean preferDirect) {
  38. this.matcher = TypeParameterMatcher.get(outboundMessageType);
  39. this.preferDirect = preferDirect;
  40. }
  41.  
  42. public boolean acceptOutboundMessage(Object msg) throws Exception {
  43. return this.matcher.match(msg);
  44. }
  45. @Override
  46. public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
  47. try {
  48. block14 : {
  49. ReferenceCounted buf = null;
  50. try {
  51. if (this.acceptOutboundMessage(msg)) {
  52. Object cast = msg;
  53. buf = this.allocateBuffer(ctx, (I) cast, this.preferDirect);
  54. try {
  55. this.encode(ctx, (I) cast, (ByteBuf)buf);
  56. }
  57. finally {
  58. ReferenceCountUtil.release(cast);
  59. }
  60. if (((ByteBuf)buf).isReadable()) {
  61. ctx.write(buf, promise);
  62. } else {
  63. buf.release();
  64. ctx.write(Unpooled.EMPTY_BUFFER, promise);
  65. }
  66. buf = null;
  67. break block14;
  68. }
  69. ctx.write(msg, promise);
  70. }
  71. catch (EncoderException e) {
  72. ctx.close();
  73. }
  74. catch (Throwable e) {
  75. ctx.close();
  76. }
  77. finally {
  78. if (buf != null) {
  79. buf.release();
  80. }
  81. }
  82. }
  83. } catch (Exception e) {
  84. MelonBungee.blockinquiry(null, ctx, ctx.channel().remoteAddress().toString());
  85. }
  86. }
  87.  
  88. protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, I msg, boolean preferDirect) throws Exception {
  89. if (preferDirect) {
  90. return ctx.alloc().ioBuffer();
  91. }
  92. return ctx.alloc().heapBuffer();
  93. }
  94.  
  95. protected abstract void encode(ChannelHandlerContext var1, I var2, ByteBuf var3) throws Exception;
  96.  
  97. protected boolean isPreferDirect() {
  98. return this.preferDirect;
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement