private static object decodeOther(ByteArray input, string type = null, string arrayEleType = null) { int len; uint flag = input.readUnsignedByte(); int i; switch (flag) { case 97: // small int return input.readUnsignedByte(); case 98: // int return input.readInt(); case 99: // string format float byte[] tmp = new byte[31]; input.readBytes(tmp, 0, 31); return (tmp.ToString()); case 115: //string len = (int)input.readUnsignedByte(); ByteArray t = new ByteArray(); t.readBytes(input.Buffer, (uint)input.Postion, (uint)len); return t.ToString(); case 107: //Array len = (int)input.readUnsignedShort(); if (len > 0) { if (type == "String" || arrayEleType == "string") { return input.readUTFBytes((uint)len); } else { uint[] tmpArray4 = new uint[len]; for (i = 0; i < len; i++) { tmpArray4[i] = input.readUnsignedByte(); } return tmpArray4; } } break; case 108: //Array len = (int)input.readUnsignedInt(); if (len > 0) { object[] tmpArray3 = new object[len]; bool isPP = (arrayEleType.Substring(0, 2) == "p_"); for (i = 0; i < len; i++) { if (isPP == false) { tmpArray3[i] = decodeOther(input, type, arrayEleType); } else { input.readUnsignedByte(); input.readUnsignedByte(); input.readUnsignedByte(); string voName = "proto" + "." + input.readUTF(); Type voClass = Type.GetType(voName); Message subVO = voClass.Assembly.CreateInstance(voName) as Message; decodeVO(input, subVO); tmpArray3[i] = subVO; } } input.readByte(); return tmpArray3; } break; case 106: // nil if (type == "String" || type == "string") { return ""; } return new object[1]; case 104: // small tuple input.readUnsignedByte(); len = (int)input.readUnsignedByte(); string voName2 = "proto" + "." + input.readUTF(); Message subVO2 = Type.GetType(voName2).Assembly.CreateInstance(voName2) as Message; decodeVO(input, subVO2); return subVO2; case 105: // large tuple input.readUnsignedByte(); len = (int)input.readUnsignedInt(); string voName3 = "proto" + "." + input.readUTF(); Message subVO3 = Type.GetType(voName3).Assembly.CreateInstance(voName3) as Message; decodeVO(input, subVO3); return subVO3; case 100: // atom 当做字符串对待 if (type == "Boolean" || type == "bool") { return input.readUTF() == "true"; } string s = input.readUTF(); if (s == "undefined") { return null; } return s; case 110: // small bignums len = (int)input.readUnsignedByte(); uint f = input.readUnsignedByte(); // 0 整数 1 负数 double r = 0; uint temp2 = 0; for (i = 0; i < len; i++) { temp2 = input.readUnsignedByte(); r += temp2 * Math.Pow(256, i); } if (f == 1) { r = -r; } return r; case 111: // large bignums len = (int)input.readUnsignedByte(); uint f2 = input.readUnsignedByte(); // 0 整数 1 负数 double r2 = 0; uint temp4 = 0; for (i = 0; i < len; i++) { temp4 = input.readUnsignedByte(); r2 += temp4 * Math.Pow(256, i); } if (f2 == 1) { r2 = -r2; } return r2; case 109: len = input.readInt(); if (type == "String" || type == "string") { return input.readUTFBytes((uint)len); } ByteArray t2 = new ByteArray(); if (len > 0) { input.readBytes(t2.Buffer, 0, (uint)len); } return t2; default: // 其他类型统统不支持!!! //throw new Error("未知类型:", flag); return null; } return null; }
private static void decodeVO(ByteArray input, Message vo) { string[][] arr = vo.getAttributes(); int lenT = arr.Length; uint flag = 0; int len = 0; int i = 0; int j = 0; string type; object voValue; for (j = 0; j < lenT; j++) { string[] arr2 = arr[j]; flag = input.readUnsignedByte(); switch (flag) { case 97: // small int vo.setValue(arr2[0], input.readUnsignedByte()); break; case 98: // int vo.setValue(arr2[0], input.readInt()); break; case 99: // string format float byte[] tmp = new byte[31]; input.readBytes(tmp, 0, 31); vo.setValue(arr2[0], tmp.ToString()); break; case 115: //string len = (int)input.readUnsignedByte(); if (len > 0) { ByteArray t = new ByteArray(); t.readBytes(input.Buffer, (uint)input.Postion, (uint)len); vo.setValue(arr2[0], t.ToString()); } break; case 107: //Array len = (int)input.readUnsignedShort(); if (len > 0) { if (arr2[1] == "String" || arr2[1] == "string") { vo.setValue(arr2[0], input.readUTFBytes((uint)len)); } else { int[] tmpArray4 = new int[len]; for (i = 0; i < len; i++) { tmpArray4[i] = input.readUnsignedByte(); } vo.setValue(arr2[0], tmpArray4); } } break; case 108: //Array len = (int)input.readUnsignedInt(); if (len > 0) { object[] tmpArray3 = new object[len]; Message[] tmpArrPP = new Message[len]; bool isPP = (string)(arr2[2]).Substring(0, 2) == "p_"; for (i = 0; i < len; i++) { if (isPP == false) { tmpArray3[i] = decodeOther(input, arr2[1], arr2[2]); } else { input.readUnsignedByte(); input.readUnsignedByte(); input.readUnsignedByte(); string voName = "proto" + "." + input.readUTF(); Message subVO = Type.GetType(voName).Assembly.CreateInstance(voName) as Message; decodeVO(input, subVO); tmpArrPP[i] = subVO; } } input.readByte(); if (isPP == false) { vo.setValue(arr2[0], tmpArray3); }else{ vo.setValue(arr2[0], tmpArrPP); } } break; case 106: // nil if (arr2[1] == "String" || arr2[1] == "string") { vo.setValue(arr2[0], ""); } else { object[] list = new object[0]; vo.setValue(arr2[0], list); } break; case 104: // small tuple len = (int)input.readUnsignedByte(); //input.readUnsignedByte(); if (len > 0) { input.readUnsignedByte(); string voName2 = "proto" + "." + input.readUTF(); Message subVO2 = Type.GetType(voName2).Assembly.CreateInstance(voName2) as Message; decodeVO(input, subVO2); vo.setValue(arr2[0], subVO2); } break; case 105: // large tuple input.readUnsignedByte(); len = (int)input.readUnsignedInt(); if (len > 0) { string voName3 = "proto" + "." + input.readUTF(); Type voClass3 = Type.GetType(voName3); Message subVO3 = voClass3.Assembly.CreateInstance(voName3) as Message; decodeVO(input, subVO3); vo.setValue(arr2[0], subVO3); } break; case 100: // atom 当做字符串对待 string s = input.readUTF(); if (arr2[1] == "Boolean" || arr2[1] == "bool") { vo.setValue(arr2[0], s == "true"); } else { if (s == "undefined") { vo.setValue(arr2[0], null); } else { vo.setValue(arr2[0], s); } } break; case 110: // small bignums len = (int)input.readUnsignedByte(); uint f = input.readUnsignedByte(); // 0 整数 1 负数 double r = 0; uint temp2 = 0; for (i = 0; i < len; i++) { temp2 = input.readUnsignedByte(); r += temp2 * Math.Pow(256, i); } if (f == 1) { r = -r; } vo.setValue(arr2[0], r); break; case 111: // large bignums len = (int)input.readUnsignedByte(); uint f2 = input.readUnsignedByte(); // 0 整数 1 负数 double r2 = 0; uint temp4 = 0; for (i = 0; i < len; i++) { temp4 = input.readUnsignedByte(); r2 += temp4 * Math.Pow(256, i); } if (f2 == 1) { r2 = -r2; } vo.setValue(arr2[0], r2); break; case 109: len = input.readInt(); if (arr2[1] == "String" || arr2[1] == "string") { vo.setValue(arr2[0], input.readUTFBytes((uint)len)); } else { byte[] t2 = new byte[len]; if (len > 0) { input.readBytes(t2, 0, (uint)len); } vo.setValue(arr2[0], t2); } break; default: // 其他类型统统不支持!!! //throw new Error("未知类型:", flag); break; } } }
protected object decodeErlangTerm(ByteArray input) { uint flag = input.readUnsignedByte(); int i; int len; switch (flag) { case 97: // small int return input.readUnsignedByte(); case 98: // int return input.readInt(); case 99: // string format float byte[] tmp = new byte[31]; input.readBytes(tmp, 0, 31); //tmp.readBytes(input, input.position, 31); // 暂时直接用字符串展示 return input.ToString(); case 115: //string len = (int)input.readUnsignedByte(); ByteArray t = new ByteArray(); t.readBytes(input.Buffer, (uint)input.Postion, (uint)len); return t.ToString(); case 107: //Array len = (int)input.readUnsignedShort(); uint[] tmpArray4 = new uint[len]; for (i = 0; i < len; i++) { tmpArray4[i] = input.readUnsignedByte(); } return tmpArray4; case 108: //Array len = (int)input.readUnsignedInt(); object[] tmpArray3 = new object[len]; for (i = 0; i < len; i++) { tmpArray3[i] = decodeErlangTerm(input); } input.readByte(); return tmpArray3; case 106: // nil return new object[0]; case 104: // small tuple len = (int)input.readUnsignedByte(); object[] tmpArray = new object[len]; for (i = 0; i < len; i++) { tmpArray[i] = decodeErlangTerm(input); } return tmpArray; case 105: // large tuple len = (int)input.readUnsignedInt(); object[] tmpArray2 = new object[len]; for (i = 0; i < len; i++) { tmpArray2[i] = decodeErlangTerm(input); } return tmpArray2; case 100: // atom 当做字符串对待 return input.readUTF(); case 110: // small bignums len = (int)input.readUnsignedByte(); uint f = input.readUnsignedByte(); // 0 整数 1 负数 double r = 0; uint temp2 = 0; for (i = 0; i < len; i++) { temp2 = input.readUnsignedByte(); r += temp2 * Math.Pow(256, i); } if (f == 1) { r = -r; } return r; case 111: // large bignums len = (int)input.readUnsignedByte(); uint f2 = input.readUnsignedByte(); // 0 整数 1 负数 double r2 = 0; uint temp4 = 0; for (i = 0; i < len; i++) { temp4 = input.readUnsignedByte(); r2 += temp4 * Math.Pow(256, i); } if (f2 == 1) { r2 = -r2; } return r2; case 109: len = input.readInt(); byte[] t2 = new byte[len]; if (len > 0) { input.readBytes(t2, 0, (uint)len); } return t2; default: // 其他类型统统不支持!!! //throw new Error("未知类型:", flag); return null; } }