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 {
* 返回值可用于业务逻辑处理。
*
* */
static deserialize(array: ArrayBuffer): YgoProPacket {
static deserialize(array: ArrayBuffer): YgoProPacket[] {
try {
if (array.byteLength < PACKET_MIN_LEN) {
throw new Error(
......@@ -51,13 +51,29 @@ export class YgoProPacket {
console.error(e);
}
const dataView = new DataView(array);
// 由于srvpro实现问题,目前可能出现粘包的情况,因此这里做下解包
const packets = [];
const packetLen = dataView.getInt16(0, littleEndian);
const proto = dataView.getInt8(2);
const exData = array.slice(3, packetLen + 2);
let offset = 0;
while (true) {
const buffer = array.slice(offset);
return new YgoProPacket(packetLen, proto, new Uint8Array(exData));
if (buffer.byteLength < PACKET_MIN_LEN) {
// 解包结束
break;
}
const dataView = new DataView(buffer);
const packetLen = dataView.getInt16(0, littleEndian);
const proto = dataView.getInt8(2);
const exData = buffer.slice(3, packetLen + 2);
packets.push(new YgoProPacket(packetLen, proto, new Uint8Array(exData)));
offset += packetLen + 2;
}
return packets;
}
}
......
......@@ -38,87 +38,92 @@ export default async function handleSocketMessage(e: MessageEvent) {
}
async function _handle(e: MessageEvent) {
const packet = YgoProPacket.deserialize(e.data);
const pb = adaptStoc(packet);
const packets = YgoProPacket.deserialize(e.data);
switch (pb.msg) {
case "stoc_join_game": {
handleJoinGame(pb);
break;
}
case "stoc_chat": {
handleChat(pb);
break;
}
case "stoc_hs_player_change": {
handleHsPlayerChange(pb);
break;
}
case "stoc_hs_watch_change": {
handleHsWatchChange(pb);
break;
}
case "stoc_hs_player_enter": {
handleHsPlayerEnter(pb);
break;
}
case "stoc_type_change": {
handleTypeChange(pb);
break;
}
case "stoc_select_hand": {
handleSelectHand(pb);
break;
}
case "stoc_hand_result": {
handleHandResult(pb);
break;
}
case "stoc_select_tp": {
handleSelectTp(pb);
break;
}
case "stoc_deck_count": {
handleDeckCount(pb);
break;
}
case "stoc_duel_start": {
handleDuelStart(pb);
break;
}
case "stoc_duel_end": {
handleDuelEnd(pb);
break;
}
case "stoc_game_msg": {
if (!replayStore.isReplay) {
// 如果不是回放模式,则记录回放数据
replayStore.record(packet);
}
await handleGameMsg(pb);
await Promise.all(
packets.map(async (packet) => {
const pb = adaptStoc(packet);
switch (pb.msg) {
case "stoc_join_game": {
handleJoinGame(pb);
break;
}
case "stoc_chat": {
handleChat(pb);
break;
}
case "stoc_hs_player_change": {
handleHsPlayerChange(pb);
break;
}
case "stoc_hs_watch_change": {
handleHsWatchChange(pb);
break;
}
case "stoc_hs_player_enter": {
handleHsPlayerEnter(pb);
break;
}
case "stoc_type_change": {
handleTypeChange(pb);
break;
}
case "stoc_select_hand": {
handleSelectHand(pb);
break;
}
case "stoc_hand_result": {
handleHandResult(pb);
break;
}
case "stoc_select_tp": {
handleSelectTp(pb);
break;
}
case "stoc_deck_count": {
handleDeckCount(pb);
break;
}
case "stoc_duel_start": {
handleDuelStart(pb);
break;
}
case "stoc_duel_end": {
handleDuelEnd(pb);
break;
}
case "stoc_game_msg": {
if (!replayStore.isReplay) {
// 如果不是回放模式,则记录回放数据
replayStore.record(packet);
}
await handleGameMsg(pb);
break;
}
case "stoc_time_limit": {
handleTimeLimit(pb.stoc_time_limit);
break;
}
case "stoc_error_msg": {
await handleErrorMsg(pb.stoc_error_msg);
break;
}
case "stoc_change_side": {
handleChangeSide(pb.stoc_change_side);
break;
}
case "stoc_waiting_side": {
handleWaitingSide(pb.stoc_waiting_side);
break;
}
default: {
console.log(packet);
break;
}
case "stoc_time_limit": {
handleTimeLimit(pb.stoc_time_limit);
break;
}
case "stoc_error_msg": {
await handleErrorMsg(pb.stoc_error_msg);
break;
}
case "stoc_change_side": {
handleChangeSide(pb.stoc_change_side);
break;
}
case "stoc_waiting_side": {
handleWaitingSide(pb.stoc_waiting_side);
break;
}
default: {
console.log(packet);
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