From f33217daddf8581d6a903bcdf9ebe9158d1b0fcf Mon Sep 17 00:00:00 2001 From: dotboy Date: Tue, 1 Jul 2014 11:44:56 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=BF=BD=E7=95=A5swp=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 677190f..f65cb8b 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ bin/ .project .class .jar +*.swp From 9fb70d9925a80189b2faeea4a0e93fec482aa3a5 Mon Sep 17 00:00:00 2001 From: dotboy Date: Tue, 1 Jul 2014 11:49:54 +0800 Subject: [PATCH 2/3] fix bug when server-side send message with protobuf using varint front-end can not decode to right value --- pom.xml | 4 ++-- .../java/com/zvidia/pomelo/protobuf/Codec.java | 18 ++++++++++++++++-- .../com/zvidia/pomelo/protobuf/Decoder.java | 14 +++++++++----- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 22b91b7..db9dc62 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.zvidia pomelo - 1.0-SNAPSHOT + 1.0-websocket junit @@ -55,4 +55,4 @@ - \ No newline at end of file + diff --git a/src/main/java/com/zvidia/pomelo/protobuf/Codec.java b/src/main/java/com/zvidia/pomelo/protobuf/Codec.java index ff072a9..fa7e4cf 100644 --- a/src/main/java/com/zvidia/pomelo/protobuf/Codec.java +++ b/src/main/java/com/zvidia/pomelo/protobuf/Codec.java @@ -16,7 +16,18 @@ public static byte[] encodeUInt32(int num) { } public static int decodeUInt32(byte[] bytes) { - return ByteUtils.bytesToInt(bytes); + int result = 0; + for(int i=0; i 0) + { + return result; + } + } + + return result; } public static byte[] encodeSInt32(int num) { @@ -24,7 +35,10 @@ public static byte[] encodeSInt32(int num) { } public static int decodeSInt32(byte[] bytes) { - return ByteUtils.bytesToInt(bytes); + int uResult = Codec.decodeUInt32(bytes); + int flag = ((uResult % 2) == 1 ) ? -1 : 1; + + return ((uResult % 2 + uResult)/2) * flag; } public static byte[] encodeUInt64(long num) { diff --git a/src/main/java/com/zvidia/pomelo/protobuf/Decoder.java b/src/main/java/com/zvidia/pomelo/protobuf/Decoder.java index d732328..d195f58 100644 --- a/src/main/java/com/zvidia/pomelo/protobuf/Decoder.java +++ b/src/main/java/com/zvidia/pomelo/protobuf/Decoder.java @@ -124,7 +124,7 @@ private Object decodeProp(String type, JSONObject proto) throws JSONException { } case _int32: case _sInt32: { - return getByte() & 0xff; + return Codec.decodeSInt32(getBytes(false));// & 0xff; } case _float: { float aFloat = buffer.getFloat(offset); @@ -184,12 +184,16 @@ private byte getByte() { } private byte[] getBytes(boolean flag) { - ByteBuffer buf = ByteBuffer.allocate(4); + ByteBuffer buf = ByteBuffer.allocate(5); int pos = offset; flag = flag || false; - int b = buffer.getInt(pos); - buf.putInt(b); - pos += 4; + byte b; + do{ + b = buffer.get(); + buf.put(b); + pos += 1; + }while(b<0); + if (!flag) { offset = pos; buffer.position(pos); From 5f06db3a4e8b37d957abefd4152cc5cc329dd4b4 Mon Sep 17 00:00:00 2001 From: dotboy Date: Tue, 1 Jul 2014 13:19:43 +0800 Subject: [PATCH 3/3] bug fix ByteBuffer getDouble with index will not change the innser state so i remove the index --- src/main/java/com/zvidia/pomelo/protobuf/Decoder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/zvidia/pomelo/protobuf/Decoder.java b/src/main/java/com/zvidia/pomelo/protobuf/Decoder.java index d195f58..8e408a3 100644 --- a/src/main/java/com/zvidia/pomelo/protobuf/Decoder.java +++ b/src/main/java/com/zvidia/pomelo/protobuf/Decoder.java @@ -127,12 +127,12 @@ private Object decodeProp(String type, JSONObject proto) throws JSONException { return Codec.decodeSInt32(getBytes(false));// & 0xff; } case _float: { - float aFloat = buffer.getFloat(offset); + float aFloat = buffer.getFloat(); offset += 4; return aFloat; } case _double: { - double aDouble = buffer.getDouble(offset); + double aDouble = buffer.getDouble(); offset += 8; return aDouble; }