mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
Compare commits
200 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
25a8b3a179 | ||
|
|
c323adb8d8 | ||
|
|
7a4c7bc8a2 | ||
|
|
a56b6aa425 | ||
|
|
eaaf57ee0e | ||
|
|
7babdd3a97 | ||
|
|
19e6462376 | ||
|
|
861f53858f | ||
|
|
db25d1acc6 | ||
|
|
0188e263dd | ||
|
|
18f20425a0 | ||
|
|
551d5048c4 | ||
|
|
d2ad63da81 | ||
|
|
932b2943dc | ||
|
|
362d6822bf | ||
|
|
7bdbdcd054 | ||
|
|
cb7058b563 | ||
|
|
27ac1642e4 | ||
|
|
d9d2a7d2ce | ||
|
|
39ff6eb2a6 | ||
|
|
b933046fb9 | ||
|
|
e59b6b44a5 | ||
|
|
a8b80cd73e | ||
|
|
d41c9db019 | ||
|
|
e1ca48603e | ||
|
|
8e4e9ed5c1 | ||
|
|
a0b536718c | ||
|
|
6bafd05c65 | ||
|
|
7d2d107518 | ||
|
|
75b2feff73 | ||
|
|
9421435ac5 | ||
|
|
1181730302 | ||
|
|
acc175e821 | ||
|
|
0ae1fcf7cd | ||
|
|
37a46607ce | ||
|
|
691bb28163 | ||
|
|
dbf492b761 | ||
|
|
43719de101 | ||
|
|
a8a683b48e | ||
|
|
4cdf034f15 | ||
|
|
9feaf15b3d | ||
|
|
ad58ed8a26 | ||
|
|
0d1c3a64d1 | ||
|
|
e963ee6aba | ||
|
|
68ddeab8fa | ||
|
|
3611bfe1af | ||
|
|
ec50e02777 | ||
|
|
3bc7304609 | ||
|
|
0cdd71c0c2 | ||
|
|
e1674f04c3 | ||
|
|
0dba8ed385 | ||
|
|
2e35f405e7 | ||
|
|
6813fd9ef7 | ||
|
|
04cb72d1a8 | ||
|
|
4284cfc372 | ||
|
|
e8397348dd | ||
|
|
7b5e62b896 | ||
|
|
123d942819 | ||
|
|
a090899bce | ||
|
|
68f4f45168 | ||
|
|
3a5de32ad0 | ||
|
|
c3e758dc87 | ||
|
|
54e6d03d36 | ||
|
|
83289b7612 | ||
|
|
b7c12846a0 | ||
|
|
b5e10b8f9f | ||
|
|
0855bf30d9 | ||
|
|
d05859b766 | ||
|
|
fe5c73b1e8 | ||
|
|
35775685c3 | ||
|
|
e60fb1d722 | ||
|
|
4e4dacbb4d | ||
|
|
8f8b844bde | ||
|
|
54f4d94ba2 | ||
|
|
4967dc2fce | ||
|
|
ece89a1b49 | ||
|
|
f874fdc618 | ||
|
|
711c04e6a9 | ||
|
|
1e0189962b | ||
|
|
f4d865a882 | ||
|
|
60b0976253 | ||
|
|
2b9efcec3f | ||
|
|
d343f3b374 | ||
|
|
ceeb9912bf | ||
|
|
be1db23ea3 | ||
|
|
36cab8e060 | ||
|
|
b1d6f7fd88 | ||
|
|
e0d8b00226 | ||
|
|
1cbe639186 | ||
|
|
f849c522bb | ||
|
|
3cadd29796 | ||
|
|
db952dc658 | ||
|
|
813e6c764d | ||
|
|
79b6b80676 | ||
|
|
d30a206a15 | ||
|
|
cc2cffacb7 | ||
|
|
d8b0e977f3 | ||
|
|
2128a82571 | ||
|
|
4e2dc64f8f | ||
|
|
d910686cb8 | ||
|
|
9937081854 | ||
|
|
7137850eb2 | ||
|
|
c3eb5cd202 | ||
|
|
1a44bfb09a | ||
|
|
e4197daa54 | ||
|
|
d0468a39f4 | ||
|
|
ad9898a209 | ||
|
|
33e51a4de9 | ||
|
|
4ccb677e84 | ||
|
|
942c2d3a67 | ||
|
|
44bef99f64 | ||
|
|
078ac2bcd2 | ||
|
|
c7887cceb7 | ||
|
|
4fbd433199 | ||
|
|
87d276caf6 | ||
|
|
0f64e909fa | ||
|
|
4bc2e800fd | ||
|
|
bf6501f2ee | ||
|
|
f92fc153e4 | ||
|
|
c10e5412c0 | ||
|
|
9477cc8171 | ||
|
|
7f3ba76a41 | ||
|
|
4545ef149b | ||
|
|
c130a93a91 | ||
|
|
cd0a1a495e | ||
|
|
3597bb6d75 | ||
|
|
39c5e13d1a | ||
|
|
e1a72bd820 | ||
|
|
f77482024e | ||
|
|
866e59811e | ||
|
|
f3b1f5be51 | ||
|
|
0fcab96431 | ||
|
|
e765d3c305 | ||
|
|
8005b55ef5 | ||
|
|
1ce90b7d17 | ||
|
|
37ea7a025c | ||
|
|
88700c2c90 | ||
|
|
90cd967333 | ||
|
|
1fbee1ac0b | ||
|
|
d6a4c5cc59 | ||
|
|
66a0a6e1db | ||
|
|
57f37a55ba | ||
|
|
84e8abc09c | ||
|
|
dbfa17be09 | ||
|
|
19729576f6 | ||
|
|
a5df196e92 | ||
|
|
a2ad158dbc | ||
|
|
ff155c623e | ||
|
|
9fe6a0a954 | ||
|
|
c2ed2f8603 | ||
|
|
a67e561a64 | ||
|
|
ef2da264f1 | ||
|
|
b397c70c34 | ||
|
|
02f2f88b6d | ||
|
|
29917efad7 | ||
|
|
9715ddd2c5 | ||
|
|
fe2b68686d | ||
|
|
7dff50d21d | ||
|
|
8d8cb17e0a | ||
|
|
4e00fce360 | ||
|
|
2d19b6cac1 | ||
|
|
18c8a9d20c | ||
|
|
82c7cbc2f0 | ||
|
|
915c28c522 | ||
|
|
a700abcdb3 | ||
|
|
0c4e2450a7 | ||
|
|
6a6d9db54d | ||
|
|
8c886023c9 | ||
|
|
465584dca2 | ||
|
|
f3f46923d4 | ||
|
|
9033c5cdd9 | ||
|
|
5d0da82d9a | ||
|
|
091f91ede3 | ||
|
|
4a92dfaa65 | ||
|
|
1c2654e117 | ||
|
|
5ce65055d2 | ||
|
|
fa6191d9ed | ||
|
|
12f46d21fe | ||
|
|
d16159c166 | ||
|
|
a4a21bd793 | ||
|
|
f161dbb0ea | ||
|
|
61e3904de8 | ||
|
|
4d536f7f86 | ||
|
|
f6e67b7493 | ||
|
|
be948c6675 | ||
|
|
0a40d9c5c6 | ||
|
|
34771ff7c7 | ||
|
|
c17ec76e94 | ||
|
|
45df4f2e71 | ||
|
|
4f10610e26 | ||
|
|
904308bc72 | ||
|
|
bad9a5fc54 | ||
|
|
bd3aa3f3c5 | ||
|
|
2eb3174b0b | ||
|
|
518017bd34 | ||
|
|
8840c0fe3e | ||
|
|
ddd0b13013 | ||
|
|
ba27feee35 | ||
|
|
93cb091894 | ||
|
|
2d56a2215a |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,9 +1,10 @@
|
|||||||
*.o
|
*.o
|
||||||
*.a
|
*.a
|
||||||
skynet
|
/skynet
|
||||||
|
/skynet.pid
|
||||||
3rd/lua/lua
|
3rd/lua/lua
|
||||||
3rd/lua/luac
|
3rd/lua/luac
|
||||||
cservice
|
/cservice
|
||||||
luaclib
|
/luaclib
|
||||||
*.so
|
*.so
|
||||||
*.dSYM
|
*.dSYM
|
||||||
|
|||||||
100
HISTORY.md
Normal file
100
HISTORY.md
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
v0.5.0 (2014-7-28)
|
||||||
|
-----------
|
||||||
|
* skynet.exit will quit service immediately.
|
||||||
|
* Add snax.gateserver, snax.loginserver, snax.msgserver
|
||||||
|
* Simplify clientsocket lib
|
||||||
|
* mongo driver support replica set
|
||||||
|
* config file support read from ENV
|
||||||
|
* add simple httpd (see examples/simpleweb.lua)
|
||||||
|
|
||||||
|
v0.4.2 (2014-7-14)
|
||||||
|
-----------
|
||||||
|
* Bugfix : invalid negative socket id
|
||||||
|
* Add optional TCP_NODELAY support
|
||||||
|
* Add worker thread weight
|
||||||
|
* Add skynet.queue
|
||||||
|
* Bugfix: socketchannel
|
||||||
|
* cluster can throw error
|
||||||
|
* Add readline and writeline to clientsocket lib
|
||||||
|
* Add cluster.reload to reload config file
|
||||||
|
* Add datacenter.wait
|
||||||
|
|
||||||
|
v0.4.1 (2014-7-7)
|
||||||
|
-----------
|
||||||
|
* Add SERVICE_NAME in loader
|
||||||
|
* Throw error back when skynet.error
|
||||||
|
* Add skynet.task
|
||||||
|
* Bugfix for last version (harbor service bugs)
|
||||||
|
|
||||||
|
v0.4.0 (2014-6-30)
|
||||||
|
-----------
|
||||||
|
* Optimize redis driver `compose_message`.
|
||||||
|
* Add module skynet.harbor for monitor harbor connect/disconnect, see test/testharborlink.lua .
|
||||||
|
* cluster.open support cluster name.
|
||||||
|
* Add new api skynet.packstring , and skynet.unpack support lua string
|
||||||
|
* socket.listen support put port into address. (address:port)
|
||||||
|
* Redesign harbor/master/dummy, remove lots of C code and rewite in lua.
|
||||||
|
* Remove block connect api, queue sending message during connecting now.
|
||||||
|
* Add skynet.time()
|
||||||
|
|
||||||
|
v0.3.2 (2014-6-23)
|
||||||
|
----------
|
||||||
|
* Bugfix : cluster (double free).
|
||||||
|
* Add socket.header() to decode big-endian package header (and fix the bug in cluster).
|
||||||
|
|
||||||
|
v0.3.1 (2014-6-16)
|
||||||
|
-----------
|
||||||
|
* Bugfix: lua mongo driver . Hold reply string before decode bson data.
|
||||||
|
* More check in bson decoding.
|
||||||
|
* Use big-endian for encoding bson objectid.
|
||||||
|
|
||||||
|
v0.3.0 (2014-6-2)
|
||||||
|
-----------
|
||||||
|
* Add cluster support
|
||||||
|
* Add single node mode
|
||||||
|
* Add daemon mode
|
||||||
|
* Bugfix: update lua-bson (signed 32bit int bug / check string length)
|
||||||
|
* Optimize timer
|
||||||
|
* Simplify message queue and optimize message dispatch
|
||||||
|
* Use jemalloc release 3.6.0
|
||||||
|
|
||||||
|
v0.2.1 (2014-5-19)
|
||||||
|
-----------
|
||||||
|
* Bugfix: check all the events already read after socket close
|
||||||
|
* Bugfix: socket data in gate service
|
||||||
|
* Bugfix: boundary problem in harbor service
|
||||||
|
* Bugfix: stdin handle is 0
|
||||||
|
|
||||||
|
v0.2.0 (2014-5-12)
|
||||||
|
-----------
|
||||||
|
|
||||||
|
* Rewrite malloc hook , use `pthread_getspecific` instead of `__thread` to get current service handle.
|
||||||
|
* Optimize global unique service query, rewrite `service_mgr` .
|
||||||
|
* Add some snax api, snax.uniqueservice (etc.) , use independent protocol `PTYPE_SNAX` .
|
||||||
|
* Add bootstrap lua script , remove some code in C .
|
||||||
|
* Use a lua loader to load lua service code (and set the lua environment), remove some code in C.
|
||||||
|
* Support preload a file before each lua serivce start.
|
||||||
|
* Add datacenter serivce.
|
||||||
|
* Add multicast api.
|
||||||
|
* Remove skynet.blockcall , simplify the implement of message queue.
|
||||||
|
* When dropping message queue (at service exit) , dispatcher will post an error back to the source of each message.
|
||||||
|
* Remove skynet.watch , monitor is not necessary for watching skynet.call . so simplemonitor.lua is move to examples.
|
||||||
|
* Remove the limit of global queue size (64K actived service limit before).
|
||||||
|
* Refactoring `skynet_command`.
|
||||||
|
|
||||||
|
v0.1.1 (2014-4-28)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* Socket channel should clear request queue when reconnect.
|
||||||
|
* Fix the issue that socket close may block the coroutine.
|
||||||
|
* Fix the issue that jemalloc api may crash on macosx (disable jemalloc on macosx).
|
||||||
|
|
||||||
|
v0.1.0 (2014-4-23)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* First release version.
|
||||||
|
|
||||||
|
First public version (2012-8-1)
|
||||||
|
------------------
|
||||||
|
|
||||||
|
* Make skynet from a private project to public.
|
||||||
25
Makefile
25
Makefile
@@ -5,7 +5,7 @@ CSERVICE_PATH ?= cservice
|
|||||||
|
|
||||||
SKYNET_BUILD_PATH ?= .
|
SKYNET_BUILD_PATH ?= .
|
||||||
|
|
||||||
CFLAGS = -g -O2 -Wall -I$(LUA_INC) $(MYCFLAGS) $(SKYNET_DEFINES)
|
CFLAGS = -g -O2 -Wall -I$(LUA_INC) $(MYCFLAGS)
|
||||||
|
|
||||||
# lua
|
# lua
|
||||||
|
|
||||||
@@ -40,13 +40,15 @@ jemalloc : $(MALLOC_STATICLIB)
|
|||||||
|
|
||||||
# skynet
|
# skynet
|
||||||
|
|
||||||
CSERVICE = snlua logger gate master harbor
|
CSERVICE = snlua logger gate harbor
|
||||||
LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack cjson clientsocket memory profile
|
LUA_CLIB = skynet socketdriver int64 bson mongo md5 netpack \
|
||||||
|
cjson clientsocket memory profile multicast \
|
||||||
|
cluster crypt
|
||||||
|
|
||||||
SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \
|
SKYNET_SRC = skynet_main.c skynet_handle.c skynet_module.c skynet_mq.c \
|
||||||
skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \
|
skynet_server.c skynet_start.c skynet_timer.c skynet_error.c \
|
||||||
skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \
|
skynet_harbor.c skynet_env.c skynet_monitor.c skynet_socket.c socket_server.c \
|
||||||
malloc_hook.c
|
malloc_hook.c skynet_daemon.c
|
||||||
|
|
||||||
all : \
|
all : \
|
||||||
$(SKYNET_BUILD_PATH)/skynet \
|
$(SKYNET_BUILD_PATH)/skynet \
|
||||||
@@ -54,7 +56,7 @@ all : \
|
|||||||
$(foreach v, $(LUA_CLIB), $(LUA_CLIB_PATH)/$(v).so)
|
$(foreach v, $(LUA_CLIB), $(LUA_CLIB_PATH)/$(v).so)
|
||||||
|
|
||||||
$(SKYNET_BUILD_PATH)/skynet : $(foreach v, $(SKYNET_SRC), skynet-src/$(v)) $(LUA_LIB) $(MALLOC_STATICLIB)
|
$(SKYNET_BUILD_PATH)/skynet : $(foreach v, $(SKYNET_SRC), skynet-src/$(v)) $(LUA_LIB) $(MALLOC_STATICLIB)
|
||||||
$(CC) $(CFLAGS) -o $@ $^ -Iskynet-src -I$(JEMALLOC_INC) $(LDFLAGS) $(EXPORT) $(SKYNET_LIBS)
|
$(CC) $(CFLAGS) -o $@ $^ -Iskynet-src -I$(JEMALLOC_INC) $(LDFLAGS) $(EXPORT) $(SKYNET_LIBS) $(SKYNET_DEFINES)
|
||||||
|
|
||||||
$(LUA_CLIB_PATH) :
|
$(LUA_CLIB_PATH) :
|
||||||
mkdir $(LUA_CLIB_PATH)
|
mkdir $(LUA_CLIB_PATH)
|
||||||
@@ -64,7 +66,7 @@ $(CSERVICE_PATH) :
|
|||||||
|
|
||||||
define CSERVICE_TEMP
|
define CSERVICE_TEMP
|
||||||
$$(CSERVICE_PATH)/$(1).so : service-src/service_$(1).c | $$(CSERVICE_PATH)
|
$$(CSERVICE_PATH)/$(1).so : service-src/service_$(1).c | $$(CSERVICE_PATH)
|
||||||
$(CC) $$(CFLAGS) $$(SHARED) $$< -o $$@ -Iskynet-src
|
$$(CC) $$(CFLAGS) $$(SHARED) $$< -o $$@ -Iskynet-src
|
||||||
endef
|
endef
|
||||||
|
|
||||||
$(foreach v, $(CSERVICE), $(eval $(call CSERVICE_TEMP,$(v))))
|
$(foreach v, $(CSERVICE), $(eval $(call CSERVICE_TEMP,$(v))))
|
||||||
@@ -91,7 +93,7 @@ $(LUA_CLIB_PATH)/netpack.so : lualib-src/lua-netpack.c | $(LUA_CLIB_PATH)
|
|||||||
$(CC) $(CFLAGS) $(SHARED) $^ -Iskynet-src -o $@
|
$(CC) $(CFLAGS) $(SHARED) $^ -Iskynet-src -o $@
|
||||||
|
|
||||||
$(LUA_CLIB_PATH)/cjson.so : | $(LUA_CLIB_PATH)
|
$(LUA_CLIB_PATH)/cjson.so : | $(LUA_CLIB_PATH)
|
||||||
cd 3rd/lua-cjson && $(MAKE) LUA_INCLUDE_DIR=../../$(LUA_INC) CC=$(CC) CJSON_LDFLAGS="$(SHARED)" && cp cjson.so ../../$@
|
cd 3rd/lua-cjson && $(MAKE) LUA_INCLUDE_DIR=../../$(LUA_INC) CC=$(CC) CJSON_LDFLAGS="$(SHARED)" && cd ../.. && cp 3rd/lua-cjson/cjson.so $@
|
||||||
|
|
||||||
$(LUA_CLIB_PATH)/clientsocket.so : lualib-src/lua-clientsocket.c | $(LUA_CLIB_PATH)
|
$(LUA_CLIB_PATH)/clientsocket.so : lualib-src/lua-clientsocket.c | $(LUA_CLIB_PATH)
|
||||||
$(CC) $(CFLAGS) $(SHARED) $^ -o $@ -lpthread
|
$(CC) $(CFLAGS) $(SHARED) $^ -o $@ -lpthread
|
||||||
@@ -102,6 +104,15 @@ $(LUA_CLIB_PATH)/memory.so : lualib-src/lua-memory.c | $(LUA_CLIB_PATH)
|
|||||||
$(LUA_CLIB_PATH)/profile.so : lualib-src/lua-profile.c | $(LUA_CLIB_PATH)
|
$(LUA_CLIB_PATH)/profile.so : lualib-src/lua-profile.c | $(LUA_CLIB_PATH)
|
||||||
$(CC) $(CFLAGS) $(SHARED) $^ -o $@
|
$(CC) $(CFLAGS) $(SHARED) $^ -o $@
|
||||||
|
|
||||||
|
$(LUA_CLIB_PATH)/multicast.so : lualib-src/lua-multicast.c | $(LUA_CLIB_PATH)
|
||||||
|
$(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@
|
||||||
|
|
||||||
|
$(LUA_CLIB_PATH)/cluster.so : lualib-src/lua-cluster.c | $(LUA_CLIB_PATH)
|
||||||
|
$(CC) $(CFLAGS) $(SHARED) -Iskynet-src $^ -o $@
|
||||||
|
|
||||||
|
$(LUA_CLIB_PATH)/crypt.so : lualib-src/lua-crypt.c | $(LUA_CLIB_PATH)
|
||||||
|
$(CC) $(CFLAGS) $(SHARED) $^ -o $@
|
||||||
|
|
||||||
clean :
|
clean :
|
||||||
rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so
|
rm -f $(SKYNET_BUILD_PATH)/skynet $(CSERVICE_PATH)/*.so $(LUA_CLIB_PATH)/*.so
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
## Build
|
## Build
|
||||||
|
|
||||||
Install autoconf first for jemalloc
|
For linux, install autoconf first for jemalloc
|
||||||
|
|
||||||
```
|
```
|
||||||
git clone git@github.com:cloudwu/skynet.git
|
git clone https://github.com/cloudwu/skynet.git
|
||||||
cd skynet
|
cd skynet
|
||||||
make 'PLATFORM' # PLATFORM can be linux, macosx, freebsd now
|
make 'PLATFORM' # PLATFORM can be linux, macosx, freebsd now
|
||||||
```
|
```
|
||||||
@@ -15,6 +15,8 @@ export PLAT=linux
|
|||||||
make
|
make
|
||||||
```
|
```
|
||||||
|
|
||||||
|
For freeBSD , use gmake instead of make .
|
||||||
|
|
||||||
## Test
|
## Test
|
||||||
|
|
||||||
Run these in different console
|
Run these in different console
|
||||||
@@ -36,5 +38,4 @@ You can also use the offical lua version , edit the makefile by yourself .
|
|||||||
|
|
||||||
* http://blog.codingnow.com/2012/09/the_design_of_skynet.html
|
* http://blog.codingnow.com/2012/09/the_design_of_skynet.html
|
||||||
* http://blog.codingnow.com/2012/08/skynet.html
|
* http://blog.codingnow.com/2012/08/skynet.html
|
||||||
* http://blog.codingnow.com/2012/08/skynet_harbor_rpc.html
|
|
||||||
* http://blog.codingnow.com/eo/skynet/
|
* http://blog.codingnow.com/eo/skynet/
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ local CMD = {}
|
|||||||
local client_fd
|
local client_fd
|
||||||
|
|
||||||
local function send_client(v)
|
local function send_client(v)
|
||||||
socket.write(client_fd, netpack.pack(jsonpack.pack(0,v)))
|
socket.write(client_fd, netpack.pack(jsonpack.pack(0, {true, v})))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function response_client(session,v)
|
local function response_client(session,v)
|
||||||
|
|||||||
@@ -2,30 +2,46 @@ package.cpath = "luaclib/?.so"
|
|||||||
|
|
||||||
local socket = require "clientsocket"
|
local socket = require "clientsocket"
|
||||||
local cjson = require "cjson"
|
local cjson = require "cjson"
|
||||||
|
local bit32 = require "bit32"
|
||||||
|
|
||||||
local fd = socket.connect("127.0.0.1", 8888)
|
local fd = assert(socket.connect("127.0.0.1", 8888))
|
||||||
|
|
||||||
local last
|
local function send_package(fd, pack)
|
||||||
local result = {}
|
local size = #pack
|
||||||
|
local package = string.char(bit32.extract(size,8,8)) ..
|
||||||
|
string.char(bit32.extract(size,0,8))..
|
||||||
|
pack
|
||||||
|
|
||||||
local function dispatch()
|
socket.send(fd, package)
|
||||||
while true do
|
end
|
||||||
local status
|
|
||||||
status, last = socket.recv(fd, last, result)
|
local function unpack_package(text)
|
||||||
if status == nil then
|
local size = #text
|
||||||
|
if size < 2 then
|
||||||
|
return nil, text
|
||||||
|
end
|
||||||
|
local s = text:byte(1) * 256 + text:byte(2)
|
||||||
|
if size < s+2 then
|
||||||
|
return nil, text
|
||||||
|
end
|
||||||
|
|
||||||
|
return text:sub(3,2+s), text:sub(3+s)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function recv_package(last)
|
||||||
|
local result
|
||||||
|
result, last = unpack_package(last)
|
||||||
|
if result then
|
||||||
|
return result, last
|
||||||
|
end
|
||||||
|
local r = socket.recv(fd)
|
||||||
|
if not r then
|
||||||
|
return nil, last
|
||||||
|
end
|
||||||
|
if r == "" then
|
||||||
error "Server closed"
|
error "Server closed"
|
||||||
end
|
end
|
||||||
if not status then
|
return unpack_package(last .. r)
|
||||||
break
|
|
||||||
end
|
|
||||||
for _, v in ipairs(result) do
|
|
||||||
local session,t,str = string.match(v, "(%d+)(.)(.*)")
|
|
||||||
assert(t == '-' or t == '+')
|
|
||||||
session = tonumber(session)
|
|
||||||
local result = cjson.decode(str)
|
|
||||||
print("Response:",session, result[1], result[2])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local session = 0
|
local session = 0
|
||||||
@@ -33,13 +49,26 @@ local session = 0
|
|||||||
local function send_request(v)
|
local function send_request(v)
|
||||||
session = session + 1
|
session = session + 1
|
||||||
local str = string.format("%d+%s",session, cjson.encode(v))
|
local str = string.format("%d+%s",session, cjson.encode(v))
|
||||||
socket.send(fd, str)
|
send_package(fd, str)
|
||||||
print("Request:", session)
|
print("Request:", session)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local last = ""
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
dispatch()
|
while true do
|
||||||
local cmd = socket.readline()
|
local v
|
||||||
|
v, last = recv_package(last)
|
||||||
|
if not v then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
local session,t,str = string.match(v, "(%d+)(.)(.*)")
|
||||||
|
assert(t == '-' or t == '+')
|
||||||
|
session = tonumber(session)
|
||||||
|
local result = cjson.decode(str)
|
||||||
|
print("Response:",session, result[1], result[2])
|
||||||
|
end
|
||||||
|
local cmd = socket.readstdin()
|
||||||
if cmd then
|
if cmd then
|
||||||
local args = {}
|
local args = {}
|
||||||
string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end )
|
string.gsub(cmd, '[^ ]+', function(v) table.insert(args, v) end )
|
||||||
|
|||||||
9
examples/cluster1.lua
Normal file
9
examples/cluster1.lua
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local cluster = require "cluster"
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
skynet.newservice("simpledb")
|
||||||
|
print(skynet.call("SIMPLEDB", "lua", "SET", "a", "foobar"))
|
||||||
|
print(skynet.call("SIMPLEDB", "lua", "GET", "a"))
|
||||||
|
cluster.open "db"
|
||||||
|
end)
|
||||||
6
examples/cluster2.lua
Normal file
6
examples/cluster2.lua
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local cluster = require "cluster"
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
print(cluster.call("db", "SIMPLEDB", "GET", "a"))
|
||||||
|
end)
|
||||||
1
examples/clustername.lua
Normal file
1
examples/clustername.lua
Normal file
@@ -0,0 +1 @@
|
|||||||
|
db = "127.0.0.1:2528"
|
||||||
@@ -4,8 +4,12 @@ logger = nil
|
|||||||
harbor = 1
|
harbor = 1
|
||||||
address = "127.0.0.1:2526"
|
address = "127.0.0.1:2526"
|
||||||
master = "127.0.0.1:2013"
|
master = "127.0.0.1:2013"
|
||||||
start = "main"
|
start = "main" -- main script
|
||||||
|
bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||||
standalone = "0.0.0.0:2013"
|
standalone = "0.0.0.0:2013"
|
||||||
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
|
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
|
||||||
|
lualoader = "lualib/loader.lua"
|
||||||
|
-- preload = "./examples/preload.lua" -- run preload.lua before every lua service run
|
||||||
snax = root.."examples/?.lua;"..root.."test/?.lua"
|
snax = root.."examples/?.lua;"..root.."test/?.lua"
|
||||||
cpath = root.."cservice/?.so"
|
cpath = root.."cservice/?.so"
|
||||||
|
-- daemon = "./skynet.pid"
|
||||||
|
|||||||
9
examples/config.c1
Normal file
9
examples/config.c1
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
thread = 8
|
||||||
|
logger = nil
|
||||||
|
harbor = 0
|
||||||
|
start = "cluster1"
|
||||||
|
bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||||
|
luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua"
|
||||||
|
lualoader = "lualib/loader.lua"
|
||||||
|
cpath = "./cservice/?.so"
|
||||||
|
cluster = "./examples/clustername.lua"
|
||||||
9
examples/config.c2
Normal file
9
examples/config.c2
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
thread = 8
|
||||||
|
logger = nil
|
||||||
|
harbor = 0
|
||||||
|
start = "cluster2"
|
||||||
|
bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||||
|
luaservice = "./service/?.lua;./test/?.lua;./examples/?.lua"
|
||||||
|
lualoader = "lualib/loader.lua"
|
||||||
|
cpath = "./cservice/?.so"
|
||||||
|
cluster = "./examples/clustername.lua"
|
||||||
8
examples/config.login
Normal file
8
examples/config.login
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
thread = 8
|
||||||
|
logger = nil
|
||||||
|
harbor = 0
|
||||||
|
start = "main"
|
||||||
|
bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||||
|
luaservice = "./service/?.lua;./examples/login/?.lua"
|
||||||
|
lualoader = "lualib/loader.lua"
|
||||||
|
cpath = "./cservice/?.so"
|
||||||
14
examples/config.mc
Normal file
14
examples/config.mc
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
root = "./"
|
||||||
|
thread = 8
|
||||||
|
logger = nil
|
||||||
|
harbor = 2
|
||||||
|
address = "127.0.0.1:2527"
|
||||||
|
master = "127.0.0.1:2013"
|
||||||
|
start = "testmulticast2" -- main script
|
||||||
|
bootstrap = "snlua bootstrap" -- The service for bootstrap
|
||||||
|
--standalone = "0.0.0.0:2013"
|
||||||
|
luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua"
|
||||||
|
lualoader = "lualib/loader.lua"
|
||||||
|
-- preload = "./examples/preload.lua" -- run preload.lua before every lua service run
|
||||||
|
snax = root.."examples/?.lua;"..root.."test/?.lua"
|
||||||
|
cpath = root.."cservice/?.so"
|
||||||
@@ -7,3 +7,4 @@ address = "127.0.0.1:2527"
|
|||||||
master = "127.0.0.1:2013"
|
master = "127.0.0.1:2013"
|
||||||
start = "main_log"
|
start = "main_log"
|
||||||
luaservice ="./service/?.lua;./test/?.lua;./examples/?.lua"
|
luaservice ="./service/?.lua;./test/?.lua;./examples/?.lua"
|
||||||
|
snax = "./examples/?.lua;./test/?.lua"
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
skynet.dispatch("text", function(session, address, text)
|
skynet.dispatch("lua", function(session, address, ...)
|
||||||
print("[GLOBALLOG]", skynet.address(address),text)
|
print("[GLOBALLOG]", skynet.address(address), ...)
|
||||||
end)
|
end)
|
||||||
skynet.register "LOG"
|
skynet.register "LOG"
|
||||||
end)
|
end)
|
||||||
|
|||||||
184
examples/login/client.lua
Normal file
184
examples/login/client.lua
Normal file
@@ -0,0 +1,184 @@
|
|||||||
|
package.cpath = "luaclib/?.so"
|
||||||
|
|
||||||
|
local socket = require "clientsocket"
|
||||||
|
local crypt = require "crypt"
|
||||||
|
local bit32 = require "bit32"
|
||||||
|
|
||||||
|
local fd = assert(socket.connect("127.0.0.1", 8001))
|
||||||
|
|
||||||
|
local function writeline(fd, text)
|
||||||
|
socket.send(fd, text .. "\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
local function unpack_line(text)
|
||||||
|
local from = text:find("\n", 1, true)
|
||||||
|
if from then
|
||||||
|
return text:sub(1, from-1), text:sub(from+1)
|
||||||
|
end
|
||||||
|
return nil, text
|
||||||
|
end
|
||||||
|
|
||||||
|
local last = ""
|
||||||
|
|
||||||
|
local function unpack_f(f)
|
||||||
|
local function try_recv(fd, last)
|
||||||
|
local result
|
||||||
|
result, last = f(last)
|
||||||
|
if result then
|
||||||
|
return result, last
|
||||||
|
end
|
||||||
|
local r = socket.recv(fd)
|
||||||
|
if not r then
|
||||||
|
return nil, last
|
||||||
|
end
|
||||||
|
if r == "" then
|
||||||
|
error "Server closed"
|
||||||
|
end
|
||||||
|
return f(last .. r)
|
||||||
|
end
|
||||||
|
|
||||||
|
return function()
|
||||||
|
while true do
|
||||||
|
local result
|
||||||
|
result, last = try_recv(fd, last)
|
||||||
|
if result then
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
socket.usleep(100)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local readline = unpack_f(unpack_line)
|
||||||
|
|
||||||
|
local challenge = crypt.base64decode(readline())
|
||||||
|
|
||||||
|
local clientkey = crypt.randomkey()
|
||||||
|
writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey)))
|
||||||
|
local secret = crypt.dhsecret(crypt.base64decode(readline()), clientkey)
|
||||||
|
|
||||||
|
print("sceret is ", crypt.hexencode(secret))
|
||||||
|
|
||||||
|
local hmac = crypt.hmac64(challenge, secret)
|
||||||
|
writeline(fd, crypt.base64encode(hmac))
|
||||||
|
|
||||||
|
local token = {
|
||||||
|
server = "sample",
|
||||||
|
user = "hello",
|
||||||
|
pass = "password",
|
||||||
|
}
|
||||||
|
|
||||||
|
local function encode_token(token)
|
||||||
|
return string.format("%s@%s:%s",
|
||||||
|
crypt.base64encode(token.user),
|
||||||
|
crypt.base64encode(token.server),
|
||||||
|
crypt.base64encode(token.pass))
|
||||||
|
end
|
||||||
|
|
||||||
|
local etoken = crypt.desencode(secret, encode_token(token))
|
||||||
|
local b = crypt.base64encode(etoken)
|
||||||
|
writeline(fd, crypt.base64encode(etoken))
|
||||||
|
|
||||||
|
local result = readline()
|
||||||
|
print(result)
|
||||||
|
local code = tonumber(string.sub(result, 1, 3))
|
||||||
|
assert(code == 200)
|
||||||
|
socket.close(fd)
|
||||||
|
|
||||||
|
local subid = crypt.base64decode(string.sub(result, 5))
|
||||||
|
|
||||||
|
print("login ok, subid=", subid)
|
||||||
|
|
||||||
|
----- connect to game server
|
||||||
|
|
||||||
|
local function send_request(v, session)
|
||||||
|
local size = #v + 4
|
||||||
|
local package = string.char(bit32.extract(size,8,8))..
|
||||||
|
string.char(bit32.extract(size,0,8))..
|
||||||
|
v..
|
||||||
|
string.char(bit32.extract(session,24,8))..
|
||||||
|
string.char(bit32.extract(session,16,8))..
|
||||||
|
string.char(bit32.extract(session,8,8))..
|
||||||
|
string.char(bit32.extract(session,0,8))
|
||||||
|
|
||||||
|
socket.send(fd, package)
|
||||||
|
return v, session
|
||||||
|
end
|
||||||
|
|
||||||
|
local function recv_response(v)
|
||||||
|
local content = v:sub(1,-6)
|
||||||
|
local ok = v:sub(-5,-5):byte()
|
||||||
|
local session = 0
|
||||||
|
for i=-4,-1 do
|
||||||
|
local c = v:byte(i)
|
||||||
|
session = session + bit32.lshift(c,(-1-i) * 8)
|
||||||
|
end
|
||||||
|
return ok ~=0 , content, session
|
||||||
|
end
|
||||||
|
|
||||||
|
local function unpack_package(text)
|
||||||
|
local size = #text
|
||||||
|
if size < 2 then
|
||||||
|
return nil, text
|
||||||
|
end
|
||||||
|
local s = text:byte(1) * 256 + text:byte(2)
|
||||||
|
if size < s+2 then
|
||||||
|
return nil, text
|
||||||
|
end
|
||||||
|
|
||||||
|
return text:sub(3,2+s), text:sub(3+s)
|
||||||
|
end
|
||||||
|
|
||||||
|
local readpackage = unpack_f(unpack_package)
|
||||||
|
|
||||||
|
local function send_package(fd, pack)
|
||||||
|
local size = #pack
|
||||||
|
local package = string.char(bit32.extract(size,8,8))..
|
||||||
|
string.char(bit32.extract(size,0,8))..
|
||||||
|
pack
|
||||||
|
|
||||||
|
socket.send(fd, package)
|
||||||
|
end
|
||||||
|
|
||||||
|
local text = "echo"
|
||||||
|
local index = 1
|
||||||
|
|
||||||
|
print("connect")
|
||||||
|
local fd = assert(socket.connect("127.0.0.1", 8888))
|
||||||
|
last = ""
|
||||||
|
|
||||||
|
local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index)
|
||||||
|
local hmac = crypt.hmac64(crypt.hashkey(handshake), secret)
|
||||||
|
|
||||||
|
|
||||||
|
send_package(fd, handshake .. ":" .. crypt.base64encode(hmac))
|
||||||
|
|
||||||
|
print(readpackage())
|
||||||
|
print("===>",send_request(text,0))
|
||||||
|
-- don't recv response
|
||||||
|
-- print("<===",recv_response(readpackage()))
|
||||||
|
|
||||||
|
print("disconnect")
|
||||||
|
socket.close(fd)
|
||||||
|
|
||||||
|
index = index + 1
|
||||||
|
|
||||||
|
print("connect again")
|
||||||
|
local fd = assert(socket.connect("127.0.0.1", 8888))
|
||||||
|
last = ""
|
||||||
|
|
||||||
|
local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index)
|
||||||
|
local hmac = crypt.hmac64(crypt.hashkey(handshake), secret)
|
||||||
|
|
||||||
|
send_package(fd, handshake .. ":" .. crypt.base64encode(hmac))
|
||||||
|
|
||||||
|
print(readpackage())
|
||||||
|
print("===>",send_request("fake",0)) -- request again (use last session 0, so the request message is fake)
|
||||||
|
print("===>",send_request("again",1)) -- request again (use new session)
|
||||||
|
print("<===",recv_response(readpackage()))
|
||||||
|
print("<===",recv_response(readpackage()))
|
||||||
|
|
||||||
|
|
||||||
|
print("disconnect")
|
||||||
|
socket.close(fd)
|
||||||
|
|
||||||
88
examples/login/gated.lua
Normal file
88
examples/login/gated.lua
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
local msgserver = require "snax.msgserver"
|
||||||
|
local crypt = require "crypt"
|
||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
local loginservice = tonumber(...)
|
||||||
|
|
||||||
|
local server = {}
|
||||||
|
local users = {}
|
||||||
|
local username_map = {}
|
||||||
|
local internal_id = 0
|
||||||
|
|
||||||
|
-- login server disallow multi login, so login_handler never be reentry
|
||||||
|
-- call by login server
|
||||||
|
function server.login_handler(uid, secret)
|
||||||
|
if users[uid] then
|
||||||
|
error(string.format("%s is already login", uid))
|
||||||
|
end
|
||||||
|
|
||||||
|
internal_id = internal_id + 1
|
||||||
|
local username = msgserver.username(uid, internal_id, servername)
|
||||||
|
|
||||||
|
-- you can use a pool to alloc new agent
|
||||||
|
local agent = skynet.newservice "msgagent"
|
||||||
|
local u = {
|
||||||
|
username = username,
|
||||||
|
agent = agent,
|
||||||
|
uid = uid,
|
||||||
|
subid = internal_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
-- trash subid (no used)
|
||||||
|
skynet.call(agent, "lua", "login", uid, internal_id, secret)
|
||||||
|
|
||||||
|
users[uid] = u
|
||||||
|
username_map[username] = u
|
||||||
|
|
||||||
|
msgserver.login(username, secret)
|
||||||
|
|
||||||
|
-- you should return unique subid
|
||||||
|
return internal_id
|
||||||
|
end
|
||||||
|
|
||||||
|
-- call by agent
|
||||||
|
function server.logout_handler(uid, subid)
|
||||||
|
local u = users[uid]
|
||||||
|
if u then
|
||||||
|
local username = msgserver.username(uid, subid, servername)
|
||||||
|
assert(u.username == username)
|
||||||
|
msgserver.logout(u.username)
|
||||||
|
users[uid] = nil
|
||||||
|
username_map[u.username] = nil
|
||||||
|
skynet.call(loginservice, "lua", "logout",uid, subid)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- call by login server
|
||||||
|
function server.kick_handler(uid, subid)
|
||||||
|
local u = users[uid]
|
||||||
|
if u then
|
||||||
|
local username = msgserver.username(uid, subid, servername)
|
||||||
|
assert(u.username == username)
|
||||||
|
-- NOTICE: logout may call skynet.exit, so you should use pcall.
|
||||||
|
pcall(skynet.call, u.agent, "lua", "logout")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- call by self (when socket disconnect)
|
||||||
|
function server.disconnect_handler(username)
|
||||||
|
local u = username_map[username]
|
||||||
|
if u then
|
||||||
|
skynet.call(u.agent, "lua", "afk")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- call by self (when recv a request from client)
|
||||||
|
function server.request_handler(username, msg, sz)
|
||||||
|
local u = username_map[username]
|
||||||
|
return skynet.tostring(skynet.rawcall(u.agent, "client", msg, sz))
|
||||||
|
end
|
||||||
|
|
||||||
|
-- call by self (when gate open)
|
||||||
|
function server.register_handler(name)
|
||||||
|
servername = name
|
||||||
|
skynet.call(loginservice, "lua", "register_gate", servername, skynet.self())
|
||||||
|
end
|
||||||
|
|
||||||
|
msgserver.start(server)
|
||||||
|
|
||||||
62
examples/login/logind.lua
Normal file
62
examples/login/logind.lua
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
local login = require "snax.loginserver"
|
||||||
|
local crypt = require "crypt"
|
||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
local server = {
|
||||||
|
host = "127.0.0.1",
|
||||||
|
port = 8001,
|
||||||
|
multilogin = false, -- disallow multilogin
|
||||||
|
name = "login_master",
|
||||||
|
}
|
||||||
|
|
||||||
|
local server_list = {}
|
||||||
|
local user_online = {}
|
||||||
|
local user_login = {}
|
||||||
|
|
||||||
|
function server.auth_handler(token)
|
||||||
|
-- the token is base64(user)@base64(server):base64(password)
|
||||||
|
local user, server, password = token:match("([^@]+)@([^:]+):(.+)")
|
||||||
|
user = crypt.base64decode(user)
|
||||||
|
server = crypt.base64decode(server)
|
||||||
|
password = crypt.base64decode(password)
|
||||||
|
assert(password == "password")
|
||||||
|
return server, user
|
||||||
|
end
|
||||||
|
|
||||||
|
function server.login_handler(server, uid, secret)
|
||||||
|
print(string.format("%s@%s is login, secret is %s", uid, server, crypt.hexencode(secret)))
|
||||||
|
local gameserver = assert(server_list[server], "Unknown server")
|
||||||
|
-- only one can login, because disallow multilogin
|
||||||
|
local last = user_online[uid]
|
||||||
|
if last then
|
||||||
|
skynet.call(last.address, "lua", "kick", uid, last.subid)
|
||||||
|
end
|
||||||
|
if user_online[uid] then
|
||||||
|
error(string.format("user %s is already online", uid))
|
||||||
|
end
|
||||||
|
|
||||||
|
local subid = tostring(skynet.call(gameserver, "lua", "login", uid, secret))
|
||||||
|
user_online[uid] = { address = gameserver, subid = subid , server = server}
|
||||||
|
return subid
|
||||||
|
end
|
||||||
|
|
||||||
|
local CMD = {}
|
||||||
|
|
||||||
|
function CMD.register_gate(server, address)
|
||||||
|
server_list[server] = address
|
||||||
|
end
|
||||||
|
|
||||||
|
function CMD.logout(uid, subid)
|
||||||
|
local u = user_online[uid]
|
||||||
|
if u then
|
||||||
|
print(string.format("%s@%s is logout", uid, u.server))
|
||||||
|
user_online[uid] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function server.command_handler(command, source, ...)
|
||||||
|
local f = assert(CMD[command])
|
||||||
|
return f(source, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
login(server)
|
||||||
12
examples/login/main.lua
Normal file
12
examples/login/main.lua
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
local loginserver = skynet.newservice("logind")
|
||||||
|
local gate = skynet.newservice("gated", loginserver)
|
||||||
|
|
||||||
|
skynet.call(gate, "lua", "open" , {
|
||||||
|
port = 8888,
|
||||||
|
maxclient = 64,
|
||||||
|
servername = "sample",
|
||||||
|
})
|
||||||
|
end)
|
||||||
53
examples/login/msgagent.lua
Normal file
53
examples/login/msgagent.lua
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "client",
|
||||||
|
id = skynet.PTYPE_CLIENT,
|
||||||
|
unpack = skynet.tostring,
|
||||||
|
}
|
||||||
|
|
||||||
|
local gate
|
||||||
|
local userid, subid
|
||||||
|
|
||||||
|
local CMD = {}
|
||||||
|
|
||||||
|
function CMD.login(source, uid, sid, secret)
|
||||||
|
-- you may use secret to make a encrypted data stream
|
||||||
|
skynet.error(string.format("%s is login", uid))
|
||||||
|
gate = source
|
||||||
|
userid = uid
|
||||||
|
subid = sid
|
||||||
|
-- you may load user data from database
|
||||||
|
end
|
||||||
|
|
||||||
|
local function logout()
|
||||||
|
if gate then
|
||||||
|
skynet.call(gate, "lua", "logout", userid, subid)
|
||||||
|
end
|
||||||
|
skynet.exit()
|
||||||
|
end
|
||||||
|
|
||||||
|
function CMD.logout(source)
|
||||||
|
-- NOTICE: The logout MAY be reentry
|
||||||
|
skynet.error(string.format("%s is logout", userid))
|
||||||
|
logout()
|
||||||
|
end
|
||||||
|
|
||||||
|
function CMD.afk(source)
|
||||||
|
-- the connection is broken, but the user may back
|
||||||
|
skynet.error(string.format("AFK"))
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
-- If you want to fork a work thread , you MUST do it in CMD.login
|
||||||
|
skynet.dispatch("lua", function(session, source, command, ...)
|
||||||
|
local f = assert(CMD[command])
|
||||||
|
skynet.ret(skynet.pack(f(source, ...)))
|
||||||
|
end)
|
||||||
|
|
||||||
|
skynet.dispatch("client", function(_,_, msg)
|
||||||
|
-- the simple ehco service
|
||||||
|
skynet.sleep(10) -- sleep a while
|
||||||
|
skynet.ret(msg)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
@@ -4,16 +4,16 @@ local max_client = 64
|
|||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
print("Server start")
|
print("Server start")
|
||||||
local service = skynet.newservice("service_mgr")
|
|
||||||
skynet.monitor "simplemonitor"
|
|
||||||
local console = skynet.newservice("console")
|
local console = skynet.newservice("console")
|
||||||
-- skynet.newservice("debug_console",8000)
|
skynet.newservice("debug_console",8000)
|
||||||
skynet.newservice("simpledb")
|
skynet.newservice("simpledb")
|
||||||
local watchdog = skynet.newservice("watchdog")
|
local watchdog = skynet.newservice("watchdog")
|
||||||
skynet.call(watchdog, "lua", "start", {
|
skynet.call(watchdog, "lua", "start", {
|
||||||
port = 8888,
|
port = 8888,
|
||||||
maxclient = max_client,
|
maxclient = max_client,
|
||||||
|
nodelay = true,
|
||||||
})
|
})
|
||||||
|
print("Watchdog listen on ", 8888)
|
||||||
|
|
||||||
skynet.exit()
|
skynet.exit()
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ local skynet = require "skynet"
|
|||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
print("Log server start")
|
print("Log server start")
|
||||||
local service = skynet.newservice("service_mgr")
|
|
||||||
skynet.monitor "simplemonitor"
|
skynet.monitor "simplemonitor"
|
||||||
local log = skynet.newservice("globallog")
|
local log = skynet.newservice("globallog")
|
||||||
skynet.exit()
|
skynet.exit()
|
||||||
|
|||||||
5
examples/preload.lua
Normal file
5
examples/preload.lua
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
-- This file will execute before every lua service start
|
||||||
|
-- See config
|
||||||
|
|
||||||
|
print("PRELOAD", ...)
|
||||||
|
|
||||||
@@ -16,7 +16,11 @@ end
|
|||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
skynet.dispatch("lua", function(session, address, cmd, ...)
|
skynet.dispatch("lua", function(session, address, cmd, ...)
|
||||||
local f = command[string.upper(cmd)]
|
local f = command[string.upper(cmd)]
|
||||||
|
if f then
|
||||||
skynet.ret(skynet.pack(f(...)))
|
skynet.ret(skynet.pack(f(...)))
|
||||||
|
else
|
||||||
|
error(string.format("Unknown command %s", tostring(cmd)))
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
skynet.register "SIMPLEDB"
|
skynet.register "SIMPLEDB"
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -12,11 +12,10 @@ skynet.register_protocol {
|
|||||||
local w = service_map[address]
|
local w = service_map[address]
|
||||||
if w then
|
if w then
|
||||||
for watcher in pairs(w) do
|
for watcher in pairs(w) do
|
||||||
skynet.send(watcher, "error", address)
|
skynet.redirect(watcher, address, "error", 0, "")
|
||||||
end
|
end
|
||||||
service_map[address] = false
|
service_map[address] = false
|
||||||
end
|
end
|
||||||
print(string.format("[:%x] exit", address))
|
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
|
||||||
74
examples/simpleweb.lua
Normal file
74
examples/simpleweb.lua
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local socket = require "socket"
|
||||||
|
local httpd = require "http.httpd"
|
||||||
|
local sockethelper = require "http.sockethelper"
|
||||||
|
local urllib = require "http.url"
|
||||||
|
local table = table
|
||||||
|
local string = string
|
||||||
|
|
||||||
|
local mode = ...
|
||||||
|
|
||||||
|
if mode == "agent" then
|
||||||
|
|
||||||
|
local function response(id, ...)
|
||||||
|
local ok, err = httpd.write_response(sockethelper.writefunc(id), ...)
|
||||||
|
if not ok then
|
||||||
|
-- if err == sockethelper.socket_error , that means socket closed.
|
||||||
|
skynet.error(string.format("fd = %d, %s", id, err))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
skynet.dispatch("lua", function (_,_,id)
|
||||||
|
socket.start(id)
|
||||||
|
-- limit request body size to 8192 (you can pass nil to unlimit)
|
||||||
|
local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192)
|
||||||
|
if code then
|
||||||
|
if code ~= 200 then
|
||||||
|
response(id, code)
|
||||||
|
else
|
||||||
|
local tmp = {}
|
||||||
|
if header.host then
|
||||||
|
table.insert(tmp, string.format("host: %s", header.host))
|
||||||
|
end
|
||||||
|
local path, query = urllib.parse(url)
|
||||||
|
table.insert(tmp, string.format("path: %s", path))
|
||||||
|
if query then
|
||||||
|
local q = urllib.parse_query(query)
|
||||||
|
for k, v in pairs(q) do
|
||||||
|
table.insert(tmp, string.format("query: %s= %s", k,v))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
response(id, code, table.concat(tmp,"\n"))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if url == sockethelper.socket_error then
|
||||||
|
skynet.error("socket closed")
|
||||||
|
else
|
||||||
|
skynet.error(url)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
socket.close(id)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
local agent = {}
|
||||||
|
for i= 1, 20 do
|
||||||
|
agent[i] = skynet.newservice(SERVICE_NAME, "agent")
|
||||||
|
end
|
||||||
|
local balance = 1
|
||||||
|
local id = socket.listen("0.0.0.0", 8001)
|
||||||
|
socket.start(id , function(id, addr)
|
||||||
|
skynet.error(string.format("%s connected, pass it to agent :%08x", addr, agent[balance]))
|
||||||
|
skynet.send(agent[balance], "lua", id)
|
||||||
|
balance = balance + 1
|
||||||
|
if balance > #agent then
|
||||||
|
balance = 1
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
|
||||||
|
end
|
||||||
@@ -1,5 +1,3 @@
|
|||||||
#include "skynet_malloc.h"
|
|
||||||
|
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
@@ -70,10 +68,17 @@ struct bson_reader {
|
|||||||
int size;
|
int size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static inline int32_t
|
||||||
|
get_length(const uint8_t * data) {
|
||||||
|
const uint8_t * b = (const uint8_t *)data;
|
||||||
|
int32_t len = b[0] | b[1]<<8 | b[2]<<16 | b[3]<<24;
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
bson_destroy(struct bson *b) {
|
bson_destroy(struct bson *b) {
|
||||||
if (b->ptr != b->buffer) {
|
if (b->ptr != b->buffer) {
|
||||||
skynet_free(b->ptr);
|
free(b->ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,10 +98,10 @@ bson_reserve(struct bson *b, int sz) {
|
|||||||
} while (b->cap <= b->size + sz);
|
} while (b->cap <= b->size + sz);
|
||||||
|
|
||||||
if (b->ptr == b->buffer) {
|
if (b->ptr == b->buffer) {
|
||||||
b->ptr = skynet_malloc(b->cap);
|
b->ptr = malloc(b->cap);
|
||||||
memcpy(b->ptr, b->buffer, b->size);
|
memcpy(b->ptr, b->buffer, b->size);
|
||||||
} else {
|
} else {
|
||||||
b->ptr = skynet_realloc(b->ptr, b->cap);
|
b->ptr = realloc(b->ptr, b->cap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,13 +263,13 @@ append_key(struct bson *bs, int type, const char *key, size_t sz) {
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) {
|
append_number(struct bson *bs, lua_State *L, const char *key, size_t sz) {
|
||||||
lua_Integer i = lua_tointeger(L, -1);
|
int64_t i = lua_tointeger(L, -1);
|
||||||
lua_Number d = lua_tonumber(L,-1);
|
lua_Number d = lua_tonumber(L,-1);
|
||||||
if (i != d) {
|
if (i != d) {
|
||||||
append_key(bs, BSON_REAL, key, sz);
|
append_key(bs, BSON_REAL, key, sz);
|
||||||
write_double(bs, d);
|
write_double(bs, d);
|
||||||
} else {
|
} else {
|
||||||
int si = (int64_t)i >> 32;
|
int si = i >> 31;
|
||||||
if (si == 0 || si == -1) {
|
if (si == 0 || si == -1) {
|
||||||
append_key(bs, BSON_INT32, key, sz);
|
append_key(bs, BSON_INT32, key, sz);
|
||||||
write_int32(bs, i);
|
write_int32(bs, i);
|
||||||
@@ -459,7 +464,7 @@ pack_dict(lua_State *L, struct bson *b, bool isarray) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pack_sorted_dict(lua_State *L, struct bson *b, int n) {
|
pack_ordered_dict(lua_State *L, struct bson *b, int n) {
|
||||||
int length = reserve_length(b);
|
int length = reserve_length(b);
|
||||||
int i;
|
int i;
|
||||||
for (i=0;i<n;i+=2) {
|
for (i=0;i<n;i+=2) {
|
||||||
@@ -534,6 +539,9 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) {
|
|||||||
break;
|
break;
|
||||||
case BSON_STRING: {
|
case BSON_STRING: {
|
||||||
int sz = read_int32(L, &t);
|
int sz = read_int32(L, &t);
|
||||||
|
if (sz <= 0) {
|
||||||
|
luaL_error(L, "Invalid bson string , length = %d", sz);
|
||||||
|
}
|
||||||
lua_pushlstring(L, read_bytes(L, &t, sz), sz-1);
|
lua_pushlstring(L, read_bytes(L, &t, sz), sz-1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -643,7 +651,7 @@ static int
|
|||||||
lmakeindex(lua_State *L) {
|
lmakeindex(lua_State *L) {
|
||||||
int32_t *bson = luaL_checkudata(L,1,"bson");
|
int32_t *bson = luaL_checkudata(L,1,"bson");
|
||||||
const uint8_t * start = (const uint8_t *)bson;
|
const uint8_t * start = (const uint8_t *)bson;
|
||||||
struct bson_reader br = { start+4, *bson - 5 };
|
struct bson_reader br = { start+4, get_length(start) - 5 };
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -806,6 +814,9 @@ lreplace(lua_State *L) {
|
|||||||
write_int64(&b, i);
|
write_int64(&b, i);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
luaL_error(L, "Can't replace type %d", type);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -816,7 +827,9 @@ ldecode(lua_State *L) {
|
|||||||
if (data == NULL) {
|
if (data == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
struct bson_reader br = { (const uint8_t *)data , *data };
|
const uint8_t * b = (const uint8_t *)data;
|
||||||
|
int32_t len = get_length(b);
|
||||||
|
struct bson_reader br = { b , len };
|
||||||
|
|
||||||
unpack_dict(L, &br, false);
|
unpack_dict(L, &br, false);
|
||||||
|
|
||||||
@@ -865,7 +878,7 @@ lencode_order(lua_State *L) {
|
|||||||
if (n%2 != 0) {
|
if (n%2 != 0) {
|
||||||
return luaL_error(L, "Invalid ordered dict");
|
return luaL_error(L, "Invalid ordered dict");
|
||||||
}
|
}
|
||||||
pack_sorted_dict(L, &b, n);
|
pack_ordered_dict(L, &b, n);
|
||||||
lua_settop(L,1);
|
lua_settop(L,1);
|
||||||
void * ud = lua_newuserdata(L, b.size);
|
void * ud = lua_newuserdata(L, b.size);
|
||||||
memcpy(ud, b.ptr, b.size);
|
memcpy(ud, b.ptr, b.size);
|
||||||
@@ -1070,7 +1083,7 @@ typeclosure(lua_State *L) {
|
|||||||
"string", // 5
|
"string", // 5
|
||||||
"binary", // 6
|
"binary", // 6
|
||||||
"objectid", // 7
|
"objectid", // 7
|
||||||
"timestamp",// 8
|
"timestamp", // 8
|
||||||
"date", // 9
|
"date", // 9
|
||||||
"regex", // 10
|
"regex", // 10
|
||||||
"minkey", // 11
|
"minkey", // 11
|
||||||
@@ -1136,14 +1149,14 @@ lobjectid(lua_State *L) {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
time_t ti = time(NULL);
|
time_t ti = time(NULL);
|
||||||
oid[2] = ti & 0xff;
|
oid[2] = (ti>>24) & 0xff;
|
||||||
oid[3] = (ti>>8) & 0xff;
|
oid[3] = (ti>>16) & 0xff;
|
||||||
oid[4] = (ti>>16) & 0xff;
|
oid[4] = (ti>>8) & 0xff;
|
||||||
oid[5] = (ti>>24) & 0xff;
|
oid[5] = ti & 0xff;
|
||||||
memcpy(oid+6 , oid_header, 5);
|
memcpy(oid+6 , oid_header, 5);
|
||||||
oid[11] = oid_counter & 0xff;
|
oid[11] = (oid_counter>>16) & 0xff;
|
||||||
oid[12] = (oid_counter>>8) & 0xff;
|
oid[12] = (oid_counter>>8) & 0xff;
|
||||||
oid[13] = (oid_counter>>16) & 0xff;
|
oid[13] = oid_counter & 0xff;
|
||||||
++oid_counter;
|
++oid_counter;
|
||||||
}
|
}
|
||||||
lua_pushlstring( L, (const char *)oid, 14);
|
lua_pushlstring( L, (const char *)oid, 14);
|
||||||
@@ -1167,7 +1180,6 @@ luaopen_bson(lua_State *L) {
|
|||||||
{ "timestamp", ltimestamp },
|
{ "timestamp", ltimestamp },
|
||||||
{ "regex", lregex },
|
{ "regex", lregex },
|
||||||
{ "binary", lbinary },
|
{ "binary", lbinary },
|
||||||
{ "regex", lregex },
|
|
||||||
{ "objectid", lobjectid },
|
{ "objectid", lobjectid },
|
||||||
{ "decode", ldecode },
|
{ "decode", ldecode },
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
@@ -1190,3 +1202,4 @@ luaopen_bson(lua_State *L) {
|
|||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -75,47 +75,12 @@ lsend(lua_State *L) {
|
|||||||
size_t sz = 0;
|
size_t sz = 0;
|
||||||
int fd = luaL_checkinteger(L,1);
|
int fd = luaL_checkinteger(L,1);
|
||||||
const char * msg = luaL_checklstring(L, 2, &sz);
|
const char * msg = luaL_checklstring(L, 2, &sz);
|
||||||
uint8_t tmp[sz + 2];
|
|
||||||
if (sz >= 0x10000) {
|
|
||||||
return luaL_error(L, "package too long %d (16bit limited)", (int)sz);
|
|
||||||
}
|
|
||||||
tmp[0] = (sz >> 8) & 0xff;
|
|
||||||
tmp[1] = sz & 0xff;
|
|
||||||
memcpy(tmp+2, msg, sz);
|
|
||||||
|
|
||||||
block_send(L, fd, (const char *)tmp, (int)sz+2);
|
block_send(L, fd, msg, (int)sz);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int
|
|
||||||
unpack(lua_State *L, uint8_t *buffer, int sz, int n) {
|
|
||||||
int size = 0;
|
|
||||||
if (sz >= 2) {
|
|
||||||
size = buffer[0] << 8 | buffer[1];
|
|
||||||
if (size > sz - 2) {
|
|
||||||
goto _block;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
goto _block;
|
|
||||||
}
|
|
||||||
++n;
|
|
||||||
lua_pushlstring(L, (const char *)buffer+2, size);
|
|
||||||
lua_rawseti(L, 3, n);
|
|
||||||
buffer += size + 2;
|
|
||||||
sz -= size + 2;
|
|
||||||
return unpack(L, buffer, sz, n);
|
|
||||||
_block:
|
|
||||||
lua_pushboolean(L, n==0 ? 0:1);
|
|
||||||
if (sz == 0) {
|
|
||||||
lua_pushnil(L);
|
|
||||||
} else {
|
|
||||||
lua_pushlstring(L, (const char *)buffer, sz);
|
|
||||||
}
|
|
||||||
return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
intger fd
|
intger fd
|
||||||
string last
|
string last
|
||||||
@@ -125,46 +90,31 @@ _block:
|
|||||||
boolean (true: data, false: block, nil: close)
|
boolean (true: data, false: block, nil: close)
|
||||||
string last
|
string last
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
struct socket_buffer {
|
||||||
|
void * buffer;
|
||||||
|
int sz;
|
||||||
|
};
|
||||||
|
|
||||||
static int
|
static int
|
||||||
lrecv(lua_State *L) {
|
lrecv(lua_State *L) {
|
||||||
int fd = luaL_checkinteger(L,1);
|
int fd = luaL_checkinteger(L,1);
|
||||||
size_t sz = 0;
|
|
||||||
const char * last = lua_tolstring(L,2,&sz);
|
|
||||||
luaL_checktype(L, 3, LUA_TTABLE);
|
|
||||||
|
|
||||||
char tmp[CACHE_SIZE];
|
char buffer[CACHE_SIZE];
|
||||||
char * buffer;
|
int r = recv(fd, buffer, CACHE_SIZE, 0);
|
||||||
int r = recv(fd, tmp, CACHE_SIZE, 0);
|
|
||||||
if (r == 0) {
|
if (r == 0) {
|
||||||
|
lua_pushliteral(L, "");
|
||||||
// close
|
// close
|
||||||
return 0;
|
return 1;
|
||||||
}
|
}
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
if (errno == EAGAIN || errno == EINTR) {
|
if (errno == EAGAIN || errno == EINTR) {
|
||||||
lua_pushboolean(L, 0);
|
return 0;
|
||||||
lua_pushvalue(L, 2);
|
|
||||||
return 2;
|
|
||||||
}
|
}
|
||||||
luaL_error(L, "socket error: %s", strerror(errno));
|
luaL_error(L, "socket error: %s", strerror(errno));
|
||||||
}
|
}
|
||||||
if (sz + r <= CACHE_SIZE) {
|
lua_pushlstring(L, buffer, r);
|
||||||
buffer = tmp;
|
return 1;
|
||||||
memmove(buffer + sz, buffer, r);
|
|
||||||
memcpy(buffer, last, sz);
|
|
||||||
} else {
|
|
||||||
buffer = lua_newuserdata(L, r + sz);
|
|
||||||
memcpy(buffer, last, sz);
|
|
||||||
memcpy(buffer + sz, tmp, r);
|
|
||||||
}
|
|
||||||
|
|
||||||
int i;
|
|
||||||
int n = lua_rawlen(L, 3);
|
|
||||||
for (i=1;i<=n;i++) {
|
|
||||||
lua_pushnil(L);
|
|
||||||
lua_rawseti(L, 3, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
return unpack(L, (uint8_t *)buffer, r+sz, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@@ -219,7 +169,7 @@ readline_stdin(void * arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
lreadline(lua_State *L) {
|
lreadstdin(lua_State *L) {
|
||||||
struct queue *q = lua_touserdata(L, lua_upvalueindex(1));
|
struct queue *q = lua_touserdata(L, lua_upvalueindex(1));
|
||||||
LOCK(q);
|
LOCK(q);
|
||||||
if (q->head == q->tail) {
|
if (q->head == q->tail) {
|
||||||
@@ -251,8 +201,8 @@ luaopen_clientsocket(lua_State *L) {
|
|||||||
|
|
||||||
struct queue * q = lua_newuserdata(L, sizeof(*q));
|
struct queue * q = lua_newuserdata(L, sizeof(*q));
|
||||||
memset(q, 0, sizeof(*q));
|
memset(q, 0, sizeof(*q));
|
||||||
lua_pushcclosure(L, lreadline, 1);
|
lua_pushcclosure(L, lreadstdin, 1);
|
||||||
lua_setfield(L, -2, "readline");
|
lua_setfield(L, -2, "readstdin");
|
||||||
|
|
||||||
pthread_t pid ;
|
pthread_t pid ;
|
||||||
pthread_create(&pid, NULL, readline_stdin, q);
|
pthread_create(&pid, NULL, readline_stdin, q);
|
||||||
|
|||||||
218
lualib-src/lua-cluster.c
Normal file
218
lualib-src/lua-cluster.c
Normal file
@@ -0,0 +1,218 @@
|
|||||||
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
uint32_t/string addr
|
||||||
|
uint32_t/session session
|
||||||
|
lightuserdata msg
|
||||||
|
uint32_t sz
|
||||||
|
|
||||||
|
return
|
||||||
|
string request
|
||||||
|
uint32_t next_session
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define TEMP_LENGTH 0x10007
|
||||||
|
|
||||||
|
static void
|
||||||
|
fill_uint32(uint8_t * buf, uint32_t n) {
|
||||||
|
buf[0] = n & 0xff;
|
||||||
|
buf[1] = (n >> 8) & 0xff;
|
||||||
|
buf[2] = (n >> 16) & 0xff;
|
||||||
|
buf[3] = (n >> 24) & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
fill_header(lua_State *L, uint8_t *buf, int sz, void *msg) {
|
||||||
|
if (sz >= 0x10000) {
|
||||||
|
skynet_free(msg);
|
||||||
|
luaL_error(L, "request message is too long %d", sz);
|
||||||
|
}
|
||||||
|
buf[0] = (sz >> 8) & 0xff;
|
||||||
|
buf[1] = sz & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
packreq_number(lua_State *L, int session, void * msg, size_t sz) {
|
||||||
|
uint32_t addr = lua_tounsigned(L,1);
|
||||||
|
uint8_t buf[TEMP_LENGTH];
|
||||||
|
fill_header(L, buf, sz+9, msg);
|
||||||
|
buf[2] = 0;
|
||||||
|
fill_uint32(buf+3, addr);
|
||||||
|
fill_uint32(buf+7, (uint32_t)session);
|
||||||
|
memcpy(buf+11,msg,sz);
|
||||||
|
|
||||||
|
lua_pushlstring(L, (const char *)buf, sz+11);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
packreq_string(lua_State *L, int session, void * msg, size_t sz) {
|
||||||
|
size_t namelen = 0;
|
||||||
|
const char *name = lua_tolstring(L, 1, &namelen);
|
||||||
|
if (name == NULL || namelen < 1 || namelen > 255) {
|
||||||
|
skynet_free(msg);
|
||||||
|
luaL_error(L, "name is too long %s", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t buf[TEMP_LENGTH];
|
||||||
|
fill_header(L, buf, sz+5+namelen, msg);
|
||||||
|
buf[2] = (uint8_t)namelen;
|
||||||
|
memcpy(buf+3, name, namelen);
|
||||||
|
fill_uint32(buf+3+namelen, (uint32_t)session);
|
||||||
|
memcpy(buf+7+namelen,msg,sz);
|
||||||
|
|
||||||
|
lua_pushlstring(L, (const char *)buf, sz+7+namelen);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lpackrequest(lua_State *L) {
|
||||||
|
void *msg = lua_touserdata(L,3);
|
||||||
|
if (msg == NULL) {
|
||||||
|
return luaL_error(L, "Invalid request message");
|
||||||
|
}
|
||||||
|
size_t sz = luaL_checkunsigned(L,4);
|
||||||
|
int session = luaL_checkinteger(L,2);
|
||||||
|
if (session <= 0) {
|
||||||
|
return luaL_error(L, "Invalid request session %d", session);
|
||||||
|
}
|
||||||
|
int addr_type = lua_type(L,1);
|
||||||
|
if (addr_type == LUA_TNUMBER) {
|
||||||
|
packreq_number(L, session, msg, sz);
|
||||||
|
} else {
|
||||||
|
packreq_string(L, session, msg, sz);
|
||||||
|
}
|
||||||
|
if (++session < 0) {
|
||||||
|
session = 1;
|
||||||
|
}
|
||||||
|
skynet_free(msg);
|
||||||
|
lua_pushinteger(L, session);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
string packed message
|
||||||
|
return
|
||||||
|
uint32_t or string addr
|
||||||
|
int session
|
||||||
|
string msg
|
||||||
|
*/
|
||||||
|
|
||||||
|
static inline uint32_t
|
||||||
|
unpack_uint32(const uint8_t * buf) {
|
||||||
|
return buf[0] | buf[1]<<8 | buf[2]<<16 | buf[3]<<24;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
unpackreq_number(lua_State *L, const uint8_t * buf, size_t sz) {
|
||||||
|
if (sz < 9) {
|
||||||
|
return luaL_error(L, "Invalid cluster message");
|
||||||
|
}
|
||||||
|
uint32_t address = unpack_uint32(buf+1);
|
||||||
|
uint32_t session = unpack_uint32(buf+5);
|
||||||
|
lua_pushunsigned(L, address);
|
||||||
|
lua_pushunsigned(L, session);
|
||||||
|
lua_pushlstring(L, (const char *)buf+9, sz-9);
|
||||||
|
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
unpackreq_string(lua_State *L, const uint8_t * buf, size_t sz) {
|
||||||
|
size_t namesz = buf[0];
|
||||||
|
if (sz < namesz + 5) {
|
||||||
|
return luaL_error(L, "Invalid cluster message");
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, (const char *)buf+1, namesz);
|
||||||
|
uint32_t session = unpack_uint32(buf + namesz + 1);
|
||||||
|
lua_pushunsigned(L, session);
|
||||||
|
lua_pushlstring(L, (const char *)buf+1+namesz+4, sz - namesz - 5);
|
||||||
|
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lunpackrequest(lua_State *L) {
|
||||||
|
size_t sz;
|
||||||
|
const char *msg = luaL_checklstring(L,1,&sz);
|
||||||
|
if (msg[0] == 0) {
|
||||||
|
return unpackreq_number(L, (const uint8_t *)msg, sz);
|
||||||
|
} else {
|
||||||
|
return unpackreq_string(L, (const uint8_t *)msg, sz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
int session
|
||||||
|
boolean ok
|
||||||
|
lightuserdata msg
|
||||||
|
int sz
|
||||||
|
return string response
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
lpackresponse(lua_State *L) {
|
||||||
|
uint32_t session = luaL_checkunsigned(L,1);
|
||||||
|
// clusterd.lua:command.socket call lpackresponse,
|
||||||
|
// and the msg/sz is return by skynet.rawcall , so don't free(msg)
|
||||||
|
int ok = lua_toboolean(L,2);
|
||||||
|
void * msg;
|
||||||
|
size_t sz;
|
||||||
|
|
||||||
|
if (lua_type(L,3) == LUA_TSTRING) {
|
||||||
|
msg = (void *)lua_tolstring(L, 3, &sz);
|
||||||
|
if (sz > 0x1000) {
|
||||||
|
sz = 0x1000;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
msg = lua_touserdata(L,3);
|
||||||
|
sz = luaL_checkunsigned(L, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t buf[TEMP_LENGTH];
|
||||||
|
fill_header(L, buf, sz+5, msg);
|
||||||
|
fill_uint32(buf+2, session);
|
||||||
|
buf[6] = ok;
|
||||||
|
memcpy(buf+7,msg,sz);
|
||||||
|
|
||||||
|
lua_pushlstring(L, (const char *)buf, sz+7);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
string packed response
|
||||||
|
return integer session
|
||||||
|
boolean ok
|
||||||
|
string msg
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
lunpackresponse(lua_State *L) {
|
||||||
|
size_t sz;
|
||||||
|
const char * buf = luaL_checklstring(L, 1, &sz);
|
||||||
|
if (sz < 5) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint32_t session = unpack_uint32((const uint8_t *)buf);
|
||||||
|
lua_pushunsigned(L, session);
|
||||||
|
lua_pushboolean(L, buf[4]);
|
||||||
|
lua_pushlstring(L, buf+5, sz-5);
|
||||||
|
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
luaopen_cluster_c(lua_State *L) {
|
||||||
|
luaL_Reg l[] = {
|
||||||
|
{ "packrequest", lpackrequest },
|
||||||
|
{ "unpackrequest", lunpackrequest },
|
||||||
|
{ "packresponse", lpackresponse },
|
||||||
|
{ "unpackresponse", lunpackresponse },
|
||||||
|
{ NULL, NULL },
|
||||||
|
};
|
||||||
|
luaL_checkversion(L);
|
||||||
|
luaL_newlib(L,l);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
859
lualib-src/lua-crypt.c
Normal file
859
lualib-src/lua-crypt.c
Normal file
@@ -0,0 +1,859 @@
|
|||||||
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define SMALL_CHUNK 256
|
||||||
|
|
||||||
|
/* the eight DES S-boxes */
|
||||||
|
|
||||||
|
uint32_t SB1[64] = {
|
||||||
|
0x01010400, 0x00000000, 0x00010000, 0x01010404,
|
||||||
|
0x01010004, 0x00010404, 0x00000004, 0x00010000,
|
||||||
|
0x00000400, 0x01010400, 0x01010404, 0x00000400,
|
||||||
|
0x01000404, 0x01010004, 0x01000000, 0x00000004,
|
||||||
|
0x00000404, 0x01000400, 0x01000400, 0x00010400,
|
||||||
|
0x00010400, 0x01010000, 0x01010000, 0x01000404,
|
||||||
|
0x00010004, 0x01000004, 0x01000004, 0x00010004,
|
||||||
|
0x00000000, 0x00000404, 0x00010404, 0x01000000,
|
||||||
|
0x00010000, 0x01010404, 0x00000004, 0x01010000,
|
||||||
|
0x01010400, 0x01000000, 0x01000000, 0x00000400,
|
||||||
|
0x01010004, 0x00010000, 0x00010400, 0x01000004,
|
||||||
|
0x00000400, 0x00000004, 0x01000404, 0x00010404,
|
||||||
|
0x01010404, 0x00010004, 0x01010000, 0x01000404,
|
||||||
|
0x01000004, 0x00000404, 0x00010404, 0x01010400,
|
||||||
|
0x00000404, 0x01000400, 0x01000400, 0x00000000,
|
||||||
|
0x00010004, 0x00010400, 0x00000000, 0x01010004
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t SB2[64] = {
|
||||||
|
0x80108020, 0x80008000, 0x00008000, 0x00108020,
|
||||||
|
0x00100000, 0x00000020, 0x80100020, 0x80008020,
|
||||||
|
0x80000020, 0x80108020, 0x80108000, 0x80000000,
|
||||||
|
0x80008000, 0x00100000, 0x00000020, 0x80100020,
|
||||||
|
0x00108000, 0x00100020, 0x80008020, 0x00000000,
|
||||||
|
0x80000000, 0x00008000, 0x00108020, 0x80100000,
|
||||||
|
0x00100020, 0x80000020, 0x00000000, 0x00108000,
|
||||||
|
0x00008020, 0x80108000, 0x80100000, 0x00008020,
|
||||||
|
0x00000000, 0x00108020, 0x80100020, 0x00100000,
|
||||||
|
0x80008020, 0x80100000, 0x80108000, 0x00008000,
|
||||||
|
0x80100000, 0x80008000, 0x00000020, 0x80108020,
|
||||||
|
0x00108020, 0x00000020, 0x00008000, 0x80000000,
|
||||||
|
0x00008020, 0x80108000, 0x00100000, 0x80000020,
|
||||||
|
0x00100020, 0x80008020, 0x80000020, 0x00100020,
|
||||||
|
0x00108000, 0x00000000, 0x80008000, 0x00008020,
|
||||||
|
0x80000000, 0x80100020, 0x80108020, 0x00108000
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t SB3[64] = {
|
||||||
|
0x00000208, 0x08020200, 0x00000000, 0x08020008,
|
||||||
|
0x08000200, 0x00000000, 0x00020208, 0x08000200,
|
||||||
|
0x00020008, 0x08000008, 0x08000008, 0x00020000,
|
||||||
|
0x08020208, 0x00020008, 0x08020000, 0x00000208,
|
||||||
|
0x08000000, 0x00000008, 0x08020200, 0x00000200,
|
||||||
|
0x00020200, 0x08020000, 0x08020008, 0x00020208,
|
||||||
|
0x08000208, 0x00020200, 0x00020000, 0x08000208,
|
||||||
|
0x00000008, 0x08020208, 0x00000200, 0x08000000,
|
||||||
|
0x08020200, 0x08000000, 0x00020008, 0x00000208,
|
||||||
|
0x00020000, 0x08020200, 0x08000200, 0x00000000,
|
||||||
|
0x00000200, 0x00020008, 0x08020208, 0x08000200,
|
||||||
|
0x08000008, 0x00000200, 0x00000000, 0x08020008,
|
||||||
|
0x08000208, 0x00020000, 0x08000000, 0x08020208,
|
||||||
|
0x00000008, 0x00020208, 0x00020200, 0x08000008,
|
||||||
|
0x08020000, 0x08000208, 0x00000208, 0x08020000,
|
||||||
|
0x00020208, 0x00000008, 0x08020008, 0x00020200
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t SB4[64] = {
|
||||||
|
0x00802001, 0x00002081, 0x00002081, 0x00000080,
|
||||||
|
0x00802080, 0x00800081, 0x00800001, 0x00002001,
|
||||||
|
0x00000000, 0x00802000, 0x00802000, 0x00802081,
|
||||||
|
0x00000081, 0x00000000, 0x00800080, 0x00800001,
|
||||||
|
0x00000001, 0x00002000, 0x00800000, 0x00802001,
|
||||||
|
0x00000080, 0x00800000, 0x00002001, 0x00002080,
|
||||||
|
0x00800081, 0x00000001, 0x00002080, 0x00800080,
|
||||||
|
0x00002000, 0x00802080, 0x00802081, 0x00000081,
|
||||||
|
0x00800080, 0x00800001, 0x00802000, 0x00802081,
|
||||||
|
0x00000081, 0x00000000, 0x00000000, 0x00802000,
|
||||||
|
0x00002080, 0x00800080, 0x00800081, 0x00000001,
|
||||||
|
0x00802001, 0x00002081, 0x00002081, 0x00000080,
|
||||||
|
0x00802081, 0x00000081, 0x00000001, 0x00002000,
|
||||||
|
0x00800001, 0x00002001, 0x00802080, 0x00800081,
|
||||||
|
0x00002001, 0x00002080, 0x00800000, 0x00802001,
|
||||||
|
0x00000080, 0x00800000, 0x00002000, 0x00802080
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t SB5[64] = {
|
||||||
|
0x00000100, 0x02080100, 0x02080000, 0x42000100,
|
||||||
|
0x00080000, 0x00000100, 0x40000000, 0x02080000,
|
||||||
|
0x40080100, 0x00080000, 0x02000100, 0x40080100,
|
||||||
|
0x42000100, 0x42080000, 0x00080100, 0x40000000,
|
||||||
|
0x02000000, 0x40080000, 0x40080000, 0x00000000,
|
||||||
|
0x40000100, 0x42080100, 0x42080100, 0x02000100,
|
||||||
|
0x42080000, 0x40000100, 0x00000000, 0x42000000,
|
||||||
|
0x02080100, 0x02000000, 0x42000000, 0x00080100,
|
||||||
|
0x00080000, 0x42000100, 0x00000100, 0x02000000,
|
||||||
|
0x40000000, 0x02080000, 0x42000100, 0x40080100,
|
||||||
|
0x02000100, 0x40000000, 0x42080000, 0x02080100,
|
||||||
|
0x40080100, 0x00000100, 0x02000000, 0x42080000,
|
||||||
|
0x42080100, 0x00080100, 0x42000000, 0x42080100,
|
||||||
|
0x02080000, 0x00000000, 0x40080000, 0x42000000,
|
||||||
|
0x00080100, 0x02000100, 0x40000100, 0x00080000,
|
||||||
|
0x00000000, 0x40080000, 0x02080100, 0x40000100
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t SB6[64] = {
|
||||||
|
0x20000010, 0x20400000, 0x00004000, 0x20404010,
|
||||||
|
0x20400000, 0x00000010, 0x20404010, 0x00400000,
|
||||||
|
0x20004000, 0x00404010, 0x00400000, 0x20000010,
|
||||||
|
0x00400010, 0x20004000, 0x20000000, 0x00004010,
|
||||||
|
0x00000000, 0x00400010, 0x20004010, 0x00004000,
|
||||||
|
0x00404000, 0x20004010, 0x00000010, 0x20400010,
|
||||||
|
0x20400010, 0x00000000, 0x00404010, 0x20404000,
|
||||||
|
0x00004010, 0x00404000, 0x20404000, 0x20000000,
|
||||||
|
0x20004000, 0x00000010, 0x20400010, 0x00404000,
|
||||||
|
0x20404010, 0x00400000, 0x00004010, 0x20000010,
|
||||||
|
0x00400000, 0x20004000, 0x20000000, 0x00004010,
|
||||||
|
0x20000010, 0x20404010, 0x00404000, 0x20400000,
|
||||||
|
0x00404010, 0x20404000, 0x00000000, 0x20400010,
|
||||||
|
0x00000010, 0x00004000, 0x20400000, 0x00404010,
|
||||||
|
0x00004000, 0x00400010, 0x20004010, 0x00000000,
|
||||||
|
0x20404000, 0x20000000, 0x00400010, 0x20004010
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t SB7[64] = {
|
||||||
|
0x00200000, 0x04200002, 0x04000802, 0x00000000,
|
||||||
|
0x00000800, 0x04000802, 0x00200802, 0x04200800,
|
||||||
|
0x04200802, 0x00200000, 0x00000000, 0x04000002,
|
||||||
|
0x00000002, 0x04000000, 0x04200002, 0x00000802,
|
||||||
|
0x04000800, 0x00200802, 0x00200002, 0x04000800,
|
||||||
|
0x04000002, 0x04200000, 0x04200800, 0x00200002,
|
||||||
|
0x04200000, 0x00000800, 0x00000802, 0x04200802,
|
||||||
|
0x00200800, 0x00000002, 0x04000000, 0x00200800,
|
||||||
|
0x04000000, 0x00200800, 0x00200000, 0x04000802,
|
||||||
|
0x04000802, 0x04200002, 0x04200002, 0x00000002,
|
||||||
|
0x00200002, 0x04000000, 0x04000800, 0x00200000,
|
||||||
|
0x04200800, 0x00000802, 0x00200802, 0x04200800,
|
||||||
|
0x00000802, 0x04000002, 0x04200802, 0x04200000,
|
||||||
|
0x00200800, 0x00000000, 0x00000002, 0x04200802,
|
||||||
|
0x00000000, 0x00200802, 0x04200000, 0x00000800,
|
||||||
|
0x04000002, 0x04000800, 0x00000800, 0x00200002
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t SB8[64] = {
|
||||||
|
0x10001040, 0x00001000, 0x00040000, 0x10041040,
|
||||||
|
0x10000000, 0x10001040, 0x00000040, 0x10000000,
|
||||||
|
0x00040040, 0x10040000, 0x10041040, 0x00041000,
|
||||||
|
0x10041000, 0x00041040, 0x00001000, 0x00000040,
|
||||||
|
0x10040000, 0x10000040, 0x10001000, 0x00001040,
|
||||||
|
0x00041000, 0x00040040, 0x10040040, 0x10041000,
|
||||||
|
0x00001040, 0x00000000, 0x00000000, 0x10040040,
|
||||||
|
0x10000040, 0x10001000, 0x00041040, 0x00040000,
|
||||||
|
0x00041040, 0x00040000, 0x10041000, 0x00001000,
|
||||||
|
0x00000040, 0x10040040, 0x00001000, 0x00041040,
|
||||||
|
0x10001000, 0x00000040, 0x10000040, 0x10040000,
|
||||||
|
0x10040040, 0x10000000, 0x00040000, 0x10001040,
|
||||||
|
0x00000000, 0x10041040, 0x00040040, 0x10000040,
|
||||||
|
0x10040000, 0x10001000, 0x10001040, 0x00000000,
|
||||||
|
0x10041040, 0x00041000, 0x00041000, 0x00001040,
|
||||||
|
0x00001040, 0x00040040, 0x10000000, 0x10041000
|
||||||
|
};
|
||||||
|
|
||||||
|
/* PC1: left and right halves bit-swap */
|
||||||
|
|
||||||
|
static uint32_t LHs[16] = {
|
||||||
|
0x00000000, 0x00000001, 0x00000100, 0x00000101,
|
||||||
|
0x00010000, 0x00010001, 0x00010100, 0x00010101,
|
||||||
|
0x01000000, 0x01000001, 0x01000100, 0x01000101,
|
||||||
|
0x01010000, 0x01010001, 0x01010100, 0x01010101
|
||||||
|
};
|
||||||
|
|
||||||
|
static uint32_t RHs[16] = {
|
||||||
|
0x00000000, 0x01000000, 0x00010000, 0x01010000,
|
||||||
|
0x00000100, 0x01000100, 0x00010100, 0x01010100,
|
||||||
|
0x00000001, 0x01000001, 0x00010001, 0x01010001,
|
||||||
|
0x00000101, 0x01000101, 0x00010101, 0x01010101,
|
||||||
|
};
|
||||||
|
|
||||||
|
/* platform-independant 32-bit integer manipulation macros */
|
||||||
|
|
||||||
|
#define GET_UINT32(n,b,i) \
|
||||||
|
{ \
|
||||||
|
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
||||||
|
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
||||||
|
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
||||||
|
| ( (uint32_t) (b)[(i) + 3] ); \
|
||||||
|
}
|
||||||
|
|
||||||
|
#define PUT_UINT32(n,b,i) \
|
||||||
|
{ \
|
||||||
|
(b)[(i) ] = (uint8_t) ( (n) >> 24 ); \
|
||||||
|
(b)[(i) + 1] = (uint8_t) ( (n) >> 16 ); \
|
||||||
|
(b)[(i) + 2] = (uint8_t) ( (n) >> 8 ); \
|
||||||
|
(b)[(i) + 3] = (uint8_t) ( (n) ); \
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initial Permutation macro */
|
||||||
|
|
||||||
|
#define DES_IP(X,Y) \
|
||||||
|
{ \
|
||||||
|
T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
|
||||||
|
T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
|
||||||
|
T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
|
||||||
|
T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
|
||||||
|
Y = ((Y << 1) | (Y >> 31)) & 0xFFFFFFFF; \
|
||||||
|
T = (X ^ Y) & 0xAAAAAAAA; Y ^= T; X ^= T; \
|
||||||
|
X = ((X << 1) | (X >> 31)) & 0xFFFFFFFF; \
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Final Permutation macro */
|
||||||
|
|
||||||
|
#define DES_FP(X,Y) \
|
||||||
|
{ \
|
||||||
|
X = ((X << 31) | (X >> 1)) & 0xFFFFFFFF; \
|
||||||
|
T = (X ^ Y) & 0xAAAAAAAA; X ^= T; Y ^= T; \
|
||||||
|
Y = ((Y << 31) | (Y >> 1)) & 0xFFFFFFFF; \
|
||||||
|
T = ((Y >> 8) ^ X) & 0x00FF00FF; X ^= T; Y ^= (T << 8); \
|
||||||
|
T = ((Y >> 2) ^ X) & 0x33333333; X ^= T; Y ^= (T << 2); \
|
||||||
|
T = ((X >> 16) ^ Y) & 0x0000FFFF; Y ^= T; X ^= (T << 16); \
|
||||||
|
T = ((X >> 4) ^ Y) & 0x0F0F0F0F; Y ^= T; X ^= (T << 4); \
|
||||||
|
}
|
||||||
|
|
||||||
|
/* DES round macro */
|
||||||
|
|
||||||
|
#define DES_ROUND(X,Y) \
|
||||||
|
{ \
|
||||||
|
T = *SK++ ^ X; \
|
||||||
|
Y ^= SB8[ (T ) & 0x3F ] ^ \
|
||||||
|
SB6[ (T >> 8) & 0x3F ] ^ \
|
||||||
|
SB4[ (T >> 16) & 0x3F ] ^ \
|
||||||
|
SB2[ (T >> 24) & 0x3F ]; \
|
||||||
|
\
|
||||||
|
T = *SK++ ^ ((X << 28) | (X >> 4)); \
|
||||||
|
Y ^= SB7[ (T ) & 0x3F ] ^ \
|
||||||
|
SB5[ (T >> 8) & 0x3F ] ^ \
|
||||||
|
SB3[ (T >> 16) & 0x3F ] ^ \
|
||||||
|
SB1[ (T >> 24) & 0x3F ]; \
|
||||||
|
}
|
||||||
|
|
||||||
|
/* DES key schedule */
|
||||||
|
|
||||||
|
static void
|
||||||
|
des_main_ks( uint32_t SK[32], const uint8_t key[8] ) {
|
||||||
|
int i;
|
||||||
|
uint32_t X, Y, T;
|
||||||
|
|
||||||
|
GET_UINT32( X, key, 0 );
|
||||||
|
GET_UINT32( Y, key, 4 );
|
||||||
|
|
||||||
|
/* Permuted Choice 1 */
|
||||||
|
|
||||||
|
T = ((Y >> 4) ^ X) & 0x0F0F0F0F; X ^= T; Y ^= (T << 4);
|
||||||
|
T = ((Y ) ^ X) & 0x10101010; X ^= T; Y ^= (T );
|
||||||
|
|
||||||
|
X = (LHs[ (X ) & 0xF] << 3) | (LHs[ (X >> 8) & 0xF ] << 2)
|
||||||
|
| (LHs[ (X >> 16) & 0xF] << 1) | (LHs[ (X >> 24) & 0xF ] )
|
||||||
|
| (LHs[ (X >> 5) & 0xF] << 7) | (LHs[ (X >> 13) & 0xF ] << 6)
|
||||||
|
| (LHs[ (X >> 21) & 0xF] << 5) | (LHs[ (X >> 29) & 0xF ] << 4);
|
||||||
|
|
||||||
|
Y = (RHs[ (Y >> 1) & 0xF] << 3) | (RHs[ (Y >> 9) & 0xF ] << 2)
|
||||||
|
| (RHs[ (Y >> 17) & 0xF] << 1) | (RHs[ (Y >> 25) & 0xF ] )
|
||||||
|
| (RHs[ (Y >> 4) & 0xF] << 7) | (RHs[ (Y >> 12) & 0xF ] << 6)
|
||||||
|
| (RHs[ (Y >> 20) & 0xF] << 5) | (RHs[ (Y >> 28) & 0xF ] << 4);
|
||||||
|
|
||||||
|
X &= 0x0FFFFFFF;
|
||||||
|
Y &= 0x0FFFFFFF;
|
||||||
|
|
||||||
|
/* calculate subkeys */
|
||||||
|
|
||||||
|
for( i = 0; i < 16; i++ )
|
||||||
|
{
|
||||||
|
if( i < 2 || i == 8 || i == 15 )
|
||||||
|
{
|
||||||
|
X = ((X << 1) | (X >> 27)) & 0x0FFFFFFF;
|
||||||
|
Y = ((Y << 1) | (Y >> 27)) & 0x0FFFFFFF;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
X = ((X << 2) | (X >> 26)) & 0x0FFFFFFF;
|
||||||
|
Y = ((Y << 2) | (Y >> 26)) & 0x0FFFFFFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
*SK++ = ((X << 4) & 0x24000000) | ((X << 28) & 0x10000000)
|
||||||
|
| ((X << 14) & 0x08000000) | ((X << 18) & 0x02080000)
|
||||||
|
| ((X << 6) & 0x01000000) | ((X << 9) & 0x00200000)
|
||||||
|
| ((X >> 1) & 0x00100000) | ((X << 10) & 0x00040000)
|
||||||
|
| ((X << 2) & 0x00020000) | ((X >> 10) & 0x00010000)
|
||||||
|
| ((Y >> 13) & 0x00002000) | ((Y >> 4) & 0x00001000)
|
||||||
|
| ((Y << 6) & 0x00000800) | ((Y >> 1) & 0x00000400)
|
||||||
|
| ((Y >> 14) & 0x00000200) | ((Y ) & 0x00000100)
|
||||||
|
| ((Y >> 5) & 0x00000020) | ((Y >> 10) & 0x00000010)
|
||||||
|
| ((Y >> 3) & 0x00000008) | ((Y >> 18) & 0x00000004)
|
||||||
|
| ((Y >> 26) & 0x00000002) | ((Y >> 24) & 0x00000001);
|
||||||
|
|
||||||
|
*SK++ = ((X << 15) & 0x20000000) | ((X << 17) & 0x10000000)
|
||||||
|
| ((X << 10) & 0x08000000) | ((X << 22) & 0x04000000)
|
||||||
|
| ((X >> 2) & 0x02000000) | ((X << 1) & 0x01000000)
|
||||||
|
| ((X << 16) & 0x00200000) | ((X << 11) & 0x00100000)
|
||||||
|
| ((X << 3) & 0x00080000) | ((X >> 6) & 0x00040000)
|
||||||
|
| ((X << 15) & 0x00020000) | ((X >> 4) & 0x00010000)
|
||||||
|
| ((Y >> 2) & 0x00002000) | ((Y << 8) & 0x00001000)
|
||||||
|
| ((Y >> 14) & 0x00000808) | ((Y >> 9) & 0x00000400)
|
||||||
|
| ((Y ) & 0x00000200) | ((Y << 7) & 0x00000100)
|
||||||
|
| ((Y >> 7) & 0x00000020) | ((Y >> 3) & 0x00000011)
|
||||||
|
| ((Y << 2) & 0x00000004) | ((Y >> 21) & 0x00000002);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* DES 64-bit block encryption/decryption */
|
||||||
|
|
||||||
|
static void
|
||||||
|
des_crypt( const uint32_t SK[32], const uint8_t input[8], uint8_t output[8] ) {
|
||||||
|
uint32_t X, Y, T;
|
||||||
|
|
||||||
|
GET_UINT32( X, input, 0 );
|
||||||
|
GET_UINT32( Y, input, 4 );
|
||||||
|
|
||||||
|
DES_IP( X, Y );
|
||||||
|
|
||||||
|
DES_ROUND( Y, X ); DES_ROUND( X, Y );
|
||||||
|
DES_ROUND( Y, X ); DES_ROUND( X, Y );
|
||||||
|
DES_ROUND( Y, X ); DES_ROUND( X, Y );
|
||||||
|
DES_ROUND( Y, X ); DES_ROUND( X, Y );
|
||||||
|
DES_ROUND( Y, X ); DES_ROUND( X, Y );
|
||||||
|
DES_ROUND( Y, X ); DES_ROUND( X, Y );
|
||||||
|
DES_ROUND( Y, X ); DES_ROUND( X, Y );
|
||||||
|
DES_ROUND( Y, X ); DES_ROUND( X, Y );
|
||||||
|
|
||||||
|
DES_FP( Y, X );
|
||||||
|
|
||||||
|
PUT_UINT32( Y, output, 0 );
|
||||||
|
PUT_UINT32( X, output, 4 );
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lrandomkey(lua_State *L) {
|
||||||
|
char tmp[8];
|
||||||
|
int i;
|
||||||
|
for (i=0;i<8;i++) {
|
||||||
|
tmp[i] = random() & 0xff;
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, tmp, 8);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
des_key(lua_State *L, uint32_t SK[32]) {
|
||||||
|
size_t keysz = 0;
|
||||||
|
const void * key = luaL_checklstring(L, 1, &keysz);
|
||||||
|
if (keysz != 8) {
|
||||||
|
luaL_error(L, "Invalid key size %d, need 8 bytes", (int)keysz);
|
||||||
|
}
|
||||||
|
des_main_ks(SK, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
ldesencode(lua_State *L) {
|
||||||
|
uint32_t SK[32];
|
||||||
|
des_key(L, SK);
|
||||||
|
|
||||||
|
size_t textsz = 0;
|
||||||
|
const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 2, &textsz);
|
||||||
|
size_t chunksz = (textsz + 8) & ~7;
|
||||||
|
uint8_t tmp[SMALL_CHUNK];
|
||||||
|
uint8_t *buffer = tmp;
|
||||||
|
if (chunksz > SMALL_CHUNK) {
|
||||||
|
buffer = lua_newuserdata(L, chunksz);
|
||||||
|
}
|
||||||
|
int i;
|
||||||
|
for (i=0;i<(int)textsz-7;i+=8) {
|
||||||
|
des_crypt(SK, text+i, buffer+i);
|
||||||
|
}
|
||||||
|
int bytes = textsz - i;
|
||||||
|
uint8_t tail[8];
|
||||||
|
int j;
|
||||||
|
for (j=0;j<8;j++) {
|
||||||
|
if (j < bytes) {
|
||||||
|
tail[j] = text[i+j];
|
||||||
|
} else if (j==bytes) {
|
||||||
|
tail[j] = 0x80;
|
||||||
|
} else {
|
||||||
|
tail[j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
des_crypt(SK, tail, buffer+i);
|
||||||
|
lua_pushlstring(L, (const char *)buffer, chunksz);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
ldesdecode(lua_State *L) {
|
||||||
|
uint32_t ESK[32];
|
||||||
|
des_key(L, ESK);
|
||||||
|
uint32_t SK[32];
|
||||||
|
int i;
|
||||||
|
for( i = 0; i < 32; i += 2 ) {
|
||||||
|
SK[i] = ESK[30 - i];
|
||||||
|
SK[i + 1] = ESK[31 - i];
|
||||||
|
}
|
||||||
|
size_t textsz = 0;
|
||||||
|
const uint8_t *text = (const uint8_t *)luaL_checklstring(L, 2, &textsz);
|
||||||
|
if ((textsz & 7) || textsz == 0) {
|
||||||
|
return luaL_error(L, "Invalid des crypt text length %d", (int)textsz);
|
||||||
|
}
|
||||||
|
uint8_t tmp[SMALL_CHUNK];
|
||||||
|
uint8_t *buffer = tmp;
|
||||||
|
if (textsz > SMALL_CHUNK) {
|
||||||
|
buffer = lua_newuserdata(L, textsz);
|
||||||
|
}
|
||||||
|
for (i=0;i<textsz;i+=8) {
|
||||||
|
des_crypt(SK, text+i, buffer+i);
|
||||||
|
}
|
||||||
|
int padding = 1;
|
||||||
|
for (i=textsz-1;i>=textsz-8;i--) {
|
||||||
|
if (buffer[i] == 0) {
|
||||||
|
padding++;
|
||||||
|
} else if (buffer[i] == 0x80) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
return luaL_error(L, "Invalid des crypt text");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (padding > 8) {
|
||||||
|
return luaL_error(L, "Invalid des crypt text");
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, (const char *)buffer, textsz - padding);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
Hash(const char * str, int sz, uint8_t key[8]) {
|
||||||
|
uint32_t djb_hash = 5381L;
|
||||||
|
uint32_t js_hash = 1315423911L;
|
||||||
|
|
||||||
|
int i;
|
||||||
|
for (i=0;i<sz;i++) {
|
||||||
|
uint8_t c = (uint8_t)str[i];
|
||||||
|
djb_hash += (djb_hash << 5) + c;
|
||||||
|
js_hash ^= ((js_hash << 5) + c + (js_hash >> 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
key[0] = djb_hash & 0xff;
|
||||||
|
key[1] = (djb_hash >> 8) & 0xff;
|
||||||
|
key[2] = (djb_hash >> 16) & 0xff;
|
||||||
|
key[3] = (djb_hash >> 24) & 0xff;
|
||||||
|
|
||||||
|
key[4] = js_hash & 0xff;
|
||||||
|
key[5] = (js_hash >> 8) & 0xff;
|
||||||
|
key[6] = (js_hash >> 16) & 0xff;
|
||||||
|
key[7] = (js_hash >> 24) & 0xff;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lhashkey(lua_State *L) {
|
||||||
|
size_t sz = 0;
|
||||||
|
const char * key = luaL_checklstring(L, 1, &sz);
|
||||||
|
uint8_t realkey[8];
|
||||||
|
Hash(key,(int)sz,realkey);
|
||||||
|
lua_pushlstring(L, (const char *)realkey, 8);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
ltohex(lua_State *L) {
|
||||||
|
static char hex[] = "0123456789abcdef";
|
||||||
|
size_t sz = 0;
|
||||||
|
const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz);
|
||||||
|
char tmp[SMALL_CHUNK];
|
||||||
|
char *buffer = tmp;
|
||||||
|
if (sz > SMALL_CHUNK/2) {
|
||||||
|
buffer = lua_newuserdata(L, sz * 2);
|
||||||
|
}
|
||||||
|
int i;
|
||||||
|
for (i=0;i<sz;i++) {
|
||||||
|
buffer[i*2] = hex[text[i] >> 4];
|
||||||
|
buffer[i*2+1] = hex[text[i] & 0xf];
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, buffer, sz * 2);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define HEX(v,c) { char tmp = (char) c; if (tmp >= '0' && tmp <= '9') { v = tmp-'0'; } else { v = tmp - 'a' + 10; } }
|
||||||
|
|
||||||
|
static int
|
||||||
|
lfromhex(lua_State *L) {
|
||||||
|
size_t sz = 0;
|
||||||
|
const char * text = luaL_checklstring(L, 1, &sz);
|
||||||
|
if (sz & 2) {
|
||||||
|
return luaL_error(L, "Invalid hex text size %d", (int)sz);
|
||||||
|
}
|
||||||
|
char tmp[SMALL_CHUNK];
|
||||||
|
char *buffer = tmp;
|
||||||
|
if (sz > SMALL_CHUNK*2) {
|
||||||
|
buffer = lua_newuserdata(L, sz / 2);
|
||||||
|
}
|
||||||
|
int i;
|
||||||
|
for (i=0;i<sz;i+=2) {
|
||||||
|
uint8_t hi,low;
|
||||||
|
HEX(hi, text[i]);
|
||||||
|
HEX(low, text[i+1]);
|
||||||
|
if (hi > 16 || low > 16) {
|
||||||
|
return luaL_error(L, "Invalid hex text", text);
|
||||||
|
}
|
||||||
|
buffer[i/2] = hi<<4 | low;
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, buffer, i/2);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constants are the integer part of the sines of integers (in radians) * 2^32.
|
||||||
|
const uint32_t k[64] = {
|
||||||
|
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee ,
|
||||||
|
0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 ,
|
||||||
|
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be ,
|
||||||
|
0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 ,
|
||||||
|
0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa ,
|
||||||
|
0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 ,
|
||||||
|
0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed ,
|
||||||
|
0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a ,
|
||||||
|
0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c ,
|
||||||
|
0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 ,
|
||||||
|
0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 ,
|
||||||
|
0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 ,
|
||||||
|
0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 ,
|
||||||
|
0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 ,
|
||||||
|
0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 ,
|
||||||
|
0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };
|
||||||
|
|
||||||
|
// r specifies the per-round shift amounts
|
||||||
|
const uint32_t r[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
|
||||||
|
5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
|
||||||
|
4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
|
||||||
|
6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21};
|
||||||
|
|
||||||
|
// leftrotate function definition
|
||||||
|
#define LEFTROTATE(x, c) (((x) << (c)) | ((x) >> (32 - (c))))
|
||||||
|
|
||||||
|
static void
|
||||||
|
hmac(uint32_t x[2], uint32_t y[2], uint32_t result[2]) {
|
||||||
|
uint32_t w[16];
|
||||||
|
uint32_t a, b, c, d, f, g, temp;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
a = 0x67452301u;
|
||||||
|
b = 0xefcdab89u;
|
||||||
|
c = 0x98badcfeu;
|
||||||
|
d = 0x10325476u;
|
||||||
|
|
||||||
|
for (i=0;i<16;i+=4) {
|
||||||
|
w[i] = x[1];
|
||||||
|
w[i+1] = x[0];
|
||||||
|
w[i+2] = y[1];
|
||||||
|
w[i+3] = y[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0; i<64; i++) {
|
||||||
|
if (i < 16) {
|
||||||
|
f = (b & c) | ((~b) & d);
|
||||||
|
g = i;
|
||||||
|
} else if (i < 32) {
|
||||||
|
f = (d & b) | ((~d) & c);
|
||||||
|
g = (5*i + 1) % 16;
|
||||||
|
} else if (i < 48) {
|
||||||
|
f = b ^ c ^ d;
|
||||||
|
g = (3*i + 5) % 16;
|
||||||
|
} else {
|
||||||
|
f = c ^ (b | (~d));
|
||||||
|
g = (7*i) % 16;
|
||||||
|
}
|
||||||
|
|
||||||
|
temp = d;
|
||||||
|
d = c;
|
||||||
|
c = b;
|
||||||
|
b = b + LEFTROTATE((a + f + k[i] + w[g]), r[i]);
|
||||||
|
a = temp;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
result[0] = c^d;
|
||||||
|
result[1] = a^b;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
read64(lua_State *L, uint32_t xx[2], uint32_t yy[2]) {
|
||||||
|
size_t sz = 0;
|
||||||
|
const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz);
|
||||||
|
if (sz != 8) {
|
||||||
|
luaL_error(L, "Invalid hmac x");
|
||||||
|
}
|
||||||
|
const uint8_t *y = (const uint8_t *)luaL_checklstring(L, 2, &sz);
|
||||||
|
if (sz != 8) {
|
||||||
|
luaL_error(L, "Invalid hmac y");
|
||||||
|
}
|
||||||
|
xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24;
|
||||||
|
xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24;
|
||||||
|
yy[0] = y[0] | y[1]<<8 | y[2]<<16 | y[3]<<24;
|
||||||
|
yy[1] = y[4] | y[5]<<8 | y[6]<<16 | y[7]<<24;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lhmac64(lua_State *L) {
|
||||||
|
uint32_t x[2], y[2];
|
||||||
|
read64(L, x, y);
|
||||||
|
uint32_t result[2];
|
||||||
|
hmac(x,y,result);
|
||||||
|
uint8_t tmp[8];
|
||||||
|
tmp[0] = result[0] & 0xff;
|
||||||
|
tmp[1] = (result[0] >> 8 )& 0xff;
|
||||||
|
tmp[2] = (result[0] >> 16 )& 0xff;
|
||||||
|
tmp[3] = (result[0] >> 24 )& 0xff;
|
||||||
|
tmp[4] = result[1] & 0xff;
|
||||||
|
tmp[5] = (result[1] >> 8 )& 0xff;
|
||||||
|
tmp[6] = (result[1] >> 16 )& 0xff;
|
||||||
|
tmp[7] = (result[1] >> 24 )& 0xff;
|
||||||
|
|
||||||
|
lua_pushlstring(L, (const char *)tmp, 8);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// powmodp64 for DH-key exchange
|
||||||
|
|
||||||
|
// The biggest 64bit prime
|
||||||
|
#define P 0xffffffffffffffc5ull
|
||||||
|
|
||||||
|
static inline uint64_t
|
||||||
|
mul_mod_p(uint64_t a, uint64_t b) {
|
||||||
|
uint64_t m = 0;
|
||||||
|
while(b) {
|
||||||
|
if(b&1) {
|
||||||
|
uint64_t t = P-a;
|
||||||
|
if ( m >= t) {
|
||||||
|
m -= t;
|
||||||
|
} else {
|
||||||
|
m += a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (a >= P - a) {
|
||||||
|
a = a * 2 - P;
|
||||||
|
} else {
|
||||||
|
a = a * 2;
|
||||||
|
}
|
||||||
|
b>>=1;
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline uint64_t
|
||||||
|
pow_mod_p(uint64_t a, uint64_t b) {
|
||||||
|
if (b==1) {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
uint64_t t = pow_mod_p(a, b>>1);
|
||||||
|
t = mul_mod_p(t,t);
|
||||||
|
if (b % 2) {
|
||||||
|
t = mul_mod_p(t, a);
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
// calc a^b % p
|
||||||
|
static uint64_t
|
||||||
|
powmodp(uint64_t a, uint64_t b) {
|
||||||
|
if (a > P)
|
||||||
|
a%=P;
|
||||||
|
return pow_mod_p(a,b);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
push64(lua_State *L, uint64_t r) {
|
||||||
|
uint8_t tmp[8];
|
||||||
|
tmp[0] = r & 0xff;
|
||||||
|
tmp[1] = (r >> 8 )& 0xff;
|
||||||
|
tmp[2] = (r >> 16 )& 0xff;
|
||||||
|
tmp[3] = (r >> 24 )& 0xff;
|
||||||
|
tmp[4] = (r >> 32 )& 0xff;
|
||||||
|
tmp[5] = (r >> 40 )& 0xff;
|
||||||
|
tmp[6] = (r >> 48 )& 0xff;
|
||||||
|
tmp[7] = (r >> 56 )& 0xff;
|
||||||
|
|
||||||
|
lua_pushlstring(L, (const char *)tmp, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
ldhsecret(lua_State *L) {
|
||||||
|
uint32_t x[2], y[2];
|
||||||
|
read64(L, x, y);
|
||||||
|
uint64_t r = powmodp((uint64_t)x[0] | (uint64_t)x[1]<<32,
|
||||||
|
(uint64_t)y[0] | (uint64_t)y[1]<<32);
|
||||||
|
|
||||||
|
push64(L, r);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define G 5
|
||||||
|
|
||||||
|
static int
|
||||||
|
ldhexchange(lua_State *L) {
|
||||||
|
size_t sz = 0;
|
||||||
|
const uint8_t *x = (const uint8_t *)luaL_checklstring(L, 1, &sz);
|
||||||
|
if (sz != 8) {
|
||||||
|
luaL_error(L, "Invalid hmac x");
|
||||||
|
}
|
||||||
|
uint32_t xx[2];
|
||||||
|
xx[0] = x[0] | x[1]<<8 | x[2]<<16 | x[3]<<24;
|
||||||
|
xx[1] = x[4] | x[5]<<8 | x[6]<<16 | x[7]<<24;
|
||||||
|
|
||||||
|
uint64_t r = powmodp(5, (uint64_t)xx[0] | (uint64_t)xx[1]<<32);
|
||||||
|
push64(L, r);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// base64
|
||||||
|
|
||||||
|
static int
|
||||||
|
lb64encode(lua_State *L) {
|
||||||
|
static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
size_t sz = 0;
|
||||||
|
const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz);
|
||||||
|
int encode_sz = (sz + 2)/3*4;
|
||||||
|
char tmp[SMALL_CHUNK];
|
||||||
|
char *buffer = tmp;
|
||||||
|
if (encode_sz > SMALL_CHUNK) {
|
||||||
|
buffer = lua_newuserdata(L, encode_sz);
|
||||||
|
}
|
||||||
|
int i,j;
|
||||||
|
j=0;
|
||||||
|
for (i=0;i<(int)sz-2;i+=3) {
|
||||||
|
uint32_t v = text[i] << 16 | text[i+1] << 8 | text[i+2];
|
||||||
|
buffer[j] = encoding[v >> 18];
|
||||||
|
buffer[j+1] = encoding[(v >> 12) & 0x3f];
|
||||||
|
buffer[j+2] = encoding[(v >> 6) & 0x3f];
|
||||||
|
buffer[j+3] = encoding[(v) & 0x3f];
|
||||||
|
j+=4;
|
||||||
|
}
|
||||||
|
int padding = sz-i;
|
||||||
|
uint32_t v;
|
||||||
|
switch(padding) {
|
||||||
|
case 1 :
|
||||||
|
v = text[i];
|
||||||
|
buffer[j] = encoding[v >> 2];
|
||||||
|
buffer[j+1] = encoding[(v & 3) << 4];
|
||||||
|
buffer[j+2] = '=';
|
||||||
|
buffer[j+3] = '=';
|
||||||
|
break;
|
||||||
|
case 2 :
|
||||||
|
v = text[i] << 8 | text[i+1];
|
||||||
|
buffer[j] = encoding[v >> 10];
|
||||||
|
buffer[j+1] = encoding[(v >> 4) & 0x3f];
|
||||||
|
buffer[j+2] = encoding[(v & 0xf) << 2];
|
||||||
|
buffer[j+3] = '=';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, buffer, encode_sz);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int
|
||||||
|
b64index(uint8_t c) {
|
||||||
|
static const int decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
|
||||||
|
int decoding_size = sizeof(decoding)/sizeof(decoding[0]);
|
||||||
|
if (c<43) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
c -= 43;
|
||||||
|
if (c>=decoding_size)
|
||||||
|
return -1;
|
||||||
|
return decoding[c];
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lb64decode(lua_State *L) {
|
||||||
|
size_t sz = 0;
|
||||||
|
const uint8_t * text = (const uint8_t *)luaL_checklstring(L, 1, &sz);
|
||||||
|
int decode_sz = (sz+3)/4*3;
|
||||||
|
char tmp[SMALL_CHUNK];
|
||||||
|
char *buffer = tmp;
|
||||||
|
if (decode_sz > SMALL_CHUNK) {
|
||||||
|
buffer = lua_newuserdata(L, decode_sz);
|
||||||
|
}
|
||||||
|
int i,j;
|
||||||
|
int output = 0;
|
||||||
|
for (i=0;i<sz;) {
|
||||||
|
int padding = 0;
|
||||||
|
int c[4];
|
||||||
|
for (j=0;j<4;) {
|
||||||
|
if (i>=sz) {
|
||||||
|
return luaL_error(L, "Invalid base64 text");
|
||||||
|
}
|
||||||
|
c[j] = b64index(text[i]);
|
||||||
|
if (c[j] == -1) {
|
||||||
|
++i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (c[j] == -2) {
|
||||||
|
++padding;
|
||||||
|
}
|
||||||
|
++i;
|
||||||
|
++j;
|
||||||
|
}
|
||||||
|
uint32_t v;
|
||||||
|
switch (padding) {
|
||||||
|
case 0:
|
||||||
|
v = (unsigned)c[0] << 18 | c[1] << 12 | c[2] << 6 | c[3];
|
||||||
|
buffer[output] = v >> 16;
|
||||||
|
buffer[output+1] = (v >> 8) & 0xff;
|
||||||
|
buffer[output+2] = v & 0xff;
|
||||||
|
output += 3;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
if (c[3] != -2 || (c[2] & 3)!=0) {
|
||||||
|
return luaL_error(L, "Invalid base64 text");
|
||||||
|
}
|
||||||
|
v = (unsigned)c[0] << 10 | c[1] << 4 | c[2] >> 2 ;
|
||||||
|
buffer[output] = v >> 8;
|
||||||
|
buffer[output+1] = v & 0xff;
|
||||||
|
output += 2;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
if (c[3] != -2 || c[2] != -2 || (c[1] & 0xf) !=0) {
|
||||||
|
return luaL_error(L, "Invalid base64 text");
|
||||||
|
}
|
||||||
|
v = (unsigned)c[0] << 2 | c[1] >> 4;
|
||||||
|
buffer[output] = v;
|
||||||
|
++ output;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return luaL_error(L, "Invalid base64 text");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, buffer, output);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
luaopen_crypt(lua_State *L) {
|
||||||
|
luaL_checkversion(L);
|
||||||
|
srandom(time(NULL));
|
||||||
|
luaL_Reg l[] = {
|
||||||
|
{ "hashkey", lhashkey },
|
||||||
|
{ "randomkey", lrandomkey },
|
||||||
|
{ "desencode", ldesencode },
|
||||||
|
{ "desdecode", ldesdecode },
|
||||||
|
{ "hexencode", ltohex },
|
||||||
|
{ "hexdecode", lfromhex },
|
||||||
|
{ "hmac64", lhmac64 },
|
||||||
|
{ "dhexchange", ldhexchange },
|
||||||
|
{ "dhsecret", ldhsecret },
|
||||||
|
{ "base64encode", lb64encode },
|
||||||
|
{ "base64decode", lb64decode },
|
||||||
|
{ NULL, NULL },
|
||||||
|
};
|
||||||
|
luaL_newlib(L,l);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -55,7 +55,7 @@ little_endian(uint32_t v) {
|
|||||||
typedef void * document;
|
typedef void * document;
|
||||||
|
|
||||||
static inline uint32_t
|
static inline uint32_t
|
||||||
get_length(const document buffer) {
|
get_length(document buffer) {
|
||||||
union {
|
union {
|
||||||
uint32_t v;
|
uint32_t v;
|
||||||
uint8_t b[4];
|
uint8_t b[4];
|
||||||
@@ -243,7 +243,7 @@ op_reply(lua_State *L) {
|
|||||||
lua_pushlightuserdata(L, (void *)doc);
|
lua_pushlightuserdata(L, (void *)doc);
|
||||||
lua_rawseti(L, 2, i);
|
lua_rawseti(L, 2, i);
|
||||||
|
|
||||||
int32_t doc_len = get_length((const document)doc);
|
int32_t doc_len = get_length((document)doc);
|
||||||
|
|
||||||
doc += doc_len;
|
doc += doc_len;
|
||||||
sz -= doc_len;
|
sz -= doc_len;
|
||||||
@@ -260,6 +260,13 @@ op_reply(lua_State *L) {
|
|||||||
lua_pushnil(L);
|
lua_pushnil(L);
|
||||||
lua_rawseti(L, 2, i);
|
lua_rawseti(L, 2, i);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (sz >= 4) {
|
||||||
|
sz -= get_length((document)doc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sz != 0) {
|
||||||
|
return luaL_error(L, "Invalid result bson document");
|
||||||
}
|
}
|
||||||
lua_pushboolean(L,1);
|
lua_pushboolean(L,1);
|
||||||
lua_pushinteger(L, id);
|
lua_pushinteger(L, id);
|
||||||
@@ -499,7 +506,7 @@ op_insert(lua_State *L) {
|
|||||||
int i;
|
int i;
|
||||||
for (i=1;i<=s;i++) {
|
for (i=1;i<=s;i++) {
|
||||||
lua_rawgeti(L,3,i);
|
lua_rawgeti(L,3,i);
|
||||||
document doc = lua_touserdata(L,3);
|
document doc = lua_touserdata(L,-1);
|
||||||
luaL_addlstring(&b, (const char *)doc, get_length(doc));
|
luaL_addlstring(&b, (const char *)doc, get_length(doc));
|
||||||
lua_pop(L,1);
|
lua_pop(L,1);
|
||||||
}
|
}
|
||||||
|
|||||||
167
lualib-src/lua-multicast.c
Normal file
167
lualib-src/lua-multicast.c
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
#include "skynet.h"
|
||||||
|
|
||||||
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
struct mc_package {
|
||||||
|
int reference;
|
||||||
|
uint32_t size;
|
||||||
|
void *data;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int
|
||||||
|
pack(lua_State *L, void *data, size_t size) {
|
||||||
|
struct mc_package * pack = skynet_malloc(sizeof(struct mc_package));
|
||||||
|
pack->reference = 0;
|
||||||
|
pack->size = (uint32_t)size;
|
||||||
|
pack->data = data;
|
||||||
|
struct mc_package ** ret = skynet_malloc(sizeof(*ret));
|
||||||
|
*ret = pack;
|
||||||
|
lua_pushlightuserdata(L, ret);
|
||||||
|
lua_pushinteger(L, sizeof(ret));
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
lightuserdata
|
||||||
|
integer size
|
||||||
|
|
||||||
|
return lightuserdata, sizeof(struct mc_package *)
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
mc_packlocal(lua_State *L) {
|
||||||
|
void * data = lua_touserdata(L, 1);
|
||||||
|
size_t size = luaL_checkunsigned(L, 2);
|
||||||
|
if (size != (uint32_t)size) {
|
||||||
|
return luaL_error(L, "Size should be 32bit integer");
|
||||||
|
}
|
||||||
|
return pack(L, data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
lightuserdata
|
||||||
|
integer size
|
||||||
|
|
||||||
|
return lightuserdata, sizeof(struct mc_package *)
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
mc_packremote(lua_State *L) {
|
||||||
|
void * data = lua_touserdata(L, 1);
|
||||||
|
size_t size = luaL_checkunsigned(L, 2);
|
||||||
|
if (size != (uint32_t)size) {
|
||||||
|
return luaL_error(L, "Size should be 32bit integer");
|
||||||
|
}
|
||||||
|
void * msg = skynet_malloc(size);
|
||||||
|
memcpy(msg, data, size);
|
||||||
|
return pack(L, msg, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
mc_packstring(lua_State *L) {
|
||||||
|
size_t size;
|
||||||
|
const char * msg = luaL_checklstring(L, 1, &size);
|
||||||
|
if (size != (uint32_t)size) {
|
||||||
|
return luaL_error(L, "string is too long");
|
||||||
|
}
|
||||||
|
void * data = skynet_malloc(size);
|
||||||
|
memcpy(data, msg, size);
|
||||||
|
return pack(L, data, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
lightuserdata struct mc_package **
|
||||||
|
integer size (must be sizeof(struct mc_package *)
|
||||||
|
|
||||||
|
return package, lightuserdata, size
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
mc_unpacklocal(lua_State *L) {
|
||||||
|
struct mc_package ** pack = lua_touserdata(L,1);
|
||||||
|
int sz = luaL_checkinteger(L,2);
|
||||||
|
if (sz != sizeof(*pack)) {
|
||||||
|
return luaL_error(L, "Invalid multicast package size %d", sz);
|
||||||
|
}
|
||||||
|
lua_settop(L, 1);
|
||||||
|
lua_pushlightuserdata(L, (*pack)->data);
|
||||||
|
lua_pushunsigned(L, (*pack)->size);
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
lightuserdata struct mc_package **
|
||||||
|
integer reference
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
mc_bindrefer(lua_State *L) {
|
||||||
|
struct mc_package ** pack = lua_touserdata(L,1);
|
||||||
|
int ref = luaL_checkinteger(L,2);
|
||||||
|
if ((*pack)->reference != 0) {
|
||||||
|
return luaL_error(L, "Can't bind a multicast package more than once");
|
||||||
|
}
|
||||||
|
(*pack)->reference = ref;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
lightuserdata struct mc_package **
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
mc_closelocal(lua_State *L) {
|
||||||
|
struct mc_package **ptr = lua_touserdata(L,1);
|
||||||
|
struct mc_package *pack = *ptr;
|
||||||
|
|
||||||
|
int ref = __sync_sub_and_fetch(&pack->reference, 1);
|
||||||
|
if (ref <= 0) {
|
||||||
|
skynet_free(pack->data);
|
||||||
|
skynet_free(pack);
|
||||||
|
if (ref < 0) {
|
||||||
|
return luaL_error(L, "Invalid multicast package reference %d", ref);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
lightuserdata struct mc_package **
|
||||||
|
return lightuserdata/size
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
mc_remote(lua_State *L) {
|
||||||
|
struct mc_package **ptr = lua_touserdata(L,1);
|
||||||
|
struct mc_package *pack = *ptr;
|
||||||
|
lua_pushlightuserdata(L, pack->data);
|
||||||
|
lua_pushunsigned(L, pack->size);
|
||||||
|
skynet_free(pack);
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
mc_nextid(lua_State *L) {
|
||||||
|
uint32_t id = luaL_checkunsigned(L, 1);
|
||||||
|
id += 256;
|
||||||
|
lua_pushunsigned(L, id);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
luaopen_multicast_c(lua_State *L) {
|
||||||
|
luaL_Reg l[] = {
|
||||||
|
{ "pack", mc_packlocal },
|
||||||
|
{ "unpack", mc_unpacklocal },
|
||||||
|
{ "bind", mc_bindrefer },
|
||||||
|
{ "close", mc_closelocal },
|
||||||
|
{ "remote", mc_remote },
|
||||||
|
{ "packstring", mc_packstring },
|
||||||
|
{ "packremote", mc_packremote },
|
||||||
|
{ "nextid", mc_nextid },
|
||||||
|
{ NULL, NULL },
|
||||||
|
};
|
||||||
|
luaL_checkversion(L);
|
||||||
|
luaL_newlib(L,l);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
@@ -393,13 +393,13 @@ lpop(lua_State *L) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
tolstring(lua_State *L, size_t *sz) {
|
tolstring(lua_State *L, size_t *sz, int index) {
|
||||||
const char * ptr;
|
const char * ptr;
|
||||||
if (lua_isuserdata(L,1)) {
|
if (lua_isuserdata(L,index)) {
|
||||||
ptr = (const char *)lua_touserdata(L,1);
|
ptr = (const char *)lua_touserdata(L,index);
|
||||||
*sz = (size_t)luaL_checkinteger(L, 2);
|
*sz = (size_t)luaL_checkinteger(L, index+1);
|
||||||
} else {
|
} else {
|
||||||
ptr = luaL_checklstring(L, 1, sz);
|
ptr = luaL_checklstring(L, index, sz);
|
||||||
}
|
}
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
@@ -413,7 +413,7 @@ write_size(uint8_t * buffer, int len) {
|
|||||||
static int
|
static int
|
||||||
lpack(lua_State *L) {
|
lpack(lua_State *L) {
|
||||||
size_t len;
|
size_t len;
|
||||||
const char * ptr = tolstring(L, &len);
|
const char * ptr = tolstring(L, &len, 1);
|
||||||
if (len > 0x10000) {
|
if (len > 0x10000) {
|
||||||
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
|
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
|
||||||
}
|
}
|
||||||
@@ -433,7 +433,7 @@ lpack_string(lua_State *L) {
|
|||||||
uint8_t tmp[SMALLSTRING+2];
|
uint8_t tmp[SMALLSTRING+2];
|
||||||
size_t len;
|
size_t len;
|
||||||
uint8_t *buffer;
|
uint8_t *buffer;
|
||||||
const char * ptr = tolstring(L, &len);
|
const char * ptr = tolstring(L, &len, 1);
|
||||||
if (len > 0x10000) {
|
if (len > 0x10000) {
|
||||||
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
|
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
|
||||||
}
|
}
|
||||||
@@ -451,16 +451,55 @@ lpack_string(lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lpack_padding(lua_State *L) {
|
||||||
|
uint8_t tmp[SMALLSTRING+2];
|
||||||
|
size_t content_sz;
|
||||||
|
uint8_t *buffer;
|
||||||
|
const char * ptr = tolstring(L, &content_sz, 2);
|
||||||
|
size_t cookie_sz = 0;
|
||||||
|
const char * cookie = luaL_checklstring(L,1,&cookie_sz);
|
||||||
|
size_t len = cookie_sz + content_sz;
|
||||||
|
|
||||||
|
if (len > 0x10000) {
|
||||||
|
return luaL_error(L, "Invalid size (too long) of data : %d", (int)len);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len <= SMALLSTRING) {
|
||||||
|
buffer = tmp;
|
||||||
|
} else {
|
||||||
|
buffer = lua_newuserdata(L, len + 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
write_size(buffer, len);
|
||||||
|
memcpy(buffer+2, ptr, content_sz);
|
||||||
|
memcpy(buffer+2+content_sz, cookie, cookie_sz);
|
||||||
|
lua_pushlstring(L, (const char *)buffer, len+2);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
ltostring(lua_State *L) {
|
ltostring(lua_State *L) {
|
||||||
void * ptr = lua_touserdata(L, 1);
|
void * ptr = lua_touserdata(L, 1);
|
||||||
int size = luaL_checkinteger(L, 2);
|
int size = luaL_checkinteger(L, 2);
|
||||||
if (ptr == NULL) {
|
if (ptr == NULL) {
|
||||||
lua_pushliteral(L, "");
|
lua_pushliteral(L, "");
|
||||||
|
} else {
|
||||||
|
if (lua_isnumber(L, 3)) {
|
||||||
|
int offset = lua_tointeger(L, 3);
|
||||||
|
if (offset < 0) {
|
||||||
|
return luaL_error(L, "Invalid offset %d", offset);
|
||||||
|
}
|
||||||
|
if (offset > size) {
|
||||||
|
offset = size;
|
||||||
|
}
|
||||||
|
lua_pushlstring(L, (const char *)ptr + offset, size-offset);
|
||||||
} else {
|
} else {
|
||||||
lua_pushlstring(L, (const char *)ptr, size);
|
lua_pushlstring(L, (const char *)ptr, size);
|
||||||
skynet_free(ptr);
|
skynet_free(ptr);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -471,6 +510,7 @@ luaopen_netpack(lua_State *L) {
|
|||||||
{ "pop", lpop },
|
{ "pop", lpop },
|
||||||
{ "pack", lpack },
|
{ "pack", lpack },
|
||||||
{ "pack_string", lpack_string },
|
{ "pack_string", lpack_string },
|
||||||
|
{ "pack_padding", lpack_padding },
|
||||||
{ "clear", lclear },
|
{ "clear", lclear },
|
||||||
{ "tostring", ltostring },
|
{ "tostring", ltostring },
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
|
|||||||
@@ -581,8 +581,16 @@ _luaseri_unpack(lua_State *L) {
|
|||||||
if (lua_isnoneornil(L,1)) {
|
if (lua_isnoneornil(L,1)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
void * buffer = lua_touserdata(L,1);
|
void * buffer;
|
||||||
int len = luaL_checkinteger(L,2);
|
int len;
|
||||||
|
if (lua_type(L,1) == LUA_TSTRING) {
|
||||||
|
size_t sz;
|
||||||
|
buffer = (void *)lua_tolstring(L,1,&sz);
|
||||||
|
len = (int)sz;
|
||||||
|
} else {
|
||||||
|
buffer = lua_touserdata(L,1);
|
||||||
|
len = luaL_checkinteger(L,2);
|
||||||
|
}
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include "skynet.h"
|
#include "skynet.h"
|
||||||
#include "lua-seri.h"
|
#include "lua-seri.h"
|
||||||
#include "service_lua.h"
|
|
||||||
|
|
||||||
#define KNRM "\x1B[0m"
|
#define KNRM "\x1B[0m"
|
||||||
#define KRED "\x1B[31m"
|
#define KRED "\x1B[31m"
|
||||||
@@ -11,18 +10,36 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
struct snlua {
|
||||||
|
lua_State * L;
|
||||||
|
struct skynet_context * ctx;
|
||||||
|
const char * preload;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int
|
||||||
|
traceback (lua_State *L) {
|
||||||
|
const char *msg = lua_tostring(L, 1);
|
||||||
|
if (msg)
|
||||||
|
luaL_traceback(L, L, msg, 1);
|
||||||
|
else {
|
||||||
|
lua_pushliteral(L, "(no error message)");
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
_cb(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
||||||
lua_State *L = ud;
|
lua_State *L = ud;
|
||||||
int trace = 1;
|
int trace = 1;
|
||||||
int r;
|
int r;
|
||||||
int top = lua_gettop(L);
|
int top = lua_gettop(L);
|
||||||
if (top == 1) {
|
if (top == 0) {
|
||||||
|
lua_pushcfunction(L, traceback);
|
||||||
lua_rawgetp(L, LUA_REGISTRYINDEX, _cb);
|
lua_rawgetp(L, LUA_REGISTRYINDEX, _cb);
|
||||||
} else {
|
} else {
|
||||||
assert(top == 2);
|
assert(top == 2);
|
||||||
lua_pushvalue(L,2);
|
|
||||||
}
|
}
|
||||||
|
lua_pushvalue(L,2);
|
||||||
|
|
||||||
lua_pushinteger(L, type);
|
lua_pushinteger(L, type);
|
||||||
lua_pushlightuserdata(L, (void *)msg);
|
lua_pushlightuserdata(L, (void *)msg);
|
||||||
@@ -132,7 +149,7 @@ _sendname(lua_State *L, struct skynet_context * context, const char * dest) {
|
|||||||
luaL_error(L, "skynet.send invalid param %s", lua_typename(L,lua_type(L,4)));
|
luaL_error(L, "skynet.send invalid param %s", lua_typename(L,lua_type(L,4)));
|
||||||
}
|
}
|
||||||
if (session < 0) {
|
if (session < 0) {
|
||||||
luaL_error(L, "skynet.send session (%d) < 0", session);
|
return 0;
|
||||||
}
|
}
|
||||||
lua_pushinteger(L,session);
|
lua_pushinteger(L,session);
|
||||||
return 1;
|
return 1;
|
||||||
@@ -204,7 +221,7 @@ _send(lua_State *L) {
|
|||||||
}
|
}
|
||||||
if (session < 0) {
|
if (session < 0) {
|
||||||
// send to invalid address
|
// send to invalid address
|
||||||
// todo: maybe throw error is better
|
// todo: maybe throw error whould be better
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
lua_pushinteger(L,session);
|
lua_pushinteger(L,session);
|
||||||
@@ -272,6 +289,16 @@ _harbor(lua_State *L) {
|
|||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lpackstring(lua_State *L) {
|
||||||
|
_luaseri_pack(L);
|
||||||
|
char * str = (char *)lua_touserdata(L, -2);
|
||||||
|
int sz = lua_tointeger(L, -1);
|
||||||
|
lua_pushlstring(L, str, sz);
|
||||||
|
skynet_free(str);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
luaopen_skynet_c(lua_State *L) {
|
luaopen_skynet_c(lua_State *L) {
|
||||||
luaL_checkversion(L);
|
luaL_checkversion(L);
|
||||||
@@ -286,19 +313,19 @@ luaopen_skynet_c(lua_State *L) {
|
|||||||
{ "harbor", _harbor },
|
{ "harbor", _harbor },
|
||||||
{ "pack", _luaseri_pack },
|
{ "pack", _luaseri_pack },
|
||||||
{ "unpack", _luaseri_unpack },
|
{ "unpack", _luaseri_unpack },
|
||||||
|
{ "packstring", lpackstring },
|
||||||
{ "callback", _callback },
|
{ "callback", _callback },
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
};
|
};
|
||||||
|
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, "skynet_lua");
|
luaL_newlibtable(L, l);
|
||||||
struct snlua *lua = lua_touserdata(L,-1);
|
|
||||||
if (lua == NULL || lua->ctx == NULL) {
|
lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context");
|
||||||
|
struct skynet_context *ctx = lua_touserdata(L,-1);
|
||||||
|
if (ctx == NULL) {
|
||||||
return luaL_error(L, "Init skynet context first");
|
return luaL_error(L, "Init skynet context first");
|
||||||
}
|
}
|
||||||
assert(lua->L == L);
|
|
||||||
|
|
||||||
luaL_newlibtable(L, l);
|
|
||||||
lua_pushlightuserdata(L, lua->ctx);
|
|
||||||
luaL_setfuncs(L,l,1);
|
luaL_setfuncs(L,l,1);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
@@ -10,11 +10,11 @@
|
|||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
#include "skynet_socket.h"
|
#include "skynet_socket.h"
|
||||||
#include "service_lua.h"
|
|
||||||
|
|
||||||
#define BACKLOG 32
|
#define BACKLOG 32
|
||||||
// 2 ** 12 == 4096
|
// 2 ** 12 == 4096
|
||||||
#define LARGE_PAGE_NODE 12
|
#define LARGE_PAGE_NODE 12
|
||||||
|
#define BUFFER_LIMIT (256 * 1024)
|
||||||
|
|
||||||
struct buffer_node {
|
struct buffer_node {
|
||||||
char * msg;
|
char * msg;
|
||||||
@@ -189,6 +189,25 @@ pop_lstring(lua_State *L, struct socket_buffer *sb, int sz, int skip) {
|
|||||||
luaL_pushresult(&b);
|
luaL_pushresult(&b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lheader(lua_State *L) {
|
||||||
|
size_t len;
|
||||||
|
const uint8_t * s = (const uint8_t *)luaL_checklstring(L, 1, &len);
|
||||||
|
if (len > 4 || len < 1) {
|
||||||
|
return luaL_error(L, "Invalid read %s", s);
|
||||||
|
}
|
||||||
|
int i;
|
||||||
|
size_t sz = 0;
|
||||||
|
for (i=0;i<(int)len;i++) {
|
||||||
|
sz <<= 8;
|
||||||
|
sz |= s[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushunsigned(L, sz);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
userdata send_buffer
|
userdata send_buffer
|
||||||
table pool
|
table pool
|
||||||
@@ -458,6 +477,14 @@ lstart(lua_State *L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
lnodelay(lua_State *L) {
|
||||||
|
struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1));
|
||||||
|
int id = luaL_checkinteger(L, 1);
|
||||||
|
skynet_socket_nodelay(ctx,id);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
luaopen_socketdriver(lua_State *L) {
|
luaopen_socketdriver(lua_State *L) {
|
||||||
luaL_checkversion(L);
|
luaL_checkversion(L);
|
||||||
@@ -470,6 +497,7 @@ luaopen_socketdriver(lua_State *L) {
|
|||||||
{ "clear", lclearbuffer },
|
{ "clear", lclearbuffer },
|
||||||
{ "readline", lreadline },
|
{ "readline", lreadline },
|
||||||
{ "str2p", lstr2p },
|
{ "str2p", lstr2p },
|
||||||
|
{ "header", lheader },
|
||||||
|
|
||||||
{ "unpack", lunpack },
|
{ "unpack", lunpack },
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
@@ -483,17 +511,15 @@ luaopen_socketdriver(lua_State *L) {
|
|||||||
{ "lsend", lsendlow },
|
{ "lsend", lsendlow },
|
||||||
{ "bind", lbind },
|
{ "bind", lbind },
|
||||||
{ "start", lstart },
|
{ "start", lstart },
|
||||||
|
{ "nodelay", lnodelay },
|
||||||
{ NULL, NULL },
|
{ NULL, NULL },
|
||||||
};
|
};
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, "skynet_lua");
|
lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context");
|
||||||
struct snlua *lua = lua_touserdata(L,-1);
|
struct skynet_context *ctx = lua_touserdata(L,-1);
|
||||||
if (lua == NULL || lua->ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
return luaL_error(L, "Init skynet context first");
|
return luaL_error(L, "Init skynet context first");
|
||||||
}
|
}
|
||||||
// assert(lua->L == L);
|
|
||||||
lua_pop(L,1);
|
|
||||||
|
|
||||||
lua_pushlightuserdata(L, lua->ctx);
|
|
||||||
luaL_setfuncs(L,l2,1);
|
luaL_setfuncs(L,l2,1);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
27
lualib/cluster.lua
Normal file
27
lualib/cluster.lua
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
local clusterd
|
||||||
|
local cluster = {}
|
||||||
|
|
||||||
|
function cluster.call(node, address, ...)
|
||||||
|
-- skynet.pack(...) will free by cluster.c.packrequest
|
||||||
|
return skynet.call(clusterd, "lua", "req", node, address, skynet.pack(...))
|
||||||
|
end
|
||||||
|
|
||||||
|
function cluster.open(port)
|
||||||
|
if type(port) == "string" then
|
||||||
|
skynet.call(clusterd, "lua", "listen", port)
|
||||||
|
else
|
||||||
|
skynet.call(clusterd, "lua", "listen", "0.0.0.0", port)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cluster.reload()
|
||||||
|
skynet.call(clusterd, "lua", "reload")
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.init(function()
|
||||||
|
clusterd = skynet.uniqueservice("clusterd")
|
||||||
|
end)
|
||||||
|
|
||||||
|
return cluster
|
||||||
18
lualib/datacenter.lua
Normal file
18
lualib/datacenter.lua
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
local datacenter = {}
|
||||||
|
|
||||||
|
function datacenter.get(...)
|
||||||
|
return skynet.call("DATACENTER", "lua", "QUERY", ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function datacenter.set(...)
|
||||||
|
return skynet.call("DATACENTER", "lua", "UPDATE", ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function datacenter.wait(...)
|
||||||
|
return skynet.call("DATACENTER", "lua", "WAIT", ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
return datacenter
|
||||||
|
|
||||||
274
lualib/http/httpd.lua
Normal file
274
lualib/http/httpd.lua
Normal file
@@ -0,0 +1,274 @@
|
|||||||
|
local table = table
|
||||||
|
|
||||||
|
local httpd = {}
|
||||||
|
|
||||||
|
local http_status_msg = {
|
||||||
|
[100] = "Continue",
|
||||||
|
[101] = "Switching Protocols",
|
||||||
|
[200] = "OK",
|
||||||
|
[201] = "Created",
|
||||||
|
[202] = "Accepted",
|
||||||
|
[203] = "Non-Authoritative Information",
|
||||||
|
[204] = "No Content",
|
||||||
|
[205] = "Reset Content",
|
||||||
|
[206] = "Partial Content",
|
||||||
|
[300] = "Multiple Choices",
|
||||||
|
[301] = "Moved Permanently",
|
||||||
|
[302] = "Found",
|
||||||
|
[303] = "See Other",
|
||||||
|
[304] = "Not Modified",
|
||||||
|
[305] = "Use Proxy",
|
||||||
|
[307] = "Temporary Redirect",
|
||||||
|
[400] = "Bad Request",
|
||||||
|
[401] = "Unauthorized",
|
||||||
|
[402] = "Payment Required",
|
||||||
|
[403] = "Forbidden",
|
||||||
|
[404] = "Not Found",
|
||||||
|
[405] = "Method Not Allowed",
|
||||||
|
[406] = "Not Acceptable",
|
||||||
|
[407] = "Proxy Authentication Required",
|
||||||
|
[408] = "Request Time-out",
|
||||||
|
[409] = "Conflict",
|
||||||
|
[410] = "Gone",
|
||||||
|
[411] = "Length Required",
|
||||||
|
[412] = "Precondition Failed",
|
||||||
|
[413] = "Request Entity Too Large",
|
||||||
|
[414] = "Request-URI Too Large",
|
||||||
|
[415] = "Unsupported Media Type",
|
||||||
|
[416] = "Requested range not satisfiable",
|
||||||
|
[417] = "Expectation Failed",
|
||||||
|
[500] = "Internal Server Error",
|
||||||
|
[501] = "Not Implemented",
|
||||||
|
[502] = "Bad Gateway",
|
||||||
|
[503] = "Service Unavailable",
|
||||||
|
[504] = "Gateway Time-out",
|
||||||
|
[505] = "HTTP Version not supported",
|
||||||
|
}
|
||||||
|
|
||||||
|
local function recvheader(readbytes, limit, lines, header)
|
||||||
|
local result
|
||||||
|
local e = header:find("\r\n\r\n", 1, true)
|
||||||
|
if e then
|
||||||
|
result = header:sub(e+4)
|
||||||
|
else
|
||||||
|
while true do
|
||||||
|
local bytes = readbytes()
|
||||||
|
header = header .. bytes
|
||||||
|
if #header > limit then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
e = header:find("\r\n\r\n", -#bytes-3, true)
|
||||||
|
if e then
|
||||||
|
result = header:sub(e+4)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local idx = 1
|
||||||
|
for v in header:gmatch("(.-)\r\n") do
|
||||||
|
if v == "" then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
lines[idx] = v
|
||||||
|
idx = idx + 1
|
||||||
|
end
|
||||||
|
for i = idx, #lines do
|
||||||
|
lines[i] = nil
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
local function parseheader(lines, from, header)
|
||||||
|
local name, value
|
||||||
|
for i=from,#lines do
|
||||||
|
local line = lines[i]
|
||||||
|
if line:byte(1) == 9 then -- tab, append last line
|
||||||
|
if name == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
header[name] = header[name] .. line:sub(2)
|
||||||
|
else
|
||||||
|
name, value = line:match "^(.-):%s*(.*)"
|
||||||
|
if name == nil or value == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
name = name:lower()
|
||||||
|
if header[name] then
|
||||||
|
header[name] = header[name] .. ", " .. value
|
||||||
|
else
|
||||||
|
header[name] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return header
|
||||||
|
end
|
||||||
|
|
||||||
|
local function chunksize(readbytes, body)
|
||||||
|
while true do
|
||||||
|
if #body > 128 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
body = body .. readbytes()
|
||||||
|
local f,e = body:find("\r\n",1,true)
|
||||||
|
if f then
|
||||||
|
return tonumber(body:sub(1,f-1),16), body:sub(e+1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function readcrln(readbytes, body)
|
||||||
|
if #body > 2 then
|
||||||
|
if body:sub(1,2) ~= "\r\n" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
return body:sub(3)
|
||||||
|
else
|
||||||
|
body = body .. readbytes(2-#body)
|
||||||
|
if body ~= "\r\n" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
return ""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local tmpline = {}
|
||||||
|
|
||||||
|
local function recvchunkedbody(readbytes, bodylimit, header, body)
|
||||||
|
local result = ""
|
||||||
|
local size = 0
|
||||||
|
|
||||||
|
while true do
|
||||||
|
local sz
|
||||||
|
sz , body = chunksize(readbytes, body)
|
||||||
|
if not sz then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if sz == 0 then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
size = size + sz
|
||||||
|
if bodylimit and size > bodylimit then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if #body >= sz then
|
||||||
|
result = result .. body:sub(1,sz)
|
||||||
|
body = body:sub(sz+1)
|
||||||
|
else
|
||||||
|
result = result .. body .. readbytes(sz - #body)
|
||||||
|
body = ""
|
||||||
|
end
|
||||||
|
body = readcrln(readbytes, body)
|
||||||
|
if not body then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
body = readcrln(readbytes, body)
|
||||||
|
if not body then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
body = recvheader(readbytes, 8192, tmpline, body)
|
||||||
|
if not body then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
header = parseheader(tmpline,1,header)
|
||||||
|
|
||||||
|
return result, header
|
||||||
|
end
|
||||||
|
|
||||||
|
local function readall(readbytes, bodylimit)
|
||||||
|
local body = recvheader(readbytes, 8192, tmpline, "")
|
||||||
|
if not body then
|
||||||
|
return 413 -- Request Entity Too Large
|
||||||
|
end
|
||||||
|
local request = assert(tmpline[1])
|
||||||
|
local method, url, httpver = request:match "^(%a+)%s+(.-)%s+HTTP/([%d%.]+)$"
|
||||||
|
assert(method and url and httpver)
|
||||||
|
httpver = assert(tonumber(httpver))
|
||||||
|
if httpver < 1.0 or httpver > 1.1 then
|
||||||
|
return 505 -- HTTP Version not supported
|
||||||
|
end
|
||||||
|
local header = parseheader(tmpline,2,{})
|
||||||
|
if not header then
|
||||||
|
return 400 -- Bad request
|
||||||
|
end
|
||||||
|
local length = header["content-length"]
|
||||||
|
if length then
|
||||||
|
length = tonumber(length)
|
||||||
|
end
|
||||||
|
local mode = header["transfer-encoding"]
|
||||||
|
if mode then
|
||||||
|
if mode ~= "identity" or mode ~= "chunked" then
|
||||||
|
return 501 -- Not Implemented
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if mode == "chunked" then
|
||||||
|
body, header = recvchunkedbody(readbytes, bodylimit, header, body)
|
||||||
|
if not body then
|
||||||
|
return 413
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- identity mode
|
||||||
|
if length then
|
||||||
|
if length > bodylimit then
|
||||||
|
return 413
|
||||||
|
end
|
||||||
|
if #body >= length then
|
||||||
|
body = body:sub(1,length)
|
||||||
|
else
|
||||||
|
local padding = readbytes(length - #body)
|
||||||
|
body = body .. padding
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return 200, url, method, header, body
|
||||||
|
end
|
||||||
|
|
||||||
|
function httpd.read_request(...)
|
||||||
|
local ok, code, url, method, header, body = pcall(readall, ...)
|
||||||
|
if ok then
|
||||||
|
return code, url, method, header, body
|
||||||
|
else
|
||||||
|
return nil, code
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function writeall(writefunc, statuscode, bodyfunc, header)
|
||||||
|
local statusline = string.format("HTTP/1.1 %03d %s\r\n", statuscode, http_status_msg[statuscode] or "")
|
||||||
|
writefunc(statusline)
|
||||||
|
if header then
|
||||||
|
for k,v in pairs(header) do
|
||||||
|
writefunc(string.format("%s: %s\r\n", k,v))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local t = type(bodyfunc)
|
||||||
|
if t == "string" then
|
||||||
|
writefunc(string.format("content-length: %d\r\n\r\n", #bodyfunc))
|
||||||
|
writefunc(bodyfunc)
|
||||||
|
elseif t == "function" then
|
||||||
|
writefunc("transfer-encoding: chunked\r\n")
|
||||||
|
while true do
|
||||||
|
local s = bodyfunc()
|
||||||
|
if s then
|
||||||
|
if s ~= "" then
|
||||||
|
writefunc(string.format("\r\n%x\r\n", #s))
|
||||||
|
writefunc(s)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
writefunc("\r\n0\r\n\r\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
assert(t == "nil")
|
||||||
|
writefunc("\r\n")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function httpd.write_response(...)
|
||||||
|
return pcall(writeall, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
return httpd
|
||||||
31
lualib/http/sockethelper.lua
Normal file
31
lualib/http/sockethelper.lua
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
local socket = require "socket"
|
||||||
|
|
||||||
|
local readbytes = socket.read
|
||||||
|
local writebytes = socket.write
|
||||||
|
|
||||||
|
local sockethelper = {}
|
||||||
|
local socket_error = setmetatable({} , { __tostring = function() return "[Socket Error]" end })
|
||||||
|
|
||||||
|
sockethelper.socket_error = socket_error
|
||||||
|
|
||||||
|
function sockethelper.readfunc(fd)
|
||||||
|
return function (sz)
|
||||||
|
local ret = readbytes(fd, sz)
|
||||||
|
if ret then
|
||||||
|
return ret
|
||||||
|
else
|
||||||
|
error(socket_error)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function sockethelper.writefunc(fd)
|
||||||
|
return function(content)
|
||||||
|
local ok = writebytes(fd, content)
|
||||||
|
if not ok then
|
||||||
|
error(socket_error)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return sockethelper
|
||||||
28
lualib/http/url.lua
Normal file
28
lualib/http/url.lua
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
local url = {}
|
||||||
|
|
||||||
|
local function decode_func(c)
|
||||||
|
return string.char(tonumber(c, 16))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function decode(str)
|
||||||
|
local str = str:gsub('+', ' ')
|
||||||
|
return str:gsub("%%(..)", decode_func)
|
||||||
|
end
|
||||||
|
|
||||||
|
function url.parse(u)
|
||||||
|
local path,query = u:match "([^?]*)%??(.*)"
|
||||||
|
if path then
|
||||||
|
path = decode(path)
|
||||||
|
end
|
||||||
|
return path, query
|
||||||
|
end
|
||||||
|
|
||||||
|
function url.parse_query(q)
|
||||||
|
local r = {}
|
||||||
|
for k,v in q:gmatch "(.-)=([^&]*)&?" do
|
||||||
|
r[decode(k)] = decode(v)
|
||||||
|
end
|
||||||
|
return r
|
||||||
|
end
|
||||||
|
|
||||||
|
return url
|
||||||
48
lualib/loader.lua
Normal file
48
lualib/loader.lua
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
local args = {}
|
||||||
|
for word in string.gmatch(..., "%S+") do
|
||||||
|
table.insert(args, word)
|
||||||
|
end
|
||||||
|
|
||||||
|
SERVICE_NAME = args[1]
|
||||||
|
|
||||||
|
local main, pattern
|
||||||
|
|
||||||
|
local err = {}
|
||||||
|
for pat in string.gmatch(LUA_SERVICE, "([^;]+);*") do
|
||||||
|
local filename = string.gsub(pat, "?", SERVICE_NAME)
|
||||||
|
local f, msg = loadfile(filename)
|
||||||
|
if not f then
|
||||||
|
table.insert(err, msg)
|
||||||
|
else
|
||||||
|
pattern = pat
|
||||||
|
main = f
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if not main then
|
||||||
|
error(table.concat(err, "\n"))
|
||||||
|
end
|
||||||
|
|
||||||
|
LUA_SERVICE = nil
|
||||||
|
package.path , LUA_PATH = LUA_PATH
|
||||||
|
package.cpath , LUA_CPATH = LUA_CPATH
|
||||||
|
|
||||||
|
local service_path = string.match(pattern, "(.*/)[^/?]+$")
|
||||||
|
|
||||||
|
if service_path then
|
||||||
|
service_path = string.gsub(service_path, "?", args[1])
|
||||||
|
package.path = service_path .. "?.lua;" .. package.path
|
||||||
|
SERVICE_PATH = service_path
|
||||||
|
else
|
||||||
|
local p = string.match(pattern, "(.*/).+$")
|
||||||
|
SERVICE_PATH = p
|
||||||
|
end
|
||||||
|
|
||||||
|
if LUA_PRELOAD then
|
||||||
|
local f = assert(loadfile(LUA_PRELOAD))
|
||||||
|
f(table.unpack(args))
|
||||||
|
LUA_PRELOAD = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
main(select(2, table.unpack(args)))
|
||||||
@@ -75,22 +75,51 @@ local function dispatch_reply(so)
|
|||||||
return reply_id, succ, result
|
return reply_id, succ, result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function __parse_addr(addr)
|
||||||
|
local host, port = string.match(addr, "([^:]+):(.+)")
|
||||||
|
return host, tonumber(port)
|
||||||
|
end
|
||||||
|
|
||||||
local function mongo_auth(mongoc)
|
local function mongo_auth(mongoc)
|
||||||
local user = rawget(mongoc, "username")
|
local user = rawget(mongoc, "username")
|
||||||
local pass = rawget(mongoc, "password")
|
local pass = rawget(mongoc, "password")
|
||||||
|
|
||||||
if user == nil or pass == nil then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
return function()
|
return function()
|
||||||
|
if user ~= nil and pass ~= nil then
|
||||||
assert(mongoc:auth(user, pass))
|
assert(mongoc:auth(user, pass))
|
||||||
end
|
end
|
||||||
|
local rs_data = mongoc:runCommand("ismaster")
|
||||||
|
if rs_data.ok == 1 then
|
||||||
|
if rs_data.hosts then
|
||||||
|
local backup = {}
|
||||||
|
for _, v in ipairs(rs_data.hosts) do
|
||||||
|
local host, port = __parse_addr(v)
|
||||||
|
table.insert(backup, {host = host, port = port})
|
||||||
|
end
|
||||||
|
mongoc.__sock:changebackup(backup)
|
||||||
|
end
|
||||||
|
if rs_data.ismaster then
|
||||||
|
return
|
||||||
|
else
|
||||||
|
local host, port = __parse_addr(rs_data.primary)
|
||||||
|
mongoc.host = host
|
||||||
|
mongoc.port = port
|
||||||
|
mongoc.__sock:changehost(host, port)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function mongo.client( conf )
|
function mongo.client( conf )
|
||||||
|
local first = conf
|
||||||
|
local backup = nil
|
||||||
|
if conf.rs then
|
||||||
|
first = conf.rs[1]
|
||||||
|
backup = conf.rs
|
||||||
|
end
|
||||||
local obj = {
|
local obj = {
|
||||||
host = conf.host,
|
host = first.host,
|
||||||
port = conf.port or 27017,
|
port = first.port or 27017,
|
||||||
}
|
}
|
||||||
|
|
||||||
obj.__id = 0
|
obj.__id = 0
|
||||||
@@ -98,7 +127,8 @@ function mongo.client( conf )
|
|||||||
host = obj.host,
|
host = obj.host,
|
||||||
port = obj.port,
|
port = obj.port,
|
||||||
response = dispatch_reply,
|
response = dispatch_reply,
|
||||||
auth = mongo_auth(conf),
|
auth = mongo_auth(obj),
|
||||||
|
backup = backup,
|
||||||
}
|
}
|
||||||
setmetatable(obj, client_meta)
|
setmetatable(obj, client_meta)
|
||||||
obj.__sock:connect(true) -- try connect only once
|
obj.__sock:connect(true) -- try connect only once
|
||||||
@@ -167,7 +197,9 @@ function mongo_db:runCommand(cmd,cmd_v,...)
|
|||||||
bson_cmd = bson_encode_order(cmd,cmd_v,...)
|
bson_cmd = bson_encode_order(cmd,cmd_v,...)
|
||||||
end
|
end
|
||||||
local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd)
|
local pack = driver.query(request_id, 0, self.__cmd, 0, 1, bson_cmd)
|
||||||
local doc = sock:request(pack, request_id).document
|
-- we must hold req (req.data), because req.document is a lightuserdata, it's a pointer to the string (req.data)
|
||||||
|
local req = sock:request(pack, request_id)
|
||||||
|
local doc = req.document
|
||||||
return bson_decode(doc)
|
return bson_decode(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -224,7 +256,9 @@ function mongo_collection:findOne(query, selector)
|
|||||||
local request_id = conn:genId()
|
local request_id = conn:genId()
|
||||||
local sock = conn.__sock
|
local sock = conn.__sock
|
||||||
local pack = driver.query(request_id, 0, self.full_name, 0, 1, query and bson_encode(query) or empty_bson, selector and bson_encode(selector))
|
local pack = driver.query(request_id, 0, self.full_name, 0, 1, query and bson_encode(query) or empty_bson, selector and bson_encode(selector))
|
||||||
local doc = sock:request(pack, request_id).document
|
-- we must hold req (req.data), because req.document is a lightuserdata, it's a pointer to the string (req.data)
|
||||||
|
local req = sock:request(pack, request_id)
|
||||||
|
local doc = req.document
|
||||||
return bson_decode(doc)
|
return bson_decode(doc)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
-- This is a deprecated module, use skynet.queue instead.
|
||||||
|
|
||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
local c = require "skynet.c"
|
local c = require "skynet.c"
|
||||||
|
|
||||||
|
|||||||
102
lualib/multicast.lua
Normal file
102
lualib/multicast.lua
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local mc = require "multicast.c"
|
||||||
|
|
||||||
|
local multicastd
|
||||||
|
local multicast = {}
|
||||||
|
local dispatch = setmetatable({} , {__mode = "kv" })
|
||||||
|
|
||||||
|
local chan = {}
|
||||||
|
local chan_meta = {
|
||||||
|
__index = chan,
|
||||||
|
__gc = unsubscribe,
|
||||||
|
__tostring = function (self)
|
||||||
|
return string.format("[Multicast:%x]",self.channel)
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
local function default_conf(conf)
|
||||||
|
conf = conf or {}
|
||||||
|
conf.pack = conf.pack or skynet.pack
|
||||||
|
conf.unpack = conf.unpack or skynet.unpack
|
||||||
|
|
||||||
|
return conf
|
||||||
|
end
|
||||||
|
|
||||||
|
function multicast.new(conf)
|
||||||
|
assert(multicastd, "Init first")
|
||||||
|
local self = {}
|
||||||
|
conf = conf or self
|
||||||
|
self.channel = conf.channel
|
||||||
|
if self.channel == nil then
|
||||||
|
self.channel = skynet.call(multicastd, "lua", "NEW")
|
||||||
|
end
|
||||||
|
self.__pack = conf.pack or skynet.pack
|
||||||
|
self.__unpack = conf.unpack or skynet.unpack
|
||||||
|
self.__dispatch = conf.dispatch
|
||||||
|
|
||||||
|
return setmetatable(self, chan_meta)
|
||||||
|
end
|
||||||
|
|
||||||
|
function chan:delete()
|
||||||
|
local c = assert(self.channel)
|
||||||
|
skynet.send(multicastd, "lua", "DEL", c)
|
||||||
|
self.channel = nil
|
||||||
|
self.__subscribe = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function chan:publish(...)
|
||||||
|
local c = assert(self.channel)
|
||||||
|
skynet.call(multicastd, "lua", "PUB", c, mc.pack(self.__pack(...)))
|
||||||
|
end
|
||||||
|
|
||||||
|
function chan:subscribe()
|
||||||
|
local c = assert(self.channel)
|
||||||
|
if self.__subscribe then
|
||||||
|
-- already subscribe
|
||||||
|
return
|
||||||
|
end
|
||||||
|
skynet.call(multicastd, "lua", "SUB", c)
|
||||||
|
self.__subscribe = true
|
||||||
|
dispatch[c] = self
|
||||||
|
end
|
||||||
|
|
||||||
|
function chan:unsubscribe()
|
||||||
|
if not self.__subscribe then
|
||||||
|
-- already unsubscribe
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local c = assert(self.channel)
|
||||||
|
skynet.send(multicastd, "lua", "USUB", c)
|
||||||
|
self.__subscribe = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local function dispatch_subscribe(channel, source, pack, msg, sz)
|
||||||
|
local self = dispatch[channel]
|
||||||
|
if not self then
|
||||||
|
mc.close(pack)
|
||||||
|
error ("Unknown channel " .. channel)
|
||||||
|
end
|
||||||
|
|
||||||
|
if self.__subscribe then
|
||||||
|
local ok, err = pcall(self.__dispatch, self, source, self.__unpack(msg, sz))
|
||||||
|
mc.close(pack)
|
||||||
|
assert(ok, err)
|
||||||
|
else
|
||||||
|
-- maybe unsubscribe first, but the message is send out. drop the message unneed
|
||||||
|
mc.close(pack)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function init()
|
||||||
|
multicastd = skynet.uniqueservice "multicastd"
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "multicast",
|
||||||
|
id = skynet.PTYPE_MULTICAST,
|
||||||
|
unpack = mc.unpack,
|
||||||
|
dispatch = dispatch_subscribe,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.init(init, "multicast")
|
||||||
|
|
||||||
|
return multicast
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
local socket = require "socket"
|
local socket = require "socket"
|
||||||
local socketchannel = require "socketchannel"
|
local socketchannel = require "socketchannel"
|
||||||
|
local int64 = require "int64"
|
||||||
|
|
||||||
local table = table
|
local table = table
|
||||||
local string = string
|
local string = string
|
||||||
@@ -94,12 +95,12 @@ function command:disconnect()
|
|||||||
setmetatable(self, nil)
|
setmetatable(self, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function compose_message(msg)
|
-- msg could be any type of value
|
||||||
if #msg == 1 then
|
local function pack_value(lines, v)
|
||||||
return msg[1] .. "\r\n"
|
if v == nil then
|
||||||
|
return
|
||||||
end
|
end
|
||||||
local lines = { "*" .. #msg }
|
|
||||||
for _,v in ipairs(msg) do
|
|
||||||
local t = type(v)
|
local t = type(v)
|
||||||
if t == "number" then
|
if t == "number" then
|
||||||
v = tostring(v)
|
v = tostring(v)
|
||||||
@@ -108,17 +109,42 @@ local function compose_message(msg)
|
|||||||
end
|
end
|
||||||
table.insert(lines,"$"..#v)
|
table.insert(lines,"$"..#v)
|
||||||
table.insert(lines,v)
|
table.insert(lines,v)
|
||||||
end
|
end
|
||||||
table.insert(lines,"")
|
|
||||||
|
|
||||||
local cmd = table.concat(lines,"\r\n")
|
local function compose_message(cmd, msg)
|
||||||
return cmd
|
local len = 1
|
||||||
|
local t = type(msg)
|
||||||
|
|
||||||
|
if t == "table" then
|
||||||
|
len = len + #msg
|
||||||
|
elseif t ~= nil then
|
||||||
|
len = len + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
local lines = {"*" .. len}
|
||||||
|
pack_value(lines, cmd)
|
||||||
|
|
||||||
|
if t == "table" then
|
||||||
|
for _,v in ipairs(msg) do
|
||||||
|
pack_value(lines, v)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
pack_value(lines, msg)
|
||||||
|
end
|
||||||
|
table.insert(lines, "")
|
||||||
|
|
||||||
|
local chunk = table.concat(lines,"\r\n")
|
||||||
|
return chunk
|
||||||
end
|
end
|
||||||
|
|
||||||
setmetatable(command, { __index = function(t,k)
|
setmetatable(command, { __index = function(t,k)
|
||||||
local cmd = string.upper(k)
|
local cmd = string.upper(k)
|
||||||
local f = function (self, ...)
|
local f = function (self, v, ...)
|
||||||
return self[1]:request(compose_message { cmd, ... }, read_response)
|
if type(v) == "table" then
|
||||||
|
return self[1]:request(compose_message(cmd, v), read_response)
|
||||||
|
else
|
||||||
|
return self[1]:request(compose_message(cmd, {v, ...}), read_response)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
t[k] = f
|
t[k] = f
|
||||||
return f
|
return f
|
||||||
@@ -131,12 +157,12 @@ end
|
|||||||
|
|
||||||
function command:exists(key)
|
function command:exists(key)
|
||||||
local fd = self[1]
|
local fd = self[1]
|
||||||
return fd:request(compose_message { "EXISTS", key }, read_boolean)
|
return fd:request(compose_message ("EXISTS", key), read_boolean)
|
||||||
end
|
end
|
||||||
|
|
||||||
function command:sismember(key, value)
|
function command:sismember(key, value)
|
||||||
local fd = self[1]
|
local fd = self[1]
|
||||||
return fd:request(compose_message { "SISMEMBER", key, value }, read_boolean)
|
return fd:request(compose_message ("SISMEMBER", {key, value}), read_boolean)
|
||||||
end
|
end
|
||||||
|
|
||||||
--- watch mode
|
--- watch mode
|
||||||
@@ -156,10 +182,10 @@ local function watch_login(obj, auth)
|
|||||||
so:request("AUTH "..auth.."\r\n", read_response)
|
so:request("AUTH "..auth.."\r\n", read_response)
|
||||||
end
|
end
|
||||||
for k in pairs(obj.__psubscribe) do
|
for k in pairs(obj.__psubscribe) do
|
||||||
so:request(compose_message { "PSUBSCRIBE", k })
|
so:request(compose_message ("PSUBSCRIBE", k))
|
||||||
end
|
end
|
||||||
for k in pairs(obj.__subscribe) do
|
for k in pairs(obj.__subscribe) do
|
||||||
so:request(compose_message { "SUBSCRIBE", k })
|
so:request(compose_message("SUBSCRIBE", k))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -192,7 +218,7 @@ local function watch_func( name )
|
|||||||
local so = self.__sock
|
local so = self.__sock
|
||||||
for i = 1, select("#", ...) do
|
for i = 1, select("#", ...) do
|
||||||
local v = select(i, ...)
|
local v = select(i, ...)
|
||||||
so:request(compose_message { NAME, v })
|
so:request(compose_message(NAME, v))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,15 +16,16 @@ local skynet = {
|
|||||||
-- read skynet.h
|
-- read skynet.h
|
||||||
PTYPE_TEXT = 0,
|
PTYPE_TEXT = 0,
|
||||||
PTYPE_RESPONSE = 1,
|
PTYPE_RESPONSE = 1,
|
||||||
-- PTYPE_MULTICAST = 2, -- DEPRECATED
|
PTYPE_MULTICAST = 2,
|
||||||
PTYPE_CLIENT = 3,
|
PTYPE_CLIENT = 3,
|
||||||
PTYPE_SYSTEM = 4,
|
PTYPE_SYSTEM = 4,
|
||||||
PTYPE_HARBOR = 5,
|
PTYPE_HARBOR = 5,
|
||||||
PTYPE_SOCKET = 6,
|
PTYPE_SOCKET = 6,
|
||||||
PTYPE_ERROR = 7,
|
PTYPE_ERROR = 7,
|
||||||
PTYPE_QUEUE = 8,
|
PTYPE_QUEUE = 8, -- use in deprecated mqueue, use skynet.queue instead
|
||||||
PTYPE_DEBUG = 9,
|
PTYPE_DEBUG = 9,
|
||||||
PTYPE_LUA = 10
|
PTYPE_LUA = 10,
|
||||||
|
PTYPE_SNAX = 11,
|
||||||
}
|
}
|
||||||
|
|
||||||
-- code cache
|
-- code cache
|
||||||
@@ -68,13 +69,13 @@ local function dispatch_error_queue()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function _error_dispatch(error_session, monitor, service)
|
local function _error_dispatch(error_session, error_source)
|
||||||
if service then
|
if error_session == 0 then
|
||||||
-- service is down
|
-- service is down
|
||||||
-- Don't remove from watching_service , because user may call dead service
|
-- Don't remove from watching_service , because user may call dead service
|
||||||
watching_service[service] = false
|
watching_service[error_source] = false
|
||||||
for session, srv in pairs(watching_session) do
|
for session, srv in pairs(watching_session) do
|
||||||
if srv == service then
|
if srv == error_source then
|
||||||
table.insert(error_queue, session)
|
table.insert(error_queue, session)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -86,21 +87,6 @@ local function _error_dispatch(error_session, monitor, service)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local watch_monitor
|
|
||||||
|
|
||||||
function skynet.watch(service)
|
|
||||||
assert(type(service) == "number")
|
|
||||||
if watch_monitor == nil then
|
|
||||||
watch_monitor = string_to_handle(c.command("MONITOR"))
|
|
||||||
assert(watch_monitor, "Need a monitor")
|
|
||||||
end
|
|
||||||
if watching_service[service] == nil then
|
|
||||||
watching_service[service] = true
|
|
||||||
-- read lualib/simplemonitor.lua
|
|
||||||
assert(skynet.call(watch_monitor, "lua", "WATCH", service), "watch a dead service")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- coroutine reuse
|
-- coroutine reuse
|
||||||
|
|
||||||
local coroutine_pool = {}
|
local coroutine_pool = {}
|
||||||
@@ -141,7 +127,7 @@ function suspend(co, result, command, param, size)
|
|||||||
if not result then
|
if not result then
|
||||||
local session = session_coroutine_id[co]
|
local session = session_coroutine_id[co]
|
||||||
local addr = session_coroutine_address[co]
|
local addr = session_coroutine_address[co]
|
||||||
if session and session ~= 0 then
|
if session then
|
||||||
c.send(addr, skynet.PTYPE_ERROR, session, "")
|
c.send(addr, skynet.PTYPE_ERROR, session, "")
|
||||||
end
|
end
|
||||||
session_coroutine_id[co] = nil
|
session_coroutine_id[co] = nil
|
||||||
@@ -165,6 +151,9 @@ function suspend(co, result, command, param, size)
|
|||||||
-- coroutine exit
|
-- coroutine exit
|
||||||
session_coroutine_id[co] = nil
|
session_coroutine_id[co] = nil
|
||||||
session_coroutine_address[co] = nil
|
session_coroutine_address[co] = nil
|
||||||
|
elseif command == "QUIT" then
|
||||||
|
-- service exit
|
||||||
|
return
|
||||||
else
|
else
|
||||||
error("Unknown command : " .. command .. "\n" .. debug.traceback(co))
|
error("Unknown command : " .. command .. "\n" .. debug.traceback(co))
|
||||||
end
|
end
|
||||||
@@ -204,12 +193,33 @@ function skynet.wait()
|
|||||||
session_id_coroutine[session] = nil
|
session_id_coroutine[session] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function globalname(name, handle)
|
||||||
|
local c = string.sub(name,1,1)
|
||||||
|
assert(c ~= ':')
|
||||||
|
if c == '.' then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h
|
||||||
|
assert(tonumber(name) == nil) -- global name can't be number
|
||||||
|
|
||||||
|
local harbor = require "skynet.harbor"
|
||||||
|
|
||||||
|
harbor.globalname(name, handle)
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
function skynet.register(name)
|
function skynet.register(name)
|
||||||
|
if not globalname(name) then
|
||||||
c.command("REG", name)
|
c.command("REG", name)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.name(name, handle)
|
function skynet.name(name, handle)
|
||||||
c.command("NAME", name .. " " .. handle)
|
if not globalname(name, handle) then
|
||||||
|
c.command("NAME", name .. " " .. skynet.address(handle))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local self_handle
|
local self_handle
|
||||||
@@ -222,7 +232,10 @@ function skynet.self()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function skynet.localname(name)
|
function skynet.localname(name)
|
||||||
return string_to_handle(c.command("QUERY", name))
|
local addr = c.command("QUERY", name)
|
||||||
|
if addr then
|
||||||
|
return string_to_handle(addr)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.launch(...)
|
function skynet.launch(...)
|
||||||
@@ -240,9 +253,22 @@ function skynet.starttime()
|
|||||||
return tonumber(c.command("STARTTIME"))
|
return tonumber(c.command("STARTTIME"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function skynet.time()
|
||||||
|
return skynet.now()/100 + skynet.starttime() -- get now first would be better
|
||||||
|
end
|
||||||
|
|
||||||
function skynet.exit()
|
function skynet.exit()
|
||||||
skynet.send(".launcher","lua","REMOVE",skynet.self())
|
skynet.send(".launcher","lua","REMOVE",skynet.self())
|
||||||
|
for co, session in pairs(session_coroutine_id) do
|
||||||
|
local address = session_coroutine_address[co]
|
||||||
|
local self = skynet.self()
|
||||||
|
if session~=0 and address then
|
||||||
|
skynet.redirect(address, self, "error", session, "")
|
||||||
|
end
|
||||||
|
end
|
||||||
c.command("EXIT")
|
c.command("EXIT")
|
||||||
|
-- quit service
|
||||||
|
coroutine_yield "QUIT"
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.kill(name)
|
function skynet.kill(name)
|
||||||
@@ -254,7 +280,12 @@ function skynet.kill(name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function skynet.getenv(key)
|
function skynet.getenv(key)
|
||||||
return c.command("GETENV",key)
|
local ret = c.command("GETENV",key)
|
||||||
|
if ret == "" then
|
||||||
|
return
|
||||||
|
else
|
||||||
|
return ret
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.setenv(key, value)
|
function skynet.setenv(key, value)
|
||||||
@@ -276,6 +307,7 @@ skynet.redirect = function(dest,source,typename,...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
skynet.pack = assert(c.pack)
|
skynet.pack = assert(c.pack)
|
||||||
|
skynet.packstring = assert(c.packstring)
|
||||||
skynet.unpack = assert(c.unpack)
|
skynet.unpack = assert(c.unpack)
|
||||||
skynet.tostring = assert(c.tostring)
|
skynet.tostring = assert(c.tostring)
|
||||||
|
|
||||||
@@ -283,7 +315,7 @@ local function yield_call(service, session)
|
|||||||
watching_session[session] = service
|
watching_session[session] = service
|
||||||
local succ, msg, sz = coroutine_yield("CALL", session)
|
local succ, msg, sz = coroutine_yield("CALL", session)
|
||||||
watching_session[session] = nil
|
watching_session[session] = nil
|
||||||
assert(succ, "Capture an error")
|
assert(succ, debug.traceback())
|
||||||
return msg,sz
|
return msg,sz
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -299,19 +331,11 @@ function skynet.call(addr, typename, ...)
|
|||||||
return p.unpack(yield_call(addr, session))
|
return p.unpack(yield_call(addr, session))
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.blockcall(addr, typename , ...)
|
|
||||||
local p = proto[typename]
|
|
||||||
c.command("LOCK")
|
|
||||||
local session = c.send(addr, p.id , nil , p.pack(...))
|
|
||||||
if session == nil then
|
|
||||||
c.command("UNLOCK")
|
|
||||||
error("call to invalid address " .. skynet.address(addr))
|
|
||||||
end
|
|
||||||
return p.unpack(yield_call(addr, session))
|
|
||||||
end
|
|
||||||
|
|
||||||
function skynet.rawcall(addr, typename, msg, sz)
|
function skynet.rawcall(addr, typename, msg, sz)
|
||||||
local p = proto[typename]
|
local p = proto[typename]
|
||||||
|
if watching_service[addr] == false then
|
||||||
|
error("Service is dead")
|
||||||
|
end
|
||||||
local session = assert(c.send(addr, p.id , nil , msg, sz), "call to invalid address")
|
local session = assert(c.send(addr, p.id , nil , msg, sz), "call to invalid address")
|
||||||
return yield_call(addr, session)
|
return yield_call(addr, session)
|
||||||
end
|
end
|
||||||
@@ -338,8 +362,8 @@ function skynet.dispatch(typename, func)
|
|||||||
p.dispatch = func
|
p.dispatch = func
|
||||||
end
|
end
|
||||||
|
|
||||||
local function unknown_request(session, address, msg, sz)
|
local function unknown_request(session, address, msg, sz, prototype)
|
||||||
print("Unknown request :" , c.tostring(msg,sz))
|
skynet.error(string.format("Unknown request (%s): %s", prototype, c.tostring(msg,sz)))
|
||||||
error(string.format("Unknown session : %d from %x", session, address))
|
error(string.format("Unknown session : %d from %x", session, address))
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -393,7 +417,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...)
|
|||||||
session_coroutine_address[co] = source
|
session_coroutine_address[co] = source
|
||||||
suspend(co, coroutine.resume(co, session,source, p.unpack(msg,sz, ...)))
|
suspend(co, coroutine.resume(co, session,source, p.unpack(msg,sz, ...)))
|
||||||
else
|
else
|
||||||
unknown_request(session, source, msg, sz)
|
unknown_request(session, source, msg, sz, proto[prototype])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -420,8 +444,7 @@ local function dispatch_message(...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function skynet.newservice(name, ...)
|
function skynet.newservice(name, ...)
|
||||||
local param = table.concat({"snlua", name, ...}, " ")
|
local handle = skynet.tostring(skynet.rawcall(".launcher", "lua" , skynet.pack("LAUNCH", "snlua", name, ...)))
|
||||||
local handle = skynet.call(".launcher", "text" , param)
|
|
||||||
if handle == "" then
|
if handle == "" then
|
||||||
return nil
|
return nil
|
||||||
else
|
else
|
||||||
@@ -430,32 +453,18 @@ function skynet.newservice(name, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function skynet.uniqueservice(global, ...)
|
function skynet.uniqueservice(global, ...)
|
||||||
local handle
|
|
||||||
if global == true then
|
if global == true then
|
||||||
handle = skynet.call("SERVICE", "lua", "LAUNCH", ...)
|
return assert(skynet.call(".service", "lua", "GLAUNCH", ...))
|
||||||
else
|
else
|
||||||
handle = skynet.call(".service", "lua", "LAUNCH", global, ...)
|
return assert(skynet.call(".service", "lua", "LAUNCH", global, ...))
|
||||||
end
|
end
|
||||||
assert(handle , "Unique service launch failed")
|
|
||||||
return handle
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.queryservice(global, ...)
|
function skynet.queryservice(global, ...)
|
||||||
local handle
|
|
||||||
if global == true then
|
if global == true then
|
||||||
handle = skynet.call("SERVICE", "lua", "QUERY", ...)
|
return assert(skynet.call(".service", "lua", "GQUERY", ...))
|
||||||
else
|
else
|
||||||
handle = skynet.call(".service", "lua", "QUERY", global, ...)
|
return assert(skynet.call(".service", "lua", "QUERY", global, ...))
|
||||||
end
|
|
||||||
assert(handle , "Unique service query failed")
|
|
||||||
return handle
|
|
||||||
end
|
|
||||||
|
|
||||||
local function group_command(cmd, handle, address)
|
|
||||||
if address then
|
|
||||||
return string.format("%s %d :%x",cmd, handle, address)
|
|
||||||
else
|
|
||||||
return string.format("%s %d",cmd,handle)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -479,66 +488,10 @@ function skynet.error(...)
|
|||||||
return c.error(table.concat(t, " "))
|
return c.error(table.concat(t, " "))
|
||||||
end
|
end
|
||||||
|
|
||||||
----- debug
|
|
||||||
|
|
||||||
local internal_info_func
|
|
||||||
|
|
||||||
function skynet.info_func(func)
|
|
||||||
internal_info_func = func
|
|
||||||
end
|
|
||||||
|
|
||||||
local dbgcmd = {}
|
|
||||||
|
|
||||||
function dbgcmd.MEM()
|
|
||||||
local kb, bytes = collectgarbage "count"
|
|
||||||
skynet.ret(skynet.pack(kb,bytes))
|
|
||||||
end
|
|
||||||
|
|
||||||
function dbgcmd.GC()
|
|
||||||
coroutine_pool = {}
|
|
||||||
collectgarbage "collect"
|
|
||||||
end
|
|
||||||
|
|
||||||
function dbgcmd.STAT()
|
|
||||||
local stat = {}
|
|
||||||
stat.mqlen = skynet.mqlen()
|
|
||||||
skynet.ret(skynet.pack(stat))
|
|
||||||
end
|
|
||||||
|
|
||||||
function dbgcmd.INFO()
|
|
||||||
if internal_info_func then
|
|
||||||
skynet.ret(skynet.pack(internal_info_func()))
|
|
||||||
else
|
|
||||||
skynet.ret(skynet.pack(nil))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function _debug_dispatch(session, address, cmd, ...)
|
|
||||||
local f = dbgcmd[cmd]
|
|
||||||
assert(f, cmd)
|
|
||||||
f(...)
|
|
||||||
end
|
|
||||||
|
|
||||||
----- register protocol
|
----- register protocol
|
||||||
do
|
do
|
||||||
local REG = skynet.register_protocol
|
local REG = skynet.register_protocol
|
||||||
|
|
||||||
REG {
|
|
||||||
name = "text",
|
|
||||||
id = skynet.PTYPE_TEXT,
|
|
||||||
pack = function (...)
|
|
||||||
local n = select ("#" , ...)
|
|
||||||
if n == 0 then
|
|
||||||
return ""
|
|
||||||
elseif n == 1 then
|
|
||||||
return tostring(...)
|
|
||||||
else
|
|
||||||
return table.concat({...}," ")
|
|
||||||
end
|
|
||||||
end,
|
|
||||||
unpack = c.tostring
|
|
||||||
}
|
|
||||||
|
|
||||||
REG {
|
REG {
|
||||||
name = "lua",
|
name = "lua",
|
||||||
id = skynet.PTYPE_LUA,
|
id = skynet.PTYPE_LUA,
|
||||||
@@ -551,19 +504,10 @@ do
|
|||||||
id = skynet.PTYPE_RESPONSE,
|
id = skynet.PTYPE_RESPONSE,
|
||||||
}
|
}
|
||||||
|
|
||||||
REG {
|
|
||||||
name = "debug",
|
|
||||||
id = skynet.PTYPE_DEBUG,
|
|
||||||
pack = skynet.pack,
|
|
||||||
unpack = skynet.unpack,
|
|
||||||
dispatch = _debug_dispatch,
|
|
||||||
}
|
|
||||||
|
|
||||||
REG {
|
REG {
|
||||||
name = "error",
|
name = "error",
|
||||||
id = skynet.PTYPE_ERROR,
|
id = skynet.PTYPE_ERROR,
|
||||||
pack = skynet.pack,
|
unpack = function(...) return ... end,
|
||||||
unpack = skynet.unpack,
|
|
||||||
dispatch = _error_dispatch,
|
dispatch = _error_dispatch,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
@@ -602,11 +546,11 @@ end
|
|||||||
local function init_service(start)
|
local function init_service(start)
|
||||||
local ok, err = xpcall(init_template, debug.traceback, start)
|
local ok, err = xpcall(init_template, debug.traceback, start)
|
||||||
if not ok then
|
if not ok then
|
||||||
print("init service failed:", err)
|
skynet.error("init service failed: " .. tostring(err))
|
||||||
skynet.send(".launcher","text", "ERROR")
|
skynet.send(".launcher","lua", "ERROR")
|
||||||
skynet.exit()
|
skynet.exit()
|
||||||
else
|
else
|
||||||
skynet.send(".launcher","text", "")
|
skynet.send(".launcher","lua", "LAUNCHOK")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -643,10 +587,26 @@ function skynet.monitor(service, query)
|
|||||||
end
|
end
|
||||||
assert(monitor, "Monitor launch failed")
|
assert(monitor, "Monitor launch failed")
|
||||||
c.command("MONITOR", string.format(":%08x", monitor))
|
c.command("MONITOR", string.format(":%08x", monitor))
|
||||||
|
return monitor
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.mqlen()
|
function skynet.mqlen()
|
||||||
return tonumber(c.command "MQLEN")
|
return tonumber(c.command "MQLEN")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function skynet.task(ret)
|
||||||
|
local t = 0
|
||||||
|
for session,co in pairs(session_id_coroutine) do
|
||||||
|
if ret then
|
||||||
|
ret[session] = debug.traceback(co)
|
||||||
|
end
|
||||||
|
t = t + 1
|
||||||
|
end
|
||||||
|
return t
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Inject internal debug framework
|
||||||
|
local debug = require "skynet.debug"
|
||||||
|
debug(skynet)
|
||||||
|
|
||||||
return skynet
|
return skynet
|
||||||
|
|||||||
56
lualib/skynet/debug.lua
Normal file
56
lualib/skynet/debug.lua
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
return function (skynet)
|
||||||
|
|
||||||
|
local internal_info_func
|
||||||
|
|
||||||
|
function skynet.info_func(func)
|
||||||
|
internal_info_func = func
|
||||||
|
end
|
||||||
|
|
||||||
|
local dbgcmd = {}
|
||||||
|
|
||||||
|
function dbgcmd.MEM()
|
||||||
|
local kb, bytes = collectgarbage "count"
|
||||||
|
skynet.ret(skynet.pack(kb,bytes))
|
||||||
|
end
|
||||||
|
|
||||||
|
function dbgcmd.GC()
|
||||||
|
coroutine_pool = {}
|
||||||
|
collectgarbage "collect"
|
||||||
|
end
|
||||||
|
|
||||||
|
function dbgcmd.STAT()
|
||||||
|
local stat = {}
|
||||||
|
stat.mqlen = skynet.mqlen()
|
||||||
|
stat.task = skynet.task()
|
||||||
|
skynet.ret(skynet.pack(stat))
|
||||||
|
end
|
||||||
|
|
||||||
|
function dbgcmd.TASK()
|
||||||
|
local task = {}
|
||||||
|
skynet.task(task)
|
||||||
|
skynet.ret(skynet.pack(task))
|
||||||
|
end
|
||||||
|
|
||||||
|
function dbgcmd.INFO()
|
||||||
|
if internal_info_func then
|
||||||
|
skynet.ret(skynet.pack(internal_info_func()))
|
||||||
|
else
|
||||||
|
skynet.ret(skynet.pack(nil))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function _debug_dispatch(session, address, cmd, ...)
|
||||||
|
local f = dbgcmd[cmd]
|
||||||
|
assert(f, cmd)
|
||||||
|
f(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "debug",
|
||||||
|
id = assert(skynet.PTYPE_DEBUG),
|
||||||
|
pack = assert(skynet.pack),
|
||||||
|
unpack = assert(skynet.unpack),
|
||||||
|
dispatch = _debug_dispatch,
|
||||||
|
}
|
||||||
|
|
||||||
|
end
|
||||||
18
lualib/skynet/harbor.lua
Normal file
18
lualib/skynet/harbor.lua
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
local harbor = {}
|
||||||
|
|
||||||
|
function harbor.globalname(name, handle)
|
||||||
|
handle = handle or skynet.self()
|
||||||
|
skynet.send(".slave", "lua", "REGISTER", name, handle)
|
||||||
|
end
|
||||||
|
|
||||||
|
function harbor.link(id)
|
||||||
|
skynet.call(".slave", "lua", "LINK", id)
|
||||||
|
end
|
||||||
|
|
||||||
|
function harbor.connect(id)
|
||||||
|
skynet.call(".slave", "lua", "CONNECT", id)
|
||||||
|
end
|
||||||
|
|
||||||
|
return harbor
|
||||||
33
lualib/skynet/queue.lua
Normal file
33
lualib/skynet/queue.lua
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local coroutine = coroutine
|
||||||
|
local pcall = pcall
|
||||||
|
local table = table
|
||||||
|
|
||||||
|
function skynet.queue()
|
||||||
|
local current_thread
|
||||||
|
local ref = 0
|
||||||
|
local thread_queue = {}
|
||||||
|
return function(f, ...)
|
||||||
|
local thread = coroutine.running()
|
||||||
|
if ref == 0 then
|
||||||
|
current_thread = thread
|
||||||
|
elseif current_thread ~= thread then
|
||||||
|
table.insert(thread_queue, thread)
|
||||||
|
skynet.wait()
|
||||||
|
assert(ref == 0)
|
||||||
|
end
|
||||||
|
ref = ref + 1
|
||||||
|
local ok, err = pcall(f, ...)
|
||||||
|
ref = ref - 1
|
||||||
|
if ref == 0 then
|
||||||
|
current_thread = nil
|
||||||
|
local co = table.remove(thread_queue,1)
|
||||||
|
if co then
|
||||||
|
skynet.wakeup(co)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(ok,err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return skynet.queue
|
||||||
@@ -1,11 +1,19 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
local snax_interface = require "snax_interface"
|
local snax_interface = require "snax.interface"
|
||||||
|
|
||||||
local snax = {}
|
local snax = {}
|
||||||
local typeclass = {}
|
local typeclass = {}
|
||||||
|
|
||||||
local G = { require = function() end }
|
local G = { require = function() end }
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "snax",
|
||||||
|
id = skynet.PTYPE_SNAX,
|
||||||
|
pack = skynet.pack,
|
||||||
|
unpack = skynet.unpack,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function snax.interface(name)
|
function snax.interface(name)
|
||||||
if typeclass[name] then
|
if typeclass[name] then
|
||||||
return typeclass[name]
|
return typeclass[name]
|
||||||
@@ -38,7 +46,7 @@ local function gen_post(type, handle)
|
|||||||
__index = function( t, k )
|
__index = function( t, k )
|
||||||
local id = assert(type.accept[k] , string.format("post %s no exist", k))
|
local id = assert(type.accept[k] , string.format("post %s no exist", k))
|
||||||
return function(...)
|
return function(...)
|
||||||
skynet_send(handle, "lua", id, ...)
|
skynet_send(handle, "snax", id, ...)
|
||||||
end
|
end
|
||||||
end })
|
end })
|
||||||
end
|
end
|
||||||
@@ -48,7 +56,7 @@ local function gen_req(type, handle)
|
|||||||
__index = function( t, k )
|
__index = function( t, k )
|
||||||
local id = assert(type.response[k] , string.format("request %s no exist", k))
|
local id = assert(type.response[k] , string.format("request %s no exist", k))
|
||||||
return function(...)
|
return function(...)
|
||||||
return skynet_call(handle, "lua", id, ...)
|
return skynet_call(handle, "snax", id, ...)
|
||||||
end
|
end
|
||||||
end })
|
end })
|
||||||
end
|
end
|
||||||
@@ -64,16 +72,14 @@ end
|
|||||||
|
|
||||||
local handle_cache = setmetatable( {} , { __mode = "kv" } )
|
local handle_cache = setmetatable( {} , { __mode = "kv" } )
|
||||||
|
|
||||||
function snax.newservice(name, ...)
|
function snax.rawnewservice(name, ...)
|
||||||
local t = snax.interface(name)
|
local t = snax.interface(name)
|
||||||
local handle = skynet.newservice("snaxd", name)
|
local handle = skynet.newservice("snaxd", name)
|
||||||
assert(handle_cache[handle] == nil)
|
assert(handle_cache[handle] == nil)
|
||||||
if t.system.init then
|
if t.system.init then
|
||||||
skynet.call(handle, "lua", t.system.init, ...)
|
skynet.call(handle, "snax", t.system.init, ...)
|
||||||
end
|
end
|
||||||
local ret = wrapper(handle, name, t)
|
return handle
|
||||||
handle_cache[handle] = ret
|
|
||||||
return ret
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function snax.bind(handle, type)
|
function snax.bind(handle, type)
|
||||||
@@ -88,9 +94,42 @@ function snax.bind(handle, type)
|
|||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function snax.newservice(name, ...)
|
||||||
|
local handle = snax.rawnewservice(name, ...)
|
||||||
|
return snax.bind(handle, name)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function service_name(global, name, ...)
|
||||||
|
if global == true then
|
||||||
|
return name
|
||||||
|
else
|
||||||
|
return global
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function snax.uniqueservice(name, ...)
|
||||||
|
local handle = assert(skynet.call(".service", "lua", "LAUNCH", "snaxd", name, ...))
|
||||||
|
return snax.bind(handle, name)
|
||||||
|
end
|
||||||
|
|
||||||
|
function snax.globalservice(name, ...)
|
||||||
|
local handle = assert(skynet.call(".service", "lua", "GLAUNCH", "snaxd", name, ...))
|
||||||
|
return snax.bind(handle, name)
|
||||||
|
end
|
||||||
|
|
||||||
|
function snax.queryservice(name)
|
||||||
|
local handle = assert(skynet.call(".service", "lua", "QUERY", "snaxd", name))
|
||||||
|
return snax.bind(handle, name)
|
||||||
|
end
|
||||||
|
|
||||||
|
function snax.queryglobal(name)
|
||||||
|
local handle = assert(skynet.call(".service", "lua", "GQUERY", "snaxd", name))
|
||||||
|
return snax.bind(handle, name)
|
||||||
|
end
|
||||||
|
|
||||||
function snax.kill(obj, ...)
|
function snax.kill(obj, ...)
|
||||||
local t = snax.interface(obj.type)
|
local t = snax.interface(obj.type)
|
||||||
skynet_call(obj.handle, "lua", t.system.exit, ...)
|
skynet_call(obj.handle, "snax", t.system.exit, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function test_result(ok, ...)
|
local function test_result(ok, ...)
|
||||||
@@ -103,7 +142,7 @@ end
|
|||||||
|
|
||||||
function snax.hotfix(obj, source, ...)
|
function snax.hotfix(obj, source, ...)
|
||||||
local t = snax.interface(obj.type)
|
local t = snax.interface(obj.type)
|
||||||
return test_result(skynet_call(obj.handle, "lua", t.system.hotfix, source, ...))
|
return test_result(skynet_call(obj.handle, "snax", t.system.hotfix, source, ...))
|
||||||
end
|
end
|
||||||
|
|
||||||
return snax
|
return snax
|
||||||
|
|||||||
130
lualib/snax/gateserver.lua
Normal file
130
lualib/snax/gateserver.lua
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local netpack = require "netpack"
|
||||||
|
local socketdriver = require "socketdriver"
|
||||||
|
|
||||||
|
local gateserver = {}
|
||||||
|
|
||||||
|
local socket -- listen socket
|
||||||
|
local queue -- message queue
|
||||||
|
local maxclient -- max client
|
||||||
|
local client_number = 0
|
||||||
|
local CMD = setmetatable({}, { __gc = function() netpack.clear(queue) end })
|
||||||
|
local nodelay = false
|
||||||
|
|
||||||
|
local connection = {}
|
||||||
|
|
||||||
|
function gateserver.openclient(fd)
|
||||||
|
if connection[fd] then
|
||||||
|
socketdriver.start(fd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function gateserver.closeclient(fd)
|
||||||
|
local c = connection[fd]
|
||||||
|
if c then
|
||||||
|
connection[fd] = false
|
||||||
|
socketdriver.close(fd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function gateserver.start(handler)
|
||||||
|
assert(handler.message)
|
||||||
|
assert(handler.connect)
|
||||||
|
|
||||||
|
function CMD.open( source, conf )
|
||||||
|
assert(not socket)
|
||||||
|
local address = conf.address or "0.0.0.0"
|
||||||
|
local port = assert(conf.port)
|
||||||
|
maxclient = conf.maxclient or 1024
|
||||||
|
nodelay = conf.nodelay
|
||||||
|
socket = socketdriver.listen(address, port)
|
||||||
|
socketdriver.start(socket)
|
||||||
|
if handler.open then
|
||||||
|
return handler.open(source, conf)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function CMD.close()
|
||||||
|
assert(socket)
|
||||||
|
socketdriver.close(socket)
|
||||||
|
socket = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
local MSG = {}
|
||||||
|
|
||||||
|
function MSG.data(fd, msg, sz)
|
||||||
|
if connection[fd] then
|
||||||
|
handler.message(fd, msg, sz)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function MSG.more()
|
||||||
|
for fd, msg, sz in netpack.pop, queue do
|
||||||
|
if connection[fd] then
|
||||||
|
handler.message(fd, msg, sz)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function MSG.open(fd, msg)
|
||||||
|
if client_number >= maxclient then
|
||||||
|
socketdriver.close(fd)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if nodelay then
|
||||||
|
socketdriver.nodelay(fd)
|
||||||
|
end
|
||||||
|
connection[fd] = true
|
||||||
|
client_number = client_number + 1
|
||||||
|
handler.connect(fd, msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function close_fd(fd)
|
||||||
|
local c = connection[fd]
|
||||||
|
if c ~= nil then
|
||||||
|
connection[fd] = nil
|
||||||
|
client_number = client_number - 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function MSG.close(fd)
|
||||||
|
if handler.disconnect then
|
||||||
|
handler.disconnect(fd)
|
||||||
|
end
|
||||||
|
close_fd(fd)
|
||||||
|
end
|
||||||
|
|
||||||
|
function MSG.error(fd, msg)
|
||||||
|
if handler.error then
|
||||||
|
handler.error(fd)
|
||||||
|
end
|
||||||
|
close_fd(fd)
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "socket",
|
||||||
|
id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6
|
||||||
|
unpack = function ( msg, sz )
|
||||||
|
return netpack.filter( queue, msg, sz)
|
||||||
|
end,
|
||||||
|
dispatch = function (_, _, q, type, ...)
|
||||||
|
queue = q
|
||||||
|
if type then
|
||||||
|
MSG[type](...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
skynet.dispatch("lua", function (_, address, cmd, ...)
|
||||||
|
local f = CMD[cmd]
|
||||||
|
if f then
|
||||||
|
skynet.ret(skynet.pack(f(address, ...)))
|
||||||
|
else
|
||||||
|
skynet.ret(skynet.pack(handler.command(cmd, address, ...)))
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
return gateserver
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
local si = require "snax_interface"
|
local si = require "snax.interface"
|
||||||
local io = io
|
local io = io
|
||||||
|
|
||||||
local hotfix = {}
|
local hotfix = {}
|
||||||
197
lualib/snax/loginserver.lua
Normal file
197
lualib/snax/loginserver.lua
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local socket = require "socket"
|
||||||
|
local crypt = require "crypt"
|
||||||
|
local table = table
|
||||||
|
local string = string
|
||||||
|
local assert = assert
|
||||||
|
|
||||||
|
--[[
|
||||||
|
|
||||||
|
Protocol:
|
||||||
|
|
||||||
|
line (\n) based text protocol
|
||||||
|
|
||||||
|
1. Server->Client : base64(8bytes random challenge)
|
||||||
|
2. Client->Server : base64(8bytes handshake client key)
|
||||||
|
3. Server: Gen a 8bytes handshake server key
|
||||||
|
4. Server->Client : base64(DH-Exchange(server key))
|
||||||
|
5. Server/Client secret := DH-Secret(client key/server key)
|
||||||
|
6. Client->Server : base64(HMAC(challenge, secret))
|
||||||
|
7. Client->Server : DES(secret, base64(token))
|
||||||
|
8. Server : call auth_handler(token) -> server, uid (A user defined method)
|
||||||
|
9. Server : call login_handler(server, uid, secret) ->subid (A user defined method)
|
||||||
|
10. Server->Client : 200 base64(subid)
|
||||||
|
|
||||||
|
Error Code:
|
||||||
|
400 Bad Request . challenge failed
|
||||||
|
401 Unauthorized . unauthorized by auth_handler
|
||||||
|
403 Forbidden . login_handler failed
|
||||||
|
406 Not Acceptable . already in login (disallow multi login)
|
||||||
|
|
||||||
|
Success:
|
||||||
|
200 base64(subid)
|
||||||
|
]]
|
||||||
|
|
||||||
|
local socket_error = {}
|
||||||
|
local function assert_socket(v, fd)
|
||||||
|
if v then
|
||||||
|
return v
|
||||||
|
else
|
||||||
|
skynet.error(string.format("auth failed: socket (fd = %d) closed", fd))
|
||||||
|
error(socket_error)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function write(fd, text)
|
||||||
|
assert_socket(socket.write(fd, text), fd)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function launch_slave(auth_handler)
|
||||||
|
local function auth(fd, addr)
|
||||||
|
fd = assert(tonumber(fd))
|
||||||
|
skynet.error(string.format("connect from %s (fd = %d)", addr, fd))
|
||||||
|
socket.start(fd)
|
||||||
|
|
||||||
|
-- set socket buffer limit (8K)
|
||||||
|
-- If the attacker send large package, close the socket
|
||||||
|
socket.limit(fd, 8192)
|
||||||
|
|
||||||
|
local challenge = crypt.randomkey()
|
||||||
|
write(fd, crypt.base64encode(challenge).."\n")
|
||||||
|
|
||||||
|
local handshake = assert_socket(socket.readline(fd), fd)
|
||||||
|
local clientkey = crypt.base64decode(handshake)
|
||||||
|
if #clientkey ~= 8 then
|
||||||
|
error "Invalid client key"
|
||||||
|
end
|
||||||
|
local serverkey = crypt.randomkey()
|
||||||
|
write(fd, crypt.base64encode(crypt.dhexchange(serverkey)).."\n")
|
||||||
|
|
||||||
|
local secret = crypt.dhsecret(clientkey, serverkey)
|
||||||
|
|
||||||
|
local response = assert_socket(socket.readline(fd), fd)
|
||||||
|
local hmac = crypt.hmac64(challenge, secret)
|
||||||
|
|
||||||
|
if hmac ~= crypt.base64decode(response) then
|
||||||
|
write(fd, "400 Bad Request\n")
|
||||||
|
error "challenge failed"
|
||||||
|
end
|
||||||
|
|
||||||
|
local etoken = assert_socket(socket.readline(fd),fd)
|
||||||
|
|
||||||
|
local token = crypt.desdecode(secret, crypt.base64decode(etoken))
|
||||||
|
|
||||||
|
local ok, server, uid = pcall(auth_handler,token)
|
||||||
|
|
||||||
|
socket.abandon(fd)
|
||||||
|
return ok, server, uid, secret
|
||||||
|
end
|
||||||
|
|
||||||
|
local function ret_pack(ok, err, ...)
|
||||||
|
if ok then
|
||||||
|
skynet.ret(skynet.pack(err, ...))
|
||||||
|
elseif err ~= socket_error then
|
||||||
|
error(err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.dispatch("lua", function(_,_,...)
|
||||||
|
ret_pack(pcall(auth, ...))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
local user_login = {}
|
||||||
|
|
||||||
|
local function accept(conf, s, fd, addr)
|
||||||
|
-- call slave auth
|
||||||
|
local ok, server, uid, secret = skynet.call(s, "lua", fd, addr)
|
||||||
|
socket.start(fd)
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
write(fd, "401 Unauthorized\n")
|
||||||
|
error(server)
|
||||||
|
end
|
||||||
|
|
||||||
|
if not conf.multilogin then
|
||||||
|
if user_login[uid] then
|
||||||
|
write(fd, "406 Not Acceptable\n")
|
||||||
|
error(string.format("User %s is already login", uid))
|
||||||
|
end
|
||||||
|
|
||||||
|
user_login[uid] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
local ok, err = pcall(conf.login_handler, server, uid, secret)
|
||||||
|
-- unlock login
|
||||||
|
user_login[uid] = nil
|
||||||
|
|
||||||
|
if ok then
|
||||||
|
err = err or ""
|
||||||
|
write(fd, "200 "..crypt.base64encode(err).."\n")
|
||||||
|
else
|
||||||
|
write(fd, "403 Forbidden\n")
|
||||||
|
error(err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function launch_master(conf)
|
||||||
|
local instance = conf.instance or 8
|
||||||
|
assert(instance > 0)
|
||||||
|
local host = conf.host or "0.0.0.0"
|
||||||
|
local port = assert(tonumber(conf.port))
|
||||||
|
local slave = {}
|
||||||
|
local balance = 1
|
||||||
|
|
||||||
|
skynet.dispatch("lua", function(_,source,command, ...)
|
||||||
|
if command == "register_slave" then
|
||||||
|
table.insert(slave, source)
|
||||||
|
skynet.ret(skynet.pack(nil))
|
||||||
|
else
|
||||||
|
skynet.ret(skynet.pack(conf.command_handler(command, ...)))
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
for i=1,instance do
|
||||||
|
skynet.newservice(SERVICE_NAME)
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.error(string.format("login server listen at : %s %d", host, port))
|
||||||
|
local id = socket.listen(host, port)
|
||||||
|
socket.start(id , function(fd, addr)
|
||||||
|
local s = slave[balance]
|
||||||
|
balance = balance + 1
|
||||||
|
if balance > #slave then
|
||||||
|
balance = 1
|
||||||
|
end
|
||||||
|
local ok, err = pcall(accept, conf, s, fd, addr)
|
||||||
|
if not ok then
|
||||||
|
if err ~= socket_error then
|
||||||
|
skynet.error(string.format("invalid client (fd = %d) error = %s", fd, err))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
socket.close(fd)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function login(conf)
|
||||||
|
local name = "." .. (conf.name or "login")
|
||||||
|
skynet.start(function()
|
||||||
|
local loginmaster = skynet.localname(name)
|
||||||
|
if loginmaster then
|
||||||
|
skynet.call(loginmaster, "lua", "register_slave")
|
||||||
|
local auth_handler = assert(conf.auth_handler)
|
||||||
|
launch_master = nil
|
||||||
|
conf = nil
|
||||||
|
launch_slave(auth_handler)
|
||||||
|
else
|
||||||
|
launch_slave = nil
|
||||||
|
conf.auth_handler = nil
|
||||||
|
assert(conf.login_handler)
|
||||||
|
assert(conf.command_handler)
|
||||||
|
skynet.register(name)
|
||||||
|
launch_master(conf)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
return login
|
||||||
318
lualib/snax/msgserver.lua
Normal file
318
lualib/snax/msgserver.lua
Normal file
@@ -0,0 +1,318 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local gateserver = require "snax.gateserver"
|
||||||
|
local netpack = require "netpack"
|
||||||
|
local crypt = require "crypt"
|
||||||
|
local socketdriver = require "socketdriver"
|
||||||
|
local assert = assert
|
||||||
|
local b64encode = crypt.base64encode
|
||||||
|
local b64decode = crypt.base64decode
|
||||||
|
|
||||||
|
--[[
|
||||||
|
|
||||||
|
Protocol:
|
||||||
|
|
||||||
|
All the number type is big-endian
|
||||||
|
|
||||||
|
Shakehands (The first package)
|
||||||
|
|
||||||
|
Client -> Server :
|
||||||
|
|
||||||
|
base64(uid)@base64(server)#base64(subid):index:base64(hmac)
|
||||||
|
|
||||||
|
Server -> Client
|
||||||
|
|
||||||
|
XXX ErrorCode
|
||||||
|
404 User Not Found
|
||||||
|
403 Index Expired
|
||||||
|
401 Unauthorized
|
||||||
|
400 Bad Request
|
||||||
|
200 OK
|
||||||
|
|
||||||
|
Req-Resp
|
||||||
|
|
||||||
|
Client -> Server : Request
|
||||||
|
word size (Not include self)
|
||||||
|
string content (size-4)
|
||||||
|
dword session
|
||||||
|
|
||||||
|
Server -> Client : Response
|
||||||
|
word size (Not include self)
|
||||||
|
string content (size-5)
|
||||||
|
byte ok (1 is ok, 0 is error)
|
||||||
|
dword session
|
||||||
|
|
||||||
|
API:
|
||||||
|
server.userid(username)
|
||||||
|
return uid, subid, server
|
||||||
|
|
||||||
|
server.username(uid, subid, server)
|
||||||
|
return username
|
||||||
|
|
||||||
|
server.login(username, secret)
|
||||||
|
update user secret
|
||||||
|
|
||||||
|
server.logout(username)
|
||||||
|
user logout
|
||||||
|
|
||||||
|
server.ip(username)
|
||||||
|
return ip when connection establish, or nil
|
||||||
|
|
||||||
|
server.start(conf)
|
||||||
|
start server
|
||||||
|
|
||||||
|
Supported skynet command:
|
||||||
|
kick username (may used by loginserver)
|
||||||
|
login username secret (used by loginserver)
|
||||||
|
logout username (used by agent)
|
||||||
|
|
||||||
|
Config for server.start:
|
||||||
|
conf.expired_number : the number of the response message cached after sending out (default is 128)
|
||||||
|
conf.login_handler(uid, secret) -> subid : the function when a new user login, alloc a subid for it. (may call by login server)
|
||||||
|
conf.logout_handler(uid, subid) : the functon when a user logout. (may call by agent)
|
||||||
|
conf.kick_handler(uid, subid) : the functon when a user logout. (may call by login server)
|
||||||
|
conf.request_handler(username, session, msg, sz) : the function when recv a new request.
|
||||||
|
conf.register_handler(servername) : call when gate open
|
||||||
|
conf.disconnect_handler(username) : call when a connection disconnect (afk)
|
||||||
|
]]
|
||||||
|
|
||||||
|
local server = {}
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "client",
|
||||||
|
id = skynet.PTYPE_CLIENT,
|
||||||
|
}
|
||||||
|
|
||||||
|
local user_online = {}
|
||||||
|
local handshake = {}
|
||||||
|
local connection = {}
|
||||||
|
|
||||||
|
function server.userid(username)
|
||||||
|
-- base64(uid)@base64(server)#base64(subid)
|
||||||
|
local uid, servername, subid = username:match "([^@]*)@([^#]*)#(.*)"
|
||||||
|
return b64decode(uid), b64decode(subid), b64decode(servername)
|
||||||
|
end
|
||||||
|
|
||||||
|
function server.username(uid, subid, servername)
|
||||||
|
return string.format("%s@%s#%s", b64encode(uid), b64encode(servername), b64encode(tostring(subid)))
|
||||||
|
end
|
||||||
|
|
||||||
|
function server.logout(username)
|
||||||
|
local u = user_online[username]
|
||||||
|
user_online[username] = nil
|
||||||
|
if u.fd then
|
||||||
|
gateserver.closeclient(u.fd)
|
||||||
|
connection[u.fd] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function server.login(username, secret)
|
||||||
|
assert(user_online[username] == nil)
|
||||||
|
user_online[username] = {
|
||||||
|
secret = secret,
|
||||||
|
version = 0,
|
||||||
|
index = 0,
|
||||||
|
username = username,
|
||||||
|
response = {}, -- response cache
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
function server.ip(username)
|
||||||
|
local u = user_online[username]
|
||||||
|
if u and u.fd then
|
||||||
|
return u.ip
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function server.start(conf)
|
||||||
|
local expired_number = conf.expired_number or 128
|
||||||
|
|
||||||
|
local handler = {}
|
||||||
|
|
||||||
|
local CMD = {
|
||||||
|
login = assert(conf.login_handler),
|
||||||
|
logout = assert(conf.logout_handler),
|
||||||
|
kick = assert(conf.kick_handler),
|
||||||
|
}
|
||||||
|
|
||||||
|
function handler.command(cmd, source, ...)
|
||||||
|
local f = assert(CMD[cmd])
|
||||||
|
return f(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function handler.open(source, gateconf)
|
||||||
|
local servername = assert(gateconf.servername)
|
||||||
|
return conf.register_handler(servername)
|
||||||
|
end
|
||||||
|
|
||||||
|
function handler.connect(fd, addr)
|
||||||
|
handshake[fd] = addr
|
||||||
|
gateserver.openclient(fd)
|
||||||
|
end
|
||||||
|
|
||||||
|
function handler.disconnect(fd)
|
||||||
|
handshake[fd] = nil
|
||||||
|
local c = connection[fd]
|
||||||
|
if c then
|
||||||
|
c.fd = nil
|
||||||
|
connection[fd] = nil
|
||||||
|
if conf.disconnect_handler then
|
||||||
|
conf.disconnect_handler(c.username)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
handler.error = handler.disconnect
|
||||||
|
|
||||||
|
-- atomic , no yield
|
||||||
|
local function do_auth(fd, message, addr)
|
||||||
|
local username, index, hmac = string.match(message, "([^:]*):([^:]*):([^:]*)")
|
||||||
|
local u = user_online[username]
|
||||||
|
if u == nil then
|
||||||
|
return "404 User Not Found"
|
||||||
|
end
|
||||||
|
local idx = assert(tonumber(index))
|
||||||
|
hmac = b64decode(hmac)
|
||||||
|
|
||||||
|
if idx <= u.version then
|
||||||
|
return "403 Index Expired"
|
||||||
|
end
|
||||||
|
|
||||||
|
local text = string.format("%s:%s", username, index)
|
||||||
|
local v = crypt.hmac64(crypt.hashkey(text), u.secret)
|
||||||
|
if v ~= hmac then
|
||||||
|
return "401 Unauthorized"
|
||||||
|
end
|
||||||
|
|
||||||
|
u.version = idx
|
||||||
|
u.fd = fd
|
||||||
|
u.ip = addr
|
||||||
|
connection[fd] = u
|
||||||
|
end
|
||||||
|
|
||||||
|
local function auth(fd, addr, msg, sz)
|
||||||
|
local message = netpack.tostring(msg, sz)
|
||||||
|
local ok, result = pcall(do_auth, fd, message, addr)
|
||||||
|
if not ok then
|
||||||
|
skynet.error(result)
|
||||||
|
result = "400 Bad Request"
|
||||||
|
end
|
||||||
|
|
||||||
|
local close = result ~= nil
|
||||||
|
|
||||||
|
if result == nil then
|
||||||
|
result = "200 OK"
|
||||||
|
end
|
||||||
|
|
||||||
|
socketdriver.send(fd, netpack.pack(result))
|
||||||
|
|
||||||
|
if close then
|
||||||
|
gateserver.closeclient(fd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local request_handler = assert(conf.request_handler)
|
||||||
|
|
||||||
|
-- u.response is a struct { return_fd , response, version, index }
|
||||||
|
local function retire_response(u)
|
||||||
|
if u.index >= expired_number * 2 then
|
||||||
|
local max = 0
|
||||||
|
local response = u.response
|
||||||
|
for k,p in pairs(response) do
|
||||||
|
if p[1] == nil then
|
||||||
|
-- request complete, check expired
|
||||||
|
if p[4] < expired_number then
|
||||||
|
response[k] = nil
|
||||||
|
else
|
||||||
|
p[4] = p[4] - expired_number
|
||||||
|
if p[4] > max then
|
||||||
|
max = p[4]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
u.index = max + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function do_request(fd, msg, sz)
|
||||||
|
local u = assert(connection[fd], "invalid fd")
|
||||||
|
local msg_sz = sz - 4
|
||||||
|
local session = netpack.tostring(msg, sz, msg_sz)
|
||||||
|
local p = u.response[session]
|
||||||
|
if p then
|
||||||
|
-- session can be reuse in the same connection
|
||||||
|
if p[3] == u.version then
|
||||||
|
local last = u.response[session]
|
||||||
|
u.response[session] = nil
|
||||||
|
p = nil
|
||||||
|
if last[2] == nil then
|
||||||
|
local error_msg = string.format("Conflict session %s", crypt.hexencode(session))
|
||||||
|
skynet.error(error_msg)
|
||||||
|
error(error_msg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if p == nil then
|
||||||
|
p = { fd }
|
||||||
|
u.response[session] = p
|
||||||
|
local ok, result = pcall(conf.request_handler, u.username, msg, msg_sz)
|
||||||
|
result = result or ""
|
||||||
|
-- NOTICE: YIELD here, socket may close.
|
||||||
|
if not ok then
|
||||||
|
skynet.error(result)
|
||||||
|
result = "\0" .. session
|
||||||
|
else
|
||||||
|
result = result .. '\1' .. session
|
||||||
|
end
|
||||||
|
|
||||||
|
p[2] = netpack.pack_string(result)
|
||||||
|
p[3] = u.version
|
||||||
|
p[4] = u.index
|
||||||
|
else
|
||||||
|
netpack.tostring(msg, sz) -- request before, so free msg
|
||||||
|
-- update version/index, change return fd.
|
||||||
|
-- resend response.
|
||||||
|
p[1] = fd
|
||||||
|
p[3] = u.version
|
||||||
|
p[4] = u.index
|
||||||
|
if p[2] == nil then
|
||||||
|
-- already request, but response is not ready
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
u.index = u.index + 1
|
||||||
|
-- the return fd is p[1] (fd may change by multi request) check connect
|
||||||
|
fd = p[1]
|
||||||
|
if connection[fd] then
|
||||||
|
socketdriver.send(fd, p[2])
|
||||||
|
end
|
||||||
|
p[1] = nil
|
||||||
|
retire_response(u)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function request(fd, msg, sz)
|
||||||
|
local ok, err = pcall(do_request, fd, msg, sz)
|
||||||
|
-- not atomic, may yield
|
||||||
|
if not ok then
|
||||||
|
skynet.error(string.format("Invalid package %s : %s", err, netpack.tostring(msg, sz)))
|
||||||
|
if connection[fd] then
|
||||||
|
gateserver.closeclient(fd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function handler.message(fd, msg, sz)
|
||||||
|
local addr = handshake[fd]
|
||||||
|
if addr then
|
||||||
|
auth(fd,addr,msg,sz)
|
||||||
|
handshake[fd] = nil
|
||||||
|
else
|
||||||
|
request(fd, msg, sz)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return gateserver.start(handler)
|
||||||
|
end
|
||||||
|
|
||||||
|
return server
|
||||||
@@ -56,13 +56,21 @@ socket_message[1] = function(id, size, data)
|
|||||||
s.read_required = nil
|
s.read_required = nil
|
||||||
wakeup(s)
|
wakeup(s)
|
||||||
end
|
end
|
||||||
elseif rrt == "string" then
|
else
|
||||||
|
if s.buffer_limit and sz > s.buffer_limit then
|
||||||
|
skynet.error(string.format("socket buffer overlow: fd=%d size=%d", id , sz))
|
||||||
|
driver.clear(s.buffer,buffer_pool)
|
||||||
|
driver.close(id)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if rrt == "string" then
|
||||||
-- read line
|
-- read line
|
||||||
if driver.readline(s.buffer,nil,rr) then
|
if driver.readline(s.buffer,nil,rr) then
|
||||||
s.read_required = nil
|
s.read_required = nil
|
||||||
wakeup(s)
|
wakeup(s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- SKYNET_SOCKET_TYPE_CONNECT = 2
|
-- SKYNET_SOCKET_TYPE_CONNECT = 2
|
||||||
@@ -151,7 +159,7 @@ function socket.bind(os_fd)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function socket.stdin()
|
function socket.stdin()
|
||||||
return socket.bind(1)
|
return socket.bind(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
function socket.start(id, func)
|
function socket.start(id, func)
|
||||||
@@ -199,6 +207,27 @@ end
|
|||||||
function socket.read(id, sz)
|
function socket.read(id, sz)
|
||||||
local s = socket_pool[id]
|
local s = socket_pool[id]
|
||||||
assert(s)
|
assert(s)
|
||||||
|
if sz == nil then
|
||||||
|
-- read some bytes
|
||||||
|
local ret = driver.readall(s.buffer, buffer_pool)
|
||||||
|
if ret ~= "" then
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
|
if not s.connected then
|
||||||
|
return false, ret
|
||||||
|
end
|
||||||
|
assert(not s.read_required)
|
||||||
|
s.read_required = 0
|
||||||
|
suspend(s)
|
||||||
|
ret = driver.readall(s.buffer, buffer_pool)
|
||||||
|
if ret ~= "" then
|
||||||
|
return ret
|
||||||
|
else
|
||||||
|
return false, ret
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local ret = driver.pop(s.buffer, buffer_pool, sz)
|
local ret = driver.pop(s.buffer, buffer_pool, sz)
|
||||||
if ret then
|
if ret then
|
||||||
return ret
|
return ret
|
||||||
@@ -266,12 +295,19 @@ end
|
|||||||
|
|
||||||
socket.write = assert(driver.send)
|
socket.write = assert(driver.send)
|
||||||
socket.lwrite = assert(driver.lsend)
|
socket.lwrite = assert(driver.lsend)
|
||||||
|
socket.header = assert(driver.header)
|
||||||
|
|
||||||
function socket.invalid(id)
|
function socket.invalid(id)
|
||||||
return socket_pool[id] == nil
|
return socket_pool[id] == nil
|
||||||
end
|
end
|
||||||
|
|
||||||
socket.listen = assert(driver.listen)
|
function socket.listen(host, port, backlog)
|
||||||
|
if port == nil then
|
||||||
|
host, port = string.match(host, "([^:]+):(.+)$")
|
||||||
|
port = tonumber(port)
|
||||||
|
end
|
||||||
|
return driver.listen(host, port, backlog)
|
||||||
|
end
|
||||||
|
|
||||||
function socket.lock(id)
|
function socket.lock(id)
|
||||||
local s = socket_pool[id]
|
local s = socket_pool[id]
|
||||||
@@ -311,4 +347,9 @@ function socket.abandon(id)
|
|||||||
socket_pool[id] = nil
|
socket_pool[id] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function socket.limit(id, limit)
|
||||||
|
local s = assert(socket_pool[id])
|
||||||
|
s.buffer_limit = limit
|
||||||
|
end
|
||||||
|
|
||||||
return socket
|
return socket
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ function socket_channel.channel(desc)
|
|||||||
local c = {
|
local c = {
|
||||||
__host = assert(desc.host),
|
__host = assert(desc.host),
|
||||||
__port = assert(desc.port),
|
__port = assert(desc.port),
|
||||||
|
__backup = desc.backup,
|
||||||
__auth = desc.auth,
|
__auth = desc.auth,
|
||||||
__response = desc.response, -- It's for session mode
|
__response = desc.response, -- It's for session mode
|
||||||
__request = {}, -- request seq { response func or session } -- It's for order mode
|
__request = {}, -- request seq { response func or session } -- It's for order mode
|
||||||
@@ -135,6 +136,9 @@ local function dispatch_by_order(self)
|
|||||||
if result ~= socket_error then
|
if result ~= socket_error then
|
||||||
errmsg = result_ok
|
errmsg = result_ok
|
||||||
end
|
end
|
||||||
|
self.__result[co] = socket_error
|
||||||
|
self.__result_data[co] = errmsg
|
||||||
|
skynet.wakeup(co)
|
||||||
wakeup_all(self, errmsg)
|
wakeup_all(self, errmsg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -149,34 +153,66 @@ local function dispatch_function(self)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function connect_backup(self)
|
||||||
|
if self.__backup then
|
||||||
|
for _, addr in ipairs(self.__backup) do
|
||||||
|
local host, port
|
||||||
|
if type(addr) == "table" then
|
||||||
|
host, port = addr.host, addr.port
|
||||||
|
else
|
||||||
|
host = addr
|
||||||
|
port = self.__port
|
||||||
|
end
|
||||||
|
skynet.error("socket: connect to backup host", host, port)
|
||||||
|
local fd = socket.open(host, port)
|
||||||
|
if fd then
|
||||||
|
self.__host = host
|
||||||
|
self.__port = port
|
||||||
|
return fd
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function connect_once(self)
|
local function connect_once(self)
|
||||||
|
if self.__closed then
|
||||||
|
return false
|
||||||
|
end
|
||||||
assert(not self.__sock and not self.__authcoroutine)
|
assert(not self.__sock and not self.__authcoroutine)
|
||||||
local fd = socket.open(self.__host, self.__port)
|
local fd = socket.open(self.__host, self.__port)
|
||||||
|
if not fd then
|
||||||
|
fd = connect_backup(self)
|
||||||
if not fd then
|
if not fd then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
self.__authcoroutine = coroutine.running()
|
end
|
||||||
|
|
||||||
self.__sock = setmetatable( {fd} , channel_socket_meta )
|
self.__sock = setmetatable( {fd} , channel_socket_meta )
|
||||||
skynet.fork(dispatch_function(self), self)
|
skynet.fork(dispatch_function(self), self)
|
||||||
|
|
||||||
if self.__auth then
|
if self.__auth then
|
||||||
|
self.__authcoroutine = coroutine.running()
|
||||||
local ok , message = pcall(self.__auth, self)
|
local ok , message = pcall(self.__auth, self)
|
||||||
if not ok then
|
if not ok then
|
||||||
close_channel_socket(self)
|
close_channel_socket(self)
|
||||||
if message ~= socket_error then
|
if message ~= socket_error then
|
||||||
|
self.__authcoroutine = false
|
||||||
skynet.error("socket: auth failed", message)
|
skynet.error("socket: auth failed", message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
self.__authcoroutine = false
|
self.__authcoroutine = false
|
||||||
|
if ok and not self.__sock then
|
||||||
|
-- auth may change host, so connect again
|
||||||
|
return connect_once(self)
|
||||||
|
end
|
||||||
return ok
|
return ok
|
||||||
end
|
end
|
||||||
|
|
||||||
self.__authcoroutine = false
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function try_connect(self , once)
|
local function try_connect(self , once)
|
||||||
local t = 100
|
local t = 0
|
||||||
while not self.__closed do
|
while not self.__closed do
|
||||||
if connect_once(self) then
|
if connect_once(self) then
|
||||||
if not once then
|
if not once then
|
||||||
@@ -289,6 +325,20 @@ function channel:close()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function channel:changehost(host, port)
|
||||||
|
self.__host = host
|
||||||
|
if port then
|
||||||
|
self.__port = port
|
||||||
|
end
|
||||||
|
if not self.__closed then
|
||||||
|
close_channel_socket(self)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function channel:changebackup(backup)
|
||||||
|
self.__backup = backup
|
||||||
|
end
|
||||||
|
|
||||||
channel_meta.__gc = channel.close
|
channel_meta.__gc = channel.close
|
||||||
|
|
||||||
local function wrapper_socket_function(f)
|
local function wrapper_socket_function(f)
|
||||||
|
|||||||
@@ -36,6 +36,5 @@ linux freebsd : SKYNET_LIBS += -lrt
|
|||||||
macosx : MALLOC_STATICLIB :=
|
macosx : MALLOC_STATICLIB :=
|
||||||
macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC
|
macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC
|
||||||
|
|
||||||
|
|
||||||
linux macosx freebsd :
|
linux macosx freebsd :
|
||||||
$(MAKE) all PLAT=$@ SKYNET_LIBS="$(SKYNET_LIBS)" SHARED="$(SHARED)" EXPORT="$(EXPORT)" MALLOC_STATICLIB="$(MALLOC_STATICLIB)" SKYNET_DEFINES="$(SKYNET_DEFINES)"
|
$(MAKE) all PLAT=$@ SKYNET_LIBS="$(SKYNET_LIBS)" SHARED="$(SHARED)" EXPORT="$(EXPORT)" MALLOC_STATICLIB="$(MALLOC_STATICLIB)" SKYNET_DEFINES="$(SKYNET_DEFINES)"
|
||||||
|
|||||||
@@ -2,6 +2,16 @@
|
|||||||
#include "skynet_harbor.h"
|
#include "skynet_harbor.h"
|
||||||
#include "skynet_socket.h"
|
#include "skynet_socket.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
harbor listen the PTYPE_HARBOR (in text)
|
||||||
|
N name : update the global name
|
||||||
|
S fd id: connect to new harbor , we should send self_id to fd first , and then recv a id (check it), and at last send queue.
|
||||||
|
A fd id: accept new harbor , we should send self_id to fd , and then send queue.
|
||||||
|
|
||||||
|
If the fd is disconnected, send message to slave in PTYPE_TEXT. D id
|
||||||
|
If we don't known a globalname, send message to slave in PTYPE_TEXT. Q name
|
||||||
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@@ -13,8 +23,22 @@
|
|||||||
#define HASH_SIZE 4096
|
#define HASH_SIZE 4096
|
||||||
#define DEFAULT_QUEUE_SIZE 1024
|
#define DEFAULT_QUEUE_SIZE 1024
|
||||||
|
|
||||||
|
// 12 is sizeof(struct remote_message_header)
|
||||||
|
#define HEADER_COOKIE_LENGTH 12
|
||||||
|
|
||||||
|
/*
|
||||||
|
message type (8bits) is in destination high 8bits
|
||||||
|
harbor id (8bits) is also in that place , but remote message doesn't need harbor id.
|
||||||
|
*/
|
||||||
|
struct remote_message_header {
|
||||||
|
uint32_t source;
|
||||||
|
uint32_t destination;
|
||||||
|
uint32_t session;
|
||||||
|
};
|
||||||
|
|
||||||
struct msg {
|
struct msg {
|
||||||
uint8_t * buffer;
|
struct remote_message_header header;
|
||||||
|
void * buffer;
|
||||||
size_t size;
|
size_t size;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -37,32 +61,34 @@ struct hashmap {
|
|||||||
struct keyvalue *node[HASH_SIZE];
|
struct keyvalue *node[HASH_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
#define STATUS_WAIT 0
|
||||||
message type (8bits) is in destination high 8bits
|
#define STATUS_HANDSHAKE 1
|
||||||
harbor id (8bits) is also in that place , but remote message doesn't need harbor id.
|
#define STATUS_HEADER 2
|
||||||
*/
|
#define STATUS_CONTENT 3
|
||||||
struct remote_message_header {
|
#define STATUS_DOWN 4
|
||||||
uint32_t source;
|
|
||||||
uint32_t destination;
|
struct slave {
|
||||||
uint32_t session;
|
int fd;
|
||||||
|
struct msg_queue *queue;
|
||||||
|
int status;
|
||||||
|
int length;
|
||||||
|
int read;
|
||||||
|
uint8_t size[4];
|
||||||
|
char * recv_buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct harbor {
|
struct harbor {
|
||||||
struct skynet_context *ctx;
|
struct skynet_context *ctx;
|
||||||
char * local_addr;
|
|
||||||
int id;
|
int id;
|
||||||
|
uint32_t slave;
|
||||||
struct hashmap * map;
|
struct hashmap * map;
|
||||||
int master_fd;
|
struct slave s[REMOTE_MAX];
|
||||||
char * master_addr;
|
|
||||||
int remote_fd[REMOTE_MAX];
|
|
||||||
bool connected[REMOTE_MAX];
|
|
||||||
char * remote_addr[REMOTE_MAX];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// hash table
|
// hash table
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct remote_message_header * header) {
|
push_queue_msg(struct msg_queue * queue, struct msg * m) {
|
||||||
// If there is only 1 free slot which is reserved to distinguish full/empty
|
// If there is only 1 free slot which is reserved to distinguish full/empty
|
||||||
// of circular buffer, expand it.
|
// of circular buffer, expand it.
|
||||||
if (((queue->tail + 1) % queue->size) == queue->head) {
|
if (((queue->tail + 1) % queue->size) == queue->head) {
|
||||||
@@ -78,16 +104,21 @@ _push_queue(struct msg_queue * queue, const void * buffer, size_t sz, struct rem
|
|||||||
queue->size *= 2;
|
queue->size *= 2;
|
||||||
}
|
}
|
||||||
struct msg * slot = &queue->data[queue->tail];
|
struct msg * slot = &queue->data[queue->tail];
|
||||||
|
*slot = *m;
|
||||||
queue->tail = (queue->tail + 1) % queue->size;
|
queue->tail = (queue->tail + 1) % queue->size;
|
||||||
|
}
|
||||||
|
|
||||||
slot->buffer = skynet_malloc(sz + sizeof(*header));
|
static void
|
||||||
memcpy(slot->buffer, buffer, sz);
|
push_queue(struct msg_queue * queue, void * buffer, size_t sz, struct remote_message_header * header) {
|
||||||
memcpy(slot->buffer + sz, header, sizeof(*header));
|
struct msg m;
|
||||||
slot->size = sz + sizeof(*header);
|
m.header = *header;
|
||||||
|
m.buffer = buffer;
|
||||||
|
m.size = sz;
|
||||||
|
push_queue_msg(queue, &m);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct msg *
|
static struct msg *
|
||||||
_pop_queue(struct msg_queue * queue) {
|
pop_queue(struct msg_queue * queue) {
|
||||||
if (queue->head == queue->tail) {
|
if (queue->head == queue->tail) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -97,7 +128,7 @@ _pop_queue(struct msg_queue * queue) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct msg_queue *
|
static struct msg_queue *
|
||||||
_new_queue() {
|
new_queue() {
|
||||||
struct msg_queue * queue = skynet_malloc(sizeof(*queue));
|
struct msg_queue * queue = skynet_malloc(sizeof(*queue));
|
||||||
queue->size = DEFAULT_QUEUE_SIZE;
|
queue->size = DEFAULT_QUEUE_SIZE;
|
||||||
queue->head = 0;
|
queue->head = 0;
|
||||||
@@ -108,20 +139,19 @@ _new_queue() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_release_queue(struct msg_queue *queue) {
|
release_queue(struct msg_queue *queue) {
|
||||||
if (queue == NULL)
|
if (queue == NULL)
|
||||||
return;
|
return;
|
||||||
struct msg * m = _pop_queue(queue);
|
struct msg * m;
|
||||||
while (m) {
|
while ((m=pop_queue(queue)) != NULL) {
|
||||||
skynet_free(m->buffer);
|
skynet_free(m->buffer);
|
||||||
m = _pop_queue(queue);
|
|
||||||
}
|
}
|
||||||
skynet_free(queue->data);
|
skynet_free(queue->data);
|
||||||
skynet_free(queue);
|
skynet_free(queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct keyvalue *
|
static struct keyvalue *
|
||||||
_hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
|
hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
|
||||||
uint32_t *ptr = (uint32_t*) name;
|
uint32_t *ptr = (uint32_t*) name;
|
||||||
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
||||||
struct keyvalue * node = hash->node[h % HASH_SIZE];
|
struct keyvalue * node = hash->node[h % HASH_SIZE];
|
||||||
@@ -139,7 +169,7 @@ _hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
|
|||||||
// Don't support erase name yet
|
// Don't support erase name yet
|
||||||
|
|
||||||
static struct void
|
static struct void
|
||||||
_hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) {
|
hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) {
|
||||||
uint32_t *ptr = name;
|
uint32_t *ptr = name;
|
||||||
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
||||||
struct keyvalue ** ptr = &hash->node[h % HASH_SIZE];
|
struct keyvalue ** ptr = &hash->node[h % HASH_SIZE];
|
||||||
@@ -157,7 +187,7 @@ _hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static struct keyvalue *
|
static struct keyvalue *
|
||||||
_hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
|
hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
|
||||||
uint32_t *ptr = (uint32_t *)name;
|
uint32_t *ptr = (uint32_t *)name;
|
||||||
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
||||||
struct keyvalue ** pkv = &hash->node[h % HASH_SIZE];
|
struct keyvalue ** pkv = &hash->node[h % HASH_SIZE];
|
||||||
@@ -173,20 +203,20 @@ _hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static struct hashmap *
|
static struct hashmap *
|
||||||
_hash_new() {
|
hash_new() {
|
||||||
struct hashmap * h = skynet_malloc(sizeof(struct hashmap));
|
struct hashmap * h = skynet_malloc(sizeof(struct hashmap));
|
||||||
memset(h,0,sizeof(*h));
|
memset(h,0,sizeof(*h));
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_hash_delete(struct hashmap *hash) {
|
hash_delete(struct hashmap *hash) {
|
||||||
int i;
|
int i;
|
||||||
for (i=0;i<HASH_SIZE;i++) {
|
for (i=0;i<HASH_SIZE;i++) {
|
||||||
struct keyvalue * node = hash->node[i];
|
struct keyvalue * node = hash->node[i];
|
||||||
while (node) {
|
while (node) {
|
||||||
struct keyvalue * next = node->next;
|
struct keyvalue * next = node->next;
|
||||||
_release_queue(node->queue);
|
release_queue(node->queue);
|
||||||
skynet_free(node);
|
skynet_free(node);
|
||||||
node = next;
|
node = next;
|
||||||
}
|
}
|
||||||
@@ -196,64 +226,50 @@ _hash_delete(struct hashmap *hash) {
|
|||||||
|
|
||||||
///////////////
|
///////////////
|
||||||
|
|
||||||
|
static void
|
||||||
|
close_harbor(struct harbor *h, int id) {
|
||||||
|
struct slave *s = &h->s[id];
|
||||||
|
s->status = STATUS_DOWN;
|
||||||
|
if (s->fd) {
|
||||||
|
skynet_socket_close(h->ctx, s->fd);
|
||||||
|
}
|
||||||
|
if (s->queue) {
|
||||||
|
release_queue(s->queue);
|
||||||
|
s->queue = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
report_harbor_down(struct harbor *h, int id) {
|
||||||
|
char down[64];
|
||||||
|
int n = sprintf(down, "D %d",id);
|
||||||
|
|
||||||
|
skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, down, n);
|
||||||
|
}
|
||||||
|
|
||||||
struct harbor *
|
struct harbor *
|
||||||
harbor_create(void) {
|
harbor_create(void) {
|
||||||
struct harbor * h = skynet_malloc(sizeof(*h));
|
struct harbor * h = skynet_malloc(sizeof(*h));
|
||||||
h->ctx = NULL;
|
memset(h,0,sizeof(*h));
|
||||||
h->id = 0;
|
h->map = hash_new();
|
||||||
h->master_fd = -1;
|
|
||||||
h->master_addr = NULL;
|
|
||||||
int i;
|
|
||||||
for (i=0;i<REMOTE_MAX;i++) {
|
|
||||||
h->remote_fd[i] = -1;
|
|
||||||
h->connected[i] = false;
|
|
||||||
h->remote_addr[i] = NULL;
|
|
||||||
}
|
|
||||||
h->map = _hash_new();
|
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
harbor_release(struct harbor *h) {
|
harbor_release(struct harbor *h) {
|
||||||
struct skynet_context *ctx = h->ctx;
|
|
||||||
if (h->master_fd >= 0) {
|
|
||||||
skynet_socket_close(ctx, h->master_fd);
|
|
||||||
}
|
|
||||||
skynet_free(h->master_addr);
|
|
||||||
skynet_free(h->local_addr);
|
|
||||||
int i;
|
int i;
|
||||||
for (i=0;i<REMOTE_MAX;i++) {
|
for (i=1;i<REMOTE_MAX;i++) {
|
||||||
if (h->remote_fd[i] >= 0) {
|
struct slave *s = &h->s[i];
|
||||||
skynet_socket_close(ctx, h->remote_fd[i]);
|
if (s->fd && s->status != STATUS_DOWN) {
|
||||||
skynet_free(h->remote_addr[i]);
|
close_harbor(h,i);
|
||||||
|
// don't call report_harbor_down.
|
||||||
|
// never call skynet_send during module exit, because of dead lock
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_hash_delete(h->map);
|
hash_delete(h->map);
|
||||||
skynet_free(h);
|
skynet_free(h);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
_connect_to(struct harbor *h, const char *ipaddress, bool blocking) {
|
|
||||||
char * port = strchr(ipaddress,':');
|
|
||||||
if (port==NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
int sz = port - ipaddress;
|
|
||||||
char tmp[sz + 1];
|
|
||||||
memcpy(tmp,ipaddress,sz);
|
|
||||||
tmp[sz] = '\0';
|
|
||||||
|
|
||||||
int portid = strtol(port+1, NULL,10);
|
|
||||||
|
|
||||||
skynet_error(h->ctx, "Harbor(%d) connect to %s:%d", h->id, tmp, portid);
|
|
||||||
|
|
||||||
if (blocking) {
|
|
||||||
return skynet_socket_block_connect(h->ctx, tmp, portid);
|
|
||||||
} else {
|
|
||||||
return skynet_socket_connect(h->ctx, tmp, portid);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
to_bigendian(uint8_t *buffer, uint32_t n) {
|
to_bigendian(uint8_t *buffer, uint32_t n) {
|
||||||
buffer[0] = (n >> 24) & 0xff;
|
buffer[0] = (n >> 24) & 0xff;
|
||||||
@@ -263,7 +279,7 @@ to_bigendian(uint8_t *buffer, uint32_t n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
_header_to_message(const struct remote_message_header * header, uint8_t * message) {
|
header_to_message(const struct remote_message_header * header, uint8_t * message) {
|
||||||
to_bigendian(message , header->source);
|
to_bigendian(message , header->source);
|
||||||
to_bigendian(message+4 , header->destination);
|
to_bigendian(message+4 , header->destination);
|
||||||
to_bigendian(message+8 , header->session);
|
to_bigendian(message+8 , header->session);
|
||||||
@@ -280,112 +296,207 @@ from_bigendian(uint32_t n) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
_message_to_header(const uint32_t *message, struct remote_message_header *header) {
|
message_to_header(const uint32_t *message, struct remote_message_header *header) {
|
||||||
header->source = from_bigendian(message[0]);
|
header->source = from_bigendian(message[0]);
|
||||||
header->destination = from_bigendian(message[1]);
|
header->destination = from_bigendian(message[1]);
|
||||||
header->session = from_bigendian(message[2]);
|
header->session = from_bigendian(message[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
// socket package
|
||||||
_send_package(struct skynet_context *ctx, int fd, const void * buffer, size_t sz) {
|
|
||||||
uint8_t * sendbuf = skynet_malloc(sz+4);
|
|
||||||
to_bigendian(sendbuf, sz);
|
|
||||||
memcpy(sendbuf+4, buffer, sz);
|
|
||||||
|
|
||||||
if (skynet_socket_send(ctx, fd, sendbuf, sz+4)) {
|
static void
|
||||||
skynet_error(ctx, "Send to %d error", fd);
|
forward_local_messsage(struct harbor *h, void *msg, int sz) {
|
||||||
|
const char * cookie = msg;
|
||||||
|
cookie += sz - HEADER_COOKIE_LENGTH;
|
||||||
|
struct remote_message_header header;
|
||||||
|
message_to_header((const uint32_t *)cookie, &header);
|
||||||
|
|
||||||
|
uint32_t destination = header.destination;
|
||||||
|
int type = (destination >> HANDLE_REMOTE_SHIFT) | PTYPE_TAG_DONTCOPY;
|
||||||
|
destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT);
|
||||||
|
|
||||||
|
if (skynet_send(h->ctx, header.source, destination, type, (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH) < 0) {
|
||||||
|
skynet_error(h->ctx, "Unknown destination :%x from :%x", destination, header.source);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) {
|
send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) {
|
||||||
uint32_t sz_header = sz+sizeof(*cookie);
|
uint32_t sz_header = sz+sizeof(*cookie);
|
||||||
uint8_t * sendbuf = skynet_malloc(sz_header+4);
|
uint8_t * sendbuf = skynet_malloc(sz_header+4);
|
||||||
to_bigendian(sendbuf, sz_header);
|
to_bigendian(sendbuf, sz_header);
|
||||||
memcpy(sendbuf+4, buffer, sz);
|
memcpy(sendbuf+4, buffer, sz);
|
||||||
_header_to_message(cookie, sendbuf+4+sz);
|
header_to_message(cookie, sendbuf+4+sz);
|
||||||
|
|
||||||
if (skynet_socket_send(ctx, fd, sendbuf, sz_header+4)) {
|
// ignore send error, because if the connection is broken, the mainloop will recv a message.
|
||||||
skynet_error(ctx, "Remote send to %d error", fd);
|
skynet_socket_send(ctx, fd, sendbuf, sz_header+4);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_update_remote_address(struct harbor *h, int harbor_id, const char * ipaddr) {
|
dispatch_name_queue(struct harbor *h, struct keyvalue * node) {
|
||||||
if (harbor_id == h->id) {
|
struct msg_queue * queue = node->queue;
|
||||||
return;
|
uint32_t handle = node->value;
|
||||||
}
|
|
||||||
assert(harbor_id > 0 && harbor_id< REMOTE_MAX);
|
|
||||||
struct skynet_context * context = h->ctx;
|
|
||||||
if (h->remote_fd[harbor_id] >=0) {
|
|
||||||
skynet_socket_close(context, h->remote_fd[harbor_id]);
|
|
||||||
skynet_free(h->remote_addr[harbor_id]);
|
|
||||||
h->remote_addr[harbor_id] = NULL;
|
|
||||||
}
|
|
||||||
h->remote_fd[harbor_id] = _connect_to(h, ipaddr, false);
|
|
||||||
h->connected[harbor_id] = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
_dispatch_queue(struct harbor *h, struct msg_queue * queue, uint32_t handle, const char name[GLOBALNAME_LENGTH] ) {
|
|
||||||
int harbor_id = handle >> HANDLE_REMOTE_SHIFT;
|
int harbor_id = handle >> HANDLE_REMOTE_SHIFT;
|
||||||
assert(harbor_id != 0);
|
assert(harbor_id != 0);
|
||||||
struct skynet_context * context = h->ctx;
|
struct skynet_context * context = h->ctx;
|
||||||
int fd = h->remote_fd[harbor_id];
|
struct slave *s = &h->s[harbor_id];
|
||||||
if (fd < 0) {
|
int fd = s->fd;
|
||||||
|
if (fd == 0) {
|
||||||
|
if (s->status == STATUS_DOWN) {
|
||||||
char tmp [GLOBALNAME_LENGTH+1];
|
char tmp [GLOBALNAME_LENGTH+1];
|
||||||
memcpy(tmp, name , GLOBALNAME_LENGTH);
|
memcpy(tmp, node->key, GLOBALNAME_LENGTH);
|
||||||
tmp[GLOBALNAME_LENGTH] = '\0';
|
tmp[GLOBALNAME_LENGTH] = '\0';
|
||||||
skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id);
|
skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id);
|
||||||
|
} else {
|
||||||
|
if (s->queue == NULL) {
|
||||||
|
s->queue = node->queue;
|
||||||
|
node->queue = NULL;
|
||||||
|
} else {
|
||||||
|
struct msg * m;
|
||||||
|
while ((m = pop_queue(queue))!=NULL) {
|
||||||
|
push_queue_msg(s->queue, m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
struct msg * m = _pop_queue(queue);
|
struct msg * m;
|
||||||
while (m) {
|
while ((m = pop_queue(queue)) != NULL) {
|
||||||
struct remote_message_header cookie;
|
m->header.destination |= (handle & HANDLE_MASK);
|
||||||
uint8_t *ptr = m->buffer + m->size - sizeof(cookie);
|
send_remote(context, fd, m->buffer, m->size, &m->header);
|
||||||
memcpy(&cookie, ptr, sizeof(cookie));
|
|
||||||
cookie.destination |= (handle & HANDLE_MASK);
|
|
||||||
_header_to_message(&cookie, ptr);
|
|
||||||
_send_package(context, fd, m->buffer, m->size);
|
|
||||||
m = _pop_queue(queue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_update_remote_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) {
|
dispatch_queue(struct harbor *h, int id) {
|
||||||
struct keyvalue * node = _hash_search(h->map, name);
|
struct slave *s = &h->s[id];
|
||||||
|
int fd = s->fd;
|
||||||
|
assert(fd != 0);
|
||||||
|
|
||||||
|
struct msg_queue *queue = s->queue;
|
||||||
|
if (queue == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
struct msg * m;
|
||||||
|
while ((m = pop_queue(queue)) != NULL) {
|
||||||
|
send_remote(h->ctx, fd, m->buffer, m->size, &m->header);
|
||||||
|
}
|
||||||
|
release_queue(queue);
|
||||||
|
s->queue = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
push_socket_data(struct harbor *h, const struct skynet_socket_message * message) {
|
||||||
|
assert(message->type == SKYNET_SOCKET_TYPE_DATA);
|
||||||
|
int fd = message->id;
|
||||||
|
int i;
|
||||||
|
int id = 0;
|
||||||
|
struct slave * s = NULL;
|
||||||
|
for (i=1;i<REMOTE_MAX;i++) {
|
||||||
|
if (h->s[i].fd == fd) {
|
||||||
|
s = &h->s[i];
|
||||||
|
id = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (s == NULL) {
|
||||||
|
skynet_free(message->buffer);
|
||||||
|
skynet_error(h->ctx, "Invalid socket fd (%d) data", fd);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uint8_t * buffer = (uint8_t *)message->buffer;
|
||||||
|
int size = message->ud;
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
switch(s->status) {
|
||||||
|
case STATUS_HANDSHAKE: {
|
||||||
|
// check id
|
||||||
|
uint8_t remote_id = buffer[0];
|
||||||
|
if (remote_id != id) {
|
||||||
|
skynet_error(h->ctx, "Invalid shakehand id (%d) from fd = %d , harbor = %d", id, fd, remote_id);
|
||||||
|
close_harbor(h,id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
++buffer;
|
||||||
|
--size;
|
||||||
|
s->status = STATUS_HEADER;
|
||||||
|
|
||||||
|
dispatch_queue(h, id);
|
||||||
|
|
||||||
|
if (size == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// go though
|
||||||
|
}
|
||||||
|
case STATUS_HEADER: {
|
||||||
|
// big endian 4 bytes length, the first one must be 0.
|
||||||
|
int need = 4 - s->read;
|
||||||
|
if (size < need) {
|
||||||
|
memcpy(s->size + s->read, buffer, size);
|
||||||
|
s->read += size;
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
memcpy(s->size + s->read, buffer, need);
|
||||||
|
buffer += need;
|
||||||
|
size -= need;
|
||||||
|
|
||||||
|
if (s->size[0] != 0) {
|
||||||
|
skynet_error(h->ctx, "Message is too long from harbor %d", id);
|
||||||
|
close_harbor(h,id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
s->length = s->size[1] << 16 | s->size[2] << 8 | s->size[3];
|
||||||
|
s->read = 0;
|
||||||
|
s->recv_buffer = skynet_malloc(s->length);
|
||||||
|
s->status = STATUS_CONTENT;
|
||||||
|
if (size == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// go though
|
||||||
|
case STATUS_CONTENT: {
|
||||||
|
int need = s->length - s->read;
|
||||||
|
if (size < need) {
|
||||||
|
memcpy(s->recv_buffer + s->read, buffer, size);
|
||||||
|
s->read += size;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
memcpy(s->recv_buffer + s->read, buffer, need);
|
||||||
|
forward_local_messsage(h, s->recv_buffer, s->length);
|
||||||
|
s->length = 0;
|
||||||
|
s->read = 0;
|
||||||
|
s->recv_buffer = NULL;
|
||||||
|
size -= need;
|
||||||
|
buffer += need;
|
||||||
|
s->status = STATUS_HEADER;
|
||||||
|
if (size == 0)
|
||||||
|
return;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
update_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) {
|
||||||
|
struct keyvalue * node = hash_search(h->map, name);
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
node = _hash_insert(h->map, name);
|
node = hash_insert(h->map, name);
|
||||||
}
|
}
|
||||||
node->value = handle;
|
node->value = handle;
|
||||||
if (node->queue) {
|
if (node->queue) {
|
||||||
_dispatch_queue(h, node->queue, handle, name);
|
dispatch_name_queue(h, node);
|
||||||
_release_queue(node->queue);
|
release_queue(node->queue);
|
||||||
node->queue = NULL;
|
node->queue = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
_request_master(struct harbor *h, const char name[GLOBALNAME_LENGTH], size_t i, uint32_t handle) {
|
|
||||||
uint8_t buffer[4+i];
|
|
||||||
to_bigendian(buffer, handle);
|
|
||||||
memcpy(buffer+4,name,i);
|
|
||||||
|
|
||||||
_send_package(h->ctx, h->master_fd, buffer, 4+i);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
update global name to master
|
|
||||||
|
|
||||||
2 bytes (size)
|
|
||||||
4 bytes (handle) (handle == 0 for request)
|
|
||||||
n bytes string (name)
|
|
||||||
*/
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int type, int session, const char * msg, size_t sz) {
|
remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int type, int session, const char * msg, size_t sz) {
|
||||||
int harbor_id = destination >> HANDLE_REMOTE_SHIFT;
|
int harbor_id = destination >> HANDLE_REMOTE_SHIFT;
|
||||||
assert(harbor_id != 0);
|
|
||||||
struct skynet_context * context = h->ctx;
|
struct skynet_context * context = h->ctx;
|
||||||
if (harbor_id == h->id) {
|
if (harbor_id == h->id) {
|
||||||
// local message
|
// local message
|
||||||
@@ -393,56 +504,117 @@ _remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fd = h->remote_fd[harbor_id];
|
struct slave * s = &h->s[harbor_id];
|
||||||
if (fd >= 0 && h->connected[harbor_id]) {
|
if (s->fd == 0 || s->status == STATUS_HANDSHAKE) {
|
||||||
|
if (s->status == STATUS_DOWN) {
|
||||||
|
// throw an error return to source
|
||||||
|
// report the destination is dead
|
||||||
|
skynet_send(context, destination, source, PTYPE_ERROR, 0 , NULL, 0);
|
||||||
|
skynet_error(context, "Drop message to harbor %d from %x to %x (session = %d, msgsz = %d)",harbor_id, source, destination,session,(int)sz);
|
||||||
|
} else {
|
||||||
|
if (s->queue == NULL) {
|
||||||
|
s->queue = new_queue();
|
||||||
|
}
|
||||||
|
struct remote_message_header header;
|
||||||
|
header.source = source;
|
||||||
|
header.destination = (type << HANDLE_REMOTE_SHIFT) | (destination & HANDLE_MASK);
|
||||||
|
header.session = (uint32_t)session;
|
||||||
|
push_queue(s->queue, (void *)msg, sz, &header);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
struct remote_message_header cookie;
|
struct remote_message_header cookie;
|
||||||
cookie.source = source;
|
cookie.source = source;
|
||||||
cookie.destination = (destination & HANDLE_MASK) | ((uint32_t)type << HANDLE_REMOTE_SHIFT);
|
cookie.destination = (destination & HANDLE_MASK) | ((uint32_t)type << HANDLE_REMOTE_SHIFT);
|
||||||
cookie.session = (uint32_t)session;
|
cookie.session = (uint32_t)session;
|
||||||
_send_remote(context, fd, msg,sz,&cookie);
|
send_remote(context, s->fd, msg,sz,&cookie);
|
||||||
} else {
|
|
||||||
// throw an error return to source
|
|
||||||
if (session != 0) {
|
|
||||||
skynet_send(context, destination, source, PTYPE_RESERVED_ERROR, session, NULL, 0);
|
|
||||||
}
|
|
||||||
skynet_error(context, "Drop message to harbor %d from %x to %x (session = %d, msgsz = %d)",harbor_id, source, destination,session,(int)sz);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
_remote_register_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) {
|
|
||||||
int i;
|
|
||||||
for (i=0;i<GLOBALNAME_LENGTH;i++) {
|
|
||||||
if (name[i] == '\0')
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (handle != 0) {
|
|
||||||
_update_remote_name(h, name, handle);
|
|
||||||
}
|
|
||||||
_request_master(h, name,i,handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_remote_send_name(struct harbor *h, uint32_t source, const char name[GLOBALNAME_LENGTH], int type, int session, const char * msg, size_t sz) {
|
remote_send_name(struct harbor *h, uint32_t source, const char name[GLOBALNAME_LENGTH], int type, int session, const char * msg, size_t sz) {
|
||||||
struct keyvalue * node = _hash_search(h->map, name);
|
struct keyvalue * node = hash_search(h->map, name);
|
||||||
if (node == NULL) {
|
if (node == NULL) {
|
||||||
node = _hash_insert(h->map, name);
|
node = hash_insert(h->map, name);
|
||||||
}
|
}
|
||||||
if (node->value == 0) {
|
if (node->value == 0) {
|
||||||
if (node->queue == NULL) {
|
if (node->queue == NULL) {
|
||||||
node->queue = _new_queue();
|
node->queue = new_queue();
|
||||||
}
|
}
|
||||||
struct remote_message_header header;
|
struct remote_message_header header;
|
||||||
header.source = source;
|
header.source = source;
|
||||||
header.destination = type << HANDLE_REMOTE_SHIFT;
|
header.destination = type << HANDLE_REMOTE_SHIFT;
|
||||||
header.session = (uint32_t)session;
|
header.session = (uint32_t)session;
|
||||||
_push_queue(node->queue, msg, sz, &header);
|
push_queue(node->queue, (void *)msg, sz, &header);
|
||||||
// 0 for request
|
char query[2+GLOBALNAME_LENGTH+1] = "Q ";
|
||||||
_remote_register_name(h, name, 0);
|
query[2+GLOBALNAME_LENGTH] = 0;
|
||||||
|
memcpy(query+2, name, GLOBALNAME_LENGTH);
|
||||||
|
skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, query, strlen(query));
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
return _remote_send_handle(h, source, node->value, type, session, msg, sz);
|
return remote_send_handle(h, source, node->value, type, session, msg, sz);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
handshake(struct harbor *h, int id) {
|
||||||
|
struct slave *s = &h->s[id];
|
||||||
|
uint8_t * handshake = skynet_malloc(1);
|
||||||
|
handshake[0] = (uint8_t)h->id;
|
||||||
|
skynet_socket_send(h->ctx, s->fd, handshake, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
harbor_command(struct harbor * h, const char * msg, size_t sz, int session, uint32_t source) {
|
||||||
|
const char * name = msg + 2;
|
||||||
|
int s = (int)sz;
|
||||||
|
s -= 2;
|
||||||
|
switch(msg[0]) {
|
||||||
|
case 'N' : {
|
||||||
|
if (s <=0 || s>= GLOBALNAME_LENGTH) {
|
||||||
|
skynet_error(h->ctx, "Invalid global name %s", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct remote_name rn;
|
||||||
|
memset(&rn, 0, sizeof(rn));
|
||||||
|
memcpy(rn.name, name, s);
|
||||||
|
rn.handle = source;
|
||||||
|
update_name(h, rn.name, rn.handle);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'S' :
|
||||||
|
case 'A' : {
|
||||||
|
char buffer[s+1];
|
||||||
|
memcpy(buffer, name, s);
|
||||||
|
buffer[s] = 0;
|
||||||
|
int fd=0, id=0;
|
||||||
|
sscanf(buffer, "%d %d",&fd,&id);
|
||||||
|
if (fd == 0 || id <= 0 || id>=REMOTE_MAX) {
|
||||||
|
skynet_error(h->ctx, "Invalid command %c %s", msg[0], buffer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
struct slave * slave = &h->s[id];
|
||||||
|
if (slave->fd != 0) {
|
||||||
|
skynet_error(h->ctx, "Harbor %d alreay exist", id);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
slave->fd = fd;
|
||||||
|
|
||||||
|
skynet_socket_start(h->ctx, fd);
|
||||||
|
handshake(h, id);
|
||||||
|
if (msg[0] == 'S') {
|
||||||
|
slave->status = STATUS_HANDSHAKE;
|
||||||
|
} else {
|
||||||
|
slave->status = STATUS_HEADER;
|
||||||
|
dispatch_queue(h,id);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
skynet_error(h->ctx, "Unknown command %s", msg);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -450,105 +622,57 @@ static int
|
|||||||
harbor_id(struct harbor *h, int fd) {
|
harbor_id(struct harbor *h, int fd) {
|
||||||
int i;
|
int i;
|
||||||
for (i=1;i<REMOTE_MAX;i++) {
|
for (i=1;i<REMOTE_MAX;i++) {
|
||||||
if (h->remote_fd[i] == fd)
|
struct slave *s = &h->s[i];
|
||||||
|
if (s->fd == fd) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
close_harbor(struct harbor *h, int fd) {
|
|
||||||
int id = harbor_id(h,fd);
|
|
||||||
if (id == 0)
|
|
||||||
return;
|
|
||||||
skynet_error(h->ctx, "Harbor %d closed",id);
|
|
||||||
skynet_socket_close(h->ctx, fd);
|
|
||||||
h->remote_fd[id] = -1;
|
|
||||||
h->connected[id] = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
open_harbor(struct harbor *h, int fd) {
|
|
||||||
int id = harbor_id(h,fd);
|
|
||||||
if (id == 0)
|
|
||||||
return;
|
|
||||||
assert(h->connected[id] == false);
|
|
||||||
h->connected[id] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
||||||
struct harbor * h = ud;
|
struct harbor * h = ud;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case PTYPE_SOCKET: {
|
case PTYPE_SOCKET: {
|
||||||
const struct skynet_socket_message * message = msg;
|
const struct skynet_socket_message * message = msg;
|
||||||
switch(message->type) {
|
switch(message->type) {
|
||||||
case SKYNET_SOCKET_TYPE_DATA:
|
case SKYNET_SOCKET_TYPE_DATA:
|
||||||
|
push_socket_data(h, message);
|
||||||
skynet_free(message->buffer);
|
skynet_free(message->buffer);
|
||||||
skynet_error(context, "recv invalid socket message (size=%d)", message->ud);
|
|
||||||
break;
|
|
||||||
case SKYNET_SOCKET_TYPE_ACCEPT:
|
|
||||||
skynet_error(context, "recv invalid socket accept message");
|
|
||||||
break;
|
break;
|
||||||
case SKYNET_SOCKET_TYPE_ERROR:
|
case SKYNET_SOCKET_TYPE_ERROR:
|
||||||
case SKYNET_SOCKET_TYPE_CLOSE:
|
case SKYNET_SOCKET_TYPE_CLOSE: {
|
||||||
close_harbor(h, message->id);
|
int id = harbor_id(h, message->id);
|
||||||
|
if (id) {
|
||||||
|
report_harbor_down(h,id);
|
||||||
|
} else {
|
||||||
|
skynet_error(context, "Unkown fd (%d) closed", message->id);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case SKYNET_SOCKET_TYPE_CONNECT:
|
case SKYNET_SOCKET_TYPE_CONNECT:
|
||||||
open_harbor(h, message->id);
|
// fd forward to this service
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
skynet_error(context, "recv invalid socket message type %d", type);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
case PTYPE_HARBOR: {
|
case PTYPE_HARBOR: {
|
||||||
// remote message in
|
harbor_command(h, msg,sz,session,source);
|
||||||
const char * cookie = msg;
|
|
||||||
cookie += sz - 12;
|
|
||||||
struct remote_message_header header;
|
|
||||||
_message_to_header((const uint32_t *)cookie, &header);
|
|
||||||
if (header.source == 0) {
|
|
||||||
if (header.destination < REMOTE_MAX) {
|
|
||||||
// 1 byte harbor id (0~255)
|
|
||||||
// update remote harbor address
|
|
||||||
char ip [sz - 11];
|
|
||||||
memcpy(ip, msg, sz-12);
|
|
||||||
ip[sz-11] = '\0';
|
|
||||||
_update_remote_address(h, header.destination, ip);
|
|
||||||
} else {
|
|
||||||
// update global name
|
|
||||||
if (sz - 12 > GLOBALNAME_LENGTH) {
|
|
||||||
char name[sz-11];
|
|
||||||
memcpy(name, msg, sz-12);
|
|
||||||
name[sz-11] = '\0';
|
|
||||||
skynet_error(context, "Global name is too long %s", name);
|
|
||||||
}
|
|
||||||
_update_remote_name(h, msg, header.destination);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
uint32_t destination = header.destination;
|
|
||||||
int type = (destination >> HANDLE_REMOTE_SHIFT) | PTYPE_TAG_DONTCOPY;
|
|
||||||
destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT);
|
|
||||||
skynet_send(context, header.source, destination, type, (int)header.session, (void *)msg, sz-12);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
case PTYPE_SYSTEM: {
|
|
||||||
// register name message
|
|
||||||
const struct remote_message *rmsg = msg;
|
|
||||||
assert (sz == sizeof(rmsg->destination));
|
|
||||||
_remote_register_name(h, rmsg->destination.name, rmsg->destination.handle);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
default: {
|
default: {
|
||||||
// remote message out
|
// remote message out
|
||||||
const struct remote_message *rmsg = msg;
|
const struct remote_message *rmsg = msg;
|
||||||
if (rmsg->destination.handle == 0) {
|
if (rmsg->destination.handle == 0) {
|
||||||
if (_remote_send_name(h, source , rmsg->destination.name, type, session, rmsg->message, rmsg->sz)) {
|
if (remote_send_name(h, source , rmsg->destination.name, type, session, rmsg->message, rmsg->sz)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (_remote_send_handle(h, source , rmsg->destination.handle, type, session, rmsg->message, rmsg->sz)) {
|
if (remote_send_handle(h, source , rmsg->destination.handle, type, session, rmsg->message, rmsg->sz)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -558,46 +682,19 @@ _mainloop(struct skynet_context * context, void * ud, int type, int session, uin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
_launch_gate(struct skynet_context * ctx, const char * local_addr) {
|
|
||||||
char tmp[128];
|
|
||||||
sprintf(tmp,"gate L ! %s %d %d 0",local_addr, PTYPE_HARBOR, REMOTE_MAX);
|
|
||||||
const char * gate_addr = skynet_command(ctx, "LAUNCH", tmp);
|
|
||||||
if (gate_addr == NULL) {
|
|
||||||
fprintf(stderr, "Harbor : launch gate failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
uint32_t gate = strtoul(gate_addr+1 , NULL, 16);
|
|
||||||
if (gate == 0) {
|
|
||||||
fprintf(stderr, "Harbor : launch gate invalid %s", gate_addr);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
const char * self_addr = skynet_command(ctx, "REG", NULL);
|
|
||||||
int n = sprintf(tmp,"broker %s",self_addr);
|
|
||||||
skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, tmp, n);
|
|
||||||
skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, "start", 5);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) {
|
harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) {
|
||||||
h->ctx = ctx;
|
h->ctx = ctx;
|
||||||
int sz = strlen(args)+1;
|
|
||||||
char master_addr[sz];
|
|
||||||
char local_addr[sz];
|
|
||||||
int harbor_id = 0;
|
int harbor_id = 0;
|
||||||
sscanf(args,"%s %s %d",master_addr, local_addr, &harbor_id);
|
uint32_t slave = 0;
|
||||||
h->master_addr = skynet_strdup(master_addr);
|
sscanf(args,"%d %u", &harbor_id, &slave);
|
||||||
h->id = harbor_id;
|
if (slave == 0) {
|
||||||
h->master_fd = _connect_to(h, master_addr, true);
|
return 1;
|
||||||
if (h->master_fd == -1) {
|
|
||||||
fprintf(stderr, "Harbor: Connect to master failed\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
h->local_addr = skynet_strdup(local_addr);
|
h->id = harbor_id;
|
||||||
|
h->slave = slave;
|
||||||
_launch_gate(ctx, local_addr);
|
skynet_callback(ctx, h, mainloop);
|
||||||
skynet_callback(ctx, h, _mainloop);
|
skynet_harbor_start(ctx);
|
||||||
_request_master(h, local_addr, strlen(local_addr), harbor_id);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ logger_release(struct logger * inst) {
|
|||||||
static int
|
static int
|
||||||
_logger(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
_logger(struct skynet_context * context, void *ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
||||||
struct logger * inst = ud;
|
struct logger * inst = ud;
|
||||||
fprintf(inst->handle, "[:%x] ",source);
|
fprintf(inst->handle, "[:%08x] ",source);
|
||||||
fwrite(msg, sz , 1, inst->handle);
|
fwrite(msg, sz , 1, inst->handle);
|
||||||
fprintf(inst->handle, "\n");
|
fprintf(inst->handle, "\n");
|
||||||
fflush(inst->handle);
|
fflush(inst->handle);
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
#ifndef SKYNET_SERVICE_LUA_H
|
|
||||||
#define SKYNET_SERVICE_LUA_H
|
|
||||||
|
|
||||||
struct snlua {
|
|
||||||
lua_State * L;
|
|
||||||
struct skynet_context * ctx;
|
|
||||||
int (*init)(struct snlua *l, struct skynet_context *ctx, const char * args);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,313 +0,0 @@
|
|||||||
#include "skynet.h"
|
|
||||||
#include "skynet_harbor.h"
|
|
||||||
#include "skynet_socket.h"
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <assert.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
#define HASH_SIZE 4096
|
|
||||||
|
|
||||||
struct name {
|
|
||||||
struct name * next;
|
|
||||||
char key[GLOBALNAME_LENGTH];
|
|
||||||
uint32_t hash;
|
|
||||||
uint32_t value;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct namemap {
|
|
||||||
struct name *node[HASH_SIZE];
|
|
||||||
};
|
|
||||||
|
|
||||||
struct master {
|
|
||||||
struct skynet_context *ctx;
|
|
||||||
int remote_fd[REMOTE_MAX];
|
|
||||||
bool connected[REMOTE_MAX];
|
|
||||||
char * remote_addr[REMOTE_MAX];
|
|
||||||
struct namemap map;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct master *
|
|
||||||
master_create() {
|
|
||||||
struct master *m = skynet_malloc(sizeof(*m));
|
|
||||||
int i;
|
|
||||||
for (i=0;i<REMOTE_MAX;i++) {
|
|
||||||
m->remote_fd[i] = -1;
|
|
||||||
m->remote_addr[i] = NULL;
|
|
||||||
m->connected[i] = false;
|
|
||||||
}
|
|
||||||
memset(&m->map, 0, sizeof(m->map));
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
master_release(struct master * m) {
|
|
||||||
int i;
|
|
||||||
struct skynet_context *ctx = m->ctx;
|
|
||||||
for (i=0;i<REMOTE_MAX;i++) {
|
|
||||||
int fd = m->remote_fd[i];
|
|
||||||
if (fd >= 0) {
|
|
||||||
assert(ctx);
|
|
||||||
skynet_socket_close(ctx, fd);
|
|
||||||
}
|
|
||||||
skynet_free(m->remote_addr[i]);
|
|
||||||
}
|
|
||||||
for (i=0;i<HASH_SIZE;i++) {
|
|
||||||
struct name * node = m->map.node[i];
|
|
||||||
while (node) {
|
|
||||||
struct name * next = node->next;
|
|
||||||
skynet_free(node);
|
|
||||||
node = next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
skynet_free(m);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct name *
|
|
||||||
_search_name(struct master *m, char name[GLOBALNAME_LENGTH]) {
|
|
||||||
uint32_t *ptr = (uint32_t *) name;
|
|
||||||
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
|
||||||
struct name * node = m->map.node[h % HASH_SIZE];
|
|
||||||
while (node) {
|
|
||||||
if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) {
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
node = node->next;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct name *
|
|
||||||
_insert_name(struct master *m, char name[GLOBALNAME_LENGTH]) {
|
|
||||||
uint32_t *ptr = (uint32_t *)name;
|
|
||||||
uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
|
|
||||||
struct name **pname = &m->map.node[h % HASH_SIZE];
|
|
||||||
struct name * node = skynet_malloc(sizeof(*node));
|
|
||||||
memcpy(node->key, name, GLOBALNAME_LENGTH);
|
|
||||||
node->next = *pname;
|
|
||||||
node->hash = h;
|
|
||||||
node->value = 0;
|
|
||||||
*pname = node;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
_copy_name(char *name, const char * buffer, size_t sz) {
|
|
||||||
if (sz < GLOBALNAME_LENGTH) {
|
|
||||||
memcpy(name, buffer, sz);
|
|
||||||
memset(name+sz, 0 , GLOBALNAME_LENGTH - sz);
|
|
||||||
} else {
|
|
||||||
memcpy(name, buffer, GLOBALNAME_LENGTH);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
_connect_to(struct master *m, int id) {
|
|
||||||
assert(m->connected[id] == false);
|
|
||||||
struct skynet_context * ctx = m->ctx;
|
|
||||||
const char *ipaddress = m->remote_addr[id];
|
|
||||||
char * portstr = strchr(ipaddress,':');
|
|
||||||
if (portstr==NULL) {
|
|
||||||
skynet_error(ctx, "Harbor %d : address invalid (%s)",id, ipaddress);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int sz = portstr - ipaddress;
|
|
||||||
char tmp[sz + 1];
|
|
||||||
memcpy(tmp,ipaddress,sz);
|
|
||||||
tmp[sz] = '\0';
|
|
||||||
int port = strtol(portstr+1,NULL,10);
|
|
||||||
skynet_error(ctx, "Master connect to harbor(%d) %s:%d", id, tmp, port);
|
|
||||||
m->remote_fd[id] = skynet_socket_connect(ctx, tmp, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void
|
|
||||||
to_bigendian(uint8_t *buffer, uint32_t n) {
|
|
||||||
buffer[0] = (n >> 24) & 0xff;
|
|
||||||
buffer[1] = (n >> 16) & 0xff;
|
|
||||||
buffer[2] = (n >> 8) & 0xff;
|
|
||||||
buffer[3] = n & 0xff;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
_send_to(struct master *m, int id, const void * buf, int sz, uint32_t handle) {
|
|
||||||
uint8_t * buffer= (uint8_t *)skynet_malloc(4 + sz + 12);
|
|
||||||
to_bigendian(buffer, sz+12);
|
|
||||||
memcpy(buffer+4, buf, sz);
|
|
||||||
to_bigendian(buffer+4+sz, 0);
|
|
||||||
to_bigendian(buffer+4+sz+4, handle);
|
|
||||||
to_bigendian(buffer+4+sz+8, 0);
|
|
||||||
|
|
||||||
sz += 4 + 12;
|
|
||||||
|
|
||||||
if (skynet_socket_send(m->ctx, m->remote_fd[id], buffer, sz)) {
|
|
||||||
skynet_error(m->ctx, "Harbor %d : send error", id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
_broadcast(struct master *m, const char *name, size_t sz, uint32_t handle) {
|
|
||||||
int i;
|
|
||||||
for (i=1;i<REMOTE_MAX;i++) {
|
|
||||||
int fd = m->remote_fd[i];
|
|
||||||
if (fd < 0 || m->connected[i]==false)
|
|
||||||
continue;
|
|
||||||
_send_to(m, i , name, sz, handle);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
_request_name(struct master *m, const char * buffer, size_t sz) {
|
|
||||||
char name[GLOBALNAME_LENGTH];
|
|
||||||
_copy_name(name, buffer, sz);
|
|
||||||
struct name * n = _search_name(m, name);
|
|
||||||
if (n == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_broadcast(m, name, GLOBALNAME_LENGTH, n->value);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
_update_name(struct master *m, uint32_t handle, const char * buffer, size_t sz) {
|
|
||||||
char name[GLOBALNAME_LENGTH];
|
|
||||||
_copy_name(name, buffer, sz);
|
|
||||||
struct name * n = _search_name(m, name);
|
|
||||||
if (n==NULL) {
|
|
||||||
n = _insert_name(m,name);
|
|
||||||
}
|
|
||||||
n->value = handle;
|
|
||||||
_broadcast(m,name,GLOBALNAME_LENGTH, handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
close_harbor(struct master *m, int harbor_id) {
|
|
||||||
if (m->connected[harbor_id]) {
|
|
||||||
struct skynet_context * context = m->ctx;
|
|
||||||
skynet_socket_close(context, m->remote_fd[harbor_id]);
|
|
||||||
m->remote_fd[harbor_id] = -1;
|
|
||||||
m->connected[harbor_id] = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
_update_address(struct master *m, int harbor_id, const char * buffer, size_t sz) {
|
|
||||||
if (m->remote_fd[harbor_id] >= 0) {
|
|
||||||
close_harbor(m, harbor_id);
|
|
||||||
}
|
|
||||||
skynet_free(m->remote_addr[harbor_id]);
|
|
||||||
char * addr = skynet_malloc(sz+1);
|
|
||||||
memcpy(addr, buffer, sz);
|
|
||||||
addr[sz] = '\0';
|
|
||||||
m->remote_addr[harbor_id] = addr;
|
|
||||||
_connect_to(m, harbor_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
socket_id(struct master *m, int id) {
|
|
||||||
int i;
|
|
||||||
for (i=1;i<REMOTE_MAX;i++) {
|
|
||||||
if (m->remote_fd[i] == id)
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
on_connected(struct master *m, int id) {
|
|
||||||
_broadcast(m, m->remote_addr[id], strlen(m->remote_addr[id]), id);
|
|
||||||
m->connected[id] = true;
|
|
||||||
int i;
|
|
||||||
for (i=1;i<REMOTE_MAX;i++) {
|
|
||||||
if (i == id)
|
|
||||||
continue;
|
|
||||||
const char * addr = m->remote_addr[i];
|
|
||||||
if (addr == NULL || m->connected[i] == false) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
_send_to(m, id , addr, strlen(addr), i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
dispatch_socket(struct master *m, const struct skynet_socket_message *msg, int sz) {
|
|
||||||
int id = socket_id(m, msg->id);
|
|
||||||
switch(msg->type) {
|
|
||||||
case SKYNET_SOCKET_TYPE_CONNECT:
|
|
||||||
assert(id);
|
|
||||||
on_connected(m, id);
|
|
||||||
break;
|
|
||||||
case SKYNET_SOCKET_TYPE_ERROR:
|
|
||||||
skynet_error(m->ctx, "socket error on harbor %d", id);
|
|
||||||
// go though, close socket
|
|
||||||
case SKYNET_SOCKET_TYPE_CLOSE:
|
|
||||||
close_harbor(m, id);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
skynet_error(m->ctx, "Invalid socket message type %d", msg->type);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
update global name to master
|
|
||||||
|
|
||||||
4 bytes (handle) (handle == 0 for request)
|
|
||||||
n bytes string (name)
|
|
||||||
*/
|
|
||||||
|
|
||||||
static int
|
|
||||||
_mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
|
|
||||||
if (type == PTYPE_SOCKET) {
|
|
||||||
dispatch_socket(ud, msg, (int)sz);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (type != PTYPE_HARBOR) {
|
|
||||||
skynet_error(context, "None harbor message recv from %x (type = %d)", source, type);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
assert(sz >= 4);
|
|
||||||
struct master *m = ud;
|
|
||||||
const uint8_t *handlen = msg;
|
|
||||||
uint32_t handle = handlen[0]<<24 | handlen[1]<<16 | handlen[2]<<8 | handlen[3];
|
|
||||||
sz -= 4;
|
|
||||||
const char * name = msg;
|
|
||||||
name += 4;
|
|
||||||
|
|
||||||
if (handle == 0) {
|
|
||||||
_request_name(m , name, sz);
|
|
||||||
} else if (handle < REMOTE_MAX) {
|
|
||||||
_update_address(m , handle, name, sz);
|
|
||||||
} else {
|
|
||||||
_update_name(m , handle, name, sz);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
master_init(struct master *m, struct skynet_context *ctx, const char * args) {
|
|
||||||
char tmp[strlen(args) + 32];
|
|
||||||
sprintf(tmp,"gate L ! %s %d %d 0",args,PTYPE_HARBOR,REMOTE_MAX);
|
|
||||||
const char * gate_addr = skynet_command(ctx, "LAUNCH", tmp);
|
|
||||||
if (gate_addr == NULL) {
|
|
||||||
skynet_error(ctx, "Master : launch gate failed");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
uint32_t gate = strtoul(gate_addr+1, NULL, 16);
|
|
||||||
if (gate == 0) {
|
|
||||||
skynet_error(ctx, "Master : launch gate invalid %s", gate_addr);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
const char * self_addr = skynet_command(ctx, "REG", NULL);
|
|
||||||
int n = sprintf(tmp,"broker %s",self_addr);
|
|
||||||
skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, tmp, n);
|
|
||||||
skynet_send(ctx, 0, gate, PTYPE_TEXT, 0, "start", 5);
|
|
||||||
|
|
||||||
skynet_callback(ctx, m, _mainloop);
|
|
||||||
|
|
||||||
m->ctx = ctx;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -4,13 +4,17 @@
|
|||||||
#include <lualib.h>
|
#include <lualib.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include "service_lua.h"
|
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct snlua {
|
||||||
|
lua_State * L;
|
||||||
|
struct skynet_context * ctx;
|
||||||
|
};
|
||||||
|
|
||||||
// LUA_CACHELIB may defined in patched lua for shared proto
|
// LUA_CACHELIB may defined in patched lua for shared proto
|
||||||
#ifdef LUA_CACHELIB
|
#ifdef LUA_CACHELIB
|
||||||
|
|
||||||
@@ -37,114 +41,6 @@ codecache(lua_State *L) {
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// time
|
|
||||||
|
|
||||||
#include <time.h>
|
|
||||||
#define NANOSEC 1000000000
|
|
||||||
|
|
||||||
#if defined(__APPLE__)
|
|
||||||
#include <mach/task.h>
|
|
||||||
#include <mach/mach.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static void
|
|
||||||
current_time(struct timespec *ti) {
|
|
||||||
#if !defined(__APPLE__)
|
|
||||||
clock_gettime(CLOCK_THREAD_CPUTIME_ID, ti);
|
|
||||||
#else
|
|
||||||
struct task_thread_times_info aTaskInfo;
|
|
||||||
mach_msg_type_number_t aTaskInfoCount = TASK_THREAD_TIMES_INFO_COUNT;
|
|
||||||
assert(KERN_SUCCESS == task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t )&aTaskInfo, &aTaskInfoCount));
|
|
||||||
ti->tv_sec = aTaskInfo.user_time.seconds;
|
|
||||||
ti->tv_nsec = aTaskInfo.user_time.microseconds * 1000;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static double
|
|
||||||
diff_time(struct timespec *ti) {
|
|
||||||
struct timespec end;
|
|
||||||
current_time(&end);
|
|
||||||
int diffsec = end.tv_sec - ti->tv_sec;
|
|
||||||
int diffnsec = end.tv_nsec - ti->tv_nsec;
|
|
||||||
if (diffnsec < 0) {
|
|
||||||
--diffsec;
|
|
||||||
diffnsec += NANOSEC;
|
|
||||||
}
|
|
||||||
return (double)diffsec + (double)diffnsec / NANOSEC;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
_try_load(lua_State *L, const char * path, int pathlen, const char * name) {
|
|
||||||
int namelen = strlen(name);
|
|
||||||
char tmp[pathlen + namelen];
|
|
||||||
int i;
|
|
||||||
for (i=0;i<pathlen;i++) {
|
|
||||||
if (path[i] == '?')
|
|
||||||
break;
|
|
||||||
tmp[i] = path[i];
|
|
||||||
}
|
|
||||||
if (path[i] == '?') {
|
|
||||||
memcpy(tmp+i,name,namelen);
|
|
||||||
memcpy(tmp+i+namelen,path+i+1,pathlen - i -1);
|
|
||||||
} else {
|
|
||||||
fprintf(stderr,"snlua : Invalid lua service path\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
tmp[namelen+pathlen-1] = '\0';
|
|
||||||
int r = luaL_loadfile(L,tmp);
|
|
||||||
if (r == LUA_OK) {
|
|
||||||
int i;
|
|
||||||
for (i=namelen+pathlen-2;i>=0;i--) {
|
|
||||||
if (tmp[i] == '/') {
|
|
||||||
lua_pushlstring(L,tmp,i+1);
|
|
||||||
lua_setglobal(L,"SERVICE_PATH");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (i<0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
lua_getglobal(L,"package");
|
|
||||||
lua_getfield(L,-1,"path");
|
|
||||||
luaL_Buffer b;
|
|
||||||
luaL_buffinit(L, &b);
|
|
||||||
luaL_addlstring(&b, tmp, i+1);
|
|
||||||
luaL_addstring(&b, "?.lua;");
|
|
||||||
luaL_addvalue(&b);
|
|
||||||
luaL_pushresult(&b);
|
|
||||||
lua_setfield(L,-2,"path");
|
|
||||||
lua_pop(L,1);
|
|
||||||
return 0;
|
|
||||||
} else if (r == LUA_ERRFILE) {
|
|
||||||
lua_pop(L,1);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
_load(lua_State *L, char ** filename) {
|
|
||||||
const char * name = strsep(filename, " \r\n");
|
|
||||||
const char * path = skynet_command(NULL, "GETENV", "luaservice");
|
|
||||||
while (path[0]) {
|
|
||||||
int pathlen;
|
|
||||||
char * pathend = strchr(path,';');
|
|
||||||
if (pathend) {
|
|
||||||
pathlen = pathend - path;
|
|
||||||
} else {
|
|
||||||
pathlen = strlen(path);
|
|
||||||
}
|
|
||||||
int r = _try_load(L, path, pathlen, name);
|
|
||||||
if (r >=0) {
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
path+=pathlen;
|
|
||||||
if (path[0]==';')
|
|
||||||
++path;
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
traceback (lua_State *L) {
|
traceback (lua_State *L) {
|
||||||
const char *msg = lua_tostring(L, 1);
|
const char *msg = lua_tostring(L, 1);
|
||||||
@@ -156,80 +52,70 @@ traceback (lua_State *L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
_report_error(lua_State *L, struct skynet_context *ctx, const char *filename, int err) {
|
|
||||||
switch (err) {
|
|
||||||
case LUA_ERRRUN:
|
|
||||||
skynet_error(ctx, "lua do [%s] error : %s", filename, lua_tostring(L,-1));
|
|
||||||
break;
|
|
||||||
case LUA_ERRMEM:
|
|
||||||
skynet_error(ctx, "lua memory error : %s",filename);
|
|
||||||
break;
|
|
||||||
case LUA_ERRERR:
|
|
||||||
skynet_error(ctx, "lua message error : %s",filename);
|
|
||||||
break;
|
|
||||||
case LUA_ERRGCMM:
|
|
||||||
skynet_error(ctx, "lua gc error : %s",filename);
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
lua_pop(L,1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_report_launcher_error(struct skynet_context *ctx) {
|
_report_launcher_error(struct skynet_context *ctx) {
|
||||||
// sizeof "ERROR" == 5
|
// sizeof "ERROR" == 5
|
||||||
skynet_sendname(ctx, ".launcher", PTYPE_TEXT, 0, "ERROR", 5);
|
skynet_sendname(ctx, ".launcher", PTYPE_TEXT, 0, "ERROR", 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
optstring(struct skynet_context *ctx, const char *key, const char * str) {
|
||||||
|
const char * ret = skynet_command(ctx, "GETENV", key);
|
||||||
|
if (ret == NULL) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_init(struct snlua *l, struct skynet_context *ctx, const char * args) {
|
_init(struct snlua *l, struct skynet_context *ctx, const char * args, size_t sz) {
|
||||||
lua_State *L = l->L;
|
lua_State *L = l->L;
|
||||||
l->ctx = ctx;
|
l->ctx = ctx;
|
||||||
lua_gc(L, LUA_GCSTOP, 0);
|
lua_gc(L, LUA_GCSTOP, 0);
|
||||||
|
lua_pushboolean(L, 1); /* signal for libraries to ignore env. vars. */
|
||||||
|
lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
|
||||||
luaL_openlibs(L);
|
luaL_openlibs(L);
|
||||||
lua_pushlightuserdata(L, l);
|
lua_pushlightuserdata(L, ctx);
|
||||||
lua_setfield(L, LUA_REGISTRYINDEX, "skynet_lua");
|
lua_setfield(L, LUA_REGISTRYINDEX, "skynet_context");
|
||||||
luaL_requiref(L, "skynet.codecache", codecache , 0);
|
luaL_requiref(L, "skynet.codecache", codecache , 0);
|
||||||
lua_pop(L,1);
|
lua_pop(L,1);
|
||||||
lua_gc(L, LUA_GCRESTART, 0);
|
|
||||||
|
|
||||||
char tmp[strlen(args)+1];
|
const char *path = optstring(ctx, "lua_path","./lualib/?.lua;./lualib/?/init.lua");
|
||||||
char *parm = tmp;
|
lua_pushstring(L, path);
|
||||||
strcpy(parm,args);
|
lua_setglobal(L, "LUA_PATH");
|
||||||
|
const char *cpath = optstring(ctx, "lua_cpath","./luaclib/?.so");
|
||||||
|
lua_pushstring(L, cpath);
|
||||||
|
lua_setglobal(L, "LUA_CPATH");
|
||||||
|
const char *service = optstring(ctx, "luaservice", "./service/?.lua");
|
||||||
|
lua_pushstring(L, service);
|
||||||
|
lua_setglobal(L, "LUA_SERVICE");
|
||||||
|
const char *preload = skynet_command(ctx, "GETENV", "preload");
|
||||||
|
lua_pushstring(L, preload);
|
||||||
|
lua_setglobal(L, "LUA_PRELOAD");
|
||||||
|
|
||||||
lua_pushcfunction(L, traceback);
|
lua_pushcfunction(L, traceback);
|
||||||
int traceback_index = lua_gettop(L);
|
assert(lua_gettop(L) == 1);
|
||||||
assert(traceback_index == 1);
|
|
||||||
|
|
||||||
const char * filename = parm;
|
const char * loader = optstring(ctx, "lualoader", "./lualib/loader.lua");
|
||||||
int r = _load(L, &parm);
|
|
||||||
if (r != 0) {
|
int r = luaL_loadfile(L,loader);
|
||||||
if (r<0) {
|
if (r != LUA_OK) {
|
||||||
skynet_error(ctx, "lua parser [%s] load error", filename);
|
skynet_error(ctx, "Can't load %s : %s", loader, lua_tostring(L, -1));
|
||||||
} else {
|
|
||||||
skynet_error(ctx, "lua parser [%s] error : %s", filename, lua_tostring(L,-1));
|
|
||||||
}
|
|
||||||
_report_launcher_error(ctx);
|
_report_launcher_error(ctx);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
int n=0;
|
lua_pushlstring(L, args, sz);
|
||||||
while(parm) {
|
r = lua_pcall(L,1,0,1);
|
||||||
const char * arg = strsep(&parm, " \r\n");
|
if (r != LUA_OK) {
|
||||||
if (arg && arg[0]!='\0') {
|
skynet_error(ctx, "lua loader error : %s", lua_tostring(L, -1));
|
||||||
lua_pushstring(L, arg);
|
_report_launcher_error(ctx);
|
||||||
++n;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
lua_settop(L,0);
|
||||||
r = lua_pcall(L,n,0,traceback_index);
|
|
||||||
if (r == LUA_OK) {
|
lua_gc(L, LUA_GCRESTART, 0);
|
||||||
r = lua_gc(L, LUA_GCCOLLECT, 0);
|
|
||||||
if (r == LUA_OK) {
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
}
|
|
||||||
_report_error(L, ctx, filename, r);
|
|
||||||
_report_launcher_error(ctx);
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
@@ -237,12 +123,7 @@ _launch(struct skynet_context * context, void *ud, int type, int session, uint32
|
|||||||
assert(type == 0 && session == 0);
|
assert(type == 0 && session == 0);
|
||||||
struct snlua *l = ud;
|
struct snlua *l = ud;
|
||||||
skynet_callback(context, NULL, NULL);
|
skynet_callback(context, NULL, NULL);
|
||||||
struct timespec ti;
|
int err = _init(l, context, msg, sz);
|
||||||
current_time(&ti);
|
|
||||||
int err = _init(l, context, msg);
|
|
||||||
double t = diff_time(&ti);
|
|
||||||
lua_pushnumber(l->L, t);
|
|
||||||
lua_setfield(l->L, LUA_REGISTRYINDEX, "skynet_boottime");
|
|
||||||
if (err) {
|
if (err) {
|
||||||
skynet_command(context, "EXIT", NULL);
|
skynet_command(context, "EXIT", NULL);
|
||||||
}
|
}
|
||||||
@@ -253,13 +134,13 @@ _launch(struct skynet_context * context, void *ud, int type, int session, uint32
|
|||||||
int
|
int
|
||||||
snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) {
|
snlua_init(struct snlua *l, struct skynet_context *ctx, const char * args) {
|
||||||
int sz = strlen(args);
|
int sz = strlen(args);
|
||||||
char * tmp = skynet_malloc(sz+1);
|
char * tmp = skynet_malloc(sz);
|
||||||
memcpy(tmp, args, sz+1);
|
memcpy(tmp, args, sz);
|
||||||
skynet_callback(ctx, l , _launch);
|
skynet_callback(ctx, l , _launch);
|
||||||
const char * self = skynet_command(ctx, "REG", NULL);
|
const char * self = skynet_command(ctx, "REG", NULL);
|
||||||
uint32_t handle_id = strtoul(self+1, NULL, 16);
|
uint32_t handle_id = strtoul(self+1, NULL, 16);
|
||||||
// it must be first message
|
// it must be first message
|
||||||
skynet_send(ctx, 0, handle_id, PTYPE_TAG_DONTCOPY,0, tmp, sz+1);
|
skynet_send(ctx, 0, handle_id, PTYPE_TAG_DONTCOPY,0, tmp, sz);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,7 +149,6 @@ snlua_create(void) {
|
|||||||
struct snlua * l = skynet_malloc(sizeof(*l));
|
struct snlua * l = skynet_malloc(sizeof(*l));
|
||||||
memset(l,0,sizeof(*l));
|
memset(l,0,sizeof(*l));
|
||||||
l->L = lua_newstate(skynet_lalloc, NULL);
|
l->L = lua_newstate(skynet_lalloc, NULL);
|
||||||
l->init = _init;
|
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
43
service/bootstrap.lua
Normal file
43
service/bootstrap.lua
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local harbor = require "skynet.harbor"
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
local standalone = skynet.getenv "standalone"
|
||||||
|
|
||||||
|
local launcher = assert(skynet.launch("snlua","launcher"))
|
||||||
|
skynet.name(".launcher", launcher)
|
||||||
|
|
||||||
|
local harbor_id = tonumber(skynet.getenv "harbor")
|
||||||
|
if harbor_id == 0 then
|
||||||
|
assert(standalone == nil)
|
||||||
|
standalone = true
|
||||||
|
skynet.setenv("standalone", "true")
|
||||||
|
|
||||||
|
local slave = skynet.newservice "cdummy"
|
||||||
|
if slave == nil then
|
||||||
|
skynet.abort()
|
||||||
|
end
|
||||||
|
skynet.name(".slave", slave)
|
||||||
|
|
||||||
|
else
|
||||||
|
if standalone then
|
||||||
|
if not skynet.newservice "cmaster" then
|
||||||
|
skynet.abort()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local slave = skynet.newservice "cslave"
|
||||||
|
if slave == nil then
|
||||||
|
skynet.abort()
|
||||||
|
end
|
||||||
|
skynet.name(".slave", slave)
|
||||||
|
end
|
||||||
|
|
||||||
|
if standalone then
|
||||||
|
local datacenter = assert(skynet.newservice "datacenterd")
|
||||||
|
skynet.name("DATACENTER", datacenter)
|
||||||
|
end
|
||||||
|
assert(skynet.newservice "service_mgr")
|
||||||
|
assert(skynet.newservice(skynet.getenv "start" or "main"))
|
||||||
|
skynet.exit()
|
||||||
|
end)
|
||||||
47
service/cdummy.lua
Normal file
47
service/cdummy.lua
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
local globalname = {}
|
||||||
|
local harbor = {}
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "harbor",
|
||||||
|
id = skynet.PTYPE_HARBOR,
|
||||||
|
pack = function(...) return ... end,
|
||||||
|
unpack = skynet.tostring,
|
||||||
|
}
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "text",
|
||||||
|
id = skynet.PTYPE_TEXT,
|
||||||
|
pack = function(...) return ... end,
|
||||||
|
unpack = skynet.tostring,
|
||||||
|
}
|
||||||
|
|
||||||
|
function harbor.REGISTER(name, handle)
|
||||||
|
assert(globalname[name] == nil)
|
||||||
|
globalname[name] = handle
|
||||||
|
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
|
||||||
|
end
|
||||||
|
|
||||||
|
function harbor.LINK(id)
|
||||||
|
skynet.ret()
|
||||||
|
end
|
||||||
|
|
||||||
|
function harbor.CONNECT(id)
|
||||||
|
skynet.error("Can't connect to other harbor in single node mode")
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
local harbor_id = tonumber(skynet.getenv "harbor")
|
||||||
|
assert(harbor_id == 0)
|
||||||
|
|
||||||
|
skynet.dispatch("lua", function (session,source,command,...)
|
||||||
|
local f = assert(harbor[command])
|
||||||
|
f(...)
|
||||||
|
end)
|
||||||
|
skynet.dispatch("text", function(session,source,command)
|
||||||
|
-- ignore all the command
|
||||||
|
end)
|
||||||
|
|
||||||
|
harbor_service = assert(skynet.launch("harbor", harbor_id, skynet.self()))
|
||||||
|
end)
|
||||||
90
service/clusterd.lua
Normal file
90
service/clusterd.lua
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local sc = require "socketchannel"
|
||||||
|
local socket = require "socket"
|
||||||
|
local cluster = require "cluster.c"
|
||||||
|
|
||||||
|
local config_name = skynet.getenv "cluster"
|
||||||
|
local node_address = {}
|
||||||
|
|
||||||
|
local function loadconfig()
|
||||||
|
local f = assert(io.open(config_name))
|
||||||
|
local source = f:read "*a"
|
||||||
|
f:close()
|
||||||
|
assert(load(source, "@"..config_name, "t", node_address))()
|
||||||
|
end
|
||||||
|
|
||||||
|
local node_session = {}
|
||||||
|
local command = {}
|
||||||
|
|
||||||
|
local function read_response(sock)
|
||||||
|
local sz = socket.header(sock:read(2))
|
||||||
|
local msg = sock:read(sz)
|
||||||
|
return cluster.unpackresponse(msg) -- session, ok, data
|
||||||
|
end
|
||||||
|
|
||||||
|
local function open_channel(t, key)
|
||||||
|
local host, port = string.match(node_address[key], "([^:]+):(.*)$")
|
||||||
|
local c = sc.channel {
|
||||||
|
host = host,
|
||||||
|
port = tonumber(port),
|
||||||
|
response = read_response,
|
||||||
|
}
|
||||||
|
assert(c:connect(true))
|
||||||
|
t[key] = c
|
||||||
|
node_session[key] = 1
|
||||||
|
return c
|
||||||
|
end
|
||||||
|
|
||||||
|
local node_channel = setmetatable({}, { __index = open_channel })
|
||||||
|
|
||||||
|
function command.reload()
|
||||||
|
loadconfig()
|
||||||
|
skynet.ret(skynet.pack(nil))
|
||||||
|
end
|
||||||
|
|
||||||
|
function command.listen(source, addr, port)
|
||||||
|
local gate = skynet.newservice("gate")
|
||||||
|
if port == nil then
|
||||||
|
addr, port = string.match(node_address[addr], "([^:]+):(.*)$")
|
||||||
|
end
|
||||||
|
skynet.call(gate, "lua", "open", { address = addr, port = port })
|
||||||
|
skynet.ret(skynet.pack(nil))
|
||||||
|
end
|
||||||
|
|
||||||
|
function command.req(source, node, addr, msg, sz)
|
||||||
|
local request
|
||||||
|
local c = node_channel[node]
|
||||||
|
local session = node_session[node]
|
||||||
|
-- msg is a local pointer, cluster.packrequest will free it
|
||||||
|
request, node_session[node] = cluster.packrequest(addr, session , msg, sz)
|
||||||
|
skynet.ret(c:request(request, session))
|
||||||
|
end
|
||||||
|
|
||||||
|
local request_fd = {}
|
||||||
|
|
||||||
|
function command.socket(source, subcmd, fd, msg)
|
||||||
|
if subcmd == "data" then
|
||||||
|
local addr, session, msg = cluster.unpackrequest(msg)
|
||||||
|
local ok , msg, sz = pcall(skynet.rawcall, addr, "lua", msg)
|
||||||
|
local response
|
||||||
|
if ok then
|
||||||
|
response = cluster.packresponse(session, true, msg, sz)
|
||||||
|
else
|
||||||
|
response = cluster.packresponse(session, false, msg)
|
||||||
|
end
|
||||||
|
socket.write(fd, response)
|
||||||
|
elseif subcmd == "open" then
|
||||||
|
skynet.error(string.format("socket accept from %s", msg))
|
||||||
|
skynet.call(source, "lua", "accept", fd)
|
||||||
|
else
|
||||||
|
skynet.error(string.format("socket %s %d : %s", subcmd, fd, msg))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
loadconfig()
|
||||||
|
skynet.dispatch("lua", function(session , source, cmd, ...)
|
||||||
|
local f = assert(command[cmd])
|
||||||
|
f(source, ...)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
125
service/cmaster.lua
Normal file
125
service/cmaster.lua
Normal file
@@ -0,0 +1,125 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local socket = require "socket"
|
||||||
|
|
||||||
|
--[[
|
||||||
|
master manage data :
|
||||||
|
1. all the slaves address : id -> ipaddr:port
|
||||||
|
2. all the global names : name -> address
|
||||||
|
|
||||||
|
master hold connections from slaves .
|
||||||
|
|
||||||
|
protocol slave->master :
|
||||||
|
package size 1 byte
|
||||||
|
type 1 byte :
|
||||||
|
'H' : HANDSHAKE, report slave id, and address.
|
||||||
|
'R' : REGISTER name address
|
||||||
|
'Q' : QUERY name
|
||||||
|
|
||||||
|
|
||||||
|
protocol master->slave:
|
||||||
|
package size 1 byte
|
||||||
|
type 1 byte :
|
||||||
|
'W' : WAIT n
|
||||||
|
'C' : CONNECT slave_id slave_address
|
||||||
|
'N' : NAME globalname address
|
||||||
|
'D' : DISCONNECT slave_id
|
||||||
|
]]
|
||||||
|
|
||||||
|
local slave_node = {}
|
||||||
|
local global_name = {}
|
||||||
|
|
||||||
|
local function read_package(fd)
|
||||||
|
local sz = socket.read(fd, 1)
|
||||||
|
assert(sz, "closed")
|
||||||
|
sz = string.byte(sz)
|
||||||
|
local content = socket.read(fd, sz)
|
||||||
|
return skynet.unpack(content)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function pack_package(...)
|
||||||
|
local message = skynet.packstring(...)
|
||||||
|
local size = #message
|
||||||
|
assert(size <= 255 , "too long")
|
||||||
|
return string.char(size) .. message
|
||||||
|
end
|
||||||
|
|
||||||
|
local function report_slave(fd, slave_id, slave_addr)
|
||||||
|
local message = pack_package("C", slave_id, slave_addr)
|
||||||
|
local n = 0
|
||||||
|
for k,v in pairs(slave_node) do
|
||||||
|
if v.fd ~= 0 then
|
||||||
|
socket.write(v.fd, message)
|
||||||
|
n = n + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
socket.write(fd, pack_package("W", n))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function handshake(fd)
|
||||||
|
local t, slave_id, slave_addr = read_package(fd)
|
||||||
|
assert(t=='H', "Invalid handshake type " .. t)
|
||||||
|
assert(slave_id ~= 0 , "Invalid slave id 0")
|
||||||
|
if slave_node[slave_id] then
|
||||||
|
error(string.format("Slave %d already register on %s", slave_id, slave_node[slave_id].addr))
|
||||||
|
end
|
||||||
|
report_slave(fd, slave_id, slave_addr)
|
||||||
|
slave_node[slave_id] = {
|
||||||
|
fd = fd,
|
||||||
|
id = slave_id,
|
||||||
|
addr = slave_addr,
|
||||||
|
}
|
||||||
|
return slave_id , slave_addr
|
||||||
|
end
|
||||||
|
|
||||||
|
local function dispatch_slave(fd)
|
||||||
|
local t, name, address = read_package(fd)
|
||||||
|
if t == 'R' then
|
||||||
|
-- register name
|
||||||
|
assert(type(address)=="number", "Invalid request")
|
||||||
|
if not global_name[name] then
|
||||||
|
global_name[name] = address
|
||||||
|
end
|
||||||
|
local message = pack_package("N", name, address)
|
||||||
|
for k,v in pairs(slave_node) do
|
||||||
|
socket.write(v.fd, message)
|
||||||
|
end
|
||||||
|
elseif t == 'Q' then
|
||||||
|
-- query name
|
||||||
|
local address = global_name[name]
|
||||||
|
if address then
|
||||||
|
socket.write(fd, pack_package("N", name, address))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
skynet.error("Invalid slave message type " .. t)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function monitor_slave(slave_id, slave_address)
|
||||||
|
local fd = slave_node[slave_id].fd
|
||||||
|
skynet.error(string.format("Harbor %d (fd=%d) report %s", slave_id, fd, slave_address))
|
||||||
|
while pcall(dispatch_slave, fd) do end
|
||||||
|
skynet.error("slave " ..slave_id .. " is down")
|
||||||
|
local message = pack_package("D", slave_id)
|
||||||
|
slave_node[slave_id].fd = 0
|
||||||
|
for k,v in pairs(slave_node) do
|
||||||
|
socket.write(v.fd, message)
|
||||||
|
end
|
||||||
|
socket.close(fd)
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
local master_addr = skynet.getenv "standalone"
|
||||||
|
skynet.error("master listen socket " .. tostring(master_addr))
|
||||||
|
local fd = socket.listen(master_addr)
|
||||||
|
socket.start(fd , function(id, addr)
|
||||||
|
skynet.error("connect from " .. addr .. " " .. id)
|
||||||
|
socket.start(id)
|
||||||
|
local ok, slave, slave_addr = pcall(handshake, id)
|
||||||
|
if ok then
|
||||||
|
skynet.fork(monitor_slave, slave, slave_addr)
|
||||||
|
else
|
||||||
|
skynet.error(string.format("disconnect fd = %d, error = %s", id, slave))
|
||||||
|
socket.close(id)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
222
service/cslave.lua
Normal file
222
service/cslave.lua
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local socket = require "socket"
|
||||||
|
|
||||||
|
local slaves = {}
|
||||||
|
local connect_queue = {}
|
||||||
|
local globalname = {}
|
||||||
|
local harbor = {}
|
||||||
|
local harbor_service
|
||||||
|
local monitor = {}
|
||||||
|
|
||||||
|
local function read_package(fd)
|
||||||
|
local sz = socket.read(fd, 1)
|
||||||
|
assert(sz, "closed")
|
||||||
|
sz = string.byte(sz)
|
||||||
|
local content = socket.read(fd, sz)
|
||||||
|
return skynet.unpack(content)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function pack_package(...)
|
||||||
|
local message = skynet.packstring(...)
|
||||||
|
local size = #message
|
||||||
|
assert(size <= 255 , "too long")
|
||||||
|
return string.char(size) .. message
|
||||||
|
end
|
||||||
|
|
||||||
|
local function monitor_clear(id)
|
||||||
|
local v = monitor[id]
|
||||||
|
if v then
|
||||||
|
monitor[id] = nil
|
||||||
|
for _, v in ipairs(v) do
|
||||||
|
skynet.redirect(v.address, 0, "response", v.session, "")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function connect_slave(slave_id, address)
|
||||||
|
local ok, err = pcall(function()
|
||||||
|
if slaves[slave_id] == nil then
|
||||||
|
local fd = assert(socket.open(address), "Can't connect to "..address)
|
||||||
|
skynet.error(string.format("Connect to harbor %d (fd=%d), %s", slave_id, fd, address))
|
||||||
|
slaves[slave_id] = fd
|
||||||
|
monitor_clear(slave_id)
|
||||||
|
socket.abandon(fd)
|
||||||
|
skynet.send(harbor_service, "harbor", string.format("S %d %d",fd,slave_id))
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
if not ok then
|
||||||
|
skynet.error(err)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function ready()
|
||||||
|
local queue = connect_queue
|
||||||
|
connect_queue = nil
|
||||||
|
for k,v in pairs(queue) do
|
||||||
|
connect_slave(k,v)
|
||||||
|
end
|
||||||
|
for name,address in pairs(globalname) do
|
||||||
|
skynet.redirect(harbor_service, address, "harbor", "N " .. name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function monitor_master(master_fd)
|
||||||
|
while true do
|
||||||
|
local ok, t, id_name, address = pcall(read_package,master_fd)
|
||||||
|
if ok then
|
||||||
|
if t == 'C' then
|
||||||
|
if connect_queue then
|
||||||
|
connect_queue[id_name] = address
|
||||||
|
else
|
||||||
|
connect_slave(id_name, address)
|
||||||
|
end
|
||||||
|
elseif t == 'N' then
|
||||||
|
globalname[id_name] = address
|
||||||
|
if connect_queue == nil then
|
||||||
|
skynet.redirect(harbor_service, address, "harbor", 0, "N " .. id_name)
|
||||||
|
end
|
||||||
|
elseif t == 'D' then
|
||||||
|
local fd = slaves[id_name]
|
||||||
|
slaves[id_name] = false
|
||||||
|
if fd then
|
||||||
|
socket.close(fd)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
skynet.error("Master disconnect")
|
||||||
|
socket.close(master_fd)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function accept_slave(fd)
|
||||||
|
socket.start(fd)
|
||||||
|
local id = socket.read(fd, 1)
|
||||||
|
if not id then
|
||||||
|
skynet.error(string.format("Connection (fd =%d) closed", fd))
|
||||||
|
socket.close(fd)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
id = string.byte(id)
|
||||||
|
if slaves[id] ~= nil then
|
||||||
|
skynet.error(string.format("Slave %d exist (fd =%d)", id, fd))
|
||||||
|
socket.close(fd)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
slaves[id] = fd
|
||||||
|
monitor_clear(id)
|
||||||
|
socket.abandon(fd)
|
||||||
|
skynet.error(string.format("Harbor %d connected (fd = %d)", id, fd))
|
||||||
|
skynet.send(harbor_service, "harbor", string.format("A %d %d", fd, id))
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "harbor",
|
||||||
|
id = skynet.PTYPE_HARBOR,
|
||||||
|
pack = function(...) return ... end,
|
||||||
|
unpack = skynet.tostring,
|
||||||
|
}
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "text",
|
||||||
|
id = skynet.PTYPE_TEXT,
|
||||||
|
pack = function(...) return ... end,
|
||||||
|
unpack = skynet.tostring,
|
||||||
|
}
|
||||||
|
|
||||||
|
local function monitor_harbor(master_fd)
|
||||||
|
return function(session, source, command)
|
||||||
|
local t = string.sub(command, 1, 1)
|
||||||
|
local arg = string.sub(command, 3)
|
||||||
|
if t == "Q" then
|
||||||
|
-- query name
|
||||||
|
if globalname[arg] then
|
||||||
|
skynet.redirect(harbor_service, globalname[arg], "harbor", 0, "N " .. arg)
|
||||||
|
else
|
||||||
|
socket.write(master_fd, pack_package("Q", arg))
|
||||||
|
end
|
||||||
|
elseif t == "D" then
|
||||||
|
-- harbor down
|
||||||
|
local id = tonumber(arg)
|
||||||
|
if slaves[id] then
|
||||||
|
monitor_clear(id)
|
||||||
|
end
|
||||||
|
slaves[id] = false
|
||||||
|
else
|
||||||
|
skynet.error("Unknown command ", command)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function harbor.REGISTER(_,_, fd, name, handle)
|
||||||
|
assert(globalname[name] == nil)
|
||||||
|
globalname[name] = handle
|
||||||
|
socket.write(fd, pack_package("R", name, handle))
|
||||||
|
skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
|
||||||
|
end
|
||||||
|
|
||||||
|
function harbor.LINK(session, source, fd, id)
|
||||||
|
if slaves[id] then
|
||||||
|
if monitor[id] == nil then
|
||||||
|
monitor[id] = {}
|
||||||
|
end
|
||||||
|
table.insert(monitor[id], { address = source, session = session })
|
||||||
|
else
|
||||||
|
skynet.ret()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function harbor.CONNECT(session, source, fd, id)
|
||||||
|
if not slaves[id] then
|
||||||
|
if monitor[id] == nil then
|
||||||
|
monitor[id] = {}
|
||||||
|
end
|
||||||
|
table.insert(monitor[id], { address = source, session = session })
|
||||||
|
else
|
||||||
|
skynet.ret()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
local master_addr = skynet.getenv "master"
|
||||||
|
local harbor_id = tonumber(skynet.getenv "harbor")
|
||||||
|
local slave_address = assert(skynet.getenv "address")
|
||||||
|
local slave_fd = socket.listen(slave_address)
|
||||||
|
skynet.error("slave connect to master " .. tostring(master_addr))
|
||||||
|
local master_fd = socket.open(master_addr)
|
||||||
|
|
||||||
|
skynet.dispatch("lua", function (session,source,command,...)
|
||||||
|
local f = assert(harbor[command])
|
||||||
|
f(session, source, master_fd, ...)
|
||||||
|
end)
|
||||||
|
skynet.dispatch("text", monitor_harbor(master_fd))
|
||||||
|
|
||||||
|
harbor_service = assert(skynet.launch("harbor", harbor_id, skynet.self()))
|
||||||
|
|
||||||
|
local hs_message = pack_package("H", harbor_id, slave_address)
|
||||||
|
socket.write(master_fd, hs_message)
|
||||||
|
local t, n = read_package(master_fd)
|
||||||
|
assert(t == "W" and type(n) == "number", "slave shakehand failed")
|
||||||
|
skynet.error(string.format("Waiting for %d harbors", n))
|
||||||
|
skynet.fork(monitor_master, master_fd)
|
||||||
|
if n > 0 then
|
||||||
|
local co = coroutine.running()
|
||||||
|
socket.start(slave_fd, function(fd, addr)
|
||||||
|
skynet.error(string.format("New connection (fd = %d, %s)",fd, addr))
|
||||||
|
if pcall(accept_slave,fd) then
|
||||||
|
local s = 0
|
||||||
|
for k,v in pairs(slaves) do
|
||||||
|
s = s + 1
|
||||||
|
end
|
||||||
|
if s >= n then
|
||||||
|
skynet.wakeup(co)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
skynet.wait()
|
||||||
|
end
|
||||||
|
socket.close(slave_fd)
|
||||||
|
skynet.error("Shakehand ready")
|
||||||
|
skynet.fork(ready)
|
||||||
|
end)
|
||||||
114
service/datacenterd.lua
Normal file
114
service/datacenterd.lua
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
|
||||||
|
local command = {}
|
||||||
|
local database = {}
|
||||||
|
local wait_queue = {}
|
||||||
|
local mode = {}
|
||||||
|
|
||||||
|
local function query(db, key, ...)
|
||||||
|
if key == nil then
|
||||||
|
return db
|
||||||
|
else
|
||||||
|
return query(db[key], ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function command.QUERY(key, ...)
|
||||||
|
local d = database[key]
|
||||||
|
if d then
|
||||||
|
return query(d, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function update(db, key, value, ...)
|
||||||
|
if select("#",...) == 0 then
|
||||||
|
local ret = db[key]
|
||||||
|
db[key] = value
|
||||||
|
return ret, value
|
||||||
|
else
|
||||||
|
if db[key] == nil then
|
||||||
|
db[key] = {}
|
||||||
|
end
|
||||||
|
return update(db[key], value, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function wakeup(db, key1, key2, value, ...)
|
||||||
|
if key1 == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local q = db[key1]
|
||||||
|
if q == nil then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if q[mode] == "queue" then
|
||||||
|
db[key1] = nil
|
||||||
|
if value then
|
||||||
|
-- throw error because can't wake up a branch
|
||||||
|
for _,v in ipairs(q) do
|
||||||
|
local session = v[1]
|
||||||
|
local source = v[2]
|
||||||
|
skynet.redirect(source, 0, "error", session, "")
|
||||||
|
end
|
||||||
|
else
|
||||||
|
return q
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- it's branch
|
||||||
|
return wakeup(q , key2, value, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function command.UPDATE(...)
|
||||||
|
local ret, value = update(database, ...)
|
||||||
|
if ret or value == nil then
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
local q = wakeup(wait_queue, ...)
|
||||||
|
if q then
|
||||||
|
for _, v in ipairs(q) do
|
||||||
|
local session = v[1]
|
||||||
|
local source = v[2]
|
||||||
|
skynet.redirect(source, 0, "response", session, skynet.pack(value))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function waitfor(session, source, db, key1, key2, ...)
|
||||||
|
if key2 == nil then
|
||||||
|
-- push queue
|
||||||
|
local q = db[key1]
|
||||||
|
if q == nil then
|
||||||
|
q = { [mode] = "queue" }
|
||||||
|
db[key1] = q
|
||||||
|
else
|
||||||
|
assert(q[mode] == "queue")
|
||||||
|
end
|
||||||
|
table.insert(q, { session, source })
|
||||||
|
else
|
||||||
|
local q = db[key1]
|
||||||
|
if q == nil then
|
||||||
|
q = { [mode] = "branch" }
|
||||||
|
db[key1] = q
|
||||||
|
else
|
||||||
|
assert(q[mode] == "branch")
|
||||||
|
end
|
||||||
|
return waitfor(session, source, q, key2, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
skynet.dispatch("lua", function (session, source, cmd, ...)
|
||||||
|
if cmd == "WAIT" then
|
||||||
|
local ret = command.QUERY(...)
|
||||||
|
if ret then
|
||||||
|
skynet.ret(skynet.pack(ret))
|
||||||
|
else
|
||||||
|
waitfor(session, source, wait_queue, ...)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
local f = assert(command[cmd])
|
||||||
|
skynet.ret(skynet.pack(f(...)))
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end)
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
local codecache = require "skynet.codecache"
|
local codecache = require "skynet.codecache"
|
||||||
local socket = require "socket"
|
local socket = require "socket"
|
||||||
|
local snax = require "snax"
|
||||||
|
|
||||||
local port = tonumber(...)
|
local port = tonumber(...)
|
||||||
local COMMAND = {}
|
local COMMAND = {}
|
||||||
@@ -87,7 +88,7 @@ end
|
|||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
local listen_socket = socket.listen ("127.0.0.1", port)
|
local listen_socket = socket.listen ("127.0.0.1", port)
|
||||||
print("Start debug console at 127.0.0.1",port)
|
skynet.error("Start debug console at 127.0.0.1 " .. port)
|
||||||
socket.start(listen_socket , function(id, addr)
|
socket.start(listen_socket , function(id, addr)
|
||||||
local function print(...)
|
local function print(...)
|
||||||
local t = { ... }
|
local t = { ... }
|
||||||
@@ -109,7 +110,10 @@ function COMMAND.help()
|
|||||||
mem = "mem : show memory status",
|
mem = "mem : show memory status",
|
||||||
gc = "gc : force every lua service do garbage collect",
|
gc = "gc : force every lua service do garbage collect",
|
||||||
start = "lanuch a new lua service",
|
start = "lanuch a new lua service",
|
||||||
|
snax = "lanuch a new snax service",
|
||||||
clearcache = "clear lua code cache",
|
clearcache = "clear lua code cache",
|
||||||
|
service = "List unique service",
|
||||||
|
task = "task address : show service task detail",
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -126,4 +130,16 @@ function COMMAND.start(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function COMMAND.snax(...)
|
||||||
|
local s = snax.newservice(...)
|
||||||
|
if s then
|
||||||
|
local addr = s.handle
|
||||||
|
return { [skynet.address(addr)] = ... }
|
||||||
|
else
|
||||||
|
return "Failed"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function COMMAND.service()
|
||||||
|
return skynet.call("SERVICE", "lua", "LIST")
|
||||||
|
end
|
||||||
|
|||||||
167
service/gate.lua
167
service/gate.lua
@@ -1,31 +1,40 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
local gateserver = require "snax.gateserver"
|
||||||
local netpack = require "netpack"
|
local netpack = require "netpack"
|
||||||
local socketdriver = require "socketdriver"
|
|
||||||
|
|
||||||
local socket
|
|
||||||
local queue
|
|
||||||
local watchdog
|
local watchdog
|
||||||
local maxclient
|
|
||||||
local client_number = 0
|
|
||||||
local CMD = setmetatable({}, { __gc = function() netpack.clear(queue) end })
|
|
||||||
|
|
||||||
local connection = {} -- fd -> connection : { fd , client, agent , ip, mode }
|
local connection = {} -- fd -> connection : { fd , client, agent , ip, mode }
|
||||||
local forwarding = {} -- agent -> connection
|
local forwarding = {} -- agent -> connection
|
||||||
|
|
||||||
function CMD.open( source , conf )
|
skynet.register_protocol {
|
||||||
assert(not socket)
|
name = "client",
|
||||||
local address = conf.address or "0.0.0.0"
|
id = skynet.PTYPE_CLIENT,
|
||||||
local port = assert(conf.port)
|
}
|
||||||
maxclient = conf.maxclient or 1024
|
|
||||||
|
local handler = {}
|
||||||
|
|
||||||
|
function handler.open(source, conf)
|
||||||
watchdog = conf.watchdog or source
|
watchdog = conf.watchdog or source
|
||||||
socket = socketdriver.listen(address, port)
|
|
||||||
socketdriver.start(socket)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function CMD.close()
|
function handler.message(fd, msg, sz)
|
||||||
assert(socket)
|
-- recv a package, forward it
|
||||||
socketdriver.close(socket)
|
local c = connection[fd]
|
||||||
socket = nil
|
local agent = c.agent
|
||||||
|
if agent then
|
||||||
|
skynet.redirect(agent, c.client, "client", 0, msg, sz)
|
||||||
|
else
|
||||||
|
skynet.send(watchdog, "lua", "socket", "data", fd, netpack.tostring(msg, sz))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function handler.connect(fd, addr)
|
||||||
|
local c = {
|
||||||
|
fd = fd,
|
||||||
|
ip = msg,
|
||||||
|
}
|
||||||
|
connection[fd] = c
|
||||||
|
skynet.send(watchdog, "lua", "socket", "open", fd, addr)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function unforward(c)
|
local function unforward(c)
|
||||||
@@ -36,120 +45,48 @@ local function unforward(c)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function start(c)
|
local function close_fd(fd)
|
||||||
if not c.mode then
|
local c = connection[fd]
|
||||||
c.mode = "open"
|
if c then
|
||||||
socketdriver.start(c.fd)
|
unforward(c)
|
||||||
|
connection[fd] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function handler.disconnect(fd)
|
||||||
|
close_fd(fd)
|
||||||
|
skynet.send(watchdog, "lua", "socket", "close", fd)
|
||||||
|
end
|
||||||
|
|
||||||
|
function handler.error(fd, msg)
|
||||||
|
close_fd(fd)
|
||||||
|
skynet.send(watchdog, "lua", "socket", "error", fd, msg)
|
||||||
|
end
|
||||||
|
|
||||||
|
local CMD = {}
|
||||||
|
|
||||||
function CMD.forward(source, fd, client, address)
|
function CMD.forward(source, fd, client, address)
|
||||||
local c = assert(connection[fd])
|
local c = assert(connection[fd])
|
||||||
unforward(c)
|
unforward(c)
|
||||||
start(c)
|
|
||||||
|
|
||||||
c.client = client or 0
|
c.client = client or 0
|
||||||
c.agent = address or source
|
c.agent = address or source
|
||||||
|
|
||||||
forwarding[c.agent] = c
|
forwarding[c.agent] = c
|
||||||
|
gateserver.openclient(fd)
|
||||||
end
|
end
|
||||||
|
|
||||||
function CMD.accept(source, fd)
|
function CMD.accept(source, fd)
|
||||||
local c = assert(connection[fd])
|
local c = assert(connection[fd])
|
||||||
unforward(c)
|
unforward(c)
|
||||||
start(c)
|
gateserver.openclient(fd)
|
||||||
end
|
end
|
||||||
|
|
||||||
function CMD.kick(source, fd)
|
function CMD.kick(source, fd)
|
||||||
local c
|
gateserver.closeclient(fd)
|
||||||
if fd then
|
|
||||||
c = connection[fd]
|
|
||||||
else
|
|
||||||
c = forwarding[source]
|
|
||||||
end
|
|
||||||
|
|
||||||
assert(c)
|
|
||||||
|
|
||||||
if c.mode ~= "close" then
|
|
||||||
c.mode = "close"
|
|
||||||
socketdriver.close(c.fd)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local MSG = {}
|
function handler.command(cmd, source, ...)
|
||||||
|
|
||||||
function MSG.data(fd, msg, sz)
|
|
||||||
-- recv a package, forward it
|
|
||||||
local c = connection[fd]
|
|
||||||
local agent = c.agent
|
|
||||||
if agent then
|
|
||||||
skynet.redirect(agent, c.client, "client", 0, msg, sz)
|
|
||||||
else
|
|
||||||
skynet.send(watchdog, "lua", "socket", "data", netpack.tostring(msg, sz))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function MSG.more()
|
|
||||||
for fd, msg, sz in netpack.pop, queue do
|
|
||||||
MSG.data(fd, msg, sz)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function MSG.open(fd, msg)
|
|
||||||
if client_number >= maxclient then
|
|
||||||
socketdriver.close(fd)
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local c = {
|
|
||||||
fd = fd,
|
|
||||||
ip = msg,
|
|
||||||
}
|
|
||||||
connection[fd] = c
|
|
||||||
client_number = client_number + 1
|
|
||||||
skynet.send(watchdog, "lua", "socket", "open", fd, msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
local function close_fd(fd, message)
|
|
||||||
local c = connection[fd]
|
|
||||||
if c then
|
|
||||||
unforward(c)
|
|
||||||
connection[fd] = nil
|
|
||||||
client_number = client_number - 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function MSG.close(fd)
|
|
||||||
close_fd(fd)
|
|
||||||
skynet.send(watchdog, "lua", "socket", "close", fd)
|
|
||||||
end
|
|
||||||
|
|
||||||
function MSG.error(fd, msg)
|
|
||||||
close_fd(fd)
|
|
||||||
skynet.send(watchdog, "lua", "socket", "error", fd, msg)
|
|
||||||
end
|
|
||||||
|
|
||||||
skynet.register_protocol {
|
|
||||||
name = "socket",
|
|
||||||
id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6
|
|
||||||
unpack = function ( msg, sz )
|
|
||||||
return netpack.filter( queue, msg, sz)
|
|
||||||
end,
|
|
||||||
dispatch = function (_, _, q, type, ...)
|
|
||||||
queue = q
|
|
||||||
if type then
|
|
||||||
MSG[type](...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
}
|
|
||||||
|
|
||||||
skynet.register_protocol {
|
|
||||||
name = "client",
|
|
||||||
id = skynet.PTYPE_CLIENT,
|
|
||||||
}
|
|
||||||
|
|
||||||
skynet.start(function()
|
|
||||||
skynet.dispatch("lua", function (_, address, cmd, ...)
|
|
||||||
local f = assert(CMD[cmd])
|
local f = assert(CMD[cmd])
|
||||||
skynet.ret(skynet.pack(f(address, ...)))
|
return f(source, ...)
|
||||||
end)
|
end
|
||||||
end)
|
|
||||||
|
gateserver.start(handler)
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ local skynet = require "skynet"
|
|||||||
local string = string
|
local string = string
|
||||||
|
|
||||||
local services = {}
|
local services = {}
|
||||||
|
|
||||||
local command = {}
|
local command = {}
|
||||||
|
local instance = {} -- for confirm (function command.LAUNCH / command.ERROR / command.LAUNCHOK)
|
||||||
|
|
||||||
local function handle_to_address(handle)
|
local function handle_to_address(handle)
|
||||||
return tonumber("0x" .. string.sub(handle , 2))
|
return tonumber("0x" .. string.sub(handle , 2))
|
||||||
@@ -19,18 +19,6 @@ function command.LIST()
|
|||||||
return list
|
return list
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.RELOAD(handle)
|
|
||||||
handle = handle_to_address(handle)
|
|
||||||
local cmd = string.match(services[handle], "snlua (.+)")
|
|
||||||
print(services[handle],cmd)
|
|
||||||
if cmd then
|
|
||||||
skynet.send(handle,"debug","RELOAD",cmd)
|
|
||||||
return {cmd}
|
|
||||||
else
|
|
||||||
return {"Support only snlua"}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function command.STAT()
|
function command.STAT()
|
||||||
local list = {}
|
local list = {}
|
||||||
for k,v in pairs(services) do
|
for k,v in pairs(services) do
|
||||||
@@ -40,7 +28,7 @@ function command.STAT()
|
|||||||
return list
|
return list
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.INFO(handle)
|
function command.INFO(_, _, handle)
|
||||||
handle = handle_to_address(handle)
|
handle = handle_to_address(handle)
|
||||||
if services[handle] == nil then
|
if services[handle] == nil then
|
||||||
return
|
return
|
||||||
@@ -50,23 +38,17 @@ function command.INFO(handle)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.TIMING(handle)
|
function command.TASK(_, _, handle)
|
||||||
handle = handle_to_address(handle)
|
handle = handle_to_address(handle)
|
||||||
if services[handle] == nil then
|
if services[handle] == nil then
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
local r = skynet.call(handle,"debug","TIMING")
|
local result = skynet.call(handle,"debug","TASK")
|
||||||
local result = {}
|
|
||||||
for k,v in pairs(r) do
|
|
||||||
v.name = services[k]
|
|
||||||
v.avg = v.ti/v.n
|
|
||||||
result[skynet.address(k)] = v
|
|
||||||
end
|
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.KILL(handle)
|
function command.KILL(_, _, handle)
|
||||||
handle = handle_to_address(handle)
|
handle = handle_to_address(handle)
|
||||||
skynet.kill(handle)
|
skynet.kill(handle)
|
||||||
local ret = { [skynet.address(handle)] = tostring(services[handle]) }
|
local ret = { [skynet.address(handle)] = tostring(services[handle]) }
|
||||||
@@ -90,23 +72,25 @@ function command.GC()
|
|||||||
return command.MEM()
|
return command.MEM()
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.REMOVE(handle)
|
function command.REMOVE(_,_, handle)
|
||||||
services[handle] = nil
|
services[handle] = nil
|
||||||
-- don't return (skynet.ret) because the handle may exit
|
-- don't return (skynet.ret) because the handle may exit
|
||||||
return NORET
|
return NORET
|
||||||
end
|
end
|
||||||
|
|
||||||
local instance = {}
|
function command.LAUNCH(address, session, service, ...)
|
||||||
|
local param = table.concat({...}, " ")
|
||||||
skynet.dispatch("text" , function(session, address , cmd)
|
local inst = skynet.launch(service, param)
|
||||||
if cmd == "" then
|
if inst then
|
||||||
-- init notice
|
services[inst] = service .. " " .. param
|
||||||
local reply = instance[address]
|
instance[inst] = { session = session, address = address }
|
||||||
if reply then
|
else
|
||||||
skynet.redirect(reply.address , 0, "response", reply.session, skynet.address(address))
|
skynet.ret("") -- launch failed
|
||||||
instance[address] = nil
|
|
||||||
end
|
end
|
||||||
elseif cmd == "ERROR" then
|
return NORET
|
||||||
|
end
|
||||||
|
|
||||||
|
function command.ERROR(address)
|
||||||
-- see serivce-src/service_lua.c
|
-- see serivce-src/service_lua.c
|
||||||
-- init failed
|
-- init failed
|
||||||
local reply = instance[address]
|
local reply = instance[address]
|
||||||
@@ -114,24 +98,45 @@ skynet.dispatch("text" , function(session, address , cmd)
|
|||||||
skynet.redirect(reply.address , 0, "response", reply.session, "")
|
skynet.redirect(reply.address , 0, "response", reply.session, "")
|
||||||
instance[address] = nil
|
instance[address] = nil
|
||||||
end
|
end
|
||||||
|
services[address] = nil
|
||||||
|
return NORET
|
||||||
|
end
|
||||||
|
|
||||||
|
function command.LAUNCHOK(address)
|
||||||
|
-- init notice
|
||||||
|
local reply = instance[address]
|
||||||
|
if reply then
|
||||||
|
skynet.redirect(reply.address , 0, "response", reply.session, skynet.address(address))
|
||||||
|
instance[address] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
return NORET
|
||||||
|
end
|
||||||
|
|
||||||
|
-- for historical reasons, launcher support text command (for C service)
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "text",
|
||||||
|
id = skynet.PTYPE_TEXT,
|
||||||
|
unpack = skynet.tostring,
|
||||||
|
dispatch = function(session, address , cmd)
|
||||||
|
if cmd == "" then
|
||||||
|
command.LAUNCHOK(address)
|
||||||
|
elseif cmd == "ERROR" then
|
||||||
|
command.ERROR(address)
|
||||||
else
|
else
|
||||||
-- launch request
|
-- launch request
|
||||||
local service, param = string.match(cmd,"([^ ]+) (.*)")
|
local service, param = string.match(cmd,"([^ ]+) (.*)")
|
||||||
local inst = skynet.launch(service, param)
|
command.LAUNCH(address, session, service, param)
|
||||||
if inst then
|
|
||||||
services[inst] = cmd
|
|
||||||
instance[inst] = { session = session, address = address }
|
|
||||||
else
|
|
||||||
skynet.ret("")
|
|
||||||
end
|
end
|
||||||
end
|
end,
|
||||||
end)
|
}
|
||||||
|
|
||||||
skynet.dispatch("lua", function(session, address, cmd , ...)
|
skynet.dispatch("lua", function(session, address, cmd , ...)
|
||||||
cmd = string.upper(cmd)
|
cmd = string.upper(cmd)
|
||||||
local f = command[cmd]
|
local f = command[cmd]
|
||||||
if f then
|
if f then
|
||||||
local ret = f(...)
|
local ret = f(address, session, ...)
|
||||||
if ret ~= NORET then
|
if ret ~= NORET then
|
||||||
skynet.ret(skynet.pack(ret))
|
skynet.ret(skynet.pack(ret))
|
||||||
end
|
end
|
||||||
|
|||||||
194
service/multicastd.lua
Normal file
194
service/multicastd.lua
Normal file
@@ -0,0 +1,194 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local mc = require "multicast.c"
|
||||||
|
local datacenter = require "datacenter"
|
||||||
|
|
||||||
|
local harbor_id = skynet.harbor(skynet.self())
|
||||||
|
|
||||||
|
local command = {}
|
||||||
|
local channel = {}
|
||||||
|
local channel_n = {}
|
||||||
|
local channel_remote = {}
|
||||||
|
local channel_id = harbor_id
|
||||||
|
local NORET = {}
|
||||||
|
|
||||||
|
local function get_address(t, id)
|
||||||
|
local v = assert(datacenter.get("multicast", id))
|
||||||
|
t[id] = v
|
||||||
|
return v
|
||||||
|
end
|
||||||
|
|
||||||
|
local node_address = setmetatable({}, { __index = get_address })
|
||||||
|
|
||||||
|
-- new LOCAL channel , The low 8bit is the same with harbor_id
|
||||||
|
function command.NEW()
|
||||||
|
while channel[channel_id] do
|
||||||
|
channel_id = mc.nextid(channel_id)
|
||||||
|
end
|
||||||
|
channel[channel_id] = {}
|
||||||
|
channel_n[channel_id] = 0
|
||||||
|
local ret = channel_id
|
||||||
|
channel_id = mc.nextid(channel_id)
|
||||||
|
return ret
|
||||||
|
end
|
||||||
|
|
||||||
|
-- MUST call by the owner node of channel, delete a remote channel
|
||||||
|
function command.DELR(source, c)
|
||||||
|
channel[c] = nil
|
||||||
|
channel_n[c] = nil
|
||||||
|
return NORET
|
||||||
|
end
|
||||||
|
|
||||||
|
-- delete a channel, if the channel is remote, forward the command to the owner node
|
||||||
|
-- otherwise, delete the channel, and call all the remote node, DELR
|
||||||
|
function command.DEL(source, c)
|
||||||
|
local node = c % 256
|
||||||
|
if node ~= harbor_id then
|
||||||
|
skynet.send(node_address[node], "lua", "DEL", c)
|
||||||
|
return NORET
|
||||||
|
end
|
||||||
|
local remote = channel_remote[c]
|
||||||
|
channel[c] = nil
|
||||||
|
channel_n[c] = nil
|
||||||
|
channel_remote[c] = nil
|
||||||
|
for node in pairs(remote) do
|
||||||
|
skynet.send(node_address[node], "lua", "DELR", c)
|
||||||
|
end
|
||||||
|
return NORET
|
||||||
|
end
|
||||||
|
|
||||||
|
-- forward multicast message to a node (channel id use the session field)
|
||||||
|
local function remote_publish(node, channel, source, ...)
|
||||||
|
skynet.redirect(node_address[node], source, "multicast", channel, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- publish a message, for local node, use the message pointer (call mc.bind to add the reference)
|
||||||
|
-- for remote node, call remote_publish. (call mc.unpack and skynet.tostring to convert message pointer to string)
|
||||||
|
local function publish(c , source, pack, size)
|
||||||
|
local group = channel[c]
|
||||||
|
if group == nil then
|
||||||
|
-- dead channel, delete the pack
|
||||||
|
mc.bind(pack, 1)
|
||||||
|
mc.close(pack)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
mc.bind(pack, channel_n[c])
|
||||||
|
local msg = skynet.tostring(pack, size)
|
||||||
|
for k in pairs(group) do
|
||||||
|
skynet.redirect(k, source, "multicast", c , msg)
|
||||||
|
end
|
||||||
|
local remote = channel_remote[c]
|
||||||
|
if remote then
|
||||||
|
local _, msg, sz = mc.unpack(pack, size)
|
||||||
|
local msg = skynet.tostring(msg,sz)
|
||||||
|
for node in pairs(remote) do
|
||||||
|
remote_publish(node, c, source, msg)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.register_protocol {
|
||||||
|
name = "multicast",
|
||||||
|
id = skynet.PTYPE_MULTICAST,
|
||||||
|
unpack = function(msg, sz)
|
||||||
|
return mc.packremote(msg, sz)
|
||||||
|
end,
|
||||||
|
dispatch = publish,
|
||||||
|
}
|
||||||
|
|
||||||
|
-- publish a message, if the caller is remote, forward the message to the owner node (by remote_publish)
|
||||||
|
-- If the caller is local, call publish
|
||||||
|
function command.PUB(source, c, pack, size)
|
||||||
|
assert(skynet.harbor(source) == harbor_id)
|
||||||
|
local node = c % 256
|
||||||
|
if node ~= harbor_id then
|
||||||
|
-- remote publish
|
||||||
|
remote_publish(node, c, source, mc.remote(pack))
|
||||||
|
else
|
||||||
|
publish(c, source, pack,size)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- the node (source) subscribe a channel
|
||||||
|
-- MUST call by channel owner node (assert source is not local and channel is create by self)
|
||||||
|
-- If channel is not exist, return true
|
||||||
|
-- Else set channel_remote[channel] true
|
||||||
|
function command.SUBR(source, c)
|
||||||
|
local node = skynet.harbor(source)
|
||||||
|
if not channel[c] then
|
||||||
|
-- channel none exist
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
assert(node ~= harbor_id and c % 256 == harbor_id)
|
||||||
|
local group = channel_remote[c]
|
||||||
|
if group == nil then
|
||||||
|
group = {}
|
||||||
|
channel_remote[c] = group
|
||||||
|
end
|
||||||
|
group[node] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
-- the service (source) subscribe a channel
|
||||||
|
-- If the channel is remote, node subscribe it by send a SUBR to the owner .
|
||||||
|
function command.SUB(source, c)
|
||||||
|
local node = c % 256
|
||||||
|
if node ~= harbor_id then
|
||||||
|
-- remote group
|
||||||
|
if channel[c] == nil then
|
||||||
|
if skynet.call(node_address[node], "lua", "SUBR", c) then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
if channel[c] == nil then
|
||||||
|
-- double check, because skynet.call whould yield, other SUB may occur.
|
||||||
|
channel[c] = {}
|
||||||
|
channel_n[c] = 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local group = channel[c]
|
||||||
|
if group and not group[source] then
|
||||||
|
channel_n[c] = channel_n[c] + 1
|
||||||
|
group[source] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- MUST call by a node, unsubscribe a channel
|
||||||
|
function command.USUBR(source, c)
|
||||||
|
local node = skynet.harbor(source)
|
||||||
|
assert(node ~= harbor_id)
|
||||||
|
local group = assert(channel_remote[c])
|
||||||
|
group[node] = nil
|
||||||
|
return NORET
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Unsubscribe a channel, if the subscriber is empty and the channel is remote, send USUBR to the channel owner
|
||||||
|
function command.USUB(source, c)
|
||||||
|
local group = assert(channel[c])
|
||||||
|
if group[source] then
|
||||||
|
group[source] = nil
|
||||||
|
channel_n[c] = channel_n[c] - 1
|
||||||
|
if channel_n[c] == 0 then
|
||||||
|
local node = c % 256
|
||||||
|
if node ~= harbor_id then
|
||||||
|
-- remote group
|
||||||
|
channel[c] = nil
|
||||||
|
channel_n[c] = nil
|
||||||
|
skynet.send(node_address[node], "lua", "USUBR", c)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return NORET
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
skynet.dispatch("lua", function(_,source, cmd, ...)
|
||||||
|
local f = assert(command[cmd])
|
||||||
|
local result = f(source, ...)
|
||||||
|
if result ~= NORET then
|
||||||
|
skynet.ret(skynet.pack(result))
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
local self = skynet.self()
|
||||||
|
local id = skynet.harbor(self)
|
||||||
|
assert(datacenter.set("multicast", id, self) == nil)
|
||||||
|
end)
|
||||||
|
|
||||||
@@ -1,72 +1,191 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
local snax = require "snax"
|
||||||
|
|
||||||
local cmd = {}
|
local cmd = {}
|
||||||
local service = {}
|
local service = {}
|
||||||
|
|
||||||
function cmd.LAUNCH(service_name, ...)
|
local function request(name, func, ...)
|
||||||
local s = service[service_name]
|
local ok, handle = pcall(func, ...)
|
||||||
if type(s) == "number" then
|
local s = service[name]
|
||||||
return s
|
|
||||||
end
|
|
||||||
|
|
||||||
if s == nil then
|
|
||||||
s = { launch = true }
|
|
||||||
service[service_name] = s
|
|
||||||
elseif s.launch then
|
|
||||||
assert(type(s) == "table")
|
assert(type(s) == "table")
|
||||||
local co = coroutine.running()
|
if ok then
|
||||||
table.insert(s, co)
|
service[name] = handle
|
||||||
skynet.wait()
|
else
|
||||||
s = service[service_name]
|
service[name] = tostring(handle)
|
||||||
assert(type(s) == "number")
|
|
||||||
return s
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local handle = skynet.newservice(service_name, ...)
|
|
||||||
for _,v in ipairs(s) do
|
for _,v in ipairs(s) do
|
||||||
skynet.wakeup(v)
|
skynet.wakeup(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
service[service_name] = handle
|
if ok then
|
||||||
|
|
||||||
return handle
|
return handle
|
||||||
|
else
|
||||||
|
error(tostring(handle))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function cmd.QUERY(service_name)
|
local function waitfor(name , func, ...)
|
||||||
local s = service[service_name]
|
local s = service[name]
|
||||||
if type(s) == "number" then
|
if type(s) == "number" then
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
local co = coroutine.running()
|
||||||
|
|
||||||
if s == nil then
|
if s == nil then
|
||||||
s = {}
|
s = {}
|
||||||
service[service_name] = s
|
service[name] = s
|
||||||
|
elseif type(s) == "string" then
|
||||||
|
error(s)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert(type(s) == "table")
|
assert(type(s) == "table")
|
||||||
local co = coroutine.running()
|
|
||||||
|
if not s.launch and func then
|
||||||
|
s.launch = true
|
||||||
|
return request(name, func, ...)
|
||||||
|
end
|
||||||
|
|
||||||
table.insert(s, co)
|
table.insert(s, co)
|
||||||
skynet.wait()
|
skynet.wait()
|
||||||
s = service[service_name]
|
s = service[name]
|
||||||
|
if type(s) == "string" then
|
||||||
|
error(s)
|
||||||
|
end
|
||||||
assert(type(s) == "number")
|
assert(type(s) == "number")
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function read_name(service_name)
|
||||||
|
if string.byte(service_name) == 64 then -- '@'
|
||||||
|
return string.sub(service_name , 2)
|
||||||
|
else
|
||||||
|
return service_name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cmd.LAUNCH(service_name, subname, ...)
|
||||||
|
local realname = read_name(service_name)
|
||||||
|
|
||||||
|
if realname == "snaxd" then
|
||||||
|
return waitfor(service_name.."."..subname, snax.rawnewservice, subname, ...)
|
||||||
|
else
|
||||||
|
return waitfor(service_name, skynet.newservice, realname, subname, ...)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cmd.QUERY(service_name, subname)
|
||||||
|
local realname = read_name(service_name)
|
||||||
|
|
||||||
|
if realname == "snaxd" then
|
||||||
|
return waitfor(service_name.."."..subname)
|
||||||
|
else
|
||||||
|
return waitfor(service_name)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function list_service()
|
||||||
|
local result = {}
|
||||||
|
for k,v in pairs(service) do
|
||||||
|
if type(v) == "string" then
|
||||||
|
v = "Error: " .. v
|
||||||
|
elseif type(v) == "table" then
|
||||||
|
v = "Querying"
|
||||||
|
else
|
||||||
|
v = skynet.address(v)
|
||||||
|
end
|
||||||
|
|
||||||
|
result[k] = v
|
||||||
|
end
|
||||||
|
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
local function register_global()
|
||||||
|
function cmd.GLAUNCH(name, ...)
|
||||||
|
local global_name = "@" .. name
|
||||||
|
return cmd.LAUNCH(global_name, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function cmd.GQUERY(name, ...)
|
||||||
|
local global_name = "@" .. name
|
||||||
|
return cmd.QUERY(global_name, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
local mgr = {}
|
||||||
|
|
||||||
|
function cmd.REPORT(m)
|
||||||
|
mgr[m] = true
|
||||||
|
skynet.watch(m)
|
||||||
|
end
|
||||||
|
|
||||||
|
local function add_list(all, m)
|
||||||
|
local harbor = "@" .. skynet.harbor(m)
|
||||||
|
local result = skynet.call(m, "lua", "LIST")
|
||||||
|
for k,v in pairs(result) do
|
||||||
|
all[k .. harbor] = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function cmd.LIST()
|
||||||
|
local result = {}
|
||||||
|
for k in pairs(mgr) do
|
||||||
|
pcall(add_list, result, k)
|
||||||
|
end
|
||||||
|
local l = list_service()
|
||||||
|
for k, v in pairs(l) do
|
||||||
|
result[k] = v
|
||||||
|
end
|
||||||
|
return result
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function register_local()
|
||||||
|
function cmd.GLAUNCH(name, ...)
|
||||||
|
local global_name = "@" .. name
|
||||||
|
return waitfor(global_name, skynet.call, "SERVICE", "lua", "LAUNCH", global_name, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function cmd.GQUERY(name, ...)
|
||||||
|
local global_name = "@" .. name
|
||||||
|
return waitfor(global_name, skynet.call, "SERVICE", "lua", "QUERY", global_name, ...)
|
||||||
|
end
|
||||||
|
|
||||||
|
function cmd.LIST()
|
||||||
|
return list_service()
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.call("SERVICE", "lua", "REPORT", skynet.self())
|
||||||
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
skynet.dispatch("lua", function(session, address, command, service_name , ...)
|
skynet.dispatch("lua", function(session, address, command, ...)
|
||||||
local f = cmd[command]
|
local f = cmd[command]
|
||||||
if f == nil then
|
if f == nil then
|
||||||
skynet.ret(skynet.pack(nil))
|
skynet.ret(skynet.pack(nil, "Invalid command " .. command))
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local ok, r = pcall(f, service_name, ...)
|
local ok, r = pcall(f, ...)
|
||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
skynet.ret(skynet.pack(r))
|
skynet.ret(skynet.pack(r))
|
||||||
else
|
else
|
||||||
skynet.ret(skynet.pack(nil))
|
skynet.ret(skynet.pack(nil, r))
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
local handle = skynet.localname ".service"
|
||||||
|
if handle then
|
||||||
|
skynet.error(".service is already register by ", skynet.address(handle))
|
||||||
|
skynet.exit()
|
||||||
|
else
|
||||||
skynet.register(".service")
|
skynet.register(".service")
|
||||||
|
end
|
||||||
if skynet.getenv "standalone" then
|
if skynet.getenv "standalone" then
|
||||||
skynet.register("SERVICE")
|
skynet.register("SERVICE")
|
||||||
|
register_global()
|
||||||
|
else
|
||||||
|
register_local()
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
local c = require "skynet.c"
|
local c = require "skynet.c"
|
||||||
local snax_interface = require "snax_interface"
|
local snax_interface = require "snax.interface"
|
||||||
local profile = require "profile"
|
local profile = require "profile"
|
||||||
|
local snax = require "snax"
|
||||||
|
|
||||||
local func = snax_interface(tostring(...), _ENV)
|
local func = snax_interface(tostring(...), _ENV)
|
||||||
local mode
|
local mode
|
||||||
@@ -20,8 +21,10 @@ local function update_stat(name, ti)
|
|||||||
t.time = t.time + ti
|
t.time = t.time + ti
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local traceback = debug.traceback
|
||||||
|
|
||||||
local function do_func(f, msg)
|
local function do_func(f, msg)
|
||||||
return pcall(f, table.unpack(msg))
|
return xpcall(f, traceback, table.unpack(msg))
|
||||||
end
|
end
|
||||||
|
|
||||||
local function dispatch(f, ...)
|
local function dispatch(f, ...)
|
||||||
@@ -41,7 +44,7 @@ local function message_dispatch()
|
|||||||
if method[2] == "accept" then
|
if method[2] == "accept" then
|
||||||
-- no return
|
-- no return
|
||||||
profile.start()
|
profile.start()
|
||||||
local ok, data = pcall(f, table.unpack(msg))
|
local ok, data = xpcall(f, traceback, table.unpack(msg))
|
||||||
local ti = profile.stop()
|
local ti = profile.stop()
|
||||||
update_stat(method[3], ti)
|
update_stat(method[3], ti)
|
||||||
if not ok then
|
if not ok then
|
||||||
@@ -49,7 +52,7 @@ local function message_dispatch()
|
|||||||
end
|
end
|
||||||
else
|
else
|
||||||
profile.start()
|
profile.start()
|
||||||
local ok, data, size = pcall(dispatch, f, table.unpack(msg))
|
local ok, data, size = xpcall(dispatch, traceback, f, table.unpack(msg))
|
||||||
local ti = profile.stop()
|
local ti = profile.stop()
|
||||||
update_stat(method[3], ti)
|
update_stat(method[3], ti)
|
||||||
if ok then
|
if ok then
|
||||||
@@ -88,9 +91,9 @@ local function timing( method, ... )
|
|||||||
profile.start()
|
profile.start()
|
||||||
if method[2] == "accept" then
|
if method[2] == "accept" then
|
||||||
-- no return
|
-- no return
|
||||||
err,msg = pcall(method[4], ...)
|
err,msg = xpcall(method[4], traceback, ...)
|
||||||
else
|
else
|
||||||
err,msg = pcall(return_f, method[4], ...)
|
err,msg = xpcall(return_f, traceback, method[4], ...)
|
||||||
end
|
end
|
||||||
local ti = profile.stop()
|
local ti = profile.stop()
|
||||||
update_stat(method[3], ti)
|
update_stat(method[3], ti)
|
||||||
@@ -98,13 +101,13 @@ local function timing( method, ... )
|
|||||||
end
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
skynet.dispatch("lua", function ( session , source , id, ...)
|
skynet.dispatch("snax", function ( session , source , id, ...)
|
||||||
local method = func[id]
|
local method = func[id]
|
||||||
|
|
||||||
if method[2] == "system" then
|
if method[2] == "system" then
|
||||||
local command = method[3]
|
local command = method[3]
|
||||||
if command == "hotfix" then
|
if command == "hotfix" then
|
||||||
local hotfix = require "snax_hotfix"
|
local hotfix = require "snax.hotfix"
|
||||||
skynet.ret(skynet.pack(hotfix(func, ...)))
|
skynet.ret(skynet.pack(hotfix(func, ...)))
|
||||||
elseif command == "init" then
|
elseif command == "init" then
|
||||||
assert(not init, "Already init")
|
assert(not init, "Already init")
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ mallctl_int64(const char* name, size_t* newval) {
|
|||||||
} else {
|
} else {
|
||||||
je_mallctl(name, &v, &len, NULL, 0);
|
je_mallctl(name, &v, &len, NULL, 0);
|
||||||
}
|
}
|
||||||
// printf("name: %s, value: %zd\n", name, v);
|
// skynet_error(NULL, "name: %s, value: %zd\n", name, v);
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,9 +117,9 @@ mallctl_opt(const char* name, int* newval) {
|
|||||||
if(newval) {
|
if(newval) {
|
||||||
int ret = je_mallctl(name, &v, &len, newval, sizeof(int));
|
int ret = je_mallctl(name, &v, &len, newval, sizeof(int));
|
||||||
if(ret == 0) {
|
if(ret == 0) {
|
||||||
printf("set new value(%d) for (%s) succeed\n", *newval, name);
|
skynet_error(NULL, "set new value(%d) for (%s) succeed\n", *newval, name);
|
||||||
} else {
|
} else {
|
||||||
printf("set new value(%d) for (%s) failed: error -> %d\n", *newval, name, ret);
|
skynet_error(NULL, "set new value(%d) for (%s) failed: error -> %d\n", *newval, name, ret);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
je_mallctl(name, &v, &len, NULL, 0);
|
je_mallctl(name, &v, &len, NULL, 0);
|
||||||
@@ -196,15 +196,15 @@ void
|
|||||||
dump_c_mem() {
|
dump_c_mem() {
|
||||||
int i;
|
int i;
|
||||||
size_t total = 0;
|
size_t total = 0;
|
||||||
printf("dump all service mem:\n");
|
skynet_error(NULL, "dump all service mem:");
|
||||||
for(i=0; i<SLOT_SIZE; i++) {
|
for(i=0; i<SLOT_SIZE; i++) {
|
||||||
mem_data* data = &mem_stats[i];
|
mem_data* data = &mem_stats[i];
|
||||||
if(data->handle != 0 && data->allocated != 0) {
|
if(data->handle != 0 && data->allocated != 0) {
|
||||||
total += data->allocated;
|
total += data->allocated;
|
||||||
printf("0x%x -> %zdkb\n", data->handle, data->allocated >> 10);
|
skynet_error(NULL, "0x%x -> %zdkb", data->handle, data->allocated >> 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("+total: %zdkb\n",total >> 10);
|
skynet_error(NULL, "+total: %zdkb",total >> 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
@@ -224,9 +224,3 @@ skynet_lalloc(void *ud, void *ptr, size_t osize, size_t nsize) {
|
|||||||
return skynet_realloc(ptr, nsize);
|
return skynet_realloc(ptr, nsize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
malloc_inithook(void) {
|
|
||||||
memset(mem_stats, 0, sizeof(mem_stats));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,17 +8,18 @@
|
|||||||
|
|
||||||
#define PTYPE_TEXT 0
|
#define PTYPE_TEXT 0
|
||||||
#define PTYPE_RESPONSE 1
|
#define PTYPE_RESPONSE 1
|
||||||
#define PTYPE_MULTICAST_DEPRECATED 2
|
#define PTYPE_MULTICAST 2
|
||||||
#define PTYPE_CLIENT 3
|
#define PTYPE_CLIENT 3
|
||||||
#define PTYPE_SYSTEM 4
|
#define PTYPE_SYSTEM 4
|
||||||
#define PTYPE_HARBOR 5
|
#define PTYPE_HARBOR 5
|
||||||
#define PTYPE_SOCKET 6
|
#define PTYPE_SOCKET 6
|
||||||
// read lualib/skynet.lua lualib/simplemonitor.lua
|
// read lualib/skynet.lua examples/simplemonitor.lua
|
||||||
#define PTYPE_RESERVED_ERROR 7
|
#define PTYPE_ERROR 7
|
||||||
// read lualib/skynet.lua lualib/mqueue.lua
|
// read lualib/skynet.lua lualib/mqueue.lua lualib/snax.lua
|
||||||
#define PTYPE_RESERVED_QUEUE 8
|
#define PTYPE_RESERVED_QUEUE 8
|
||||||
#define PTYPE_RESERVED_DEBUG 9
|
#define PTYPE_RESERVED_DEBUG 9
|
||||||
#define PTYPE_RESERVED_LUA 10
|
#define PTYPE_RESERVED_LUA 10
|
||||||
|
#define PTYPE_RESERVED_SNAX 11
|
||||||
|
|
||||||
#define PTYPE_TAG_DONTCOPY 0x10000
|
#define PTYPE_TAG_DONTCOPY 0x10000
|
||||||
#define PTYPE_TAG_ALLOCSESSION 0x20000
|
#define PTYPE_TAG_ALLOCSESSION 0x20000
|
||||||
|
|||||||
96
skynet-src/skynet_daemon.c
Normal file
96
skynet-src/skynet_daemon.c
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/file.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "skynet_daemon.h"
|
||||||
|
|
||||||
|
static int
|
||||||
|
check_pid(const char *pidfile) {
|
||||||
|
int pid = 0;
|
||||||
|
FILE *f = fopen(pidfile,"r");
|
||||||
|
if (f == NULL)
|
||||||
|
return 0;
|
||||||
|
int n = fscanf(f,"%d", &pid);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
if (n !=1 || pid == 0 || pid == getpid()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (kill(pid, 0) && errno == ESRCH)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
write_pid(const char *pidfile) {
|
||||||
|
FILE *f;
|
||||||
|
int pid = 0;
|
||||||
|
int fd = open(pidfile, O_RDWR|O_CREAT, 0644);
|
||||||
|
if (fd == -1) {
|
||||||
|
fprintf(stderr, "Can't create %s.\n", pidfile);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
f = fdopen(fd, "r+");
|
||||||
|
if (f == NULL) {
|
||||||
|
fprintf(stderr, "Can't open %s.\n", pidfile);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flock(fd, LOCK_EX|LOCK_NB) == -1) {
|
||||||
|
int n = fscanf(f, "%d", &pid);
|
||||||
|
fclose(f);
|
||||||
|
if (n != 1) {
|
||||||
|
fprintf(stderr, "Can't lock and read pidfile.\n");
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Can't lock pidfile, lock is held by pid %d.\n", pid);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pid = getpid();
|
||||||
|
if (!fprintf(f,"%d\n", pid)) {
|
||||||
|
fprintf(stderr, "Can't write pid.\n");
|
||||||
|
close(fd);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
fflush(f);
|
||||||
|
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
daemon_init(const char *pidfile) {
|
||||||
|
int pid = check_pid(pidfile);
|
||||||
|
|
||||||
|
if (pid) {
|
||||||
|
fprintf(stderr, "Skynet is already running, pid = %d.\n", pid);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef __APPLE__
|
||||||
|
fprintf(stderr, "'daemon' is deprecated: first deprecated in OS X 10.5 , use launchd instead.\n");
|
||||||
|
#else
|
||||||
|
if (daemon(1,0)) {
|
||||||
|
fprintf(stderr, "Can't daemonize.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
pid = write_pid(pidfile);
|
||||||
|
if (pid == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
daemon_exit(const char *pidfile) {
|
||||||
|
return unlink(pidfile);
|
||||||
|
}
|
||||||
7
skynet-src/skynet_daemon.h
Normal file
7
skynet-src/skynet_daemon.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#ifndef skynet_daemon_h
|
||||||
|
#define skynet_daemon_h
|
||||||
|
|
||||||
|
int daemon_init(const char *pidfile);
|
||||||
|
int daemon_exit(const char *pidfile);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define DEFAULT_SLOT_SIZE 4
|
#define DEFAULT_SLOT_SIZE 4
|
||||||
|
#define MAX_SLOT_SIZE 0x40000000
|
||||||
|
|
||||||
struct handle_name {
|
struct handle_name {
|
||||||
char * name;
|
char * name;
|
||||||
@@ -168,6 +169,7 @@ static void
|
|||||||
_insert_name_before(struct handle_storage *s, char *name, uint32_t handle, int before) {
|
_insert_name_before(struct handle_storage *s, char *name, uint32_t handle, int before) {
|
||||||
if (s->name_count >= s->name_cap) {
|
if (s->name_count >= s->name_cap) {
|
||||||
s->name_cap *= 2;
|
s->name_cap *= 2;
|
||||||
|
assert(s->name_cap <= MAX_SLOT_SIZE);
|
||||||
struct handle_name * n = skynet_malloc(s->name_cap * sizeof(struct handle_name));
|
struct handle_name * n = skynet_malloc(s->name_cap * sizeof(struct handle_name));
|
||||||
int i;
|
int i;
|
||||||
for (i=0;i<before;i++) {
|
for (i=0;i<before;i++) {
|
||||||
|
|||||||
@@ -7,33 +7,19 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
static struct skynet_context * REMOTE = 0;
|
static struct skynet_context * REMOTE = 0;
|
||||||
static unsigned int HARBOR = 0;
|
static unsigned int HARBOR = ~0;
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session) {
|
skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session) {
|
||||||
int type = rmsg->sz >> HANDLE_REMOTE_SHIFT;
|
int type = rmsg->sz >> HANDLE_REMOTE_SHIFT;
|
||||||
rmsg->sz &= HANDLE_MASK;
|
rmsg->sz &= HANDLE_MASK;
|
||||||
assert(type != PTYPE_SYSTEM && type != PTYPE_HARBOR);
|
assert(type != PTYPE_SYSTEM && type != PTYPE_HARBOR && REMOTE);
|
||||||
skynet_context_send(REMOTE, rmsg, sizeof(*rmsg) , source, type , session);
|
skynet_context_send(REMOTE, rmsg, sizeof(*rmsg) , source, type , session);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
skynet_harbor_register(struct remote_name *rname) {
|
|
||||||
int i;
|
|
||||||
int number = 1;
|
|
||||||
for (i=0;i<GLOBALNAME_LENGTH;i++) {
|
|
||||||
char c = rname->name[i];
|
|
||||||
if (!(c >= '0' && c <='9')) {
|
|
||||||
number = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert(number == 0);
|
|
||||||
skynet_context_send(REMOTE, rname, sizeof(*rname), 0, PTYPE_SYSTEM , 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
skynet_harbor_message_isremote(uint32_t handle) {
|
skynet_harbor_message_isremote(uint32_t handle) {
|
||||||
|
assert(HARBOR != ~0);
|
||||||
int h = (handle & ~HANDLE_MASK);
|
int h = (handle & ~HANDLE_MASK);
|
||||||
return h != HARBOR && h !=0;
|
return h != HARBOR && h !=0;
|
||||||
}
|
}
|
||||||
@@ -43,16 +29,7 @@ skynet_harbor_init(int harbor) {
|
|||||||
HARBOR = (unsigned int)harbor << HANDLE_REMOTE_SHIFT;
|
HARBOR = (unsigned int)harbor << HANDLE_REMOTE_SHIFT;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
skynet_harbor_start(const char * master, const char *local) {
|
skynet_harbor_start(void *ctx) {
|
||||||
size_t sz = strlen(master) + strlen(local) + 32;
|
REMOTE = ctx;
|
||||||
char args[sz];
|
|
||||||
sprintf(args, "%s %s %d",master,local,HARBOR >> HANDLE_REMOTE_SHIFT);
|
|
||||||
struct skynet_context * inst = skynet_context_new("harbor",args);
|
|
||||||
if (inst == NULL) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
REMOTE = inst;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,9 +23,8 @@ struct remote_message {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session);
|
void skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session);
|
||||||
void skynet_harbor_register(struct remote_name *rname);
|
|
||||||
int skynet_harbor_message_isremote(uint32_t handle);
|
int skynet_harbor_message_isremote(uint32_t handle);
|
||||||
void skynet_harbor_init(int harbor);
|
void skynet_harbor_init(int harbor);
|
||||||
int skynet_harbor_start(const char * master, const char *local);
|
void skynet_harbor_start(void * ctx);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,14 +4,18 @@
|
|||||||
struct skynet_config {
|
struct skynet_config {
|
||||||
int thread;
|
int thread;
|
||||||
int harbor;
|
int harbor;
|
||||||
const char * logger;
|
const char * daemon;
|
||||||
const char * module_path;
|
const char * module_path;
|
||||||
const char * master;
|
const char * bootstrap;
|
||||||
const char * local;
|
const char * logger;
|
||||||
const char * start;
|
|
||||||
const char * standalone;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#define THREAD_WORKER 0
|
||||||
|
#define THREAD_MAIN 1
|
||||||
|
#define THREAD_SOCKET 2
|
||||||
|
#define THREAD_TIMER 3
|
||||||
|
#define THREAD_MONITOR 4
|
||||||
|
|
||||||
void skynet_start(struct skynet_config * config);
|
void skynet_start(struct skynet_config * config);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "skynet_imp.h"
|
#include "skynet_imp.h"
|
||||||
#include "skynet_env.h"
|
#include "skynet_env.h"
|
||||||
|
#include "skynet_server.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -10,6 +11,7 @@
|
|||||||
#include <lualib.h>
|
#include <lualib.h>
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
static int
|
static int
|
||||||
optint(const char *key, int opt) {
|
optint(const char *key, int opt) {
|
||||||
@@ -34,6 +36,7 @@ optboolean(const char *key, int opt) {
|
|||||||
return strcmp(str,"true")==0;
|
return strcmp(str,"true")==0;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
optstring(const char *key,const char * opt) {
|
optstring(const char *key,const char * opt) {
|
||||||
const char * str = skynet_getenv(key);
|
const char * str = skynet_getenv(key);
|
||||||
@@ -49,7 +52,6 @@ optstring(const char *key,const char * opt) {
|
|||||||
|
|
||||||
static void
|
static void
|
||||||
_init_env(lua_State *L) {
|
_init_env(lua_State *L) {
|
||||||
lua_pushglobaltable(L);
|
|
||||||
lua_pushnil(L); /* first key */
|
lua_pushnil(L); /* first key */
|
||||||
while (lua_next(L, -2) != 0) {
|
while (lua_next(L, -2) != 0) {
|
||||||
int keyt = lua_type(L, -2);
|
int keyt = lua_type(L, -2);
|
||||||
@@ -81,12 +83,26 @@ int sigign() {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char * load_config = "\
|
||||||
|
local config_name = ...\
|
||||||
|
local f = assert(io.open(config_name))\
|
||||||
|
local code = assert(f:read \'*a\')\
|
||||||
|
local function getenv(name) return assert(os.getenv(name), name) end\
|
||||||
|
code = string.gsub(code, \'%$([%w_%d]+)\', getenv)\
|
||||||
|
print(code)\
|
||||||
|
f:close()\
|
||||||
|
local result = {}\
|
||||||
|
assert(load(code,\'=(load)\',\'t\',result))()\
|
||||||
|
return result\
|
||||||
|
";
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[]) {
|
main(int argc, char *argv[]) {
|
||||||
const char * config_file = "config";
|
const char * config_file = "config";
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
config_file = argv[1];
|
config_file = argv[1];
|
||||||
}
|
}
|
||||||
|
skynet_globalinit();
|
||||||
skynet_env_init();
|
skynet_env_init();
|
||||||
|
|
||||||
sigign();
|
sigign();
|
||||||
@@ -95,11 +111,12 @@ main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
struct lua_State *L = lua_newstate(skynet_lalloc, NULL);
|
struct lua_State *L = lua_newstate(skynet_lalloc, NULL);
|
||||||
luaL_openlibs(L); // link lua lib
|
luaL_openlibs(L); // link lua lib
|
||||||
lua_close(L);
|
|
||||||
|
|
||||||
L = luaL_newstate();
|
int err = luaL_loadstring(L, load_config);
|
||||||
|
assert(err == LUA_OK);
|
||||||
|
lua_pushstring(L, config_file);
|
||||||
|
|
||||||
int err = luaL_dofile(L, config_file);
|
err = lua_pcall(L, 1, 1, 0);
|
||||||
if (err) {
|
if (err) {
|
||||||
fprintf(stderr,"%s\n",lua_tostring(L,-1));
|
fprintf(stderr,"%s\n",lua_tostring(L,-1));
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
@@ -107,30 +124,17 @@ main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
_init_env(L);
|
_init_env(L);
|
||||||
|
|
||||||
#ifdef LUA_CACHELIB
|
|
||||||
printf("Skynet lua code cache enable\n");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const char *path = optstring("lua_path","./lualib/?.lua;./lualib/?/init.lua");
|
|
||||||
setenv("LUA_PATH",path,1);
|
|
||||||
const char *cpath = optstring("lua_cpath","./luaclib/?.so");
|
|
||||||
setenv("LUA_CPATH",cpath,1);
|
|
||||||
optstring("luaservice","./service/?.lua");
|
|
||||||
|
|
||||||
config.thread = optint("thread",8);
|
config.thread = optint("thread",8);
|
||||||
config.module_path = optstring("cpath","./cservice/?.so");
|
config.module_path = optstring("cpath","./cservice/?.so");
|
||||||
config.logger = optstring("logger",NULL);
|
|
||||||
config.harbor = optint("harbor", 1);
|
config.harbor = optint("harbor", 1);
|
||||||
config.master = optstring("master","127.0.0.1:2012");
|
config.bootstrap = optstring("bootstrap","snlua bootstrap");
|
||||||
config.start = optstring("start","main.lua");
|
config.daemon = optstring("daemon", NULL);
|
||||||
config.local = optstring("address","127.0.0.1:2525");
|
config.logger = optstring("logger", NULL);
|
||||||
config.standalone = optstring("standalone",NULL);
|
|
||||||
|
|
||||||
lua_close(L);
|
lua_close(L);
|
||||||
|
|
||||||
skynet_start(&config);
|
skynet_start(&config);
|
||||||
|
skynet_globalexit();
|
||||||
printf("skynet exit\n");
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,11 @@
|
|||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#ifdef NOUSE_JEMALLOC
|
|
||||||
|
|
||||||
#define skynet_malloc malloc
|
#define skynet_malloc malloc
|
||||||
#define skynet_calloc calloc
|
#define skynet_calloc calloc
|
||||||
#define skynet_realloc realloc
|
#define skynet_realloc realloc
|
||||||
#define skynet_free free
|
#define skynet_free free
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void * skynet_malloc(size_t sz);
|
void * skynet_malloc(size_t sz);
|
||||||
void * skynet_calloc(size_t nmemb,size_t size);
|
void * skynet_calloc(size_t nmemb,size_t size);
|
||||||
void * skynet_realloc(void *ptr, size_t size);
|
void * skynet_realloc(void *ptr, size_t size);
|
||||||
|
|||||||
@@ -13,12 +13,8 @@
|
|||||||
|
|
||||||
// 0 means mq is not in global mq.
|
// 0 means mq is not in global mq.
|
||||||
// 1 means mq is in global mq , or the message is dispatching.
|
// 1 means mq is in global mq , or the message is dispatching.
|
||||||
// 2 means message is dispatching with locked session set.
|
|
||||||
// 3 means mq is not in global mq, and locked session has been set.
|
|
||||||
|
|
||||||
#define MQ_IN_GLOBAL 1
|
#define MQ_IN_GLOBAL 1
|
||||||
#define MQ_DISPATCHING 2
|
|
||||||
#define MQ_LOCKED 3
|
|
||||||
|
|
||||||
struct message_queue {
|
struct message_queue {
|
||||||
uint32_t handle;
|
uint32_t handle;
|
||||||
@@ -27,17 +23,18 @@ struct message_queue {
|
|||||||
int tail;
|
int tail;
|
||||||
int lock;
|
int lock;
|
||||||
int release;
|
int release;
|
||||||
int lock_session;
|
|
||||||
int in_global;
|
int in_global;
|
||||||
struct skynet_message *queue;
|
struct skynet_message *queue;
|
||||||
|
struct message_queue *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct global_queue {
|
struct global_queue {
|
||||||
uint32_t head;
|
uint32_t head;
|
||||||
uint32_t tail;
|
uint32_t tail;
|
||||||
struct message_queue ** queue;
|
struct message_queue ** queue;
|
||||||
bool * flag;
|
// We use a separated flag array to ensure the mq is pushed.
|
||||||
|
// See the comments below.
|
||||||
|
struct message_queue *list;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct global_queue *Q = NULL;
|
static struct global_queue *Q = NULL;
|
||||||
@@ -47,36 +44,61 @@ static struct global_queue *Q = NULL;
|
|||||||
|
|
||||||
#define GP(p) ((p) % MAX_GLOBAL_MQ)
|
#define GP(p) ((p) % MAX_GLOBAL_MQ)
|
||||||
|
|
||||||
static void
|
void
|
||||||
skynet_globalmq_push(struct message_queue * queue) {
|
skynet_globalmq_push(struct message_queue * queue) {
|
||||||
struct global_queue *q= Q;
|
struct global_queue *q= Q;
|
||||||
|
|
||||||
uint32_t tail = GP(__sync_fetch_and_add(&q->tail,1));
|
uint32_t tail = GP(__sync_fetch_and_add(&q->tail,1));
|
||||||
q->queue[tail] = queue;
|
|
||||||
__sync_synchronize();
|
// only one thread can set the slot (change q->queue[tail] from NULL to queue)
|
||||||
q->flag[tail] = true;
|
if (!__sync_bool_compare_and_swap(&q->queue[tail], NULL, queue)) {
|
||||||
|
// The queue may full seldom, save queue in list
|
||||||
|
assert(queue->next == NULL);
|
||||||
|
struct message_queue * last;
|
||||||
|
do {
|
||||||
|
last = q->list;
|
||||||
|
queue->next = last;
|
||||||
|
} while(!__sync_bool_compare_and_swap(&q->list, last, queue));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct message_queue *
|
struct message_queue *
|
||||||
skynet_globalmq_pop() {
|
skynet_globalmq_pop() {
|
||||||
struct global_queue *q = Q;
|
struct global_queue *q = Q;
|
||||||
uint32_t head = q->head;
|
uint32_t head = q->head;
|
||||||
|
|
||||||
|
if (head == q->tail) {
|
||||||
|
// The queue is empty.
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t head_ptr = GP(head);
|
uint32_t head_ptr = GP(head);
|
||||||
if (head_ptr == GP(q->tail)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!q->flag[head_ptr]) {
|
struct message_queue * list = q->list;
|
||||||
return NULL;
|
if (list) {
|
||||||
|
// If q->list is not empty, try to load it back to the queue
|
||||||
|
struct message_queue *newhead = list->next;
|
||||||
|
if (__sync_bool_compare_and_swap(&q->list, list, newhead)) {
|
||||||
|
// try load list only once, if success , push it back to the queue.
|
||||||
|
list->next = NULL;
|
||||||
|
skynet_globalmq_push(list);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
__sync_synchronize();
|
|
||||||
|
|
||||||
struct message_queue * mq = q->queue[head_ptr];
|
struct message_queue * mq = q->queue[head_ptr];
|
||||||
|
if (mq == NULL) {
|
||||||
|
// globalmq push not complete
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
if (!__sync_bool_compare_and_swap(&q->head, head, head+1)) {
|
if (!__sync_bool_compare_and_swap(&q->head, head, head+1)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
q->flag[head_ptr] = false;
|
// only one thread can get the slot (change q->queue[head_ptr] to NULL)
|
||||||
|
if (!__sync_bool_compare_and_swap(&q->queue[head_ptr], mq, NULL)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return mq;
|
return mq;
|
||||||
}
|
}
|
||||||
@@ -89,16 +111,20 @@ skynet_mq_create(uint32_t handle) {
|
|||||||
q->head = 0;
|
q->head = 0;
|
||||||
q->tail = 0;
|
q->tail = 0;
|
||||||
q->lock = 0;
|
q->lock = 0;
|
||||||
|
// When the queue is create (always between service create and service init) ,
|
||||||
|
// set in_global flag to avoid push it to global queue .
|
||||||
|
// If the service init success, skynet_context_new will call skynet_mq_force_push to push it to global queue.
|
||||||
q->in_global = MQ_IN_GLOBAL;
|
q->in_global = MQ_IN_GLOBAL;
|
||||||
q->release = 0;
|
q->release = 0;
|
||||||
q->lock_session = 0;
|
|
||||||
q->queue = skynet_malloc(sizeof(struct skynet_message) * q->cap);
|
q->queue = skynet_malloc(sizeof(struct skynet_message) * q->cap);
|
||||||
|
q->next = NULL;
|
||||||
|
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_release(struct message_queue *q) {
|
_release(struct message_queue *q) {
|
||||||
|
assert(q->next == NULL);
|
||||||
skynet_free(q->queue);
|
skynet_free(q->queue);
|
||||||
skynet_free(q);
|
skynet_free(q);
|
||||||
}
|
}
|
||||||
@@ -161,45 +187,11 @@ expand_queue(struct message_queue *q) {
|
|||||||
q->queue = new_queue;
|
q->queue = new_queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
_unlock(struct message_queue *q) {
|
|
||||||
// this api use in push a unlock message, so the in_global flags must not be 0 ,
|
|
||||||
// but the q is not exist in global queue.
|
|
||||||
if (q->in_global == MQ_LOCKED) {
|
|
||||||
skynet_globalmq_push(q);
|
|
||||||
q->in_global = MQ_IN_GLOBAL;
|
|
||||||
} else {
|
|
||||||
assert(q->in_global == MQ_DISPATCHING);
|
|
||||||
}
|
|
||||||
q->lock_session = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
_pushhead(struct message_queue *q, struct skynet_message *message) {
|
|
||||||
int head = q->head - 1;
|
|
||||||
if (head < 0) {
|
|
||||||
head = q->cap - 1;
|
|
||||||
}
|
|
||||||
if (head == q->tail) {
|
|
||||||
expand_queue(q);
|
|
||||||
--q->tail;
|
|
||||||
head = q->cap - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
q->queue[head] = *message;
|
|
||||||
q->head = head;
|
|
||||||
|
|
||||||
_unlock(q);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_mq_push(struct message_queue *q, struct skynet_message *message) {
|
skynet_mq_push(struct message_queue *q, struct skynet_message *message) {
|
||||||
assert(message);
|
assert(message);
|
||||||
LOCK(q)
|
LOCK(q)
|
||||||
|
|
||||||
if (q->lock_session !=0 && message->session == q->lock_session) {
|
|
||||||
_pushhead(q,message);
|
|
||||||
} else {
|
|
||||||
q->queue[q->tail] = *message;
|
q->queue[q->tail] = *message;
|
||||||
if (++ q->tail >= q->cap) {
|
if (++ q->tail >= q->cap) {
|
||||||
q->tail = 0;
|
q->tail = 0;
|
||||||
@@ -209,65 +201,23 @@ skynet_mq_push(struct message_queue *q, struct skynet_message *message) {
|
|||||||
expand_queue(q);
|
expand_queue(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (q->lock_session == 0) {
|
|
||||||
if (q->in_global == 0) {
|
if (q->in_global == 0) {
|
||||||
q->in_global = MQ_IN_GLOBAL;
|
q->in_global = MQ_IN_GLOBAL;
|
||||||
skynet_globalmq_push(q);
|
skynet_globalmq_push(q);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
UNLOCK(q)
|
UNLOCK(q)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
skynet_mq_lock(struct message_queue *q, int session) {
|
|
||||||
LOCK(q)
|
|
||||||
assert(q->lock_session == 0);
|
|
||||||
assert(q->in_global == MQ_IN_GLOBAL);
|
|
||||||
q->in_global = MQ_DISPATCHING;
|
|
||||||
q->lock_session = session;
|
|
||||||
UNLOCK(q)
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
skynet_mq_unlock(struct message_queue *q) {
|
|
||||||
LOCK(q)
|
|
||||||
_unlock(q);
|
|
||||||
UNLOCK(q)
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_mq_init() {
|
skynet_mq_init() {
|
||||||
struct global_queue *q = skynet_malloc(sizeof(*q));
|
struct global_queue *q = skynet_malloc(sizeof(*q));
|
||||||
memset(q,0,sizeof(*q));
|
memset(q,0,sizeof(*q));
|
||||||
q->queue = skynet_malloc(MAX_GLOBAL_MQ * sizeof(struct message_queue *));
|
q->queue = skynet_malloc(MAX_GLOBAL_MQ * sizeof(struct message_queue *));
|
||||||
q->flag = skynet_malloc(MAX_GLOBAL_MQ * sizeof(bool));
|
memset(q->queue, 0, sizeof(struct message_queue *) * MAX_GLOBAL_MQ);
|
||||||
memset(q->flag, 0, sizeof(bool) * MAX_GLOBAL_MQ);
|
|
||||||
Q=q;
|
Q=q;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
skynet_mq_force_push(struct message_queue * queue) {
|
|
||||||
assert(queue->in_global);
|
|
||||||
skynet_globalmq_push(queue);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
skynet_mq_pushglobal(struct message_queue *queue) {
|
|
||||||
LOCK(queue)
|
|
||||||
assert(queue->in_global);
|
|
||||||
if (queue->in_global == MQ_DISPATCHING) {
|
|
||||||
// lock message queue just now.
|
|
||||||
queue->in_global = MQ_LOCKED;
|
|
||||||
}
|
|
||||||
if (queue->lock_session == 0) {
|
|
||||||
skynet_globalmq_push(queue);
|
|
||||||
queue->in_global = MQ_IN_GLOBAL;
|
|
||||||
}
|
|
||||||
UNLOCK(queue)
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_mq_mark_release(struct message_queue *q) {
|
skynet_mq_mark_release(struct message_queue *q) {
|
||||||
LOCK(q)
|
LOCK(q)
|
||||||
@@ -279,31 +229,24 @@ skynet_mq_mark_release(struct message_queue *q) {
|
|||||||
UNLOCK(q)
|
UNLOCK(q)
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static void
|
||||||
_drop_queue(struct message_queue *q) {
|
_drop_queue(struct message_queue *q, message_drop drop_func, void *ud) {
|
||||||
// todo: send message back to message source
|
|
||||||
struct skynet_message msg;
|
struct skynet_message msg;
|
||||||
int s = 0;
|
|
||||||
while(!skynet_mq_pop(q, &msg)) {
|
while(!skynet_mq_pop(q, &msg)) {
|
||||||
++s;
|
drop_func(&msg, ud);
|
||||||
skynet_free(msg.data);
|
|
||||||
}
|
}
|
||||||
_release(q);
|
_release(q);
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
skynet_mq_release(struct message_queue *q) {
|
skynet_mq_release(struct message_queue *q, message_drop drop_func, void *ud) {
|
||||||
int ret = 0;
|
|
||||||
LOCK(q)
|
LOCK(q)
|
||||||
|
|
||||||
if (q->release) {
|
if (q->release) {
|
||||||
UNLOCK(q)
|
UNLOCK(q)
|
||||||
ret = _drop_queue(q);
|
_drop_queue(q, drop_func, ud);
|
||||||
} else {
|
} else {
|
||||||
skynet_mq_force_push(q);
|
skynet_globalmq_push(q);
|
||||||
UNLOCK(q)
|
UNLOCK(q)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,25 +13,24 @@ struct skynet_message {
|
|||||||
|
|
||||||
struct message_queue;
|
struct message_queue;
|
||||||
|
|
||||||
|
void skynet_globalmq_push(struct message_queue * queue);
|
||||||
struct message_queue * skynet_globalmq_pop(void);
|
struct message_queue * skynet_globalmq_pop(void);
|
||||||
|
|
||||||
struct message_queue * skynet_mq_create(uint32_t handle);
|
struct message_queue * skynet_mq_create(uint32_t handle);
|
||||||
void skynet_mq_mark_release(struct message_queue *q);
|
void skynet_mq_mark_release(struct message_queue *q);
|
||||||
int skynet_mq_release(struct message_queue *q);
|
|
||||||
|
typedef void (*message_drop)(struct skynet_message *, void *);
|
||||||
|
|
||||||
|
void skynet_mq_release(struct message_queue *q, message_drop drop_func, void *ud);
|
||||||
uint32_t skynet_mq_handle(struct message_queue *);
|
uint32_t skynet_mq_handle(struct message_queue *);
|
||||||
|
|
||||||
// 0 for success
|
// 0 for success
|
||||||
int skynet_mq_pop(struct message_queue *q, struct skynet_message *message);
|
int skynet_mq_pop(struct message_queue *q, struct skynet_message *message);
|
||||||
void skynet_mq_push(struct message_queue *q, struct skynet_message *message);
|
void skynet_mq_push(struct message_queue *q, struct skynet_message *message);
|
||||||
void skynet_mq_lock(struct message_queue *q, int session);
|
|
||||||
void skynet_mq_unlock(struct message_queue *q);
|
|
||||||
|
|
||||||
// return the length of message queue, for debug
|
// return the length of message queue, for debug
|
||||||
int skynet_mq_length(struct message_queue *q);
|
int skynet_mq_length(struct message_queue *q);
|
||||||
|
|
||||||
void skynet_mq_force_push(struct message_queue *q);
|
|
||||||
void skynet_mq_pushglobal(struct message_queue *q);
|
|
||||||
|
|
||||||
void skynet_mq_init();
|
void skynet_mq_init();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -8,6 +8,9 @@
|
|||||||
#include "skynet_harbor.h"
|
#include "skynet_harbor.h"
|
||||||
#include "skynet_env.h"
|
#include "skynet_env.h"
|
||||||
#include "skynet_monitor.h"
|
#include "skynet_monitor.h"
|
||||||
|
#include "skynet_imp.h"
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
@@ -49,11 +52,12 @@ struct skynet_context {
|
|||||||
|
|
||||||
struct skynet_node {
|
struct skynet_node {
|
||||||
int total;
|
int total;
|
||||||
|
int init;
|
||||||
uint32_t monitor_exit;
|
uint32_t monitor_exit;
|
||||||
|
pthread_key_t handle_key;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct skynet_node G_NODE = { 0,0 };
|
static struct skynet_node G_NODE;
|
||||||
static __thread uint32_t handle_tls = 0xffffffff;
|
|
||||||
|
|
||||||
int
|
int
|
||||||
skynet_context_total() {
|
skynet_context_total() {
|
||||||
@@ -61,22 +65,28 @@ skynet_context_total() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_context_inc() {
|
context_inc() {
|
||||||
__sync_fetch_and_add(&G_NODE.total,1);
|
__sync_fetch_and_add(&G_NODE.total,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_context_dec() {
|
context_dec() {
|
||||||
__sync_fetch_and_sub(&G_NODE.total,1);
|
__sync_fetch_and_sub(&G_NODE.total,1);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t
|
uint32_t
|
||||||
skynet_current_handle(void) {
|
skynet_current_handle(void) {
|
||||||
return handle_tls;
|
if (G_NODE.init) {
|
||||||
|
void * handle = pthread_getspecific(G_NODE.handle_key);
|
||||||
|
return (uint32_t)(uintptr_t)handle;
|
||||||
|
} else {
|
||||||
|
uintptr_t v = (uint32_t)(-THREAD_MAIN);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_id_to_hex(char * str, uint32_t id) {
|
id_to_hex(char * str, uint32_t id) {
|
||||||
int i;
|
int i;
|
||||||
static char hex[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
|
static char hex[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
|
||||||
str[0] = ':';
|
str[0] = ':';
|
||||||
@@ -86,6 +96,20 @@ _id_to_hex(char * str, uint32_t id) {
|
|||||||
str[9] = '\0';
|
str[9] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct drop_t {
|
||||||
|
uint32_t handle;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
drop_message(struct skynet_message *msg, void *ud) {
|
||||||
|
struct drop_t *d = ud;
|
||||||
|
skynet_free(msg->data);
|
||||||
|
uint32_t source = d->handle;
|
||||||
|
assert(source);
|
||||||
|
// report error to the message source
|
||||||
|
skynet_send(NULL, source, msg->source, PTYPE_ERROR, 0, NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
struct skynet_context *
|
struct skynet_context *
|
||||||
skynet_context_new(const char * name, const char *param) {
|
skynet_context_new(const char * name, const char *param) {
|
||||||
struct skynet_module * mod = skynet_module_query(name);
|
struct skynet_module * mod = skynet_module_query(name);
|
||||||
@@ -111,7 +135,7 @@ skynet_context_new(const char * name, const char *param) {
|
|||||||
ctx->handle = skynet_handle_register(ctx);
|
ctx->handle = skynet_handle_register(ctx);
|
||||||
struct message_queue * queue = ctx->queue = skynet_mq_create(ctx->handle);
|
struct message_queue * queue = ctx->queue = skynet_mq_create(ctx->handle);
|
||||||
// init function maybe use ctx->handle, so it must init at last
|
// init function maybe use ctx->handle, so it must init at last
|
||||||
_context_inc();
|
context_inc();
|
||||||
|
|
||||||
CHECKCALLING_BEGIN(ctx)
|
CHECKCALLING_BEGIN(ctx)
|
||||||
int r = skynet_module_instance_init(mod, inst, ctx, param);
|
int r = skynet_module_instance_init(mod, inst, ctx, param);
|
||||||
@@ -121,16 +145,18 @@ skynet_context_new(const char * name, const char *param) {
|
|||||||
if (ret) {
|
if (ret) {
|
||||||
ctx->init = true;
|
ctx->init = true;
|
||||||
}
|
}
|
||||||
skynet_mq_force_push(queue);
|
skynet_globalmq_push(queue);
|
||||||
if (ret) {
|
if (ret) {
|
||||||
skynet_error(ret, "LAUNCH %s %s", name, param ? param : "");
|
skynet_error(ret, "LAUNCH %s %s", name, param ? param : "");
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
} else {
|
} else {
|
||||||
skynet_error(ctx, "FAILED launch %s", name);
|
skynet_error(ctx, "FAILED launch %s", name);
|
||||||
|
uint32_t handle = ctx->handle;
|
||||||
skynet_context_release(ctx);
|
skynet_context_release(ctx);
|
||||||
skynet_handle_retire(ctx->handle);
|
skynet_handle_retire(handle);
|
||||||
skynet_mq_release(queue);
|
struct drop_t d = { handle };
|
||||||
|
skynet_mq_release(queue, drop_message, &d);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -148,17 +174,17 @@ skynet_context_grab(struct skynet_context *ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_delete_context(struct skynet_context *ctx) {
|
delete_context(struct skynet_context *ctx) {
|
||||||
skynet_module_instance_release(ctx->mod, ctx->instance);
|
skynet_module_instance_release(ctx->mod, ctx->instance);
|
||||||
skynet_mq_mark_release(ctx->queue);
|
skynet_mq_mark_release(ctx->queue);
|
||||||
skynet_free(ctx);
|
skynet_free(ctx);
|
||||||
_context_dec();
|
context_dec();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct skynet_context *
|
struct skynet_context *
|
||||||
skynet_context_release(struct skynet_context *ctx) {
|
skynet_context_release(struct skynet_context *ctx) {
|
||||||
if (__sync_sub_and_fetch(&ctx->ref,1) == 0) {
|
if (__sync_sub_and_fetch(&ctx->ref,1) == 0) {
|
||||||
_delete_context(ctx);
|
delete_context(ctx);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return ctx;
|
return ctx;
|
||||||
@@ -199,59 +225,80 @@ static void
|
|||||||
_dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) {
|
_dispatch_message(struct skynet_context *ctx, struct skynet_message *msg) {
|
||||||
assert(ctx->init);
|
assert(ctx->init);
|
||||||
CHECKCALLING_BEGIN(ctx)
|
CHECKCALLING_BEGIN(ctx)
|
||||||
handle_tls = ctx->handle;
|
pthread_setspecific(G_NODE.handle_key, (void *)(uintptr_t)(ctx->handle));
|
||||||
int type = msg->sz >> HANDLE_REMOTE_SHIFT;
|
int type = msg->sz >> HANDLE_REMOTE_SHIFT;
|
||||||
size_t sz = msg->sz & HANDLE_MASK;
|
size_t sz = msg->sz & HANDLE_MASK;
|
||||||
if (!ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz)) {
|
if (!ctx->cb(ctx, ctx->cb_ud, type, msg->session, msg->source, msg->data, sz)) {
|
||||||
skynet_free(msg->data);
|
skynet_free(msg->data);
|
||||||
}
|
}
|
||||||
handle_tls = 0xffffffff;
|
|
||||||
CHECKCALLING_END(ctx)
|
CHECKCALLING_END(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
void
|
||||||
skynet_context_message_dispatch(struct skynet_monitor *sm) {
|
skynet_context_dispatchall(struct skynet_context * ctx) {
|
||||||
struct message_queue * q = skynet_globalmq_pop();
|
// for skynet_error
|
||||||
|
struct skynet_message msg;
|
||||||
|
struct message_queue *q = ctx->queue;
|
||||||
|
while (!skynet_mq_pop(q,&msg)) {
|
||||||
|
_dispatch_message(ctx, &msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct message_queue *
|
||||||
|
skynet_context_message_dispatch(struct skynet_monitor *sm, struct message_queue *q, int weight) {
|
||||||
|
if (q == NULL) {
|
||||||
|
q = skynet_globalmq_pop();
|
||||||
if (q==NULL)
|
if (q==NULL)
|
||||||
return 1;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t handle = skynet_mq_handle(q);
|
uint32_t handle = skynet_mq_handle(q);
|
||||||
|
|
||||||
struct skynet_context * ctx = skynet_handle_grab(handle);
|
struct skynet_context * ctx = skynet_handle_grab(handle);
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
int s = skynet_mq_release(q);
|
struct drop_t d = { handle };
|
||||||
if (s>0) {
|
skynet_mq_release(q, drop_message, &d);
|
||||||
skynet_error(NULL, "Drop message queue %x (%d messages)", handle,s);
|
return skynet_globalmq_pop();
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int i,n=1;
|
||||||
struct skynet_message msg;
|
struct skynet_message msg;
|
||||||
|
|
||||||
|
for (i=0;i<n;i++) {
|
||||||
if (skynet_mq_pop(q,&msg)) {
|
if (skynet_mq_pop(q,&msg)) {
|
||||||
skynet_context_release(ctx);
|
skynet_context_release(ctx);
|
||||||
return 0;
|
return skynet_globalmq_pop();
|
||||||
|
} else if (i==0 && weight >= 0) {
|
||||||
|
n = skynet_mq_length(q);
|
||||||
|
n >>= weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
skynet_monitor_trigger(sm, msg.source , handle);
|
skynet_monitor_trigger(sm, msg.source , handle);
|
||||||
|
|
||||||
if (ctx->cb == NULL) {
|
if (ctx->cb == NULL) {
|
||||||
skynet_free(msg.data);
|
skynet_free(msg.data);
|
||||||
skynet_error(NULL, "Drop message from %x to %x without callback , size = %d",msg.source, handle, (int)msg.sz);
|
|
||||||
} else {
|
} else {
|
||||||
_dispatch_message(ctx, &msg);
|
_dispatch_message(ctx, &msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
skynet_monitor_trigger(sm, 0,0);
|
||||||
|
}
|
||||||
|
|
||||||
assert(q == ctx->queue);
|
assert(q == ctx->queue);
|
||||||
skynet_mq_pushglobal(q);
|
struct message_queue *nq = skynet_globalmq_pop();
|
||||||
|
if (nq) {
|
||||||
|
// If global mq is not empty , push q back, and return next queue (nq)
|
||||||
|
// Else (global mq is empty or block, don't push q back, and return q again (for next dispatch)
|
||||||
|
skynet_globalmq_push(q);
|
||||||
|
q = nq;
|
||||||
|
}
|
||||||
skynet_context_release(ctx);
|
skynet_context_release(ctx);
|
||||||
|
|
||||||
skynet_monitor_trigger(sm, 0,0);
|
return q;
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
_copy_name(char name[GLOBALNAME_LENGTH], const char * addr) {
|
copy_name(char name[GLOBALNAME_LENGTH], const char * addr) {
|
||||||
int i;
|
int i;
|
||||||
for (i=0;i<GLOBALNAME_LENGTH && addr[i];i++) {
|
for (i=0;i<GLOBALNAME_LENGTH && addr[i];i++) {
|
||||||
name[i] = addr[i];
|
name[i] = addr[i];
|
||||||
@@ -287,59 +334,50 @@ handle_exit(struct skynet_context * context, uint32_t handle) {
|
|||||||
skynet_handle_retire(handle);
|
skynet_handle_retire(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
// skynet command
|
||||||
skynet_command(struct skynet_context * context, const char * cmd , const char * param) {
|
|
||||||
if (strcmp(cmd,"TIMEOUT") == 0) {
|
struct command_func {
|
||||||
|
const char *name;
|
||||||
|
const char * (*func)(struct skynet_context * context, const char * param);
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
cmd_timeout(struct skynet_context * context, const char * param) {
|
||||||
char * session_ptr = NULL;
|
char * session_ptr = NULL;
|
||||||
int ti = strtol(param, &session_ptr, 10);
|
int ti = strtol(param, &session_ptr, 10);
|
||||||
int session = skynet_context_newsession(context);
|
int session = skynet_context_newsession(context);
|
||||||
skynet_timeout(context->handle, ti, session);
|
skynet_timeout(context->handle, ti, session);
|
||||||
sprintf(context->result, "%d", session);
|
sprintf(context->result, "%d", session);
|
||||||
return context->result;
|
return context->result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"LOCK") == 0) {
|
static const char *
|
||||||
if (context->init == false) {
|
cmd_reg(struct skynet_context * context, const char * param) {
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
skynet_mq_lock(context->queue, context->session_id+1);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(cmd,"UNLOCK") == 0) {
|
|
||||||
if (context->init == false) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
skynet_mq_unlock(context->queue);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strcmp(cmd,"REG") == 0) {
|
|
||||||
if (param == NULL || param[0] == '\0') {
|
if (param == NULL || param[0] == '\0') {
|
||||||
sprintf(context->result, ":%x", context->handle);
|
sprintf(context->result, ":%x", context->handle);
|
||||||
return context->result;
|
return context->result;
|
||||||
} else if (param[0] == '.') {
|
} else if (param[0] == '.') {
|
||||||
return skynet_handle_namehandle(context->handle, param + 1);
|
return skynet_handle_namehandle(context->handle, param + 1);
|
||||||
} else {
|
} else {
|
||||||
assert(context->handle!=0);
|
skynet_error(context, "Can't register global name %s in C", param);
|
||||||
struct remote_name *rname = skynet_malloc(sizeof(*rname));
|
|
||||||
_copy_name(rname->name, param);
|
|
||||||
rname->handle = context->handle;
|
|
||||||
skynet_harbor_register(rname);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"QUERY") == 0) {
|
static const char *
|
||||||
|
cmd_query(struct skynet_context * context, const char * param) {
|
||||||
if (param[0] == '.') {
|
if (param[0] == '.') {
|
||||||
uint32_t handle = skynet_handle_findname(param+1);
|
uint32_t handle = skynet_handle_findname(param+1);
|
||||||
|
if (handle) {
|
||||||
sprintf(context->result, ":%x", handle);
|
sprintf(context->result, ":%x", handle);
|
||||||
return context->result;
|
return context->result;
|
||||||
}
|
}
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"NAME") == 0) {
|
static const char *
|
||||||
|
cmd_name(struct skynet_context * context, const char * param) {
|
||||||
int size = strlen(param);
|
int size = strlen(param);
|
||||||
char name[size+1];
|
char name[size+1];
|
||||||
char handle[size+1];
|
char handle[size+1];
|
||||||
@@ -354,26 +392,26 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
|
|||||||
if (name[0] == '.') {
|
if (name[0] == '.') {
|
||||||
return skynet_handle_namehandle(handle_id, name + 1);
|
return skynet_handle_namehandle(handle_id, name + 1);
|
||||||
} else {
|
} else {
|
||||||
struct remote_name *rname = skynet_malloc(sizeof(*rname));
|
skynet_error(context, "Can't set global name %s in C", name);
|
||||||
_copy_name(rname->name, name);
|
|
||||||
rname->handle = handle_id;
|
|
||||||
skynet_harbor_register(rname);
|
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"NOW") == 0) {
|
static const char *
|
||||||
|
cmd_now(struct skynet_context * context, const char * param) {
|
||||||
uint32_t ti = skynet_gettime();
|
uint32_t ti = skynet_gettime();
|
||||||
sprintf(context->result,"%u",ti);
|
sprintf(context->result,"%u",ti);
|
||||||
return context->result;
|
return context->result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"EXIT") == 0) {
|
static const char *
|
||||||
|
cmd_exit(struct skynet_context * context, const char * param) {
|
||||||
handle_exit(context, 0);
|
handle_exit(context, 0);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"KILL") == 0) {
|
static const char *
|
||||||
|
cmd_kill(struct skynet_context * context, const char * param) {
|
||||||
uint32_t handle = 0;
|
uint32_t handle = 0;
|
||||||
if (param[0] == ':') {
|
if (param[0] == ':') {
|
||||||
handle = strtoul(param+1, NULL, 16);
|
handle = strtoul(param+1, NULL, 16);
|
||||||
@@ -387,9 +425,10 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
|
|||||||
handle_exit(context, handle);
|
handle_exit(context, handle);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"LAUNCH") == 0) {
|
static const char *
|
||||||
|
cmd_launch(struct skynet_context * context, const char * param) {
|
||||||
size_t sz = strlen(param);
|
size_t sz = strlen(param);
|
||||||
char tmp[sz+1];
|
char tmp[sz+1];
|
||||||
strcpy(tmp,param);
|
strcpy(tmp,param);
|
||||||
@@ -400,16 +439,18 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
|
|||||||
if (inst == NULL) {
|
if (inst == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
_id_to_hex(context->result, inst->handle);
|
id_to_hex(context->result, inst->handle);
|
||||||
return context->result;
|
return context->result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"GETENV") == 0) {
|
static const char *
|
||||||
|
cmd_getenv(struct skynet_context * context, const char * param) {
|
||||||
return skynet_getenv(param);
|
return skynet_getenv(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"SETENV") == 0) {
|
static const char *
|
||||||
|
cmd_setenv(struct skynet_context * context, const char * param) {
|
||||||
size_t sz = strlen(param);
|
size_t sz = strlen(param);
|
||||||
char key[sz+1];
|
char key[sz+1];
|
||||||
int i;
|
int i;
|
||||||
@@ -424,29 +465,33 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
|
|||||||
|
|
||||||
skynet_setenv(key,param);
|
skynet_setenv(key,param);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"STARTTIME") == 0) {
|
static const char *
|
||||||
|
cmd_starttime(struct skynet_context * context, const char * param) {
|
||||||
uint32_t sec = skynet_gettime_fixsec();
|
uint32_t sec = skynet_gettime_fixsec();
|
||||||
sprintf(context->result,"%u",sec);
|
sprintf(context->result,"%u",sec);
|
||||||
return context->result;
|
return context->result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"ENDLESS") == 0) {
|
static const char *
|
||||||
|
cmd_endless(struct skynet_context * context, const char * param) {
|
||||||
if (context->endless) {
|
if (context->endless) {
|
||||||
strcpy(context->result, "1");
|
strcpy(context->result, "1");
|
||||||
context->endless = false;
|
context->endless = false;
|
||||||
return context->result;
|
return context->result;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"ABORT") == 0) {
|
static const char *
|
||||||
|
cmd_abort(struct skynet_context * context, const char * param) {
|
||||||
skynet_handle_retireall();
|
skynet_handle_retireall();
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd,"MONITOR") == 0) {
|
static const char *
|
||||||
|
cmd_monitor(struct skynet_context * context, const char * param) {
|
||||||
uint32_t handle=0;
|
uint32_t handle=0;
|
||||||
if (param == NULL || param[0] == '\0') {
|
if (param == NULL || param[0] == '\0') {
|
||||||
if (G_NODE.monitor_exit) {
|
if (G_NODE.monitor_exit) {
|
||||||
@@ -467,12 +512,42 @@ skynet_command(struct skynet_context * context, const char * cmd , const char *
|
|||||||
}
|
}
|
||||||
G_NODE.monitor_exit = handle;
|
G_NODE.monitor_exit = handle;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(cmd, "MQLEN") == 0) {
|
static const char *
|
||||||
|
cmd_mqlen(struct skynet_context * context, const char * param) {
|
||||||
int len = skynet_mq_length(context->queue);
|
int len = skynet_mq_length(context->queue);
|
||||||
sprintf(context->result, "%d", len);
|
sprintf(context->result, "%d", len);
|
||||||
return context->result;
|
return context->result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct command_func cmd_funcs[] = {
|
||||||
|
{ "TIMEOUT", cmd_timeout },
|
||||||
|
{ "REG", cmd_reg },
|
||||||
|
{ "QUERY", cmd_query },
|
||||||
|
{ "NAME", cmd_name },
|
||||||
|
{ "NOW", cmd_now },
|
||||||
|
{ "EXIT", cmd_exit },
|
||||||
|
{ "KILL", cmd_kill },
|
||||||
|
{ "LAUNCH", cmd_launch },
|
||||||
|
{ "GETENV", cmd_getenv },
|
||||||
|
{ "SETENV", cmd_setenv },
|
||||||
|
{ "STARTTIME", cmd_starttime },
|
||||||
|
{ "ENDLESS", cmd_endless },
|
||||||
|
{ "ABORT", cmd_abort },
|
||||||
|
{ "MONITOR", cmd_monitor },
|
||||||
|
{ "MQLEN", cmd_mqlen },
|
||||||
|
{ NULL, NULL },
|
||||||
|
};
|
||||||
|
|
||||||
|
const char *
|
||||||
|
skynet_command(struct skynet_context * context, const char * cmd , const char * param) {
|
||||||
|
struct command_func * method = &cmd_funcs[0];
|
||||||
|
while(method->name) {
|
||||||
|
if (strcmp(cmd, method->name) == 0) {
|
||||||
|
return method->func(context, param);
|
||||||
|
}
|
||||||
|
++method;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -496,12 +571,16 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da
|
|||||||
*data = msg;
|
*data = msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert((*sz & HANDLE_MASK) == *sz);
|
|
||||||
*sz |= type << HANDLE_REMOTE_SHIFT;
|
*sz |= type << HANDLE_REMOTE_SHIFT;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * data, size_t sz) {
|
skynet_send(struct skynet_context * context, uint32_t source, uint32_t destination , int type, int session, void * data, size_t sz) {
|
||||||
|
if ((sz & HANDLE_MASK) != sz) {
|
||||||
|
skynet_error(context, "The message to %x is too large (sz = %lu)", destination, sz);
|
||||||
|
skynet_free(data);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
_filter_args(context, type, &session, (void **)&data, &sz);
|
_filter_args(context, type, &session, (void **)&data, &sz);
|
||||||
|
|
||||||
if (source == 0) {
|
if (source == 0) {
|
||||||
@@ -526,7 +605,6 @@ skynet_send(struct skynet_context * context, uint32_t source, uint32_t destinati
|
|||||||
|
|
||||||
if (skynet_context_push(destination, &smsg)) {
|
if (skynet_context_push(destination, &smsg)) {
|
||||||
skynet_free(data);
|
skynet_free(data);
|
||||||
skynet_error(NULL, "Drop message from %x to %x (type=%d)(size=%d)", source, destination, type&0xff, (int)(sz & HANDLE_MASK));
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -545,14 +623,13 @@ skynet_sendname(struct skynet_context * context, const char * addr , int type, i
|
|||||||
if (type & PTYPE_TAG_DONTCOPY) {
|
if (type & PTYPE_TAG_DONTCOPY) {
|
||||||
skynet_free(data);
|
skynet_free(data);
|
||||||
}
|
}
|
||||||
skynet_error(context, "Drop message to %s", addr);
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_filter_args(context, type, &session, (void **)&data, &sz);
|
_filter_args(context, type, &session, (void **)&data, &sz);
|
||||||
|
|
||||||
struct remote_message * rmsg = skynet_malloc(sizeof(*rmsg));
|
struct remote_message * rmsg = skynet_malloc(sizeof(*rmsg));
|
||||||
_copy_name(rmsg->destination.name, addr);
|
copy_name(rmsg->destination.name, addr);
|
||||||
rmsg->destination.handle = 0;
|
rmsg->destination.handle = 0;
|
||||||
rmsg->message = data;
|
rmsg->message = data;
|
||||||
rmsg->sz = sz;
|
rmsg->sz = sz;
|
||||||
@@ -590,3 +667,28 @@ skynet_context_send(struct skynet_context * ctx, void * msg, size_t sz, uint32_t
|
|||||||
|
|
||||||
skynet_mq_push(ctx->queue, &smsg);
|
skynet_mq_push(ctx->queue, &smsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
skynet_globalinit(void) {
|
||||||
|
G_NODE.total = 0;
|
||||||
|
G_NODE.monitor_exit = 0;
|
||||||
|
G_NODE.init = 1;
|
||||||
|
if (pthread_key_create(&G_NODE.handle_key, NULL)) {
|
||||||
|
fprintf(stderr, "pthread_key_create failed");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
// set mainthread's key
|
||||||
|
skynet_initthread(THREAD_MAIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
skynet_globalexit(void) {
|
||||||
|
pthread_key_delete(G_NODE.handle_key);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
skynet_initthread(int m) {
|
||||||
|
uintptr_t v = (uint32_t)(-m);
|
||||||
|
pthread_setspecific(G_NODE.handle_key, (void *)v);
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,9 +16,14 @@ void skynet_context_init(struct skynet_context *, uint32_t handle);
|
|||||||
int skynet_context_push(uint32_t handle, struct skynet_message *message);
|
int skynet_context_push(uint32_t handle, struct skynet_message *message);
|
||||||
void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, uint32_t source, int type, int session);
|
void skynet_context_send(struct skynet_context * context, void * msg, size_t sz, uint32_t source, int type, int session);
|
||||||
int skynet_context_newsession(struct skynet_context *);
|
int skynet_context_newsession(struct skynet_context *);
|
||||||
int skynet_context_message_dispatch(struct skynet_monitor *); // return 1 when block
|
struct message_queue * skynet_context_message_dispatch(struct skynet_monitor *, struct message_queue *, int weight); // return next queue
|
||||||
int skynet_context_total();
|
int skynet_context_total();
|
||||||
|
void skynet_context_dispatchall(struct skynet_context * context); // for skynet_error output before exit
|
||||||
|
|
||||||
void skynet_context_endless(uint32_t handle); // for monitor
|
void skynet_context_endless(uint32_t handle); // for monitor
|
||||||
|
|
||||||
|
void skynet_globalinit(void);
|
||||||
|
void skynet_globalexit(void);
|
||||||
|
void skynet_initthread(int m);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -133,12 +133,6 @@ skynet_socket_connect(struct skynet_context *ctx, const char *host, int port) {
|
|||||||
return socket_server_connect(SOCKET_SERVER, source, host, port);
|
return socket_server_connect(SOCKET_SERVER, source, host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
skynet_socket_block_connect(struct skynet_context *ctx, const char *host, int port) {
|
|
||||||
uint32_t source = skynet_context_handle(ctx);
|
|
||||||
return socket_server_block_connect(SOCKET_SERVER, source, host, port);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
skynet_socket_bind(struct skynet_context *ctx, int fd) {
|
skynet_socket_bind(struct skynet_context *ctx, int fd) {
|
||||||
uint32_t source = skynet_context_handle(ctx);
|
uint32_t source = skynet_context_handle(ctx);
|
||||||
@@ -156,3 +150,8 @@ skynet_socket_start(struct skynet_context *ctx, int id) {
|
|||||||
uint32_t source = skynet_context_handle(ctx);
|
uint32_t source = skynet_context_handle(ctx);
|
||||||
socket_server_start(SOCKET_SERVER, source, id);
|
socket_server_start(SOCKET_SERVER, source, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
skynet_socket_nodelay(struct skynet_context *ctx, int id) {
|
||||||
|
socket_server_nodelay(SOCKET_SERVER, id);
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ int skynet_socket_send(struct skynet_context *ctx, int id, void *buffer, int sz)
|
|||||||
void skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz);
|
void skynet_socket_send_lowpriority(struct skynet_context *ctx, int id, void *buffer, int sz);
|
||||||
int skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog);
|
int skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog);
|
||||||
int skynet_socket_connect(struct skynet_context *ctx, const char *host, int port);
|
int skynet_socket_connect(struct skynet_context *ctx, const char *host, int port);
|
||||||
int skynet_socket_block_connect(struct skynet_context *ctx, const char *host, int port);
|
|
||||||
int skynet_socket_bind(struct skynet_context *ctx, int fd);
|
int skynet_socket_bind(struct skynet_context *ctx, int fd);
|
||||||
void skynet_socket_close(struct skynet_context *ctx, int id);
|
void skynet_socket_close(struct skynet_context *ctx, int id);
|
||||||
void skynet_socket_start(struct skynet_context *ctx, int id);
|
void skynet_socket_start(struct skynet_context *ctx, int id);
|
||||||
|
void skynet_socket_nodelay(struct skynet_context *ctx, int id);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,9 +5,9 @@
|
|||||||
#include "skynet_handle.h"
|
#include "skynet_handle.h"
|
||||||
#include "skynet_module.h"
|
#include "skynet_module.h"
|
||||||
#include "skynet_timer.h"
|
#include "skynet_timer.h"
|
||||||
#include "skynet_harbor.h"
|
|
||||||
#include "skynet_monitor.h"
|
#include "skynet_monitor.h"
|
||||||
#include "skynet_socket.h"
|
#include "skynet_socket.h"
|
||||||
|
#include "skynet_daemon.h"
|
||||||
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@@ -27,6 +27,7 @@ struct monitor {
|
|||||||
struct worker_parm {
|
struct worker_parm {
|
||||||
struct monitor *m;
|
struct monitor *m;
|
||||||
int id;
|
int id;
|
||||||
|
int weight;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define CHECK_ABORT if (skynet_context_total()==0) break;
|
#define CHECK_ABORT if (skynet_context_total()==0) break;
|
||||||
@@ -50,6 +51,7 @@ wakeup(struct monitor *m, int busy) {
|
|||||||
static void *
|
static void *
|
||||||
_socket(void *p) {
|
_socket(void *p) {
|
||||||
struct monitor * m = p;
|
struct monitor * m = p;
|
||||||
|
skynet_initthread(THREAD_SOCKET);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int r = skynet_socket_poll();
|
int r = skynet_socket_poll();
|
||||||
if (r==0)
|
if (r==0)
|
||||||
@@ -81,6 +83,7 @@ _monitor(void *p) {
|
|||||||
struct monitor * m = p;
|
struct monitor * m = p;
|
||||||
int i;
|
int i;
|
||||||
int n = m->count;
|
int n = m->count;
|
||||||
|
skynet_initthread(THREAD_MONITOR);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
CHECK_ABORT
|
CHECK_ABORT
|
||||||
for (i=0;i<n;i++) {
|
for (i=0;i<n;i++) {
|
||||||
@@ -98,6 +101,7 @@ _monitor(void *p) {
|
|||||||
static void *
|
static void *
|
||||||
_timer(void *p) {
|
_timer(void *p) {
|
||||||
struct monitor * m = p;
|
struct monitor * m = p;
|
||||||
|
skynet_initthread(THREAD_TIMER);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
skynet_updatetime();
|
skynet_updatetime();
|
||||||
CHECK_ABORT
|
CHECK_ABORT
|
||||||
@@ -115,10 +119,14 @@ static void *
|
|||||||
_worker(void *p) {
|
_worker(void *p) {
|
||||||
struct worker_parm *wp = p;
|
struct worker_parm *wp = p;
|
||||||
int id = wp->id;
|
int id = wp->id;
|
||||||
|
int weight = wp->weight;
|
||||||
struct monitor *m = wp->m;
|
struct monitor *m = wp->m;
|
||||||
struct skynet_monitor *sm = m->m[id];
|
struct skynet_monitor *sm = m->m[id];
|
||||||
|
skynet_initthread(THREAD_WORKER);
|
||||||
|
struct message_queue * q = NULL;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (skynet_context_message_dispatch(sm)) {
|
q = skynet_context_message_dispatch(sm, q, weight);
|
||||||
|
if (q == NULL) {
|
||||||
CHECK_ABORT
|
CHECK_ABORT
|
||||||
if (pthread_mutex_lock(&m->mutex) == 0) {
|
if (pthread_mutex_lock(&m->mutex) == 0) {
|
||||||
++ m->sleep;
|
++ m->sleep;
|
||||||
@@ -163,10 +171,20 @@ _start(int thread) {
|
|||||||
create_thread(&pid[1], _timer, m);
|
create_thread(&pid[1], _timer, m);
|
||||||
create_thread(&pid[2], _socket, m);
|
create_thread(&pid[2], _socket, m);
|
||||||
|
|
||||||
|
static int weight[] = {
|
||||||
|
-1, -1, -1, -1, 0, 0, 0, 0,
|
||||||
|
1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
|
2, 2, 2, 2, 2, 2, 2, 2,
|
||||||
|
3, 3, 3, 3, 3, 3, 3, 3, };
|
||||||
struct worker_parm wp[thread];
|
struct worker_parm wp[thread];
|
||||||
for (i=0;i<thread;i++) {
|
for (i=0;i<thread;i++) {
|
||||||
wp[i].m = m;
|
wp[i].m = m;
|
||||||
wp[i].id = i;
|
wp[i].id = i;
|
||||||
|
if (i < sizeof(weight)/sizeof(weight[0])) {
|
||||||
|
wp[i].weight= weight[i];
|
||||||
|
} else {
|
||||||
|
wp[i].weight = 0;
|
||||||
|
}
|
||||||
create_thread(&pid[i+3], _worker, &wp[i]);
|
create_thread(&pid[i+3], _worker, &wp[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,16 +195,27 @@ _start(int thread) {
|
|||||||
free_monitor(m);
|
free_monitor(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static void
|
||||||
_start_master(const char * master) {
|
bootstrap(struct skynet_context * logger, const char * cmdline) {
|
||||||
struct skynet_context *ctx = skynet_context_new("master", master);
|
int sz = strlen(cmdline);
|
||||||
if (ctx == NULL)
|
char name[sz+1];
|
||||||
return 1;
|
char args[sz+1];
|
||||||
return 0;
|
sscanf(cmdline, "%s %s", name, args);
|
||||||
|
struct skynet_context *ctx = skynet_context_new(name, args);
|
||||||
|
if (ctx == NULL) {
|
||||||
|
skynet_error(NULL, "Bootstrap error : %s\n", cmdline);
|
||||||
|
skynet_context_dispatchall(logger);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
skynet_start(struct skynet_config * config) {
|
skynet_start(struct skynet_config * config) {
|
||||||
|
if (config->daemon) {
|
||||||
|
if (daemon_init(config->daemon)) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
skynet_harbor_init(config->harbor);
|
skynet_harbor_init(config->harbor);
|
||||||
skynet_handle_init(config->harbor);
|
skynet_handle_init(config->harbor);
|
||||||
skynet_mq_init();
|
skynet_mq_init();
|
||||||
@@ -194,32 +223,17 @@ skynet_start(struct skynet_config * config) {
|
|||||||
skynet_timer_init();
|
skynet_timer_init();
|
||||||
skynet_socket_init();
|
skynet_socket_init();
|
||||||
|
|
||||||
struct skynet_context *ctx;
|
struct skynet_context *ctx = skynet_context_new("logger", config->logger);
|
||||||
ctx = skynet_context_new("logger", config->logger);
|
|
||||||
if (ctx == NULL) {
|
if (ctx == NULL) {
|
||||||
fprintf(stderr,"launch logger error");
|
fprintf(stderr, "Can't launch logger service\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (config->standalone) {
|
bootstrap(ctx, config->bootstrap);
|
||||||
if (_start_master(config->standalone)) {
|
|
||||||
fprintf(stderr, "Init fail : mater");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// harbor must be init first
|
|
||||||
if (skynet_harbor_start(config->master , config->local)) {
|
|
||||||
fprintf(stderr, "Init fail : no master");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx = skynet_context_new("snlua", "launcher");
|
|
||||||
if (ctx) {
|
|
||||||
skynet_command(ctx, "REG", ".launcher");
|
|
||||||
ctx = skynet_context_new("snlua", config->start);
|
|
||||||
}
|
|
||||||
|
|
||||||
_start(config->thread);
|
_start(config->thread);
|
||||||
skynet_socket_free();
|
skynet_socket_free();
|
||||||
|
if (config->daemon) {
|
||||||
|
daemon_exit(config->daemon);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,9 @@
|
|||||||
|
|
||||||
typedef void (*timer_execute_func)(void *ud,void *arg);
|
typedef void (*timer_execute_func)(void *ud,void *arg);
|
||||||
|
|
||||||
|
#define LOCK(q) while (__sync_lock_test_and_set(&(q)->lock,1)) {}
|
||||||
|
#define UNLOCK(q) __sync_lock_release(&(q)->lock);
|
||||||
|
|
||||||
#define TIME_NEAR_SHIFT 8
|
#define TIME_NEAR_SHIFT 8
|
||||||
#define TIME_NEAR (1 << TIME_NEAR_SHIFT)
|
#define TIME_NEAR (1 << TIME_NEAR_SHIFT)
|
||||||
#define TIME_LEVEL_SHIFT 6
|
#define TIME_LEVEL_SHIFT 6
|
||||||
@@ -45,13 +48,14 @@ struct timer {
|
|||||||
int time;
|
int time;
|
||||||
uint32_t current;
|
uint32_t current;
|
||||||
uint32_t starttime;
|
uint32_t starttime;
|
||||||
|
uint64_t current_point;
|
||||||
|
uint64_t origin_point;
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct timer * TI = NULL;
|
static struct timer * TI = NULL;
|
||||||
|
|
||||||
static inline struct timer_node *
|
static inline struct timer_node *
|
||||||
link_clear(struct link_list *list)
|
link_clear(struct link_list *list) {
|
||||||
{
|
|
||||||
struct timer_node * ret = list->head.next;
|
struct timer_node * ret = list->head.next;
|
||||||
list->head.next = 0;
|
list->head.next = 0;
|
||||||
list->tail = &(list->head);
|
list->tail = &(list->head);
|
||||||
@@ -60,23 +64,20 @@ link_clear(struct link_list *list)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
link(struct link_list *list,struct timer_node *node)
|
link(struct link_list *list,struct timer_node *node) {
|
||||||
{
|
|
||||||
list->tail->next = node;
|
list->tail->next = node;
|
||||||
list->tail = node;
|
list->tail = node;
|
||||||
node->next=0;
|
node->next=0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
add_node(struct timer *T,struct timer_node *node)
|
add_node(struct timer *T,struct timer_node *node) {
|
||||||
{
|
|
||||||
int time=node->expire;
|
int time=node->expire;
|
||||||
int current_time=T->time;
|
int current_time=T->time;
|
||||||
|
|
||||||
if ((time|TIME_NEAR_MASK)==(current_time|TIME_NEAR_MASK)) {
|
if ((time|TIME_NEAR_MASK)==(current_time|TIME_NEAR_MASK)) {
|
||||||
link(&T->near[time&TIME_NEAR_MASK],node);
|
link(&T->near[time&TIME_NEAR_MASK],node);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
int i;
|
int i;
|
||||||
int mask=TIME_NEAR << TIME_LEVEL_SHIFT;
|
int mask=TIME_NEAR << TIME_LEVEL_SHIFT;
|
||||||
for (i=0;i<3;i++) {
|
for (i=0;i<3;i++) {
|
||||||
@@ -90,21 +91,21 @@ add_node(struct timer *T,struct timer_node *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
timer_add(struct timer *T,void *arg,size_t sz,int time)
|
timer_add(struct timer *T,void *arg,size_t sz,int time) {
|
||||||
{
|
|
||||||
struct timer_node *node = (struct timer_node *)skynet_malloc(sizeof(*node)+sz);
|
struct timer_node *node = (struct timer_node *)skynet_malloc(sizeof(*node)+sz);
|
||||||
memcpy(node+1,arg,sz);
|
memcpy(node+1,arg,sz);
|
||||||
|
|
||||||
while (__sync_lock_test_and_set(&T->lock,1)) {};
|
LOCK(T);
|
||||||
|
|
||||||
node->expire=time+T->time;
|
node->expire=time+T->time;
|
||||||
add_node(T,node);
|
add_node(T,node);
|
||||||
|
|
||||||
__sync_lock_release(&T->lock);
|
UNLOCK(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
timer_shift(struct timer *T) {
|
timer_shift(struct timer *T) {
|
||||||
|
LOCK(T);
|
||||||
int mask = TIME_NEAR;
|
int mask = TIME_NEAR;
|
||||||
int time = (++T->time) >> TIME_NEAR_SHIFT;
|
int time = (++T->time) >> TIME_NEAR_SHIFT;
|
||||||
int i=0;
|
int i=0;
|
||||||
@@ -125,15 +126,11 @@ timer_shift(struct timer *T) {
|
|||||||
time >>= TIME_LEVEL_SHIFT;
|
time >>= TIME_LEVEL_SHIFT;
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
UNLOCK(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
static inline void
|
||||||
timer_execute(struct timer *T) {
|
dispatch_list(struct timer_node *current) {
|
||||||
int idx = T->time & TIME_NEAR_MASK;
|
|
||||||
|
|
||||||
while (T->near[idx].head.next) {
|
|
||||||
struct timer_node *current = link_clear(&T->near[idx]);
|
|
||||||
|
|
||||||
do {
|
do {
|
||||||
struct timer_event * event = (struct timer_event *)(current+1);
|
struct timer_event * event = (struct timer_event *)(current+1);
|
||||||
struct skynet_message message;
|
struct skynet_message message;
|
||||||
@@ -148,27 +145,38 @@ timer_execute(struct timer *T) {
|
|||||||
current=current->next;
|
current=current->next;
|
||||||
skynet_free(temp);
|
skynet_free(temp);
|
||||||
} while (current);
|
} while (current);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
timer_execute(struct timer *T) {
|
||||||
|
LOCK(T);
|
||||||
|
int idx = T->time & TIME_NEAR_MASK;
|
||||||
|
|
||||||
|
while (T->near[idx].head.next) {
|
||||||
|
struct timer_node *current = link_clear(&T->near[idx]);
|
||||||
|
UNLOCK(T);
|
||||||
|
// dispatch_list don't need lock T
|
||||||
|
dispatch_list(current);
|
||||||
|
LOCK(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UNLOCK(T);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
timer_update(struct timer *T)
|
timer_update(struct timer *T) {
|
||||||
{
|
|
||||||
while (__sync_lock_test_and_set(&T->lock,1)) {};
|
|
||||||
|
|
||||||
// try to dispatch timeout 0 (rare condition)
|
// try to dispatch timeout 0 (rare condition)
|
||||||
timer_execute(T);
|
timer_execute(T);
|
||||||
|
|
||||||
// shift time first, and then dispatch timer message
|
// shift time first, and then dispatch timer message
|
||||||
timer_shift(T);
|
timer_shift(T);
|
||||||
|
|
||||||
timer_execute(T);
|
timer_execute(T);
|
||||||
|
|
||||||
__sync_lock_release(&T->lock);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct timer *
|
static struct timer *
|
||||||
timer_create_timer()
|
timer_create_timer() {
|
||||||
{
|
|
||||||
struct timer *r=(struct timer *)skynet_malloc(sizeof(struct timer));
|
struct timer *r=(struct timer *)skynet_malloc(sizeof(struct timer));
|
||||||
memset(r,0,sizeof(*r));
|
memset(r,0,sizeof(*r));
|
||||||
|
|
||||||
@@ -212,18 +220,41 @@ skynet_timeout(uint32_t handle, int time, int session) {
|
|||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t
|
// centisecond: 1/100 second
|
||||||
_gettime(void) {
|
static void
|
||||||
uint32_t t;
|
systime(uint32_t *sec, uint32_t *cs) {
|
||||||
#if !defined(__APPLE__)
|
#if !defined(__APPLE__)
|
||||||
struct timespec ti;
|
struct timespec ti;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &ti);
|
clock_gettime(CLOCK_REALTIME, &ti);
|
||||||
t = (uint32_t)(ti.tv_sec & 0xffffff) * 100;
|
*sec = (uint32_t)ti.tv_sec;
|
||||||
|
*cs = (uint32_t)(ti.tv_nsec / 10000000);
|
||||||
|
#else
|
||||||
|
struct timeval tv;
|
||||||
|
gettimeofday(&tv, NULL);
|
||||||
|
*sec = tv.tv_sec;
|
||||||
|
*cs = tv.tv_usec / 10000;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t
|
||||||
|
gettime() {
|
||||||
|
uint64_t t;
|
||||||
|
#if !defined(__APPLE__)
|
||||||
|
|
||||||
|
#ifdef CLOCK_MONOTONIC_RAW
|
||||||
|
#define CLOCK_TIMER CLOCK_MONOTONIC_RAW
|
||||||
|
#else
|
||||||
|
#define CLOCK_TIMER CLOCK_MONOTONIC
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct timespec ti;
|
||||||
|
clock_gettime(CLOCK_TIMER, &ti);
|
||||||
|
t = (uint64_t)ti.tv_sec * 100;
|
||||||
t += ti.tv_nsec / 10000000;
|
t += ti.tv_nsec / 10000000;
|
||||||
#else
|
#else
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
gettimeofday(&tv, NULL);
|
gettimeofday(&tv, NULL);
|
||||||
t = (uint32_t)(tv.tv_sec & 0xffffff) * 100;
|
t = (uint64_t)tv.tv_sec * 100;
|
||||||
t += tv.tv_usec / 10000;
|
t += tv.tv_usec / 10000;
|
||||||
#endif
|
#endif
|
||||||
return t;
|
return t;
|
||||||
@@ -231,10 +262,19 @@ _gettime(void) {
|
|||||||
|
|
||||||
void
|
void
|
||||||
skynet_updatetime(void) {
|
skynet_updatetime(void) {
|
||||||
uint32_t ct = _gettime();
|
uint64_t cp = gettime();
|
||||||
if (ct != TI->current) {
|
if(cp < TI->current_point) {
|
||||||
int diff = ct>=TI->current?ct-TI->current:(0xffffff+1)*100-TI->current+ct;
|
skynet_error(NULL, "time diff error: change from %lld to %lld", cp, TI->current_point);
|
||||||
TI->current = ct;
|
} else if (cp != TI->current_point) {
|
||||||
|
uint32_t diff = (uint32_t)(cp - TI->current_point);
|
||||||
|
TI->current_point = cp;
|
||||||
|
|
||||||
|
uint32_t oc = TI->current;
|
||||||
|
TI->current += diff;
|
||||||
|
if (TI->current < oc) {
|
||||||
|
// when cs > 0xffffffff(about 497 days), time rewind
|
||||||
|
TI->starttime += 0xffffffff / 100;
|
||||||
|
}
|
||||||
int i;
|
int i;
|
||||||
for (i=0;i<diff;i++) {
|
for (i=0;i<diff;i++) {
|
||||||
timer_update(TI);
|
timer_update(TI);
|
||||||
@@ -255,18 +295,9 @@ skynet_gettime(void) {
|
|||||||
void
|
void
|
||||||
skynet_timer_init(void) {
|
skynet_timer_init(void) {
|
||||||
TI = timer_create_timer();
|
TI = timer_create_timer();
|
||||||
TI->current = _gettime();
|
systime(&TI->starttime, &TI->current);
|
||||||
|
uint64_t point = gettime();
|
||||||
#if !defined(__APPLE__)
|
TI->current_point = point;
|
||||||
struct timespec ti;
|
TI->origin_point = point;
|
||||||
clock_gettime(CLOCK_REALTIME, &ti);
|
|
||||||
uint32_t sec = (uint32_t)ti.tv_sec;
|
|
||||||
#else
|
|
||||||
struct timeval tv;
|
|
||||||
gettimeofday(&tv, NULL);
|
|
||||||
uint32_t sec = (uint32_t)tv.tv_sec;
|
|
||||||
#endif
|
|
||||||
uint32_t mono = _gettime() / 100;
|
|
||||||
|
|
||||||
TI->starttime = sec - mono;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/tcp.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -34,6 +35,8 @@
|
|||||||
#define PRIORITY_HIGH 0
|
#define PRIORITY_HIGH 0
|
||||||
#define PRIORITY_LOW 1
|
#define PRIORITY_LOW 1
|
||||||
|
|
||||||
|
#define HASH_ID(id) (((unsigned)id) % MAX_SOCKET)
|
||||||
|
|
||||||
struct write_buffer {
|
struct write_buffer {
|
||||||
struct write_buffer * next;
|
struct write_buffer * next;
|
||||||
char *ptr;
|
char *ptr;
|
||||||
@@ -107,6 +110,12 @@ struct request_start {
|
|||||||
uintptr_t opaque;
|
uintptr_t opaque;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct request_setopt {
|
||||||
|
int id;
|
||||||
|
int what;
|
||||||
|
int value;
|
||||||
|
};
|
||||||
|
|
||||||
struct request_package {
|
struct request_package {
|
||||||
uint8_t header[8]; // 6 bytes dummy
|
uint8_t header[8]; // 6 bytes dummy
|
||||||
union {
|
union {
|
||||||
@@ -117,6 +126,7 @@ struct request_package {
|
|||||||
struct request_listen listen;
|
struct request_listen listen;
|
||||||
struct request_bind bind;
|
struct request_bind bind;
|
||||||
struct request_start start;
|
struct request_start start;
|
||||||
|
struct request_setopt setopt;
|
||||||
} u;
|
} u;
|
||||||
uint8_t dummy[256];
|
uint8_t dummy[256];
|
||||||
};
|
};
|
||||||
@@ -144,9 +154,11 @@ reserve_id(struct socket_server *ss) {
|
|||||||
if (id < 0) {
|
if (id < 0) {
|
||||||
id = __sync_and_and_fetch(&(ss->alloc_id), 0x7fffffff);
|
id = __sync_and_and_fetch(&(ss->alloc_id), 0x7fffffff);
|
||||||
}
|
}
|
||||||
struct socket *s = &ss->slot[id % MAX_SOCKET];
|
struct socket *s = &ss->slot[HASH_ID(id)];
|
||||||
if (s->type == SOCKET_TYPE_INVALID) {
|
if (s->type == SOCKET_TYPE_INVALID) {
|
||||||
if (__sync_bool_compare_and_swap(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) {
|
if (__sync_bool_compare_and_swap(&s->type, SOCKET_TYPE_INVALID, SOCKET_TYPE_RESERVE)) {
|
||||||
|
s->id = id;
|
||||||
|
s->fd = -1;
|
||||||
return id;
|
return id;
|
||||||
} else {
|
} else {
|
||||||
// retry
|
// retry
|
||||||
@@ -265,7 +277,7 @@ check_wb_list(struct wb_list *s) {
|
|||||||
|
|
||||||
static struct socket *
|
static struct socket *
|
||||||
new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) {
|
new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) {
|
||||||
struct socket * s = &ss->slot[id % MAX_SOCKET];
|
struct socket * s = &ss->slot[HASH_ID(id)];
|
||||||
assert(s->type == SOCKET_TYPE_RESERVE);
|
assert(s->type == SOCKET_TYPE_RESERVE);
|
||||||
|
|
||||||
if (add) {
|
if (add) {
|
||||||
@@ -287,7 +299,7 @@ new_fd(struct socket_server *ss, int id, int fd, uintptr_t opaque, bool add) {
|
|||||||
|
|
||||||
// return -1 when connecting
|
// return -1 when connecting
|
||||||
static int
|
static int
|
||||||
open_socket(struct socket_server *ss, struct request_open * request, struct socket_message *result, bool blocking) {
|
open_socket(struct socket_server *ss, struct request_open * request, struct socket_message *result) {
|
||||||
int id = request->id;
|
int id = request->id;
|
||||||
result->opaque = request->opaque;
|
result->opaque = request->opaque;
|
||||||
result->id = id;
|
result->id = id;
|
||||||
@@ -316,18 +328,14 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
socket_keepalive(sock);
|
socket_keepalive(sock);
|
||||||
if (!blocking) {
|
|
||||||
sp_nonblocking(sock);
|
sp_nonblocking(sock);
|
||||||
}
|
|
||||||
status = connect( sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
|
status = connect( sock, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
|
||||||
if ( status != 0 && errno != EINPROGRESS) {
|
if ( status != 0 && errno != EINPROGRESS) {
|
||||||
close(sock);
|
close(sock);
|
||||||
sock = -1;
|
sock = -1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (blocking) {
|
|
||||||
sp_nonblocking(sock);
|
sp_nonblocking(sock);
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -359,7 +367,7 @@ open_socket(struct socket_server *ss, struct request_open * request, struct sock
|
|||||||
return -1;
|
return -1;
|
||||||
_failed:
|
_failed:
|
||||||
freeaddrinfo( ai_list );
|
freeaddrinfo( ai_list );
|
||||||
ss->slot[id % MAX_SOCKET].type = SOCKET_TYPE_INVALID;
|
ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID;
|
||||||
return SOCKET_ERROR;
|
return SOCKET_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -504,7 +512,7 @@ send_buffer_empty(struct socket *s) {
|
|||||||
static int
|
static int
|
||||||
send_socket(struct socket_server *ss, struct request_send * request, struct socket_message *result, int priority) {
|
send_socket(struct socket_server *ss, struct request_send * request, struct socket_message *result, int priority) {
|
||||||
int id = request->id;
|
int id = request->id;
|
||||||
struct socket * s = &ss->slot[id % MAX_SOCKET];
|
struct socket * s = &ss->slot[HASH_ID(id)];
|
||||||
if (s->type == SOCKET_TYPE_INVALID || s->id != id
|
if (s->type == SOCKET_TYPE_INVALID || s->id != id
|
||||||
|| s->type == SOCKET_TYPE_HALFCLOSE
|
|| s->type == SOCKET_TYPE_HALFCLOSE
|
||||||
|| s->type == SOCKET_TYPE_PACCEPT) {
|
|| s->type == SOCKET_TYPE_PACCEPT) {
|
||||||
@@ -512,7 +520,7 @@ send_socket(struct socket_server *ss, struct request_send * request, struct sock
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
assert(s->type != SOCKET_TYPE_PLISTEN && s->type != SOCKET_TYPE_LISTEN);
|
assert(s->type != SOCKET_TYPE_PLISTEN && s->type != SOCKET_TYPE_LISTEN);
|
||||||
if (send_buffer_empty(s)) {
|
if (send_buffer_empty(s) && s->type == SOCKET_TYPE_CONNECTED) {
|
||||||
int n = write(s->fd, request->buffer, request->sz);
|
int n = write(s->fd, request->buffer, request->sz);
|
||||||
if (n<0) {
|
if (n<0) {
|
||||||
switch(errno) {
|
switch(errno) {
|
||||||
@@ -558,7 +566,7 @@ _failed:
|
|||||||
result->id = id;
|
result->id = id;
|
||||||
result->ud = 0;
|
result->ud = 0;
|
||||||
result->data = NULL;
|
result->data = NULL;
|
||||||
ss->slot[id % MAX_SOCKET].type = SOCKET_TYPE_INVALID;
|
ss->slot[HASH_ID(id)].type = SOCKET_TYPE_INVALID;
|
||||||
|
|
||||||
return SOCKET_ERROR;
|
return SOCKET_ERROR;
|
||||||
}
|
}
|
||||||
@@ -566,7 +574,7 @@ _failed:
|
|||||||
static int
|
static int
|
||||||
close_socket(struct socket_server *ss, struct request_close *request, struct socket_message *result) {
|
close_socket(struct socket_server *ss, struct request_close *request, struct socket_message *result) {
|
||||||
int id = request->id;
|
int id = request->id;
|
||||||
struct socket * s = &ss->slot[id % MAX_SOCKET];
|
struct socket * s = &ss->slot[HASH_ID(id)];
|
||||||
if (s->type == SOCKET_TYPE_INVALID || s->id != id) {
|
if (s->type == SOCKET_TYPE_INVALID || s->id != id) {
|
||||||
result->id = id;
|
result->id = id;
|
||||||
result->opaque = request->opaque;
|
result->opaque = request->opaque;
|
||||||
@@ -614,7 +622,7 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc
|
|||||||
result->opaque = request->opaque;
|
result->opaque = request->opaque;
|
||||||
result->ud = 0;
|
result->ud = 0;
|
||||||
result->data = NULL;
|
result->data = NULL;
|
||||||
struct socket *s = &ss->slot[id % MAX_SOCKET];
|
struct socket *s = &ss->slot[HASH_ID(id)];
|
||||||
if (s->type == SOCKET_TYPE_INVALID || s->id !=id) {
|
if (s->type == SOCKET_TYPE_INVALID || s->id !=id) {
|
||||||
return SOCKET_ERROR;
|
return SOCKET_ERROR;
|
||||||
}
|
}
|
||||||
@@ -635,6 +643,17 @@ start_socket(struct socket_server *ss, struct request_start *request, struct soc
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
setopt_socket(struct socket_server *ss, struct request_setopt *request) {
|
||||||
|
int id = request->id;
|
||||||
|
struct socket *s = &ss->slot[HASH_ID(id)];
|
||||||
|
if (s->type == SOCKET_TYPE_INVALID || s->id !=id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int v = request->value;
|
||||||
|
setsockopt(s->fd, IPPROTO_TCP, request->what, &v, sizeof(v));
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
block_readpipe(int pipefd, void *buffer, int sz) {
|
block_readpipe(int pipefd, void *buffer, int sz) {
|
||||||
for (;;) {
|
for (;;) {
|
||||||
@@ -687,7 +706,7 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) {
|
|||||||
case 'K':
|
case 'K':
|
||||||
return close_socket(ss,(struct request_close *)buffer, result);
|
return close_socket(ss,(struct request_close *)buffer, result);
|
||||||
case 'O':
|
case 'O':
|
||||||
return open_socket(ss, (struct request_open *)buffer, result, false);
|
return open_socket(ss, (struct request_open *)buffer, result);
|
||||||
case 'X':
|
case 'X':
|
||||||
result->opaque = 0;
|
result->opaque = 0;
|
||||||
result->id = 0;
|
result->id = 0;
|
||||||
@@ -698,6 +717,9 @@ ctrl_cmd(struct socket_server *ss, struct socket_message *result) {
|
|||||||
return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_HIGH);
|
return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_HIGH);
|
||||||
case 'P':
|
case 'P':
|
||||||
return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_LOW);
|
return send_socket(ss, (struct request_send *)buffer, result, PRIORITY_LOW);
|
||||||
|
case 'T':
|
||||||
|
setopt_socket(ss, (struct request_setopt *)buffer);
|
||||||
|
return -1;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "socket-server: Unknown ctrl %c.\n",type);
|
fprintf(stderr, "socket-server: Unknown ctrl %c.\n",type);
|
||||||
return -1;
|
return -1;
|
||||||
@@ -765,7 +787,9 @@ report_connect(struct socket_server *ss, struct socket *s, struct socket_message
|
|||||||
result->opaque = s->opaque;
|
result->opaque = s->opaque;
|
||||||
result->id = s->id;
|
result->id = s->id;
|
||||||
result->ud = 0;
|
result->ud = 0;
|
||||||
|
if (send_buffer_empty(s)) {
|
||||||
sp_write(ss->event_fd, s->fd, s, false);
|
sp_write(ss->event_fd, s->fd, s, false);
|
||||||
|
}
|
||||||
union sockaddr_all u;
|
union sockaddr_all u;
|
||||||
socklen_t slen = sizeof(u);
|
socklen_t slen = sizeof(u);
|
||||||
if (getpeername(s->fd, &u.s, &slen) == 0) {
|
if (getpeername(s->fd, &u.s, &slen) == 0) {
|
||||||
@@ -815,6 +839,22 @@ report_accept(struct socket_server *ss, struct socket *s, struct socket_message
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void
|
||||||
|
clear_closed_event(struct socket_server *ss, struct socket_message * result, int type) {
|
||||||
|
if (type == SOCKET_CLOSE || type == SOCKET_ERROR) {
|
||||||
|
int id = result->id;
|
||||||
|
int i;
|
||||||
|
for (i=ss->event_index; i<ss->event_n; i++) {
|
||||||
|
struct event *e = &ss->ev[i];
|
||||||
|
struct socket *s = e->s;
|
||||||
|
if (s) {
|
||||||
|
if (s->type == SOCKET_TYPE_INVALID && s->id == id) {
|
||||||
|
e->s = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// return type
|
// return type
|
||||||
int
|
int
|
||||||
@@ -823,9 +863,10 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int
|
|||||||
if (ss->checkctrl) {
|
if (ss->checkctrl) {
|
||||||
if (has_cmd(ss)) {
|
if (has_cmd(ss)) {
|
||||||
int type = ctrl_cmd(ss, result);
|
int type = ctrl_cmd(ss, result);
|
||||||
if (type != -1)
|
if (type != -1) {
|
||||||
|
clear_closed_event(ss, result, type);
|
||||||
return type;
|
return type;
|
||||||
else
|
} else
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
ss->checkctrl = 0;
|
ss->checkctrl = 0;
|
||||||
@@ -865,12 +906,14 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int
|
|||||||
int type = send_buffer(ss, s, result);
|
int type = send_buffer(ss, s, result);
|
||||||
if (type == -1)
|
if (type == -1)
|
||||||
break;
|
break;
|
||||||
|
clear_closed_event(ss, result, type);
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
if (e->read) {
|
if (e->read) {
|
||||||
int type = forward_message(ss, s, result);
|
int type = forward_message(ss, s, result);
|
||||||
if (type == -1)
|
if (type == -1)
|
||||||
break;
|
break;
|
||||||
|
clear_closed_event(ss, result, type);
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -920,27 +963,13 @@ socket_server_connect(struct socket_server *ss, uintptr_t opaque, const char * a
|
|||||||
return request.u.open.id;
|
return request.u.open.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
|
||||||
socket_server_block_connect(struct socket_server *ss, uintptr_t opaque, const char * addr, int port) {
|
|
||||||
struct request_package request;
|
|
||||||
struct socket_message result;
|
|
||||||
open_request(ss, &request, opaque, addr, port);
|
|
||||||
int ret = open_socket(ss, &request.u.open, &result, true);
|
|
||||||
if (ret == SOCKET_OPEN) {
|
|
||||||
return result.id;
|
|
||||||
} else {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// return -1 when error
|
// return -1 when error
|
||||||
int64_t
|
int64_t
|
||||||
socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) {
|
socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz) {
|
||||||
struct socket * s = &ss->slot[id % MAX_SOCKET];
|
struct socket * s = &ss->slot[HASH_ID(id)];
|
||||||
if (s->id != id || s->type == SOCKET_TYPE_INVALID) {
|
if (s->id != id || s->type == SOCKET_TYPE_INVALID) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
assert(s->type != SOCKET_TYPE_RESERVE);
|
|
||||||
|
|
||||||
struct request_package request;
|
struct request_package request;
|
||||||
request.u.send.id = id;
|
request.u.send.id = id;
|
||||||
@@ -953,11 +982,10 @@ socket_server_send(struct socket_server *ss, int id, const void * buffer, int sz
|
|||||||
|
|
||||||
void
|
void
|
||||||
socket_server_send_lowpriority(struct socket_server *ss, int id, const void * buffer, int sz) {
|
socket_server_send_lowpriority(struct socket_server *ss, int id, const void * buffer, int sz) {
|
||||||
struct socket * s = &ss->slot[id % MAX_SOCKET];
|
struct socket * s = &ss->slot[HASH_ID(id)];
|
||||||
if (s->id != id || s->type == SOCKET_TYPE_INVALID) {
|
if (s->id != id || s->type == SOCKET_TYPE_INVALID) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
assert(s->type != SOCKET_TYPE_RESERVE);
|
|
||||||
|
|
||||||
struct request_package request;
|
struct request_package request;
|
||||||
request.u.send.id = id;
|
request.u.send.id = id;
|
||||||
@@ -1049,4 +1077,11 @@ socket_server_start(struct socket_server *ss, uintptr_t opaque, int id) {
|
|||||||
send_request(ss, &request, 'S', sizeof(request.u.start));
|
send_request(ss, &request, 'S', sizeof(request.u.start));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
socket_server_nodelay(struct socket_server *ss, int id) {
|
||||||
|
struct request_package request;
|
||||||
|
request.u.setopt.id = id;
|
||||||
|
request.u.setopt.what = TCP_NODELAY;
|
||||||
|
request.u.setopt.value = 1;
|
||||||
|
send_request(ss, &request, 'T', sizeof(request.u.setopt));
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,6 +36,6 @@ int socket_server_listen(struct socket_server *, uintptr_t opaque, const char *
|
|||||||
int socket_server_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port);
|
int socket_server_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port);
|
||||||
int socket_server_bind(struct socket_server *, uintptr_t opaque, int fd);
|
int socket_server_bind(struct socket_server *, uintptr_t opaque, int fd);
|
||||||
|
|
||||||
int socket_server_block_connect(struct socket_server *, uintptr_t opaque, const char * addr, int port);
|
void socket_server_nodelay(struct socket_server *, int id);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
local skynet = require "skynet"
|
|
||||||
local mqueue = require "mqueue"
|
|
||||||
|
|
||||||
skynet.start(function()
|
|
||||||
local id = 0
|
|
||||||
local pingserver = skynet.newservice "pingserver"
|
|
||||||
mqueue.register(function(str)
|
|
||||||
id = id + 1
|
|
||||||
str = string.format("id = %d , %s",id, str)
|
|
||||||
return skynet.call(pingserver, "lua", "PING", str)
|
|
||||||
end)
|
|
||||||
end)
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
local skynet = require "skynet"
|
local skynet = require "skynet"
|
||||||
|
local queue = require "skynet.queue"
|
||||||
|
|
||||||
local i = 0
|
local i = 0
|
||||||
local hello = "hello"
|
local hello = "hello"
|
||||||
@@ -8,9 +9,27 @@ function response.ping(hello)
|
|||||||
return hello
|
return hello
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- response.sleep and accept.hello share one lock
|
||||||
|
local lock
|
||||||
|
|
||||||
|
function accept.sleep(queue, n)
|
||||||
|
if queue then
|
||||||
|
lock(
|
||||||
|
function()
|
||||||
|
print("queue=",queue, n)
|
||||||
|
skynet.sleep(n)
|
||||||
|
end)
|
||||||
|
else
|
||||||
|
print("queue=",queue, n)
|
||||||
|
skynet.sleep(n)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function accept.hello()
|
function accept.hello()
|
||||||
|
lock(function()
|
||||||
i = i + 1
|
i = i + 1
|
||||||
print (i, hello)
|
print (i, hello)
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function response.error()
|
function response.error()
|
||||||
@@ -19,6 +38,8 @@ end
|
|||||||
|
|
||||||
function init( ... )
|
function init( ... )
|
||||||
print ("ping server start:", ...)
|
print ("ping server start:", ...)
|
||||||
|
-- init queue
|
||||||
|
lock = queue()
|
||||||
|
|
||||||
-- You can return "queue" for queue service mode
|
-- You can return "queue" for queue service mode
|
||||||
-- return "queue"
|
-- return "queue"
|
||||||
|
|||||||
23
test/testdatacenter.lua
Normal file
23
test/testdatacenter.lua
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local datacenter = require "datacenter"
|
||||||
|
|
||||||
|
local function f1()
|
||||||
|
print("====1==== wait hello")
|
||||||
|
print("\t1>",datacenter.wait ("hello"))
|
||||||
|
print("====1==== wait key.foobar")
|
||||||
|
print("\t1>", pcall(datacenter.wait,"key")) -- will failed, because "key" is a branch
|
||||||
|
print("\t1>",datacenter.wait ("key", "foobar"))
|
||||||
|
end
|
||||||
|
|
||||||
|
local function f2()
|
||||||
|
skynet.sleep(10)
|
||||||
|
print("====2==== set key.foobar")
|
||||||
|
datacenter.set("key", "foobar", "bingo")
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
datacenter.set("hello", "world")
|
||||||
|
print(datacenter.get "hello")
|
||||||
|
skynet.fork(f1)
|
||||||
|
skynet.fork(f2)
|
||||||
|
end)
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user