TIL

Tower Defense Online Project 트러블 슈팅 - 1

추운날_너를_기다리며 2024. 11. 12. 09:56

이상한 페이로드 출력

 

 

id를 꺼낸 경우

C2SRegisterRequest { id: '\n\x04asdf\x12\x04asdf\x1A\x04asdf' }

 

제대로 파싱을 하지 못하는 에러가 발생함.

 

test code

console.log("패킷 타입 : "+packetType)
        console.log("페이로드 길이 : "+payloadLength)
        switch (packetType) {
          case PACKET_TYPE.REGISTER_REQUEST: {
            const protoMessages = getProtoMessages();
            const Register = protoMessages.common.C2SRegisterRequest;
            const registerMessage = Register.decode(payloadData);
            const id = registerMessage.id;
            const password = registerMessage.password;
            const email = registerMessage.email;

            console.log(registerMessage)

            const p = { id:"asdf", password:"asdf", email:"asdf"}
            const message = Register.create(p);
            const Packet = Register.encode(message).finish();
            console.log(Packet)
            console.log(payloadData)
            console.log(Register.decode(Packet))
            // const user = getUserBySocket(socket);
            // if (!user) {
            //   console.error("PING Error : 유저를 찾을 수 없습니다.");
            // }
            // user.handlePong(pingMessage);
            break;
          }
          case 0: {
            const payload = packetParser(payloadData);
            const handler = getHandlerById(packetType);
            
            handler({ socket, payload });
          }
        }

 

테스트 결과

패킷 타입 : 1
페이로드 길이 : 20
C2SRegisterRequest { id: '\n\x04asdf\x12\x04asdf\x1A\x04asdf' }
<Buffer 0a 04 61 73 64 66 12 04 61 73 64 66 1a 04 61 73 64 66>
<Buffer 0a 12 0a 04 61 73 64 66 12 04 61 73 64 66 1a 04 61 73 64 66>
C2SRegisterRequest { id: 'asdf', password: 'asdf', email: 'asdf' }

파싱한 payloadData 가 2byte 더 들고 옴을 확인함.

\n 가 페이로드에 섞여서 들어오고 있다. 헤더를 파싱할 때 잘못하지 않았는지 확인해보자.

 

1      // 패킷 타입
5      // 버전 길이
1.0.0  // 버전 (길이 5 확인)
1      // 시퀀스 
16     // 헤더 길이 2+1+5+4+4 = 16 확인
<Buffer >
패킷 타입 : 1
페이로드 길이 : 20
C2SRegisterRequest { id: 'asdf', password: 'asdf', email: 'asdf' }
<Buffer 0a 04 61 73 64 66 12 04 61 73 64 66 1a 04 61 73 64 66>
<Buffer 0a 12 0a 04 61 73 64 66 12 04 61 73 64 66 1a 04 61 73 64 66>
C2SRegisterRequest { id: 'asdf', password: 'asdf', email: 'asdf' }

헤더를 파싱하는데 문제가 없다고 판단된다.

클라이언트가 \\n 을 하나 더 붙여서 보내고 있다고 생각된다.

 

const registerMessage = Register.decode(payloadData.subarray(2));

앞에 2바이트를 잘라내 해결했다.

 

하지만 ping, packet message만 읽히고 나머지가 안 읽힌다.

{
  common: {
    Packet: Type {
      options: undefined,
      parsedOptions: null,
      name: 'Packet',
      parent: [Namespace],
      resolved: false,
      comment: null,
      filename: 'C:/Users/dksw/Desktop/01.Projects/04. 스파르타 부트캠프/towerDefenseOnline/src/protobuf/request/common.proto',        
      nested: undefined,
      _nestedArray: [],
      fields: [Object],
      oneofs: undefined,
      extensions: undefined,
      reserved: undefined,
      group: undefined,
      _fieldsById: null,
      _fieldsArray: null,
      _oneofsArray: null,
      _ctor: null
    },
    Ping: Type {
      options: undefined,
      parsedOptions: null,
      name: 'Ping',
      parent: [Namespace],
      resolved: false,
      comment: null,
      filename: 'C:/Users/dksw/Desktop/01.Projects/04. 스파르타 부트캠프/towerDefenseOnline/src/protobuf/request/common.proto',        
      nested: undefined,
      _nestedArray: [],
      fields: [Object],
      oneofs: undefined,
      extensions: undefined,
      reserved: undefined,
      group: undefined,
      _fieldsById: null,
      _fieldsArray: null,
      _oneofsArray: null,
      _ctor: null
    }
  }
}

 

