Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.43 KB | None | 0 0
  1. package one.video.streaming.oktp;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.Map;
  7. import java.util.PriorityQueue;
  8. import java.util.concurrent.atomic.AtomicInteger;
  9. import java.util.concurrent.atomic.AtomicLong;
  10. import one.video.streaming.oktp.packets.attr3MTUTestACK;
  11. import one.video.streaming.oktp.packets.attr7DHFailure;
  12. import one.video.streaming.util.Copy;
  13.  
  14. public class Protocol extends ProtocolBase {
  15.     public interface UDPConnectionBase {
  16.         void DHConnected();
  17.  
  18.         void handleReceiveMsg(int seq, ReceiveMsgBase msg);
  19.     }
  20.  
  21.     private final UDPConnectionBase _udpConn;
  22.     private final Payload _payload00;
  23.     private final UDPSendBase _udpSend;
  24.     private final UDPPacket _udpPacket;
  25.     private AtomicInteger _nstream;
  26.     private AtomicLong ___h;
  27.     private AtomicLong _total;
  28.     private PriorityQueue _streams;
  29.     private Map _streamsMap;
  30.     private Map _recvMsgMap;
  31.     private final WindowPacket _windowPacket;
  32.     private MTUTest _mtuTest;
  33.     private final DHConn _dhConn;
  34.     private boolean _resendPing;
  35.     private UDPPing _udpPing;
  36.     private long _rate;
  37.  
  38.     public Protocol(UDPConnectionBase udpConn, UDPSendBase udpSend, int timeoutInactive, Sig sig) {
  39.         super(sig);
  40.         this._nstream = new AtomicInteger(-1);
  41.         this.___h = new AtomicLong();
  42.         this._total = new AtomicLong();
  43.         this._streams = new PriorityQueue();
  44.         this._streamsMap = new HashMap();
  45.         this._recvMsgMap = new HashMap();
  46.         this._udpConn = udpConn;
  47.         this._udpPacket = new UDPPacket(sig);
  48.         this._payload00 = new Payload(udpSend, this._udpPacket, false, false);
  49.         this._mtuTest = new MTUTest(udpSend, this._udpPacket);
  50.         this._udpSend = udpSend;
  51.         this._windowPacket = new WindowPacket(this._mtuTest.getMtu(), ((long)timeoutInactive));
  52.         this._dhConn = new DHConn(this);
  53.     }
  54.  
  55.     public void setKey(byte[] key) {
  56.         super.setKey(key);
  57.         this._udpPacket.setKey(key);
  58.     }
  59.  
  60.     public void setSeq(int seq) {
  61.         this._udpPacket.setSeq(seq);
  62.     }
  63.  
  64.     private long send(int arg13, long maxsize, boolean more) {
  65.         long newsize;
  66.         Iterator it = this._streams.iterator();
  67.         long size = 0;
  68.         while(true) {
  69.             if(it.hasNext()) {
  70.                 newsize = (((long)it.next().send(this._payload00, arg13, maxsize, this._windowPacket.maxTotalSendSeq(), more))) + size;
  71.                 if(newsize < maxsize && !this._payload00.getSendError()) {
  72.                     size = newsize;
  73.                     continue;
  74.                 }
  75.             }
  76.             else {
  77.                 return size;
  78.             }
  79.  
  80.             return newsize;
  81.         }
  82.  
  83.         return size;
  84.     }
  85.  
  86.     protected ReceiveMsgBase GetReceiveMsg(byte nstream, AtomicLong total, int limit) {
  87.         return new ReceiveMsgBase(nstream, total, limit);
  88.     }
  89.  
  90.     protected void handle6_DHResp(int seq, ByteBuffer buf) {
  91.         buf.get();
  92.         byte[] v4 = Bytes.decode(buf);
  93.         byte[] v5 = Bytes.decode(buf);
  94.         if(v4 != null && v5 != null) {
  95.             this._dhConn.handle6_DHResp(seq, this._payload00, buf, v4, v5);
  96.             if(this._udpPacket.getSeq() != 0) {
  97.                 this._udpConn.DHConnected();
  98.             }
  99.         }
  100.     }
  101.  
  102.     protected void handle8_Ping(int seq, boolean more, int val, ByteBuffer buf) {
  103.         int v0 = this._udpPacket.getSeq() != 0 ? 1 : 0;
  104.         if(v0 == 0 || (more) || (this._windowPacket.pingTimeout3000())) {
  105.             this._resendPing = true;
  106.             this.initUDPPing();
  107.             this._udpPing.send8PingOnce(this._payload00);
  108.         }
  109.     }
  110.  
  111.     public void SetRate(long min, long max) {
  112.         this._rate = Math.max(0, Math.min(max, this._rate + min));
  113.     }
  114.  
  115.     protected void handle5_DHReq(ByteBuffer buf) {
  116.         buf.get();
  117.         byte[] peerKey = Bytes.decode(buf);
  118.         if(peerKey != null) {
  119.             this._dhConn.handle5_DHReq(this._payload00, buf, peerKey);
  120.         }
  121.     }
  122.  
  123.     protected void handle1_Seq(ByteBuffer buf, byte nstream) {
  124.         Object v0 = this._streamsMap.get(Integer.valueOf(nstream));
  125.         if(v0 != null) {
  126.             ((Stream)v0).handle1_Seq(buf);
  127.         }
  128.     }
  129.  
  130.     protected void handle0_Msg(ByteBuffer buf, byte nstream, int pos, Integer newSeq) {
  131.         Object msg = this._recvMsgMap.get(Integer.valueOf(nstream));
  132.         if(msg == null) {
  133.             ReceiveMsgBase v0_1 = this.GetReceiveMsg(nstream, this._total, 20000000);
  134.             this._recvMsgMap.put(Integer.valueOf(nstream), v0_1);
  135.         }
  136.  
  137.         ((ReceiveMsgBase)msg).decodeRanges(pos, buf);
  138.         if(newSeq != null) {
  139.             ((ReceiveMsgBase)msg).orderPos(newSeq.intValue());
  140.         }
  141.  
  142.         if(((ReceiveMsgBase)msg).isReadDone()) {
  143.             ((ReceiveMsgBase)msg).Receive();
  144.             this._udpConn.handleReceiveMsg(pos, ((ReceiveMsgBase)msg));
  145.         }
  146.     }
  147.  
  148.     protected void handle3_MTUTestACK(ByteBuffer buf, int recvSeq, int recvSize) {
  149.         int v0 = buf.position();
  150.         this._windowPacket.handle3_MTUTestACK(buf, recvSeq, recvSize, this._mtuTest.getMtu());
  151.         buf.position(v0);
  152.         this._mtuTest.decodeSize(buf);
  153.         this._payload00.setMinMtu(this._mtuTest.getMtu());
  154.     }
  155.  
  156.     public boolean sendQueue() {
  157.         int v1_2;
  158.         int v0_2;
  159.         long v2_1;
  160.         long v6;
  161.         Iterator v3;
  162.         long __V2;
  163.         int __V1;
  164.         boolean res;
  165.         try {
  166.             this._payload00.resetError();
  167.             if(this._resendPing) {
  168.                 this.initUDPPing();
  169.                 res = this._udpPing.resend(this._payload00);
  170.             }
  171.             else {
  172.                 goto label_20;
  173.             }
  174.         }
  175.         catch(Throwable v0) {
  176.             goto label_86;
  177.         }
  178.  
  179.         if(this._payload00.getSendError()) {
  180.             this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
  181.             return res;
  182.             try {
  183.             label_20:
  184.                 if(this._udpPacket.getSeq() == 0) {
  185.                     this._dhConn.resendKey(this._payload00);
  186.                 }
  187.  
  188.                 if(this._udpPacket.getSeq() != 0) {
  189.                     goto label_44;
  190.                 }
  191.  
  192.                 this._payload00.sendUnrel();
  193.             }
  194.             catch(Throwable v0) {
  195.                 goto label_86;
  196.             }
  197.  
  198.             res = true;
  199.             if(!this._payload00.getSendError()) {
  200.                 return res;
  201.             }
  202.  
  203.             this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
  204.             return res;
  205.             try {
  206.             label_44:
  207.                 if(!this._payload00.getSendError()) {
  208.                     goto label_60;
  209.                 }
  210.             }
  211.             catch(Throwable v0) {
  212.                 goto label_86;
  213.             }
  214.  
  215.             res = true;
  216.             if(!this._payload00.getSendError()) {
  217.                 return res;
  218.             }
  219.  
  220.             this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
  221.             return res;
  222.             try {
  223.             label_60:
  224.                 __V1 = this._windowPacket.V1();
  225.                 __V2 = this._windowPacket.V2();
  226.                 boolean fixtime = false;
  227.                 if(__V2 > 0) {
  228.                     v3 = this._streams.iterator();
  229.                     do {
  230.                         if(v3.hasNext()) {
  231.                             if(!v3.next().isEmpty()) {
  232.                                 continue;
  233.                             }
  234.  
  235.                             break;
  236.                         }
  237.  
  238.                         goto label_76;
  239.                     }
  240.                     while(true);
  241.  
  242.                     fixtime = true;
  243.                 }
  244.  
  245.             label_76:
  246.                 v3 = this._streams.iterator();
  247.                 while(v3.hasNext()) {
  248.                     v3.next().flush();
  249.                 }
  250.  
  251.                 this._windowPacket.encode(this._payload00, fixtime);
  252.                 this._mtuTest.sendTestMTU(this._payload00);
  253.                 if(!this._payload00.getSendError()) {
  254.                     goto label_118;
  255.                 }
  256.             }
  257.             catch(Throwable v0) {
  258.                 goto label_86;
  259.             }
  260.  
  261.             res = true;
  262.             if(!this._payload00.getSendError()) {
  263.                 return res;
  264.             }
  265.  
  266.             this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
  267.             return res;
  268.             try {
  269.             label_118:
  270.                 Iterator v1_1 = this._recvMsgMap.values().iterator();
  271.                 while(v1_1.hasNext()) {
  272.                     v1_1.next().encodeRangesSend(this._payload00);
  273.                 }
  274.  
  275.                 if(!this._payload00.getSendError()) {
  276.                     goto label_143;
  277.                 }
  278.             }
  279.             catch(Throwable v0) {
  280.                 goto label_86;
  281.             }
  282.  
  283.             res = true;
  284.             if(!this._payload00.getSendError()) {
  285.                 return res;
  286.             }
  287.  
  288.             this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
  289.             return res;
  290.         label_143:
  291.             if(__V2 <= 0) {
  292.                 res = true;
  293.                 if(!this._payload00.getSendError()) {
  294.                     return res;
  295.                 }
  296.  
  297.                 this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
  298.                 return res;
  299.             }
  300.  
  301.             try {
  302.                 v6 = this.send(__V1, __V2, false);
  303.                 v2_1 = __V2 - v6;
  304.                 v0_2 = 0;
  305.                 if(this._rate > 100) {
  306.                     v2_1 = Math.min(v2_1, this._rate);
  307.                     v1_2 = 0;
  308.                     while(true) {
  309.                     label_168:
  310.                         if(v1_2 < 10 && v2_1 > 100) {
  311.                             __V2 = this.send(0, v2_1, true);
  312.                             v0_2 = ((int)((((long)v0_2)) + __V2));
  313.                             if(__V2 >= 100 && !this._payload00.getSendError()) {
  314.                                 goto label_208;
  315.                             }
  316.                         }
  317.  
  318.                         goto label_183;
  319.                     }
  320.                 }
  321.  
  322.                 goto label_187;
  323.             }
  324.             catch(Throwable v0) {
  325.                 goto label_86;
  326.             }
  327.  
  328.         label_208:
  329.             v2_1 -= __V2;
  330.             ++v1_2;
  331.             goto label_168;
  332.             try {
  333.             label_183:
  334.                 this._rate -= ((long)v0_2);
  335.             label_187:
  336.                 long v0_3 = (((long)v0_2)) + v6;
  337.                 if(v0_3 > 0) {
  338.                     this._windowPacket.handle_a(v0_3);
  339.                 }
  340.  
  341.                 this._payload00.sendUnrel();
  342.             }
  343.             catch(Throwable v0) {
  344.                 goto label_86;
  345.             }
  346.  
  347.             res = true;
  348.             if(!this._payload00.getSendError()) {
  349.                 return res;
  350.             }
  351.  
  352.             this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
  353.         }
  354.  
  355.         return res;
  356.     label_86:
  357.         if(this._payload00.getSendError()) {
  358.             this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
  359.         }
  360.  
  361.         throw v0;
  362.     }
  363.  
  364.     void sendDHFailure(byte[] key) {
  365.         Bytes.encode(this._payload00.getBuffer(new attr7DHFailure(0), false, key.length), key);
  366.         this._payload00.encodeAttr();
  367.         this._payload00.sendUnrel();
  368.     }
  369.  
  370.     public long qsize() {
  371.         Iterator it = this._streamsMap.values().iterator();
  372.         long size;
  373.         for(size = 0; it.hasNext(); size = it.next().qsize() + size) {
  374.         }
  375.  
  376.         return size;
  377.     }
  378.  
  379.     public Stream createStream(int stype) {
  380.         int max = 20;
  381.         int nstream = this._nstream.incrementAndGet();
  382.         if(nstream >= max) {
  383.             throw new IllegalStateException("Too many streams in one channel: " + nstream + " max=" + max);
  384.         }
  385.  
  386.         Stream stream = new Stream(nstream, this.___h, 20000000, stype, 1);
  387.         this._streams.add(stream);
  388.         this._streamsMap.put(Integer.valueOf(nstream), stream);
  389.         return stream;
  390.     }
  391.  
  392.     protected void handle2_MTUTest(ByteBuffer buf) {
  393.         int size = buf.remaining();
  394.         if(size <= this._payload00.size() - 20) {
  395.             Copy.safe_copy(buf, this._payload00.getBuffer(new attr3MTUTestACK(((int)this.a), ((int)this.b)), false, size), size);  // _recvSeq, _recvSize
  396.             this._payload00.encodeAttr();
  397.         }
  398.     }
  399.  
  400.     public float getLoss005() {
  401.         return this._windowPacket.getLoss005();
  402.     }
  403.  
  404.     public void dhReset(int ms) {
  405.         this._dhConn.reset(ms);
  406.     }
  407.  
  408.     public float getLoss03() {
  409.         return this._windowPacket.getLoss03();
  410.     }
  411.  
  412.     public int d(int arg2) {
  413.         return this._windowPacket.a(arg2);
  414.     }
  415.  
  416.     public float getRttMS() {
  417.         return this._windowPacket.getRttMS();
  418.     }
  419.  
  420.     public long range() {
  421.         return this._windowPacket.rangeValue();
  422.     }
  423.  
  424.     public void normalRangeState() {
  425.         this._windowPacket.normalRange();
  426.     }
  427.  
  428.     public void decayRangeState() {
  429.         this._windowPacket.decayRange();
  430.     }
  431.  
  432.     public boolean seqUDPPacket() {
  433.         boolean v0 = this._udpPacket.getSeq() != 0 ? true : false;
  434.         return v0;
  435.     }
  436.  
  437.     public long timeout() {
  438.         return this._windowPacket.timeout();
  439.     }
  440.  
  441.     public void resendPing() {
  442.         this._resendPing = true;
  443.     }
  444.  
  445.     public int getMtu() {
  446.         return this._mtuTest.getMtu();
  447.     }
  448.  
  449.     public double getNetOverhead() {
  450.         Iterator it = this._streamsMap.values().iterator();
  451.         long count = 0;
  452.         long size = 0;
  453.         while(it.hasNext()) {
  454.             Object obj = it.next();
  455.             size += ((Stream)obj).size();
  456.             count = ((Stream)obj).count() + count;
  457.         }
  458.  
  459.         return (((double)size)) / (((double)count));
  460.     }
  461.  
  462.     public boolean checkUDPPing() {
  463.         boolean v0 = this._udpPing == null || !this._udpPing.closed() ? false : true;
  464.         return v0;
  465.     }
  466.  
  467.     private void initUDPPing() {
  468.         if(this._udpPing == null) {
  469.             this._udpPing = new UDPPing(3000, 500, this._udpPacket);
  470.         }
  471.     }
  472. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement