From 28d65644fefcc0f82865764ce93ec7edb46e6599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9C=B0=E5=B9=B3=E7=BA=BF?= <22250530@qq.com> Date: Tue, 15 Sep 2015 20:42:36 +0800 Subject: [PATCH] --- .../redkale/net/icep/BindingIcepServlet.java | 6 ++- .../redkale/net/icep/IcepPrepareServlet.java | 5 +- .../wentch/redkale/net/icep/IcepRequest.java | 12 ++--- .../wentch/redkale/net/icep/IcepServlet.java | 2 + .../redkale/net/icep/stun/StunHeader.java | 48 +++++++------------ 5 files changed, 31 insertions(+), 42 deletions(-) diff --git a/src/com/wentch/redkale/net/icep/BindingIcepServlet.java b/src/com/wentch/redkale/net/icep/BindingIcepServlet.java index fa588b1ef..58504d43c 100644 --- a/src/com/wentch/redkale/net/icep/BindingIcepServlet.java +++ b/src/com/wentch/redkale/net/icep/BindingIcepServlet.java @@ -23,7 +23,6 @@ public class BindingIcepServlet extends IcepServlet { StunPacket packet = request.getStunPacket(); ByteBuffer buffer = response.getContext().pollBuffer(); packet.addAttribute(new XorMappedAddressAttribute(request.getRemoteAddress())); - packet.addAttribute(new MappedAddressAttribute(request.getRemoteAddress())); packet.getHeader().setRequestid((StunHeader.TYPE_SUCCESS | StunHeader.ACTION_BINDING)); packet.encode(buffer); buffer.flip(); @@ -31,4 +30,9 @@ public class BindingIcepServlet extends IcepServlet { response.finish(buffer); } + @Override + public short getRequestid() { + return 0x0001; + } + } diff --git a/src/com/wentch/redkale/net/icep/IcepPrepareServlet.java b/src/com/wentch/redkale/net/icep/IcepPrepareServlet.java index e87bc58f4..e0bc2eca4 100644 --- a/src/com/wentch/redkale/net/icep/IcepPrepareServlet.java +++ b/src/com/wentch/redkale/net/icep/IcepPrepareServlet.java @@ -16,10 +16,11 @@ import java.util.*; */ public class IcepPrepareServlet extends PrepareServlet { - private final HashMap servletmaps = new HashMap<>(); + private final HashMap servletmaps = new HashMap<>(); public IcepPrepareServlet() { - this.servletmaps.put(0x0001, new BindingIcepServlet()); + BindingIcepServlet servlet = new BindingIcepServlet(); + this.servletmaps.put(servlet.getRequestid(), new BindingIcepServlet()); } @Override diff --git a/src/com/wentch/redkale/net/icep/IcepRequest.java b/src/com/wentch/redkale/net/icep/IcepRequest.java index aab8ecf04..6026a7682 100644 --- a/src/com/wentch/redkale/net/icep/IcepRequest.java +++ b/src/com/wentch/redkale/net/icep/IcepRequest.java @@ -17,7 +17,7 @@ import java.nio.*; */ public class IcepRequest extends Request { - private int requestid; + private short requestid; private StunPacket stunPacket; @@ -31,12 +31,10 @@ public class IcepRequest extends Request { Utility.println(Utility.now() + "-------" + getRemoteAddress() + " ", buffer); if (buffer.remaining() < 20) return -1; this.requestid = buffer.getShort(); - short typeid = (short) ((this.requestid >> 8) & 0xffff); - short actionid = (short) (this.requestid & 0xffff); - int bodysize = buffer.getShort() & 0xffff; - byte[] bytes = new byte[16]; + char bodysize = buffer.getChar(); + byte[] bytes = new byte[16]; buffer.get(bytes); - StunHeader header = new StunHeader(typeid, actionid, bytes); + StunHeader header = new StunHeader(this.requestid, bytes); this.stunPacket = new StunPacket(header); return 0; } @@ -59,7 +57,7 @@ public class IcepRequest extends Request { } - public int getRequestid() { + public short getRequestid() { return requestid; } diff --git a/src/com/wentch/redkale/net/icep/IcepServlet.java b/src/com/wentch/redkale/net/icep/IcepServlet.java index 657184320..5b16f3279 100644 --- a/src/com/wentch/redkale/net/icep/IcepServlet.java +++ b/src/com/wentch/redkale/net/icep/IcepServlet.java @@ -16,6 +16,8 @@ public abstract class IcepServlet implements Servlet AnyValue conf; //当前HttpServlet的配置 + public abstract short getRequestid(); + @Override public final boolean equals(Object obj) { return obj != null && obj.getClass() == this.getClass(); diff --git a/src/com/wentch/redkale/net/icep/stun/StunHeader.java b/src/com/wentch/redkale/net/icep/stun/StunHeader.java index 8bafdeb26..e6ab7daaa 100644 --- a/src/com/wentch/redkale/net/icep/stun/StunHeader.java +++ b/src/com/wentch/redkale/net/icep/stun/StunHeader.java @@ -47,11 +47,9 @@ public class StunHeader implements IcepCoder { public static final short ACTION_CHANNELBIND = 0x0009; //----------------------------------------------------------- - private short typeid; //无符号 2bytes + private short requestid; //无符号 2bytes 首位2bits必定是00, 所以该值不会为负数 - private short actionid; //无符号 2bytes - - private int bodysize; //无符号 2bytes + private char bodysize; //无符号 2bytes private byte[] transactionid; //RFC5389 =MAGIC_COOKIE + byte[12] = byte[16]; @@ -67,18 +65,15 @@ public class StunHeader implements IcepCoder { return transactions; } - public StunHeader(short typeid, short actionid, byte[] transactionid0) { - this.typeid = typeid; - this.actionid = actionid; + public StunHeader(short requestid, byte[] transactionid0) { + this.requestid = requestid; this.transactionid = transactionid0 == null ? generateTransactionid() : transactionid0; } @Override public StunHeader decode(final ByteBuffer buffer) { - short requestid = buffer.getShort(); - this.typeid = (short) (requestid << 2); - this.actionid = (short) (requestid & 0xff); - this.bodysize = buffer.getShort() & 0xffff; + this.requestid = buffer.getShort(); + this.bodysize = buffer.getChar(); this.transactionid = new byte[16]; buffer.get(transactionid); return this; @@ -86,17 +81,15 @@ public class StunHeader implements IcepCoder { @Override public ByteBuffer encode(final ByteBuffer buffer) { - buffer.put((byte) this.typeid); - buffer.put((byte) this.actionid); - buffer.putShort((short) this.bodysize); //bodysize + buffer.putShort(this.requestid); + buffer.putChar(this.bodysize); buffer.put(transactionid); return buffer; } @Override public String toString() { - return this.getClass().getSimpleName() + "[typeid = " + format(typeid) + ", actionid = " + format(actionid) - + ", bodysize = " + (int) (bodysize) + ", transactionid = " + Utility.binToHexString(transactionid) + "]"; + return this.getClass().getSimpleName() + "[requestid = " + format(requestid) + ", bodysize = " + (int) (bodysize) + ", transactionid = " + Utility.binToHexString(transactionid) + "]"; } private static String format(short value) { @@ -108,28 +101,19 @@ public class StunHeader implements IcepCoder { } public void setRequestid(int requestid) { - this.typeid = (short) (requestid >> 8); - this.actionid = (short) (requestid & 0xff); + this.requestid = (short) requestid; + } + + public void setRequestid(short requestid) { + this.requestid = requestid; } public void setBodysize(char bodysize) { this.bodysize = bodysize; } - public void setTypeid(short typeid) { - this.typeid = typeid; - } - - public void setActionid(short actionid) { - this.actionid = actionid; - } - - public short getTypeid() { - return typeid; - } - - public short getActionid() { - return actionid; + public void setBodysize(int bodysize) { + this.bodysize = (char) bodysize; } public int getBodysize() {