This repository has been archived by the owner on Jun 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move packet decoding and encoding to their own files.
- Loading branch information
Showing
4 changed files
with
202 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const varint = require( 'varint' ); | ||
|
||
const State = require( '../state.js' ); | ||
const Packet = require( './packet.js' ); | ||
|
||
class Handshake extends Packet { | ||
constructor( version, address, port, nextState ) { | ||
super( State.HANDSHAKING, 0 ); | ||
|
||
this.version = version; | ||
this.address = address; | ||
this.port = port; | ||
|
||
this.nextState = nextState; | ||
} | ||
|
||
encode() { | ||
let portBuffer = Buffer.alloc( 2 ); | ||
portBuffer.writeUint16BE( this.port, 0 ); | ||
|
||
return this.createPacket( Buffer.concat( [ | ||
Buffer.from( varint.encode( this.version ) ), | ||
Buffer.from( varint.encode( this.address.length ) ), | ||
Buffer.from( this.address, 'utf8' ), | ||
portBuffer, | ||
Buffer.from( varint.encode( this.nextState ) ) | ||
] ) ); | ||
} | ||
|
||
static decode( buffer ) { | ||
let offset = 0; | ||
|
||
let version = varint.decode( buffer, offset ); | ||
offset += varint.decode.bytes; | ||
|
||
let addressLength = varint.decode( buffer, offset ); | ||
offset += varint.decode.bytes; | ||
|
||
let address = buffer.toString( 'utf8', offset, offset + addressLength ); | ||
offset += addressLength; | ||
|
||
let port = buffer.readUInt16BE( offset ); | ||
offset += 2; | ||
|
||
let nextState = varint.decode( buffer, offset ); | ||
|
||
return new Handshake( version, address, port, nextState ); | ||
} | ||
} | ||
|
||
module.exports = { Handshake }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const varint = require( 'varint' ); | ||
|
||
class Packet { | ||
constructor( state, id ) { | ||
this.state = state; | ||
this.id = id; | ||
} | ||
|
||
createPacket( data ) { | ||
return Buffer.concat( [ | ||
Buffer.from( varint.encode( varint.encodingLength( this.id ) + data.length ) ), | ||
Buffer.from( varint.encode( this.id ) ), | ||
data | ||
] ); | ||
} | ||
|
||
static decode( buffer ) { | ||
let offset = 0; | ||
|
||
let length = varint.decode( buffer ); | ||
offset += varint.decode.bytes; | ||
|
||
let id = varint.decode( buffer, offset ); | ||
offset += varint.decode.bytes; | ||
|
||
let packet = buffer.slice( offset, buffer.length ); | ||
|
||
return { length, id, packet }; | ||
} | ||
} | ||
|
||
module.exports = Packet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
const varint = require( 'varint' ); | ||
|
||
const Long = require( 'long' ); | ||
|
||
const State = require( '../state.js' ); | ||
const Packet = require( './packet.js' ); | ||
|
||
const state = State.STATUS; | ||
|
||
class Request extends Packet { | ||
constructor() { | ||
super( state, 0 ); | ||
} | ||
|
||
encode() { | ||
return this.createPacket(); | ||
} | ||
|
||
static decode( data ) { | ||
return new Request(); | ||
} | ||
} | ||
|
||
class Response extends Packet { | ||
constructor( status ) { | ||
super( state, 0 ); | ||
|
||
this.status = status; | ||
} | ||
|
||
encode() { | ||
let statusString = JSON.stringify( this.status ); | ||
|
||
return this.createPacket( Buffer.concat( [ | ||
Buffer.from( varint.encode( statusString.length ) ), | ||
Buffer.from( statusString, 'utf8' ) | ||
] ) ); | ||
} | ||
|
||
static decode( buffer ) { | ||
let statusLength = varint.decode( buffer ); | ||
|
||
let status = buffer.toString( 'utf8', varint.decode.length, varint.decode.length + statusLength ); | ||
|
||
return new Response( JSON.parse( status ) ); | ||
} | ||
} | ||
|
||
class Ping extends Packet { | ||
constructor( payload ) { | ||
super( state, 1 ); | ||
|
||
this.payload = payload; | ||
} | ||
|
||
encode() { | ||
let payloadBuffer = Buffer.alloc( 8 ); | ||
payloadBuffer.writeUInt32BE( this.payload.low , 0 ); | ||
payloadBuffer.writeUInt32BE( this.payload.high, 0 ); | ||
|
||
return this.createPacket( payloadBuffer ); | ||
} | ||
|
||
static decode( buffer ) { | ||
let low = buffer.readUInt32BE( buffer ); | ||
let high = buffer.readUInt32BE( buffer, 4 ); | ||
|
||
return new Ping( new Long( low, high ) ); | ||
} | ||
} | ||
|
||
class Pong extends Packet { | ||
constructor( payload ) { | ||
super( state, 1 ); | ||
|
||
this.payload = payload; | ||
} | ||
|
||
encode() { | ||
let payloadBuffer = Buffer.alloc( 8 ); | ||
payloadBuffer.writeUInt32BE( this.payload.low , 0 ); | ||
payloadBuffer.writeUInt32BE( this.payload.high, 0 ); | ||
|
||
return this.createPacket( payloadBuffer ); | ||
} | ||
|
||
static decode( buffer ) { | ||
let low = buffer.readUInt32BE( buffer ); | ||
let high = buffer.readUInt32BE( buffer, 4 ); | ||
|
||
return new Pong( new Long( low, high ) ); | ||
} | ||
} | ||
|
||
module.exports = { Request, Response, Ping, Pong }; |