Commit 94b7862d authored by Chunchi Che's avatar Chunchi Che

add unpack logic

parent b45308de
Pipeline #23660 passed with stages
in 15 minutes and 21 seconds
...@@ -40,7 +40,7 @@ export class YgoProPacket { ...@@ -40,7 +40,7 @@ export class YgoProPacket {
* 返回值可用于业务逻辑处理。 * 返回值可用于业务逻辑处理。
* *
* */ * */
static deserialize(array: ArrayBuffer): YgoProPacket { static deserialize(array: ArrayBuffer): YgoProPacket[] {
try { try {
if (array.byteLength < PACKET_MIN_LEN) { if (array.byteLength < PACKET_MIN_LEN) {
throw new Error( throw new Error(
...@@ -51,13 +51,29 @@ export class YgoProPacket { ...@@ -51,13 +51,29 @@ export class YgoProPacket {
console.error(e); console.error(e);
} }
const dataView = new DataView(array); // 由于srvpro实现问题,目前可能出现粘包的情况,因此这里做下解包
const packets = [];
let offset = 0;
while (true) {
const buffer = array.slice(offset);
if (buffer.byteLength < PACKET_MIN_LEN) {
// 解包结束
break;
}
const dataView = new DataView(buffer);
const packetLen = dataView.getInt16(0, littleEndian); const packetLen = dataView.getInt16(0, littleEndian);
const proto = dataView.getInt8(2); const proto = dataView.getInt8(2);
const exData = array.slice(3, packetLen + 2); const exData = buffer.slice(3, packetLen + 2);
packets.push(new YgoProPacket(packetLen, proto, new Uint8Array(exData)));
offset += packetLen + 2;
}
return new YgoProPacket(packetLen, proto, new Uint8Array(exData)); return packets;
} }
} }
......
...@@ -38,7 +38,10 @@ export default async function handleSocketMessage(e: MessageEvent) { ...@@ -38,7 +38,10 @@ export default async function handleSocketMessage(e: MessageEvent) {
} }
async function _handle(e: MessageEvent) { async function _handle(e: MessageEvent) {
const packet = YgoProPacket.deserialize(e.data); const packets = YgoProPacket.deserialize(e.data);
await Promise.all(
packets.map(async (packet) => {
const pb = adaptStoc(packet); const pb = adaptStoc(packet);
switch (pb.msg) { switch (pb.msg) {
...@@ -121,4 +124,6 @@ async function _handle(e: MessageEvent) { ...@@ -121,4 +124,6 @@ async function _handle(e: MessageEvent) {
break; break;
} }
} }
}),
);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment