forked from cloudwu/skynet
-
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.
- Loading branch information
Showing
5 changed files
with
150 additions
and
7 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,69 @@ | ||
local stm = require "stm" | ||
local sprotoloader = require "sprotoloader" | ||
local sproto = require "sproto" | ||
local setmetatable = setmetatable | ||
|
||
local sharemap = {} | ||
|
||
function sharemap.register(protofile) | ||
-- use global slot 0 for type define | ||
sprotoloader.register(protofile, 0) | ||
end | ||
|
||
local sprotoobj | ||
local function loadsp() | ||
if sprotoobj == nil then | ||
sprotoobj = sprotoloader.load(0) | ||
end | ||
return sprotoobj | ||
end | ||
|
||
function sharemap:commit() | ||
self.__obj(sprotoobj:encode(self.__typename, self.__data)) | ||
end | ||
|
||
function sharemap:copy() | ||
return stm.copy(self.__obj) | ||
end | ||
|
||
function sharemap.writer(typename, obj) | ||
local sp = loadsp() | ||
obj = obj or {} | ||
local stmobj = stm.new(sp:encode(typename,obj)) | ||
obj.__typename = typename | ||
obj.__obj = stmobj | ||
obj.__data = obj | ||
obj.commit = sharemap.commit | ||
obj.copy = sharemap.copy | ||
return setmetatable(obj, { __index = obj.__data, __newindex = obj.__data }) | ||
end | ||
|
||
local function decode(msg, sz, self) | ||
local data = self.__data | ||
for k in pairs(data) do | ||
data[k] = nil | ||
end | ||
return sprotoobj:decode(self.__typename, msg, sz, data) | ||
end | ||
|
||
function sharemap:update() | ||
return self.__obj(decode, self) | ||
end | ||
|
||
function sharemap.reader(typename, stmcpy) | ||
local sp = loadsp() | ||
local stmobj = stm.newcopy(stmcpy) | ||
local _, data = stmobj(function(msg, sz) | ||
return sp:decode(typename, msg, sz) | ||
end) | ||
|
||
local obj = { | ||
__typename = typename, | ||
__obj = stmobj, | ||
__data = data, | ||
update = sharemap.update, | ||
} | ||
return setmetatable(obj, { __index = obj.__data, __newindex = error }) | ||
end | ||
|
||
return sharemap |
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,5 @@ | ||
.foobar { | ||
x 0 : integer | ||
y 1 : integer | ||
s 2 : string | ||
} |
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,48 @@ | ||
local skynet = require "skynet" | ||
local sharemap = require "sharemap" | ||
|
||
local mode = ... | ||
|
||
if mode == "slave" then | ||
--slave | ||
|
||
local function dump(reader) | ||
reader:update() | ||
print("x=", reader.x) | ||
print("y=", reader.y) | ||
print("s=", reader.s) | ||
end | ||
|
||
skynet.start(function() | ||
local reader | ||
skynet.dispatch("lua", function(_,_,cmd,...) | ||
if cmd == "init" then | ||
reader = sharemap.reader(...) | ||
else | ||
assert(cmd == "ping") | ||
dump(reader) | ||
end | ||
skynet.ret() | ||
end) | ||
end) | ||
|
||
else | ||
-- master | ||
skynet.start(function() | ||
-- register share type schema | ||
sharemap.register("./test/sharemap.sp") | ||
local slave = skynet.newservice(SERVICE_NAME, "slave") | ||
local writer = sharemap.writer("foobar", { x=0,y=0,s="hello" }) | ||
skynet.call(slave, "lua", "init", "foobar", writer:copy()) | ||
writer.x = 1 | ||
writer:commit() | ||
skynet.call(slave, "lua", "ping") | ||
writer.y = 2 | ||
writer:commit() | ||
skynet.call(slave, "lua", "ping") | ||
writer.s = "world" | ||
writer:commit() | ||
skynet.call(slave, "lua", "ping") | ||
end) | ||
|
||
end |