common.proto

message S2CLoginResponse {
  bool success = 1;
  string message = 2;
  string token = 3;
  GlobalFailCode failCode = 4;
}

enum GlobalFailCode {
  NONE = 0;
  UNKNOWN_ERROR = 1;
  INVALID_REQUEST = 2;
  AUTHENTICATION_FAILED = 3;
}

message C2SLoginRequest {
  string id = 1;
  string password = 2;
}

 

중간에 enum이 껴있으면 loadProto.js가 proto를 전부 읽어오지 못한다.

 

enum을 제일 밑으로 내렸을 때

enum을 제외하고 전부 읽을 수 있는 모습

{
  common: {
    Packet: Type {
      options: undefined,
      parsedOptions: null,
      name: 'Packet',
      parent: [Namespace],
      resolved: false,
      comment: null,
      filename: 'C:/Users/dksw/Desktop/01.Projects/04. 스파르타 부트캠프/towerDefenseOnline/src/protobuf/request/common.proto',        
      nested: undefined,
      _nestedArray: [],
      fields: [Object],
      oneofs: undefined,
      extensions: undefined,
      reserved: undefined,
      group: undefined,
      _fieldsById: null,
      _fieldsArray: null,
      _oneofsArray: null,
      _ctor: null
    },
    Ping: Type {
      options: undefined,
      parsedOptions: null,
      name: 'Ping',
      parent: [Namespace],
      resolved: false,
      comment: null,
      filename: 'C:/Users/dksw/Desktop/01.Projects/04. 스파르타 부트캠프/towerDefenseOnline/src/protobuf/request/common.proto',        
      nested: undefined,
      _nestedArray: [],
      fields: [Object],
      oneofs: undefined,
      extensions: undefined,
      reserved: undefined,
      group: undefined,
      _fieldsById: null,
      _fieldsArray: null,
      _oneofsArray: null,
      _ctor: null
    },
    C2SRegisterRequest: Type {
      options: undefined,
      parsedOptions: null,
      name: 'C2SRegisterRequest',
      parent: [Namespace],
      resolved: false,
      comment: null,
      filename: 'C:/Users/dksw/Desktop/01.Projects/04. 스파르타 부트캠프/towerDefenseOnline/src/protobuf/request/common.proto',        
      nested: undefined,
      _nestedArray: [],
      fields: [Object],
      oneofs: undefined,
      extensions: undefined,
      reserved: undefined,
      group: undefined,
      _fieldsById: null,
      _fieldsArray: null,
      _oneofsArray: null,
      _ctor: null
    },
    S2CRegisterResponse: Type {
      options: undefined,
      parsedOptions: null,
      name: 'S2CRegisterResponse',
      parent: [Namespace],
      resolved: false,
      comment: null,
      filename: 'C:/Users/dksw/Desktop/01.Projects/04. 스파르타 부트캠프/towerDefenseOnline/src/protobuf/request/common.proto',        
      nested: undefined,
      _nestedArray: [],
      fields: [Object],
      oneofs: undefined,
      extensions: undefined,
      reserved: undefined,
      group: undefined,
      _fieldsById: null,
      _fieldsArray: null,
      _oneofsArray: null,
      _ctor: null
    },
    C2SLoginRequest: Type {
      options: undefined,
      parsedOptions: null,
      name: 'C2SLoginRequest',
      parent: [Namespace],
      resolved: false,
      comment: null,
      filename: 'C:/Users/dksw/Desktop/01.Projects/04. 스파르타 부트캠프/towerDefenseOnline/src/protobuf/request/common.proto',        
      nested: undefined,
      _nestedArray: [],
      fields: [Object],
      oneofs: undefined,
      extensions: undefined,
      reserved: undefined,
      group: undefined,
      _fieldsById: null,
      _fieldsArray: null,
      _oneofsArray: null,
      _ctor: null
    },
    S2CLoginResponse: Type {
      options: undefined,
      parsedOptions: null,
      name: 'S2CLoginResponse',
      parent: [Namespace],
      resolved: false,
      comment: null,
      filename: 'C:/Users/dksw/Desktop/01.Projects/04. 스파르타 부트캠프/towerDefenseOnline/src/protobuf/request/common.proto',        
      nested: undefined,
      _nestedArray: [],
      fields: [Object],
      oneofs: undefined,
      extensions: undefined,
      reserved: undefined,
      group: undefined,
      _fieldsById: null,
      _fieldsArray: null,
      _oneofsArray: null,
      _ctor: null
    }
  }
}