Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience method to skip bytes? #46

Open
lukehorvat opened this issue May 2, 2022 · 1 comment · May be fixed by #48
Open

Convenience method to skip bytes? #46

lukehorvat opened this issue May 2, 2022 · 1 comment · May be fixed by #48

Comments

@lukehorvat
Copy link

Hi, I just started using SmartBuffer to wrap client/server packets and so far it's working splendidly. 👌

That said, I just reached a point where I need to skip a few bytes when reading/writing a packet, because these bytes are "reserved" for something that isn't implemented yet. So I did the following to keep my read/write functions as concise as possible:

type PacketData = {
  type: number;
  playerId: number;
  position: number;
  command: string;
  flags: number;
  timestamp: number;
};

function readPacket(packet: SmartBuffer): PacketData {
  return {
    type: packet.readUInt8(),
    playerId: packet.readUInt32LE(),
    position: packet.readUInt16LE(),
    command: packet.readStringNT(),
    flags: (packet.readOffset += 2) && packet.readUInt8(), // skip and read.
    timestamp: packet.readUInt32LE(),
  };
}

function writePacket({ type, playerId, position, command, flags, timestamp }: PacketData): SmartBuffer {
  return new SmartBuffer()
    .writeUInt8(type)
    .writeUInt32LE(playerId)
    .writeUInt16LE(position)
    .writeStringNT(command)
    .writeBuffer(Buffer.alloc(2)) // skip by writing an empty buffer. can't set writeOffset since we don't have a reference to the buffer.
    .writeUInt8(flags)
    .writeUInt32LE(timestamp);
}

As you can see from my efforts to keep things concise, the lines where I need to skip bytes are a little clunky.

I saw in v3 there was a skip method, but it was dropped in v4. What was the reason for dropping it and would you be open to the idea of adding it back in?

Also - just throwing this out there even though I don't think it would be as nice as a skip method - one idea I had as an alternative to skip was to add another param to the read/write methods. It would be a boolean that, if set to true, would make the offset you specified relative to the readOffset/writeOffset (and would also auto-increment them so you can chain further calls). Example:

function writePacket({ type, playerId, position, command, flags, timestamp }: PacketData): SmartBuffer {
  return new SmartBuffer()
    .writeUInt8(type)
    .writeUInt32LE(playerId)
    .writeUInt16LE(position)
    .writeStringNT(command)
    .writeUInt8(flags, 2, true) // skip and write.
    .writeUInt32LE(timestamp);
}

wdyt? Thanks.

@JoshGlazebrook
Copy link
Owner

I think it sounds like a good idea. Are you able to make a PR?

@lukehorvat lukehorvat linked a pull request May 5, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants