-
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.
perf[protocol]: all protocol files are generated in a single protocol…
… file
- Loading branch information
1 parent
1ca4db6
commit 2635369
Showing
6 changed files
with
165 additions
and
8 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
42 changes: 42 additions & 0 deletions
42
protocol/src/main/resources/lua-one/ProtocolManagerTemplate.lua
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,42 @@ | ||
local protocols = {} | ||
|
||
local ProtocolManager = {} | ||
|
||
-- table扩展方法,map的大小 | ||
function table.mapSize(map) | ||
local size = 0 | ||
for _,_ in pairs(map) do | ||
size = size + 1 | ||
end | ||
return size | ||
end | ||
|
||
function ProtocolManager.getProtocol(protocolId) | ||
local protocol = protocols[protocolId] | ||
if protocol == nil then | ||
error("[protocolId:" + protocolId + "]协议不存在") | ||
end | ||
return protocol | ||
end | ||
|
||
function ProtocolManager.write(buffer, packet) | ||
local protocolId = packet:protocolId() | ||
-- 写入协议号 | ||
buffer:writeShort(protocolId) | ||
-- 写入包体 | ||
ProtocolManager.getProtocol(protocolId):write(buffer, packet) | ||
end | ||
|
||
function ProtocolManager.read(buffer) | ||
local protocolId = buffer:readShort() | ||
return ProtocolManager.getProtocol(protocolId):read(buffer) | ||
end | ||
|
||
function initProtocol() | ||
{} | ||
{} | ||
{} | ||
end | ||
|
||
ProtocolManager.initProtocol = initProtocol | ||
return ProtocolManager |
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,10 @@ | ||
{} | ||
local {} = {} | ||
function {}:new() | ||
local obj = { | ||
{} | ||
} | ||
setmetatable(obj, self) | ||
self.__index = self | ||
return obj | ||
end |
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,35 @@ | ||
function Protocols.{}:protocolId() | ||
return {} | ||
end | ||
|
||
function Protocols.{}:protocolName() | ||
return {} | ||
end | ||
|
||
function Protocols.{}:__tostring() | ||
local jsonTemplate = "{}" | ||
local result = string.format(jsonTemplate, {}) | ||
return result | ||
end | ||
|
||
function Protocols.{}:write(buffer, packet) | ||
if packet == nil then | ||
buffer:writeInt(0) | ||
return | ||
end | ||
{} | ||
end | ||
|
||
function Protocols.{}:read(buffer) | ||
local length = buffer:readInt() | ||
if length == 0 then | ||
return nil | ||
end | ||
local beforeReadIndex = buffer:getReadOffset() | ||
local packet = {}:new() | ||
{} | ||
if length > 0 then | ||
buffer:setReadOffset(beforeReadIndex + length) | ||
end | ||
return packet | ||
end |
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