[Change] refactor project, using simple game sample packet define

This commit is contained in:
tiennm99 committed 2023-03-19 16:05:53 +07:00
1 parent 6c1620b00e
commit 0e7c5064f7
31 files changed
+1085 -895

No files matched your search

File renamed without changes.
-1
View File
@@ -1 +0,0 @@
.idea
+1 -1
View File
@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<title>Title</title>
<script src="flatbuffers.js"></script>
<script src="sample_generated.js"></script>
<script src="schema_generated.js"></script>
<script src="script.js"></script>
</head>
<body>
-575
View File
@@ -1,575 +0,0 @@
// automatically generated by the FlatBuffers compiler, do not modify
/**
* @const
* @namespace
*/
var MyGame = MyGame || {};
/**
* @const
* @namespace
*/
MyGame.Sample = MyGame.Sample || {};
/**
* @enum {number}
*/
MyGame.Sample.Color = {
Red: 0,
Green: 1,
Blue: 2
};
/**
* @enum {string}
*/
MyGame.Sample.ColorName = {
'0': 'Red',
'1': 'Green',
'2': 'Blue'
};
/**
* @enum {number}
*/
MyGame.Sample.Equipment = {
NONE: 0,
Weapon: 1
};
/**
* @enum {string}
*/
MyGame.Sample.EquipmentName = {
'0': 'NONE',
'1': 'Weapon'
};
/**
* @constructor
*/
MyGame.Sample.Vec3 = function() {
/**
* @type {flatbuffers.ByteBuffer}
*/
this.bb = null;
/**
* @type {number}
*/
this.bb_pos = 0;
};
/**
* @param {number} i
* @param {flatbuffers.ByteBuffer} bb
* @returns {MyGame.Sample.Vec3}
*/
MyGame.Sample.Vec3.prototype.__init = function(i, bb) {
this.bb_pos = i;
this.bb = bb;
return this;
};
/**
* @returns {number}
*/
MyGame.Sample.Vec3.prototype.x = function() {
return this.bb.readFloat32(this.bb_pos);
};
/**
* @returns {number}
*/
MyGame.Sample.Vec3.prototype.y = function() {
return this.bb.readFloat32(this.bb_pos + 4);
};
/**
* @returns {number}
*/
MyGame.Sample.Vec3.prototype.z = function() {
return this.bb.readFloat32(this.bb_pos + 8);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} x
* @param {number} y
* @param {number} z
* @returns {flatbuffers.Offset}
*/
MyGame.Sample.Vec3.createVec3 = function(builder, x, y, z) {
builder.prep(4, 12);
builder.writeFloat32(z);
builder.writeFloat32(y);
builder.writeFloat32(x);
return builder.offset();
};
/**
* @constructor
*/
MyGame.Sample.Monster = function() {
/**
* @type {flatbuffers.ByteBuffer}
*/
this.bb = null;
/**
* @type {number}
*/
this.bb_pos = 0;
};
/**
* @param {number} i
* @param {flatbuffers.ByteBuffer} bb
* @returns {MyGame.Sample.Monster}
*/
MyGame.Sample.Monster.prototype.__init = function(i, bb) {
this.bb_pos = i;
this.bb = bb;
return this;
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {MyGame.Sample.Monster=} obj
* @returns {MyGame.Sample.Monster}
*/
MyGame.Sample.Monster.getRootAsMonster = function(bb, obj) {
return (obj || new MyGame.Sample.Monster).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {MyGame.Sample.Monster=} obj
* @returns {MyGame.Sample.Monster}
*/
MyGame.Sample.Monster.getSizePrefixedRootAsMonster = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new MyGame.Sample.Monster).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @param {MyGame.Sample.Vec3=} obj
* @returns {MyGame.Sample.Vec3|null}
*/
MyGame.Sample.Monster.prototype.pos = function(obj) {
var offset = this.bb.__offset(this.bb_pos, 4);
return offset ? (obj || new MyGame.Sample.Vec3).__init(this.bb_pos + offset, this.bb) : null;
};
/**
* @returns {number}
*/
MyGame.Sample.Monster.prototype.mana = function() {
var offset = this.bb.__offset(this.bb_pos, 6);
return offset ? this.bb.readInt16(this.bb_pos + offset) : 150;
};
/**
* @returns {number}
*/
MyGame.Sample.Monster.prototype.hp = function() {
var offset = this.bb.__offset(this.bb_pos, 8);
return offset ? this.bb.readInt16(this.bb_pos + offset) : 100;
};
/**
* @param {flatbuffers.Encoding=} optionalEncoding
* @returns {string|Uint8Array|null}
*/
MyGame.Sample.Monster.prototype.name = function(optionalEncoding) {
var offset = this.bb.__offset(this.bb_pos, 10);
return offset ? this.bb.__string(this.bb_pos + offset, optionalEncoding) : null;
};
/**
* @param {number} index
* @returns {number}
*/
MyGame.Sample.Monster.prototype.inventory = function(index) {
var offset = this.bb.__offset(this.bb_pos, 14);
return offset ? this.bb.readUint8(this.bb.__vector(this.bb_pos + offset) + index) : 0;
};
/**
* @returns {number}
*/
MyGame.Sample.Monster.prototype.inventoryLength = function() {
var offset = this.bb.__offset(this.bb_pos, 14);
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns {Uint8Array}
*/
MyGame.Sample.Monster.prototype.inventoryArray = function() {
var offset = this.bb.__offset(this.bb_pos, 14);
return offset ? new Uint8Array(this.bb.bytes().buffer, this.bb.bytes().byteOffset + this.bb.__vector(this.bb_pos + offset), this.bb.__vector_len(this.bb_pos + offset)) : null;
};
/**
* @returns {MyGame.Sample.Color}
*/
MyGame.Sample.Monster.prototype.color = function() {
var offset = this.bb.__offset(this.bb_pos, 16);
return offset ? /** @type {MyGame.Sample.Color} */ (this.bb.readInt8(this.bb_pos + offset)) : MyGame.Sample.Color.Blue;
};
/**
* @param {number} index
* @param {MyGame.Sample.Weapon=} obj
* @returns {MyGame.Sample.Weapon}
*/
MyGame.Sample.Monster.prototype.weapons = function(index, obj) {
var offset = this.bb.__offset(this.bb_pos, 18);
return offset ? (obj || new MyGame.Sample.Weapon).__init(this.bb.__indirect(this.bb.__vector(this.bb_pos + offset) + index * 4), this.bb) : null;
};
/**
* @returns {number}
*/
MyGame.Sample.Monster.prototype.weaponsLength = function() {
var offset = this.bb.__offset(this.bb_pos, 18);
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @returns {MyGame.Sample.Equipment}
*/
MyGame.Sample.Monster.prototype.equippedType = function() {
var offset = this.bb.__offset(this.bb_pos, 20);
return offset ? /** @type {MyGame.Sample.Equipment} */ (this.bb.readUint8(this.bb_pos + offset)) : MyGame.Sample.Equipment.NONE;
};
/**
* @param {flatbuffers.Table} obj
* @returns {?flatbuffers.Table}
*/
MyGame.Sample.Monster.prototype.equipped = function(obj) {
var offset = this.bb.__offset(this.bb_pos, 22);
return offset ? this.bb.__union(obj, this.bb_pos + offset) : null;
};
/**
* @param {number} index
* @param {MyGame.Sample.Vec3=} obj
* @returns {MyGame.Sample.Vec3}
*/
MyGame.Sample.Monster.prototype.path = function(index, obj) {
var offset = this.bb.__offset(this.bb_pos, 24);
return offset ? (obj || new MyGame.Sample.Vec3).__init(this.bb.__vector(this.bb_pos + offset) + index * 12, this.bb) : null;
};
/**
* @returns {number}
*/
MyGame.Sample.Monster.prototype.pathLength = function() {
var offset = this.bb.__offset(this.bb_pos, 24);
return offset ? this.bb.__vector_len(this.bb_pos + offset) : 0;
};
/**
* @param {flatbuffers.Builder} builder
*/
MyGame.Sample.Monster.startMonster = function(builder) {
builder.startObject(11);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} posOffset
*/
MyGame.Sample.Monster.addPos = function(builder, posOffset) {
builder.addFieldStruct(0, posOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} mana
*/
MyGame.Sample.Monster.addMana = function(builder, mana) {
builder.addFieldInt16(1, mana, 150);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} hp
*/
MyGame.Sample.Monster.addHp = function(builder, hp) {
builder.addFieldInt16(2, hp, 100);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} nameOffset
*/
MyGame.Sample.Monster.addName = function(builder, nameOffset) {
builder.addFieldOffset(3, nameOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} inventoryOffset
*/
MyGame.Sample.Monster.addInventory = function(builder, inventoryOffset) {
builder.addFieldOffset(5, inventoryOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {Array.<number>} data
* @returns {flatbuffers.Offset}
*/
MyGame.Sample.Monster.createInventoryVector = function(builder, data) {
builder.startVector(1, data.length, 1);
for (var i = data.length - 1; i >= 0; i--) {
builder.addInt8(data[i]);
}
return builder.endVector();
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} numElems
*/
MyGame.Sample.Monster.startInventoryVector = function(builder, numElems) {
builder.startVector(1, numElems, 1);
};
/**
* @param {flatbuffers.Builder} builder
* @param {MyGame.Sample.Color} color
*/
MyGame.Sample.Monster.addColor = function(builder, color) {
builder.addFieldInt8(6, color, MyGame.Sample.Color.Blue);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} weaponsOffset
*/
MyGame.Sample.Monster.addWeapons = function(builder, weaponsOffset) {
builder.addFieldOffset(7, weaponsOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {Array.<flatbuffers.Offset>} data
* @returns {flatbuffers.Offset}
*/
MyGame.Sample.Monster.createWeaponsVector = function(builder, data) {
builder.startVector(4, data.length, 4);
for (var i = data.length - 1; i >= 0; i--) {
builder.addOffset(data[i]);
}
return builder.endVector();
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} numElems
*/
MyGame.Sample.Monster.startWeaponsVector = function(builder, numElems) {
builder.startVector(4, numElems, 4);
};
/**
* @param {flatbuffers.Builder} builder
* @param {MyGame.Sample.Equipment} equippedType
*/
MyGame.Sample.Monster.addEquippedType = function(builder, equippedType) {
builder.addFieldInt8(8, equippedType, MyGame.Sample.Equipment.NONE);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} equippedOffset
*/
MyGame.Sample.Monster.addEquipped = function(builder, equippedOffset) {
builder.addFieldOffset(9, equippedOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} pathOffset
*/
MyGame.Sample.Monster.addPath = function(builder, pathOffset) {
builder.addFieldOffset(10, pathOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} numElems
*/
MyGame.Sample.Monster.startPathVector = function(builder, numElems) {
builder.startVector(12, numElems, 4);
};
/**
* @param {flatbuffers.Builder} builder
* @returns {flatbuffers.Offset}
*/
MyGame.Sample.Monster.endMonster = function(builder) {
var offset = builder.endObject();
return offset;
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} offset
*/
MyGame.Sample.Monster.finishMonsterBuffer = function(builder, offset) {
builder.finish(offset);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} offset
*/
MyGame.Sample.Monster.finishSizePrefixedMonsterBuffer = function(builder, offset) {
builder.finish(offset, undefined, true);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} posOffset
* @param {number} mana
* @param {number} hp
* @param {flatbuffers.Offset} nameOffset
* @param {flatbuffers.Offset} inventoryOffset
* @param {MyGame.Sample.Color} color
* @param {flatbuffers.Offset} weaponsOffset
* @param {MyGame.Sample.Equipment} equippedType
* @param {flatbuffers.Offset} equippedOffset
* @param {flatbuffers.Offset} pathOffset
* @returns {flatbuffers.Offset}
*/
MyGame.Sample.Monster.createMonster = function(builder, posOffset, mana, hp, nameOffset, inventoryOffset, color, weaponsOffset, equippedType, equippedOffset, pathOffset) {
MyGame.Sample.Monster.startMonster(builder);
MyGame.Sample.Monster.addPos(builder, posOffset);
MyGame.Sample.Monster.addMana(builder, mana);
MyGame.Sample.Monster.addHp(builder, hp);
MyGame.Sample.Monster.addName(builder, nameOffset);
MyGame.Sample.Monster.addInventory(builder, inventoryOffset);
MyGame.Sample.Monster.addColor(builder, color);
MyGame.Sample.Monster.addWeapons(builder, weaponsOffset);
MyGame.Sample.Monster.addEquippedType(builder, equippedType);
MyGame.Sample.Monster.addEquipped(builder, equippedOffset);
MyGame.Sample.Monster.addPath(builder, pathOffset);
return MyGame.Sample.Monster.endMonster(builder);
}
/**
* @constructor
*/
MyGame.Sample.Weapon = function() {
/**
* @type {flatbuffers.ByteBuffer}
*/
this.bb = null;
/**
* @type {number}
*/
this.bb_pos = 0;
};
/**
* @param {number} i
* @param {flatbuffers.ByteBuffer} bb
* @returns {MyGame.Sample.Weapon}
*/
MyGame.Sample.Weapon.prototype.__init = function(i, bb) {
this.bb_pos = i;
this.bb = bb;
return this;
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {MyGame.Sample.Weapon=} obj
* @returns {MyGame.Sample.Weapon}
*/
MyGame.Sample.Weapon.getRootAsWeapon = function(bb, obj) {
return (obj || new MyGame.Sample.Weapon).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {MyGame.Sample.Weapon=} obj
* @returns {MyGame.Sample.Weapon}
*/
MyGame.Sample.Weapon.getSizePrefixedRootAsWeapon = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new MyGame.Sample.Weapon).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @param {flatbuffers.Encoding=} optionalEncoding
* @returns {string|Uint8Array|null}
*/
MyGame.Sample.Weapon.prototype.name = function(optionalEncoding) {
var offset = this.bb.__offset(this.bb_pos, 4);
return offset ? this.bb.__string(this.bb_pos + offset, optionalEncoding) : null;
};
/**
* @returns {number}
*/
MyGame.Sample.Weapon.prototype.damage = function() {
var offset = this.bb.__offset(this.bb_pos, 6);
return offset ? this.bb.readInt16(this.bb_pos + offset) : 0;
};
/**
* @param {flatbuffers.Builder} builder
*/
MyGame.Sample.Weapon.startWeapon = function(builder) {
builder.startObject(2);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} nameOffset
*/
MyGame.Sample.Weapon.addName = function(builder, nameOffset) {
builder.addFieldOffset(0, nameOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} damage
*/
MyGame.Sample.Weapon.addDamage = function(builder, damage) {
builder.addFieldInt16(1, damage, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @returns {flatbuffers.Offset}
*/
MyGame.Sample.Weapon.endWeapon = function(builder) {
var offset = builder.endObject();
return offset;
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} nameOffset
* @param {number} damage
* @returns {flatbuffers.Offset}
*/
MyGame.Sample.Weapon.createWeapon = function(builder, nameOffset, damage) {
MyGame.Sample.Weapon.startWeapon(builder);
MyGame.Sample.Weapon.addName(builder, nameOffset);
MyGame.Sample.Weapon.addDamage(builder, damage);
return MyGame.Sample.Weapon.endWeapon(builder);
}
// Exports for Node.js and RequireJS
this.MyGame = MyGame;
+561
View File
@@ -0,0 +1,561 @@
// automatically generated by the FlatBuffers compiler, do not modify
/**
* @const
* @namespace
*/
var com = com || {};
/**
* @const
* @namespace
*/
com.miti99 = com.miti99 || {};
/**
* @const
* @namespace
*/
com.miti99.netty = com.miti99.netty || {};
/**
* @const
* @namespace
*/
com.miti99.netty.fbs = com.miti99.netty.fbs || {};
/**
* @enum {number}
*/
com.miti99.netty.fbs.PacketData = {
NONE: 0,
LoginRequest: 1,
LoginResponse: 2
};
/**
* @enum {string}
*/
com.miti99.netty.fbs.PacketDataName = {
'0': 'NONE',
'1': 'LoginRequest',
'2': 'LoginResponse'
};
/**
* @constructor
*/
com.miti99.netty.fbs.Vec2 = function() {
/**
* @type {flatbuffers.ByteBuffer}
*/
this.bb = null;
/**
* @type {number}
*/
this.bb_pos = 0;
};
/**
* @param {number} i
* @param {flatbuffers.ByteBuffer} bb
* @returns {com.miti99.netty.fbs.Vec2}
*/
com.miti99.netty.fbs.Vec2.prototype.__init = function(i, bb) {
this.bb_pos = i;
this.bb = bb;
return this;
};
/**
* @returns {number}
*/
com.miti99.netty.fbs.Vec2.prototype.x = function() {
return this.bb.readFloat64(this.bb_pos);
};
/**
* @returns {number}
*/
com.miti99.netty.fbs.Vec2.prototype.y = function() {
return this.bb.readFloat64(this.bb_pos + 8);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} x
* @param {number} y
* @returns {flatbuffers.Offset}
*/
com.miti99.netty.fbs.Vec2.createVec2 = function(builder, x, y) {
builder.prep(8, 16);
builder.writeFloat64(y);
builder.writeFloat64(x);
return builder.offset();
};
/**
* @constructor
*/
com.miti99.netty.fbs.Player = function() {
/**
* @type {flatbuffers.ByteBuffer}
*/
this.bb = null;
/**
* @type {number}
*/
this.bb_pos = 0;
};
/**
* @param {number} i
* @param {flatbuffers.ByteBuffer} bb
* @returns {com.miti99.netty.fbs.Player}
*/
com.miti99.netty.fbs.Player.prototype.__init = function(i, bb) {
this.bb_pos = i;
this.bb = bb;
return this;
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {com.miti99.netty.fbs.Player=} obj
* @returns {com.miti99.netty.fbs.Player}
*/
com.miti99.netty.fbs.Player.getRootAsPlayer = function(bb, obj) {
return (obj || new com.miti99.netty.fbs.Player).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {com.miti99.netty.fbs.Player=} obj
* @returns {com.miti99.netty.fbs.Player}
*/
com.miti99.netty.fbs.Player.getSizePrefixedRootAsPlayer = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new com.miti99.netty.fbs.Player).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @returns {number}
*/
com.miti99.netty.fbs.Player.prototype.userId = function() {
var offset = this.bb.__offset(this.bb_pos, 4);
return offset ? this.bb.readInt32(this.bb_pos + offset) : 0;
};
/**
* @param {flatbuffers.Encoding=} optionalEncoding
* @returns {string|Uint8Array|null}
*/
com.miti99.netty.fbs.Player.prototype.userName = function(optionalEncoding) {
var offset = this.bb.__offset(this.bb_pos, 6);
return offset ? this.bb.__string(this.bb_pos + offset, optionalEncoding) : null;
};
/**
* @param {com.miti99.netty.fbs.Vec2=} obj
* @returns {com.miti99.netty.fbs.Vec2|null}
*/
com.miti99.netty.fbs.Player.prototype.position = function(obj) {
var offset = this.bb.__offset(this.bb_pos, 8);
return offset ? (obj || new com.miti99.netty.fbs.Vec2).__init(this.bb_pos + offset, this.bb) : null;
};
/**
* @returns {number}
*/
com.miti99.netty.fbs.Player.prototype.rotation = function() {
var offset = this.bb.__offset(this.bb_pos, 10);
return offset ? this.bb.readFloat64(this.bb_pos + offset) : 0.0;
};
/**
* @returns {number}
*/
com.miti99.netty.fbs.Player.prototype.team = function() {
var offset = this.bb.__offset(this.bb_pos, 12);
return offset ? this.bb.readInt32(this.bb_pos + offset) : 0;
};
/**
* @param {flatbuffers.Builder} builder
*/
com.miti99.netty.fbs.Player.startPlayer = function(builder) {
builder.startObject(5);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} userId
*/
com.miti99.netty.fbs.Player.addUserId = function(builder, userId) {
builder.addFieldInt32(0, userId, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} userNameOffset
*/
com.miti99.netty.fbs.Player.addUserName = function(builder, userNameOffset) {
builder.addFieldOffset(1, userNameOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} positionOffset
*/
com.miti99.netty.fbs.Player.addPosition = function(builder, positionOffset) {
builder.addFieldStruct(2, positionOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} rotation
*/
com.miti99.netty.fbs.Player.addRotation = function(builder, rotation) {
builder.addFieldFloat64(3, rotation, 0.0);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} team
*/
com.miti99.netty.fbs.Player.addTeam = function(builder, team) {
builder.addFieldInt32(4, team, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @returns {flatbuffers.Offset}
*/
com.miti99.netty.fbs.Player.endPlayer = function(builder) {
var offset = builder.endObject();
return offset;
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} userId
* @param {flatbuffers.Offset} userNameOffset
* @param {flatbuffers.Offset} positionOffset
* @param {number} rotation
* @param {number} team
* @returns {flatbuffers.Offset}
*/
com.miti99.netty.fbs.Player.createPlayer = function(builder, userId, userNameOffset, positionOffset, rotation, team) {
com.miti99.netty.fbs.Player.startPlayer(builder);
com.miti99.netty.fbs.Player.addUserId(builder, userId);
com.miti99.netty.fbs.Player.addUserName(builder, userNameOffset);
com.miti99.netty.fbs.Player.addPosition(builder, positionOffset);
com.miti99.netty.fbs.Player.addRotation(builder, rotation);
com.miti99.netty.fbs.Player.addTeam(builder, team);
return com.miti99.netty.fbs.Player.endPlayer(builder);
}
/**
* @constructor
*/
com.miti99.netty.fbs.LoginRequest = function() {
/**
* @type {flatbuffers.ByteBuffer}
*/
this.bb = null;
/**
* @type {number}
*/
this.bb_pos = 0;
};
/**
* @param {number} i
* @param {flatbuffers.ByteBuffer} bb
* @returns {com.miti99.netty.fbs.LoginRequest}
*/
com.miti99.netty.fbs.LoginRequest.prototype.__init = function(i, bb) {
this.bb_pos = i;
this.bb = bb;
return this;
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {com.miti99.netty.fbs.LoginRequest=} obj
* @returns {com.miti99.netty.fbs.LoginRequest}
*/
com.miti99.netty.fbs.LoginRequest.getRootAsLoginRequest = function(bb, obj) {
return (obj || new com.miti99.netty.fbs.LoginRequest).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {com.miti99.netty.fbs.LoginRequest=} obj
* @returns {com.miti99.netty.fbs.LoginRequest}
*/
com.miti99.netty.fbs.LoginRequest.getSizePrefixedRootAsLoginRequest = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new com.miti99.netty.fbs.LoginRequest).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @returns {number}
*/
com.miti99.netty.fbs.LoginRequest.prototype.userId = function() {
var offset = this.bb.__offset(this.bb_pos, 4);
return offset ? this.bb.readInt32(this.bb_pos + offset) : 0;
};
/**
* @param {flatbuffers.Builder} builder
*/
com.miti99.netty.fbs.LoginRequest.startLoginRequest = function(builder) {
builder.startObject(1);
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} userId
*/
com.miti99.netty.fbs.LoginRequest.addUserId = function(builder, userId) {
builder.addFieldInt32(0, userId, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @returns {flatbuffers.Offset}
*/
com.miti99.netty.fbs.LoginRequest.endLoginRequest = function(builder) {
var offset = builder.endObject();
return offset;
};
/**
* @param {flatbuffers.Builder} builder
* @param {number} userId
* @returns {flatbuffers.Offset}
*/
com.miti99.netty.fbs.LoginRequest.createLoginRequest = function(builder, userId) {
com.miti99.netty.fbs.LoginRequest.startLoginRequest(builder);
com.miti99.netty.fbs.LoginRequest.addUserId(builder, userId);
return com.miti99.netty.fbs.LoginRequest.endLoginRequest(builder);
}
/**
* @constructor
*/
com.miti99.netty.fbs.LoginResponse = function() {
/**
* @type {flatbuffers.ByteBuffer}
*/
this.bb = null;
/**
* @type {number}
*/
this.bb_pos = 0;
};
/**
* @param {number} i
* @param {flatbuffers.ByteBuffer} bb
* @returns {com.miti99.netty.fbs.LoginResponse}
*/
com.miti99.netty.fbs.LoginResponse.prototype.__init = function(i, bb) {
this.bb_pos = i;
this.bb = bb;
return this;
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {com.miti99.netty.fbs.LoginResponse=} obj
* @returns {com.miti99.netty.fbs.LoginResponse}
*/
com.miti99.netty.fbs.LoginResponse.getRootAsLoginResponse = function(bb, obj) {
return (obj || new com.miti99.netty.fbs.LoginResponse).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {com.miti99.netty.fbs.LoginResponse=} obj
* @returns {com.miti99.netty.fbs.LoginResponse}
*/
com.miti99.netty.fbs.LoginResponse.getSizePrefixedRootAsLoginResponse = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new com.miti99.netty.fbs.LoginResponse).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @param {com.miti99.netty.fbs.Player=} obj
* @returns {com.miti99.netty.fbs.Player|null}
*/
com.miti99.netty.fbs.LoginResponse.prototype.player = function(obj) {
var offset = this.bb.__offset(this.bb_pos, 4);
return offset ? (obj || new com.miti99.netty.fbs.Player).__init(this.bb.__indirect(this.bb_pos + offset), this.bb) : null;
};
/**
* @param {flatbuffers.Builder} builder
*/
com.miti99.netty.fbs.LoginResponse.startLoginResponse = function(builder) {
builder.startObject(1);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} playerOffset
*/
com.miti99.netty.fbs.LoginResponse.addPlayer = function(builder, playerOffset) {
builder.addFieldOffset(0, playerOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @returns {flatbuffers.Offset}
*/
com.miti99.netty.fbs.LoginResponse.endLoginResponse = function(builder) {
var offset = builder.endObject();
return offset;
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} playerOffset
* @returns {flatbuffers.Offset}
*/
com.miti99.netty.fbs.LoginResponse.createLoginResponse = function(builder, playerOffset) {
com.miti99.netty.fbs.LoginResponse.startLoginResponse(builder);
com.miti99.netty.fbs.LoginResponse.addPlayer(builder, playerOffset);
return com.miti99.netty.fbs.LoginResponse.endLoginResponse(builder);
}
/**
* @constructor
*/
com.miti99.netty.fbs.Packet = function() {
/**
* @type {flatbuffers.ByteBuffer}
*/
this.bb = null;
/**
* @type {number}
*/
this.bb_pos = 0;
};
/**
* @param {number} i
* @param {flatbuffers.ByteBuffer} bb
* @returns {com.miti99.netty.fbs.Packet}
*/
com.miti99.netty.fbs.Packet.prototype.__init = function(i, bb) {
this.bb_pos = i;
this.bb = bb;
return this;
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {com.miti99.netty.fbs.Packet=} obj
* @returns {com.miti99.netty.fbs.Packet}
*/
com.miti99.netty.fbs.Packet.getRootAsPacket = function(bb, obj) {
return (obj || new com.miti99.netty.fbs.Packet).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @param {flatbuffers.ByteBuffer} bb
* @param {com.miti99.netty.fbs.Packet=} obj
* @returns {com.miti99.netty.fbs.Packet}
*/
com.miti99.netty.fbs.Packet.getSizePrefixedRootAsPacket = function(bb, obj) {
bb.setPosition(bb.position() + flatbuffers.SIZE_PREFIX_LENGTH);
return (obj || new com.miti99.netty.fbs.Packet).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
/**
* @returns {com.miti99.netty.fbs.PacketData}
*/
com.miti99.netty.fbs.Packet.prototype.dataType = function() {
var offset = this.bb.__offset(this.bb_pos, 4);
return offset ? /** @type {com.miti99.netty.fbs.PacketData} */ (this.bb.readUint8(this.bb_pos + offset)) : com.miti99.netty.fbs.PacketData.NONE;
};
/**
* @param {flatbuffers.Table} obj
* @returns {?flatbuffers.Table}
*/
com.miti99.netty.fbs.Packet.prototype.data = function(obj) {
var offset = this.bb.__offset(this.bb_pos, 6);
return offset ? this.bb.__union(obj, this.bb_pos + offset) : null;
};
/**
* @param {flatbuffers.Builder} builder
*/
com.miti99.netty.fbs.Packet.startPacket = function(builder) {
builder.startObject(2);
};
/**
* @param {flatbuffers.Builder} builder
* @param {com.miti99.netty.fbs.PacketData} dataType
*/
com.miti99.netty.fbs.Packet.addDataType = function(builder, dataType) {
builder.addFieldInt8(0, dataType, com.miti99.netty.fbs.PacketData.NONE);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} dataOffset
*/
com.miti99.netty.fbs.Packet.addData = function(builder, dataOffset) {
builder.addFieldOffset(1, dataOffset, 0);
};
/**
* @param {flatbuffers.Builder} builder
* @returns {flatbuffers.Offset}
*/
com.miti99.netty.fbs.Packet.endPacket = function(builder) {
var offset = builder.endObject();
return offset;
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} offset
*/
com.miti99.netty.fbs.Packet.finishPacketBuffer = function(builder, offset) {
builder.finish(offset);
};
/**
* @param {flatbuffers.Builder} builder
* @param {flatbuffers.Offset} offset
*/
com.miti99.netty.fbs.Packet.finishSizePrefixedPacketBuffer = function(builder, offset) {
builder.finish(offset, undefined, true);
};
/**
* @param {flatbuffers.Builder} builder
* @param {com.miti99.netty.fbs.PacketData} dataType
* @param {flatbuffers.Offset} dataOffset
* @returns {flatbuffers.Offset}
*/
com.miti99.netty.fbs.Packet.createPacket = function(builder, dataType, dataOffset) {
com.miti99.netty.fbs.Packet.startPacket(builder);
com.miti99.netty.fbs.Packet.addDataType(builder, dataType);
com.miti99.netty.fbs.Packet.addData(builder, dataOffset);
return com.miti99.netty.fbs.Packet.endPacket(builder);
}
// Exports for Node.js and RequireJS
this.com = com;
+61
View File
@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>learn-netty</artifactId>
<dependencies>
<dependency>
<artifactId>gson</artifactId>
<groupId>com.google.code.gson</groupId>
<version>2.10.1</version>
</dependency>
<dependency>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
<version>1.4.6</version>
</dependency>
<dependency>
<artifactId>flatbuffers-java</artifactId>
<groupId>com.google.flatbuffers</groupId>
<version>1.12.0</version>
</dependency>
<dependency>
<artifactId>netty-all</artifactId>
<groupId>io.netty</groupId>
<scope>compile</scope>
<version>4.1.89.Final</version>
</dependency>
<dependency>
<artifactId>junit-jupiter</artifactId>
<groupId>org.junit.jupiter</groupId>
<scope>test</scope>
<version>5.9.2</version>
</dependency>
<dependency>
<artifactId>lombok</artifactId>
<groupId>org.projectlombok</groupId>
<scope>provided</scope>
<version>1.18.26</version>
</dependency>
</dependencies>
<groupId>com.miti99</groupId>
<modelVersion>4.0.0</modelVersion>
<modules>
<module>server</module>
</modules>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<version>1.0-SNAPSHOT</version>
</project>
+7 -37
View File
@@ -2,47 +2,17 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<artifactId>server</artifactId>
<dependencies>
<dependency>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
<version>1.4.5</version>
</dependency>
<dependency>
<artifactId>flatbuffers-java</artifactId>
<groupId>com.google.flatbuffers</groupId>
<version>1.12.0</version>
</dependency>
<dependency>
<artifactId>netty-all</artifactId>
<groupId>io.netty</groupId>
<scope>compile</scope>
<version>4.1.89.Final</version>
</dependency>
<dependency>
<artifactId>junit-jupiter</artifactId>
<groupId>org.junit.jupiter</groupId>
<scope>test</scope>
<version>5.9.2</version>
</dependency>
<dependency>
<artifactId>lombok</artifactId>
<groupId>org.projectlombok</groupId>
<scope>provided</scope>
<version>1.18.26</version>
</dependency>
</dependencies>
<groupId>com.miti99</groupId>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>com.miti99</groupId>
<artifactId>learn-netty</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<version>1.0-SNAPSHOT</version>
<version>${parent.version}</version>
</project>
@@ -1,15 +0,0 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Sample;
public final class Color {
private Color() { }
public static final byte Red = 0;
public static final byte Green = 1;
public static final byte Blue = 2;
public static final String[] names = { "Red", "Green", "Blue", };
public static String name(int e) { return names[e]; }
}
@@ -1,14 +0,0 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Sample;
public final class Equipment {
private Equipment() { }
public static final byte NONE = 0;
public static final byte Weapon = 1;
public static final String[] names = { "NONE", "Weapon", };
public static String name(int e) { return names[e]; }
}
@@ -1,76 +0,0 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Sample;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class Monster extends Table {
public static void ValidateVersion() { Constants.FLATBUFFERS_1_12_0(); }
public static Monster getRootAsMonster(ByteBuffer _bb) { return getRootAsMonster(_bb, new Monster()); }
public static Monster getRootAsMonster(ByteBuffer _bb, Monster obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public Monster __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public MyGame.Sample.Vec3 pos() { return pos(new MyGame.Sample.Vec3()); }
public MyGame.Sample.Vec3 pos(MyGame.Sample.Vec3 obj) { int o = __offset(4); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; }
public short mana() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 150; }
public short hp() { int o = __offset(8); return o != 0 ? bb.getShort(o + bb_pos) : 100; }
public String name() { int o = __offset(10); return o != 0 ? __string(o + bb_pos) : null; }
public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(10, 1); }
public ByteBuffer nameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 10, 1); }
public int inventory(int j) { int o = __offset(14); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
public int inventoryLength() { int o = __offset(14); return o != 0 ? __vector_len(o) : 0; }
public ByteVector inventoryVector() { return inventoryVector(new ByteVector()); }
public ByteVector inventoryVector(ByteVector obj) { int o = __offset(14); return o != 0 ? obj.__assign(__vector(o), bb) : null; }
public ByteBuffer inventoryAsByteBuffer() { return __vector_as_bytebuffer(14, 1); }
public ByteBuffer inventoryInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 14, 1); }
public byte color() { int o = __offset(16); return o != 0 ? bb.get(o + bb_pos) : 2; }
public MyGame.Sample.Weapon weapons(int j) { return weapons(new MyGame.Sample.Weapon(), j); }
public MyGame.Sample.Weapon weapons(MyGame.Sample.Weapon obj, int j) { int o = __offset(18); return o != 0 ? obj.__assign(__indirect(__vector(o) + j * 4), bb) : null; }
public int weaponsLength() { int o = __offset(18); return o != 0 ? __vector_len(o) : 0; }
public MyGame.Sample.Weapon.Vector weaponsVector() { return weaponsVector(new MyGame.Sample.Weapon.Vector()); }
public MyGame.Sample.Weapon.Vector weaponsVector(MyGame.Sample.Weapon.Vector obj) { int o = __offset(18); return o != 0 ? obj.__assign(__vector(o), 4, bb) : null; }
public byte equippedType() { int o = __offset(20); return o != 0 ? bb.get(o + bb_pos) : 0; }
public Table equipped(Table obj) { int o = __offset(22); return o != 0 ? __union(obj, o + bb_pos) : null; }
public MyGame.Sample.Vec3 path(int j) { return path(new MyGame.Sample.Vec3(), j); }
public MyGame.Sample.Vec3 path(MyGame.Sample.Vec3 obj, int j) { int o = __offset(24); return o != 0 ? obj.__assign(__vector(o) + j * 12, bb) : null; }
public int pathLength() { int o = __offset(24); return o != 0 ? __vector_len(o) : 0; }
public MyGame.Sample.Vec3.Vector pathVector() { return pathVector(new MyGame.Sample.Vec3.Vector()); }
public MyGame.Sample.Vec3.Vector pathVector(MyGame.Sample.Vec3.Vector obj) { int o = __offset(24); return o != 0 ? obj.__assign(__vector(o), 12, bb) : null; }
public static void startMonster(FlatBufferBuilder builder) { builder.startTable(11); }
public static void addPos(FlatBufferBuilder builder, int posOffset) { builder.addStruct(0, posOffset, 0); }
public static void addMana(FlatBufferBuilder builder, short mana) { builder.addShort(1, mana, 150); }
public static void addHp(FlatBufferBuilder builder, short hp) { builder.addShort(2, hp, 100); }
public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(3, nameOffset, 0); }
public static void addInventory(FlatBufferBuilder builder, int inventoryOffset) { builder.addOffset(5, inventoryOffset, 0); }
public static int createInventoryVector(FlatBufferBuilder builder, byte[] data) { return builder.createByteVector(data); }
public static int createInventoryVector(FlatBufferBuilder builder, ByteBuffer data) { return builder.createByteVector(data); }
public static void startInventoryVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); }
public static void addColor(FlatBufferBuilder builder, byte color) { builder.addByte(6, color, 2); }
public static void addWeapons(FlatBufferBuilder builder, int weaponsOffset) { builder.addOffset(7, weaponsOffset, 0); }
public static int createWeaponsVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); }
public static void startWeaponsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
public static void addEquippedType(FlatBufferBuilder builder, byte equippedType) { builder.addByte(8, equippedType, 0); }
public static void addEquipped(FlatBufferBuilder builder, int equippedOffset) { builder.addOffset(9, equippedOffset, 0); }
public static void addPath(FlatBufferBuilder builder, int pathOffset) { builder.addOffset(10, pathOffset, 0); }
public static void startPathVector(FlatBufferBuilder builder, int numElems) { builder.startVector(12, numElems, 4); }
public static int endMonster(FlatBufferBuilder builder) {
int o = builder.endTable();
return o;
}
public static void finishMonsterBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset); }
public static void finishSizePrefixedMonsterBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset); }
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public Monster get(int j) { return get(new Monster(), j); }
public Monster get(Monster obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
}
}
@@ -1,34 +0,0 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Sample;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class Vec3 extends Struct {
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public Vec3 __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public float x() { return bb.getFloat(bb_pos + 0); }
public float y() { return bb.getFloat(bb_pos + 4); }
public float z() { return bb.getFloat(bb_pos + 8); }
public static int createVec3(FlatBufferBuilder builder, float x, float y, float z) {
builder.prep(4, 12);
builder.putFloat(z);
builder.putFloat(y);
builder.putFloat(x);
return builder.offset();
}
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public Vec3 get(int j) { return get(new Vec3(), j); }
public Vec3 get(Vec3 obj, int j) { return obj.__assign(__element(j), bb); }
}
}
@@ -1,47 +0,0 @@
// automatically generated by the FlatBuffers compiler, do not modify
package MyGame.Sample;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class Weapon extends Table {
public static void ValidateVersion() { Constants.FLATBUFFERS_1_12_0(); }
public static Weapon getRootAsWeapon(ByteBuffer _bb) { return getRootAsWeapon(_bb, new Weapon()); }
public static Weapon getRootAsWeapon(ByteBuffer _bb, Weapon obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public Weapon __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public String name() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }
public ByteBuffer nameAsByteBuffer() { return __vector_as_bytebuffer(4, 1); }
public ByteBuffer nameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 4, 1); }
public short damage() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) : 0; }
public static int createWeapon(FlatBufferBuilder builder,
int nameOffset,
short damage) {
builder.startTable(2);
Weapon.addName(builder, nameOffset);
Weapon.addDamage(builder, damage);
return Weapon.endWeapon(builder);
}
public static void startWeapon(FlatBufferBuilder builder) { builder.startTable(2); }
public static void addName(FlatBufferBuilder builder, int nameOffset) { builder.addOffset(0, nameOffset, 0); }
public static void addDamage(FlatBufferBuilder builder, short damage) { builder.addShort(1, damage, 0); }
public static int endWeapon(FlatBufferBuilder builder) {
int o = builder.endTable();
return o;
}
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public Weapon get(int j) { return get(new Weapon(), j); }
public Weapon get(Weapon obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
}
}
@@ -27,29 +27,33 @@ public class Main {
var childGroup = Epoll.isAvailable() ? new EpollEventLoopGroup() : new NioEventLoopGroup();
val serverBootstrap = new ServerBootstrap();
serverBootstrap.group(parentGroup, childGroup).channel(
NioServerSocketChannel.class
).handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(new ChannelInitializer<>() {
@Override
protected void initChannel(Channel channel) {
channel.pipeline()
.addLast(new LoggingHandler(LogLevel.DEBUG))
.addLast(new HttpServerCodec())
.addLast(new HttpObjectAggregator(65536))
.addLast(new WebSocketServerCompressionHandler())
.addLast(new WebSocketServerProtocolHandler("/fbs", null, true))
.addLast(new WebsocketDecoder())
.addLast(new WebsocketHandler());
}
})
serverBootstrap
.group(parentGroup, childGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.DEBUG))
.childHandler(
new ChannelInitializer<>() {
@Override
protected void initChannel(Channel channel) {
channel.pipeline()
channel
.pipeline()
.addLast(new LoggingHandler(LogLevel.DEBUG))
.addLast(new HttpServerCodec())
.addLast(new HttpObjectAggregator(65536))
.addLast(new WebSocketServerCompressionHandler())
.addLast(new WebSocketServerProtocolHandler("/fbs", null, true))
.addLast(new WebsocketDecoder())
.addLast(new WebsocketHandler());
}
})
.childHandler(
new ChannelInitializer<>() {
@Override
protected void initChannel(Channel channel) {
channel
.pipeline()
.addLast(new LoggingHandler(LogLevel.INFO))
.addLast(new HttpServerCodec())
.addLast(new HttpObjectAggregator(65536))
@@ -57,8 +61,7 @@ public class Main {
.addLast(new WebSocketServerProtocolHandler("/json", null, true))
.addLast(new WebsocketHandler());
}
}
);
});
try {
val channelFuture = serverBootstrap.bind(1202).sync();
@@ -0,0 +1,24 @@
package com.miti99.netty;
import com.miti99.netty.json.response.BaseJsonResponse;
import io.netty.channel.Channel;
import java.nio.ByteBuffer;
public class NetworkUtil {
public static void sendJsonResponse(Channel channel, BaseJsonResponse response) {
sendTextResponse(channel, response.toJson());
}
public static void sendTextResponse(Channel channel, String response) {
channel.writeAndFlush(response);
}
public static void sendBinaryResponse(Channel channel, ByteBuffer byteBuffer) {
channel.writeAndFlush(byteBuffer);
}
public static void sendBinaryResponse(Channel channel, byte[] bytes) {
channel.writeAndFlush(bytes);
}
}
@@ -1,6 +1,6 @@
package com.miti99.netty;
import MyGame.Sample.Monster;
import com.miti99.netty.fbs.Packet;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
@@ -14,8 +14,10 @@ import lombok.extern.slf4j.Slf4j;
public class WebsocketDecoder extends MessageToMessageDecoder<BinaryWebSocketFrame> {
@Override
protected void decode(ChannelHandlerContext channelHandlerContext,
BinaryWebSocketFrame binaryWebSocketFrame, List<Object> list) {
protected void decode(
ChannelHandlerContext channelHandlerContext,
BinaryWebSocketFrame binaryWebSocketFrame,
List<Object> list) {
var byteBuf = binaryWebSocketFrame.content();
byteBuf.retain();
@@ -25,8 +27,8 @@ public class WebsocketDecoder extends MessageToMessageDecoder<BinaryWebSocketFra
var byteBuffer = ByteBuffer.wrap(bytes);
var monster = Monster.getRootAsMonster(byteBuffer);
var packet = Packet.getRootAsPacket(byteBuffer);
list.add(monster);
list.add(packet);
}
}
@@ -1,10 +1,15 @@
package com.miti99.netty;
import MyGame.Sample.Equipment;
import MyGame.Sample.Monster;
import MyGame.Sample.Weapon;
import com.google.flatbuffers.FlatBufferBuilder;
import com.google.gson.Gson;
import com.miti99.netty.fbs.LoginRequest;
import com.miti99.netty.fbs.LoginResponse;import com.miti99.netty.fbs.Packet;
import com.miti99.netty.fbs.PacketData;
import com.miti99.netty.fbs.Player;
import com.miti99.netty.fbs.Vec2;
import com.miti99.netty.json.request.BaseJsonRequest;
import com.miti99.netty.json.request.LoginJsonRequest;
import com.miti99.netty.json.response.LoginJsonResponse;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
@@ -17,44 +22,48 @@ public class WebsocketHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof Monster monster) {
var hp = monster.hp();
var mana = monster.mana();
var name = monster.name();
if (msg instanceof Packet packet) {
if (packet.dataType() == PacketData.LoginRequest) {
var request = new LoginRequest();
packet.data(request);
var builder = new FlatBufferBuilder(0);
log.info("hp : {}, mana : {}, name : {}", hp, mana, name);
var usernameOffset = builder.createString("user_" + request.userId());
Player.startPlayer(builder);
Player.addUserId(builder, request.userId());
Player.addUserName(builder, usernameOffset);
Player.addPosition(builder, Vec2.createVec2(builder, 12, 2));
Player.addRotation(builder, 12.02);
Player.addTeam(builder, 1999);
var playerOffset = Player.endPlayer(builder);
var pos = monster.pos();
var x = pos.x();
var y = pos.y();
var z = pos.z();
var responseOffset = LoginResponse.createLoginResponse(builder, playerOffset);
log.info("x : {}, y : {}, z : {}", x, y, z);
Packet.startPacket(builder);
Packet.addDataType(builder, PacketData.LoginResponse);
Packet.addData(builder, responseOffset);
var packetOffset = Packet.endPacket(builder);
builder.finish(packetOffset);
var invLength = monster.inventoryLength();
var thirdItem = monster.inventory(2);
var weaponsLength = monster.weaponsLength();
var secondWeaponName = monster.weapons(1).name();
var secondWeaponDamage = monster.weapons(1).damage();
log.info(
"invLength : {}, thirdItem : {}, weaponsLength : {}, secondWeaponName : {}, secondWeaponDamage : {}",
invLength, thirdItem, weaponsLength, secondWeaponName, secondWeaponDamage);
var unionType = monster.equippedType();
if (unionType == Equipment.Weapon) {
var weapon = (Weapon) monster.equipped(new Weapon());
var weaponName = weapon.name(); // "Axe"
var weaponDamage = weapon.damage(); // 5
log.info("weaponName : {}, weaponDamage : {}", weaponName, weaponDamage);
var bytes = builder.dataBuffer();
NetworkUtil.sendBinaryResponse(ctx.channel(), bytes);
} else {
log.warn("unknown packet data type: {}", packet.dataType());
}
} else if (msg instanceof TextWebSocketFrame textWebSocketFrame) {
log.info(textWebSocketFrame.text());
}else {
try {
BaseJsonRequest request = BaseJsonRequest.fromJson(textWebSocketFrame.text());
if (request instanceof LoginJsonRequest loginRequest) {
var response =
new LoginJsonResponse(loginRequest.getUserId(), "user_" + loginRequest.getUserId());
NetworkUtil.sendJsonResponse(ctx.channel(), response);
} else {
NetworkUtil.sendTextResponse(ctx.channel(), new Gson().toJson(request));
}
} catch (Exception e) {
log.error("can not parse text message: {}", textWebSocketFrame.text(), e);
}
} else {
ctx.fireChannelRead(msg);
}
}
@@ -0,0 +1,41 @@
// automatically generated by the FlatBuffers compiler, do not modify
package com.miti99.netty.fbs;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class LoginRequest extends Table {
public static void ValidateVersion() { Constants.FLATBUFFERS_1_12_0(); }
public static LoginRequest getRootAsLoginRequest(ByteBuffer _bb) { return getRootAsLoginRequest(_bb, new LoginRequest()); }
public static LoginRequest getRootAsLoginRequest(ByteBuffer _bb, LoginRequest obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public LoginRequest __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public int userId() { int o = __offset(4); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
public static int createLoginRequest(FlatBufferBuilder builder,
int userId) {
builder.startTable(1);
LoginRequest.addUserId(builder, userId);
return LoginRequest.endLoginRequest(builder);
}
public static void startLoginRequest(FlatBufferBuilder builder) { builder.startTable(1); }
public static void addUserId(FlatBufferBuilder builder, int userId) { builder.addInt(0, userId, 0); }
public static int endLoginRequest(FlatBufferBuilder builder) {
int o = builder.endTable();
return o;
}
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public LoginRequest get(int j) { return get(new LoginRequest(), j); }
public LoginRequest get(LoginRequest obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
}
}
@@ -0,0 +1,42 @@
// automatically generated by the FlatBuffers compiler, do not modify
package com.miti99.netty.fbs;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class LoginResponse extends Table {
public static void ValidateVersion() { Constants.FLATBUFFERS_1_12_0(); }
public static LoginResponse getRootAsLoginResponse(ByteBuffer _bb) { return getRootAsLoginResponse(_bb, new LoginResponse()); }
public static LoginResponse getRootAsLoginResponse(ByteBuffer _bb, LoginResponse obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public LoginResponse __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public com.miti99.netty.fbs.Player player() { return player(new com.miti99.netty.fbs.Player()); }
public com.miti99.netty.fbs.Player player(com.miti99.netty.fbs.Player obj) { int o = __offset(4); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
public static int createLoginResponse(FlatBufferBuilder builder,
int playerOffset) {
builder.startTable(1);
LoginResponse.addPlayer(builder, playerOffset);
return LoginResponse.endLoginResponse(builder);
}
public static void startLoginResponse(FlatBufferBuilder builder) { builder.startTable(1); }
public static void addPlayer(FlatBufferBuilder builder, int playerOffset) { builder.addOffset(0, playerOffset, 0); }
public static int endLoginResponse(FlatBufferBuilder builder) {
int o = builder.endTable();
return o;
}
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public LoginResponse get(int j) { return get(new LoginResponse(), j); }
public LoginResponse get(LoginResponse obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
}
}
@@ -0,0 +1,47 @@
// automatically generated by the FlatBuffers compiler, do not modify
package com.miti99.netty.fbs;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class Packet extends Table {
public static void ValidateVersion() { Constants.FLATBUFFERS_1_12_0(); }
public static Packet getRootAsPacket(ByteBuffer _bb) { return getRootAsPacket(_bb, new Packet()); }
public static Packet getRootAsPacket(ByteBuffer _bb, Packet obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public Packet __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public byte dataType() { int o = __offset(4); return o != 0 ? bb.get(o + bb_pos) : 0; }
public Table data(Table obj) { int o = __offset(6); return o != 0 ? __union(obj, o + bb_pos) : null; }
public static int createPacket(FlatBufferBuilder builder,
byte data_type,
int dataOffset) {
builder.startTable(2);
Packet.addData(builder, dataOffset);
Packet.addDataType(builder, data_type);
return Packet.endPacket(builder);
}
public static void startPacket(FlatBufferBuilder builder) { builder.startTable(2); }
public static void addDataType(FlatBufferBuilder builder, byte dataType) { builder.addByte(0, dataType, 0); }
public static void addData(FlatBufferBuilder builder, int dataOffset) { builder.addOffset(1, dataOffset, 0); }
public static int endPacket(FlatBufferBuilder builder) {
int o = builder.endTable();
return o;
}
public static void finishPacketBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset); }
public static void finishSizePrefixedPacketBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset); }
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public Packet get(int j) { return get(new Packet(), j); }
public Packet get(Packet obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
}
}
@@ -0,0 +1,15 @@
// automatically generated by the FlatBuffers compiler, do not modify
package com.miti99.netty.fbs;
public final class PacketData {
private PacketData() { }
public static final byte NONE = 0;
public static final byte LoginRequest = 1;
public static final byte LoginResponse = 2;
public static final String[] names = { "NONE", "LoginRequest", "LoginResponse", };
public static String name(int e) { return names[e]; }
}
@@ -0,0 +1,45 @@
// automatically generated by the FlatBuffers compiler, do not modify
package com.miti99.netty.fbs;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class Player extends Table {
public static void ValidateVersion() { Constants.FLATBUFFERS_1_12_0(); }
public static Player getRootAsPlayer(ByteBuffer _bb) { return getRootAsPlayer(_bb, new Player()); }
public static Player getRootAsPlayer(ByteBuffer _bb, Player obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public Player __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public int userId() { int o = __offset(4); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
public String userName() { int o = __offset(6); return o != 0 ? __string(o + bb_pos) : null; }
public ByteBuffer userNameAsByteBuffer() { return __vector_as_bytebuffer(6, 1); }
public ByteBuffer userNameInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 6, 1); }
public com.miti99.netty.fbs.Vec2 position() { return position(new com.miti99.netty.fbs.Vec2()); }
public com.miti99.netty.fbs.Vec2 position(com.miti99.netty.fbs.Vec2 obj) { int o = __offset(8); return o != 0 ? obj.__assign(o + bb_pos, bb) : null; }
public double rotation() { int o = __offset(10); return o != 0 ? bb.getDouble(o + bb_pos) : 0.0; }
public int team() { int o = __offset(12); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
public static void startPlayer(FlatBufferBuilder builder) { builder.startTable(5); }
public static void addUserId(FlatBufferBuilder builder, int userId) { builder.addInt(0, userId, 0); }
public static void addUserName(FlatBufferBuilder builder, int userNameOffset) { builder.addOffset(1, userNameOffset, 0); }
public static void addPosition(FlatBufferBuilder builder, int positionOffset) { builder.addStruct(2, positionOffset, 0); }
public static void addRotation(FlatBufferBuilder builder, double rotation) { builder.addDouble(3, rotation, 0.0); }
public static void addTeam(FlatBufferBuilder builder, int team) { builder.addInt(4, team, 0); }
public static int endPlayer(FlatBufferBuilder builder) {
int o = builder.endTable();
return o;
}
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public Player get(int j) { return get(new Player(), j); }
public Player get(Player obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
}
}
@@ -0,0 +1,32 @@
// automatically generated by the FlatBuffers compiler, do not modify
package com.miti99.netty.fbs;
import java.nio.*;
import java.lang.*;
import java.util.*;
import com.google.flatbuffers.*;
@SuppressWarnings("unused")
public final class Vec2 extends Struct {
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
public Vec2 __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
public double x() { return bb.getDouble(bb_pos + 0); }
public double y() { return bb.getDouble(bb_pos + 8); }
public static int createVec2(FlatBufferBuilder builder, double x, double y) {
builder.prep(8, 16);
builder.putDouble(y);
builder.putDouble(x);
return builder.offset();
}
public static final class Vector extends BaseVector {
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
public Vec2 get(int j) { return get(new Vec2(), j); }
public Vec2 get(Vec2 obj, int j) { return obj.__assign(__element(j), bb); }
}
}
@@ -0,0 +1,9 @@
package com.miti99.netty.json;
import lombok.Getter;
@Getter
public abstract class BaseJsonPacket {
private JsonPacketId id;
}
@@ -0,0 +1,21 @@
package com.miti99.netty.json;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public enum JsonPacketId {
PING,
LOGIN,
PLAYER_MOVE,
PLAYER_ATTACK,
MATCH_INFO,
;
public static JsonPacketId valueOf(int id) {
if (id < 0 || id >= values().length) {
log.error("No element matches {}", id);
throw new IllegalArgumentException("No element matches " + id);
}
return values()[id];
}
}
@@ -0,0 +1,20 @@
package com.miti99.netty.json;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import lombok.Getter;
public class JsonPacketIdDeserializer implements JsonDeserializer<JsonPacketId> {
@Getter private static final JsonPacketIdDeserializer instance = new JsonPacketIdDeserializer();
@Override
public JsonPacketId deserialize(
JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext)
throws JsonParseException {
return JsonPacketId.valueOf(jsonElement.getAsInt());
}
}
@@ -0,0 +1,19 @@
package com.miti99.netty.json.request;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.miti99.netty.json.BaseJsonPacket;
import com.miti99.netty.json.JsonPacketId;
import com.miti99.netty.json.JsonPacketIdDeserializer;
public abstract class BaseJsonRequest extends BaseJsonPacket {
private static final Gson gson =
new GsonBuilder()
.registerTypeAdapter(JsonPacketId.class, JsonPacketIdDeserializer.getInstance())
.create();
public static BaseJsonRequest fromJson(String data) {
return gson.fromJson(data, BaseJsonRequest.class);
}
}
@@ -0,0 +1,9 @@
package com.miti99.netty.json.request;
import lombok.Getter;
@Getter
public class LoginJsonRequest extends BaseJsonRequest {
int userId;
}
@@ -0,0 +1,12 @@
package com.miti99.netty.json.response;
import com.google.gson.Gson;
public abstract class BaseJsonResponse {
private static final Gson gson = new Gson();
public String toJson() {
return gson.toJson(this);
}
}
@@ -0,0 +1,10 @@
package com.miti99.netty.json.response;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class LoginJsonResponse extends BaseJsonResponse {
int userId;
String userName;
}
@@ -1,33 +0,0 @@
// Example IDL file for our monster's schema.
namespace MyGame.Sample;
enum Color:byte { Red = 0, Green, Blue = 2 }
union Equipment { Weapon } // Optionally add more tables.
struct Vec3 {
x:float;
y:float;
z:float;
}
table Monster {
pos:Vec3; // Struct.
mana:short = 150;
hp:short = 100;
name:string;
friendly:bool = false (deprecated);
inventory:[ubyte]; // Vector of scalars.
color:Color = Blue; // Enum.
weapons:[Weapon]; // Vector of tables.
equipped:Equipment; // Union.
path:[Vec3]; // Vector of structs.
}
table Weapon {
name:string;
damage:short;
}
root_type Monster;
@@ -0,0 +1,33 @@
namespace com.miti99.netty.fbs;
struct Vec2 {
x: double;
y: double;
}
table Player {
userId: int;
userName: string;
position: Vec2;
rotation: double;
team: int;
}
table LoginRequest {
userId: int;
}
table LoginResponse {
player: Player;
}
union PacketData {
LoginRequest,
LoginResponse
}
table Packet {
data: PacketData;
}
root_type Packet;