Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package one.video.streaming.oktp;
- import java.nio.ByteBuffer;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.PriorityQueue;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.concurrent.atomic.AtomicLong;
- import one.video.streaming.oktp.packets.attr3MTUTestACK;
- import one.video.streaming.oktp.packets.attr7DHFailure;
- import one.video.streaming.util.Copy;
- public class Protocol extends ProtocolBase {
- public interface UDPConnectionBase {
- void DHConnected();
- void handleReceiveMsg(int seq, ReceiveMsgBase msg);
- }
- private final UDPConnectionBase _udpConn;
- private final Payload _payload00;
- private final UDPSendBase _udpSend;
- private final UDPPacket _udpPacket;
- private AtomicInteger _nstream;
- private AtomicLong ___h;
- private AtomicLong _total;
- private PriorityQueue _streams;
- private Map _streamsMap;
- private Map _recvMsgMap;
- private final WindowPacket _windowPacket;
- private MTUTest _mtuTest;
- private final DHConn _dhConn;
- private boolean _resendPing;
- private UDPPing _udpPing;
- private long _rate;
- public Protocol(UDPConnectionBase udpConn, UDPSendBase udpSend, int timeoutInactive, Sig sig) {
- super(sig);
- this._nstream = new AtomicInteger(-1);
- this.___h = new AtomicLong();
- this._total = new AtomicLong();
- this._streams = new PriorityQueue();
- this._streamsMap = new HashMap();
- this._recvMsgMap = new HashMap();
- this._udpConn = udpConn;
- this._udpPacket = new UDPPacket(sig);
- this._payload00 = new Payload(udpSend, this._udpPacket, false, false);
- this._mtuTest = new MTUTest(udpSend, this._udpPacket);
- this._udpSend = udpSend;
- this._windowPacket = new WindowPacket(this._mtuTest.getMtu(), ((long)timeoutInactive));
- this._dhConn = new DHConn(this);
- }
- public void setKey(byte[] key) {
- super.setKey(key);
- this._udpPacket.setKey(key);
- }
- public void setSeq(int seq) {
- this._udpPacket.setSeq(seq);
- }
- private long send(int arg13, long maxsize, boolean more) {
- long newsize;
- Iterator it = this._streams.iterator();
- long size = 0;
- while(true) {
- if(it.hasNext()) {
- newsize = (((long)it.next().send(this._payload00, arg13, maxsize, this._windowPacket.maxTotalSendSeq(), more))) + size;
- if(newsize < maxsize && !this._payload00.getSendError()) {
- size = newsize;
- continue;
- }
- }
- else {
- return size;
- }
- return newsize;
- }
- return size;
- }
- protected ReceiveMsgBase GetReceiveMsg(byte nstream, AtomicLong total, int limit) {
- return new ReceiveMsgBase(nstream, total, limit);
- }
- protected void handle6_DHResp(int seq, ByteBuffer buf) {
- buf.get();
- byte[] v4 = Bytes.decode(buf);
- byte[] v5 = Bytes.decode(buf);
- if(v4 != null && v5 != null) {
- this._dhConn.handle6_DHResp(seq, this._payload00, buf, v4, v5);
- if(this._udpPacket.getSeq() != 0) {
- this._udpConn.DHConnected();
- }
- }
- }
- protected void handle8_Ping(int seq, boolean more, int val, ByteBuffer buf) {
- int v0 = this._udpPacket.getSeq() != 0 ? 1 : 0;
- if(v0 == 0 || (more) || (this._windowPacket.pingTimeout3000())) {
- this._resendPing = true;
- this.initUDPPing();
- this._udpPing.send8PingOnce(this._payload00);
- }
- }
- public void SetRate(long min, long max) {
- this._rate = Math.max(0, Math.min(max, this._rate + min));
- }
- protected void handle5_DHReq(ByteBuffer buf) {
- buf.get();
- byte[] peerKey = Bytes.decode(buf);
- if(peerKey != null) {
- this._dhConn.handle5_DHReq(this._payload00, buf, peerKey);
- }
- }
- protected void handle1_Seq(ByteBuffer buf, byte nstream) {
- Object v0 = this._streamsMap.get(Integer.valueOf(nstream));
- if(v0 != null) {
- ((Stream)v0).handle1_Seq(buf);
- }
- }
- protected void handle0_Msg(ByteBuffer buf, byte nstream, int pos, Integer newSeq) {
- Object msg = this._recvMsgMap.get(Integer.valueOf(nstream));
- if(msg == null) {
- ReceiveMsgBase v0_1 = this.GetReceiveMsg(nstream, this._total, 20000000);
- this._recvMsgMap.put(Integer.valueOf(nstream), v0_1);
- }
- ((ReceiveMsgBase)msg).decodeRanges(pos, buf);
- if(newSeq != null) {
- ((ReceiveMsgBase)msg).orderPos(newSeq.intValue());
- }
- if(((ReceiveMsgBase)msg).isReadDone()) {
- ((ReceiveMsgBase)msg).Receive();
- this._udpConn.handleReceiveMsg(pos, ((ReceiveMsgBase)msg));
- }
- }
- protected void handle3_MTUTestACK(ByteBuffer buf, int recvSeq, int recvSize) {
- int v0 = buf.position();
- this._windowPacket.handle3_MTUTestACK(buf, recvSeq, recvSize, this._mtuTest.getMtu());
- buf.position(v0);
- this._mtuTest.decodeSize(buf);
- this._payload00.setMinMtu(this._mtuTest.getMtu());
- }
- public boolean sendQueue() {
- int v1_2;
- int v0_2;
- long v2_1;
- long v6;
- Iterator v3;
- long __V2;
- int __V1;
- boolean res;
- try {
- this._payload00.resetError();
- if(this._resendPing) {
- this.initUDPPing();
- res = this._udpPing.resend(this._payload00);
- }
- else {
- goto label_20;
- }
- }
- catch(Throwable v0) {
- goto label_86;
- }
- if(this._payload00.getSendError()) {
- this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
- return res;
- try {
- label_20:
- if(this._udpPacket.getSeq() == 0) {
- this._dhConn.resendKey(this._payload00);
- }
- if(this._udpPacket.getSeq() != 0) {
- goto label_44;
- }
- this._payload00.sendUnrel();
- }
- catch(Throwable v0) {
- goto label_86;
- }
- res = true;
- if(!this._payload00.getSendError()) {
- return res;
- }
- this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
- return res;
- try {
- label_44:
- if(!this._payload00.getSendError()) {
- goto label_60;
- }
- }
- catch(Throwable v0) {
- goto label_86;
- }
- res = true;
- if(!this._payload00.getSendError()) {
- return res;
- }
- this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
- return res;
- try {
- label_60:
- __V1 = this._windowPacket.V1();
- __V2 = this._windowPacket.V2();
- boolean fixtime = false;
- if(__V2 > 0) {
- v3 = this._streams.iterator();
- do {
- if(v3.hasNext()) {
- if(!v3.next().isEmpty()) {
- continue;
- }
- break;
- }
- goto label_76;
- }
- while(true);
- fixtime = true;
- }
- label_76:
- v3 = this._streams.iterator();
- while(v3.hasNext()) {
- v3.next().flush();
- }
- this._windowPacket.encode(this._payload00, fixtime);
- this._mtuTest.sendTestMTU(this._payload00);
- if(!this._payload00.getSendError()) {
- goto label_118;
- }
- }
- catch(Throwable v0) {
- goto label_86;
- }
- res = true;
- if(!this._payload00.getSendError()) {
- return res;
- }
- this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
- return res;
- try {
- label_118:
- Iterator v1_1 = this._recvMsgMap.values().iterator();
- while(v1_1.hasNext()) {
- v1_1.next().encodeRangesSend(this._payload00);
- }
- if(!this._payload00.getSendError()) {
- goto label_143;
- }
- }
- catch(Throwable v0) {
- goto label_86;
- }
- res = true;
- if(!this._payload00.getSendError()) {
- return res;
- }
- this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
- return res;
- label_143:
- if(__V2 <= 0) {
- res = true;
- if(!this._payload00.getSendError()) {
- return res;
- }
- this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
- return res;
- }
- try {
- v6 = this.send(__V1, __V2, false);
- v2_1 = __V2 - v6;
- v0_2 = 0;
- if(this._rate > 100) {
- v2_1 = Math.min(v2_1, this._rate);
- v1_2 = 0;
- while(true) {
- label_168:
- if(v1_2 < 10 && v2_1 > 100) {
- __V2 = this.send(0, v2_1, true);
- v0_2 = ((int)((((long)v0_2)) + __V2));
- if(__V2 >= 100 && !this._payload00.getSendError()) {
- goto label_208;
- }
- }
- goto label_183;
- }
- }
- goto label_187;
- }
- catch(Throwable v0) {
- goto label_86;
- }
- label_208:
- v2_1 -= __V2;
- ++v1_2;
- goto label_168;
- try {
- label_183:
- this._rate -= ((long)v0_2);
- label_187:
- long v0_3 = (((long)v0_2)) + v6;
- if(v0_3 > 0) {
- this._windowPacket.handle_a(v0_3);
- }
- this._payload00.sendUnrel();
- }
- catch(Throwable v0) {
- goto label_86;
- }
- res = true;
- if(!this._payload00.getSendError()) {
- return res;
- }
- this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
- }
- return res;
- label_86:
- if(this._payload00.getSendError()) {
- this._mtuTest = new MTUTest(this._udpSend, this._udpPacket, this._mtuTest.getMtu() - 50);
- }
- throw v0;
- }
- void sendDHFailure(byte[] key) {
- Bytes.encode(this._payload00.getBuffer(new attr7DHFailure(0), false, key.length), key);
- this._payload00.encodeAttr();
- this._payload00.sendUnrel();
- }
- public long qsize() {
- Iterator it = this._streamsMap.values().iterator();
- long size;
- for(size = 0; it.hasNext(); size = it.next().qsize() + size) {
- }
- return size;
- }
- public Stream createStream(int stype) {
- int max = 20;
- int nstream = this._nstream.incrementAndGet();
- if(nstream >= max) {
- throw new IllegalStateException("Too many streams in one channel: " + nstream + " max=" + max);
- }
- Stream stream = new Stream(nstream, this.___h, 20000000, stype, 1);
- this._streams.add(stream);
- this._streamsMap.put(Integer.valueOf(nstream), stream);
- return stream;
- }
- protected void handle2_MTUTest(ByteBuffer buf) {
- int size = buf.remaining();
- if(size <= this._payload00.size() - 20) {
- Copy.safe_copy(buf, this._payload00.getBuffer(new attr3MTUTestACK(((int)this.a), ((int)this.b)), false, size), size); // _recvSeq, _recvSize
- this._payload00.encodeAttr();
- }
- }
- public float getLoss005() {
- return this._windowPacket.getLoss005();
- }
- public void dhReset(int ms) {
- this._dhConn.reset(ms);
- }
- public float getLoss03() {
- return this._windowPacket.getLoss03();
- }
- public int d(int arg2) {
- return this._windowPacket.a(arg2);
- }
- public float getRttMS() {
- return this._windowPacket.getRttMS();
- }
- public long range() {
- return this._windowPacket.rangeValue();
- }
- public void normalRangeState() {
- this._windowPacket.normalRange();
- }
- public void decayRangeState() {
- this._windowPacket.decayRange();
- }
- public boolean seqUDPPacket() {
- boolean v0 = this._udpPacket.getSeq() != 0 ? true : false;
- return v0;
- }
- public long timeout() {
- return this._windowPacket.timeout();
- }
- public void resendPing() {
- this._resendPing = true;
- }
- public int getMtu() {
- return this._mtuTest.getMtu();
- }
- public double getNetOverhead() {
- Iterator it = this._streamsMap.values().iterator();
- long count = 0;
- long size = 0;
- while(it.hasNext()) {
- Object obj = it.next();
- size += ((Stream)obj).size();
- count = ((Stream)obj).count() + count;
- }
- return (((double)size)) / (((double)count));
- }
- public boolean checkUDPPing() {
- boolean v0 = this._udpPing == null || !this._udpPing.closed() ? false : true;
- return v0;
- }
- private void initUDPPing() {
- if(this._udpPing == null) {
- this._udpPing = new UDPPing(3000, 500, this._udpPacket);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement