mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c7887cceb7 | ||
|
|
4fbd433199 | ||
|
|
87d276caf6 | ||
|
|
0f64e909fa | ||
|
|
4bc2e800fd | ||
|
|
bf6501f2ee | ||
|
|
f92fc153e4 | ||
|
|
c10e5412c0 | ||
|
|
9477cc8171 |
@@ -1,3 +1,9 @@
|
|||||||
|
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)
|
v0.3.0 (2014-6-2)
|
||||||
-----------
|
-----------
|
||||||
* Add cluster support
|
* Add cluster support
|
||||||
|
|||||||
2
Makefile
2
Makefile
@@ -66,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))))
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
## Build
|
## Build
|
||||||
|
|
||||||
Install autoconf first for jemalloc
|
For linux, install autoconf first for jemalloc
|
||||||
|
|
||||||
```
|
```
|
||||||
git clone git@github.com:cloudwu/skynet.git
|
git clone git@github.com:cloudwu/skynet.git
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -68,6 +68,13 @@ 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) {
|
||||||
@@ -532,8 +539,8 @@ 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) {
|
if (sz <= 0) {
|
||||||
luaL_error(L, "Invalid bson string , length = 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;
|
||||||
@@ -644,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 (;;) {
|
||||||
@@ -820,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);
|
||||||
|
|
||||||
@@ -1140,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);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ 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),
|
||||||
}
|
}
|
||||||
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 +167,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 +226,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
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ macosx : EXPORT :=
|
|||||||
macosx linux : SKYNET_LIBS += -ldl
|
macosx linux : SKYNET_LIBS += -ldl
|
||||||
linux freebsd : SKYNET_LIBS += -lrt
|
linux freebsd : SKYNET_LIBS += -lrt
|
||||||
|
|
||||||
# Turn off jemalloc and malloc hook on macosx
|
# Turn off jemalloc and malloc hook on macosx and freebsd
|
||||||
|
|
||||||
macosx : MALLOC_STATICLIB :=
|
macosx : MALLOC_STATICLIB :=
|
||||||
macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC
|
macosx : SKYNET_DEFINES :=-DNOUSE_JEMALLOC
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ 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;
|
pthread_key_t handle_key;
|
||||||
};
|
};
|
||||||
@@ -75,8 +76,13 @@ context_dec() {
|
|||||||
|
|
||||||
uint32_t
|
uint32_t
|
||||||
skynet_current_handle(void) {
|
skynet_current_handle(void) {
|
||||||
void * handle = pthread_getspecific(G_NODE.handle_key);
|
if (G_NODE.init) {
|
||||||
return (uint32_t)(uintptr_t)handle;
|
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
|
||||||
@@ -650,6 +656,7 @@ void
|
|||||||
skynet_globalinit(void) {
|
skynet_globalinit(void) {
|
||||||
G_NODE.total = 0;
|
G_NODE.total = 0;
|
||||||
G_NODE.monitor_exit = 0;
|
G_NODE.monitor_exit = 0;
|
||||||
|
G_NODE.init = 1;
|
||||||
if (pthread_key_create(&G_NODE.handle_key, NULL)) {
|
if (pthread_key_create(&G_NODE.handle_key, NULL)) {
|
||||||
fprintf(stderr, "pthread_key_create failed");
|
fprintf(stderr, "pthread_key_create failed");
|
||||||
exit(1);
|
exit(1);
|
||||||
|
|||||||
Reference in New Issue
Block a user