-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref[typescript]: refactor typescript protocol
- Loading branch information
1 parent
e96120d
commit 231f010
Showing
7 changed files
with
104 additions
and
97 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
11 changes: 11 additions & 0 deletions
11
protocol/src/main/resources/typescript/IProtocolRegistration.ts
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,11 @@ | ||
import IByteBuffer from "./IByteBuffer"; | ||
|
||
interface IProtocolRegistration<T> { | ||
protocolId(): number; | ||
|
||
write(buffer: IByteBuffer, packet: T | null): void; | ||
|
||
read(buffer: IByteBuffer): T | null; | ||
} | ||
|
||
export default IProtocolRegistration; |
28 changes: 0 additions & 28 deletions
28
protocol/src/main/resources/typescript/ProtocolClassTemplate.ts
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 |
---|---|---|
@@ -1,32 +1,4 @@ | ||
${protocol_note} | ||
class ${protocol_name} { | ||
${protocol_field_definition} | ||
|
||
static PROTOCOL_ID: number = ${protocol_id}; | ||
|
||
protocolId(): number { | ||
return ${protocol_name}.PROTOCOL_ID; | ||
} | ||
|
||
static write(buffer: IByteBuffer, packet: ${protocol_name} | null) { | ||
if (packet === null) { | ||
buffer.writeInt(0); | ||
return; | ||
} | ||
${protocol_write_serialization} | ||
} | ||
|
||
static read(buffer: IByteBuffer): ${protocol_name} | null { | ||
const length = buffer.readInt(); | ||
if (length === 0) { | ||
return null; | ||
} | ||
const beforeReadIndex = buffer.getReadOffset(); | ||
const packet = new ${protocol_name}(); | ||
${protocol_read_deserialization} | ||
if (length > 0) { | ||
buffer.setReadOffset(beforeReadIndex + length); | ||
} | ||
return packet; | ||
} | ||
} |
18 changes: 14 additions & 4 deletions
18
protocol/src/main/resources/typescript/ProtocolManagerTemplate.ts
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
27 changes: 27 additions & 0 deletions
27
protocol/src/main/resources/typescript/ProtocolRegistrationTemplate.ts
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,27 @@ | ||
export class ${protocol_name}Registration implements IProtocolRegistration<${protocol_name}> { | ||
protocolId(): number { | ||
return ${protocol_id}; | ||
} | ||
|
||
write(buffer: IByteBuffer, packet: ${protocol_name} | null) { | ||
if (packet === null) { | ||
buffer.writeInt(0); | ||
return; | ||
} | ||
${protocol_write_serialization} | ||
} | ||
|
||
read(buffer: IByteBuffer): ${protocol_name} | null { | ||
const length = buffer.readInt(); | ||
if (length === 0) { | ||
return null; | ||
} | ||
const beforeReadIndex = buffer.getReadOffset(); | ||
const packet = new ${protocol_name}(); | ||
${protocol_read_deserialization} | ||
if (length > 0) { | ||
buffer.setReadOffset(beforeReadIndex + length); | ||
} | ||
return packet; | ||
} | ||
} |
32 changes: 2 additions & 30 deletions
32
protocol/src/main/resources/typescript/ProtocolTemplate.ts
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 |
---|---|---|
@@ -1,35 +1,7 @@ | ||
${protocol_imports} | ||
${protocol_note} | ||
class ${protocol_name} { | ||
${protocol_field_definition} | ||
|
||
static PROTOCOL_ID: number = ${protocol_id}; | ||
${protocol_class} | ||
|
||
protocolId(): number { | ||
return ${protocol_name}.PROTOCOL_ID; | ||
} | ||
|
||
static write(buffer: IByteBuffer, packet: ${protocol_name} | null) { | ||
if (packet === null) { | ||
buffer.writeInt(0); | ||
return; | ||
} | ||
${protocol_write_serialization} | ||
} | ||
|
||
static read(buffer: IByteBuffer): ${protocol_name} | null { | ||
const length = buffer.readInt(); | ||
if (length === 0) { | ||
return null; | ||
} | ||
const beforeReadIndex = buffer.getReadOffset(); | ||
const packet = new ${protocol_name}(); | ||
${protocol_read_deserialization} | ||
if (length > 0) { | ||
buffer.setReadOffset(beforeReadIndex + length); | ||
} | ||
return packet; | ||
} | ||
} | ||
${protocol_registration} | ||
|
||
export default ${protocol_name}; |
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