Skip to content

Commit

Permalink
read first version of skynet
Browse files Browse the repository at this point in the history
  • Loading branch information
tanbin committed Mar 23, 2018
1 parent b744c14 commit 84ea71e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions src_code/agent.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local skynet = require "skynet"

-- 输出传入 agent.lua 文件的参数
print("agent",...)

skynet.callback(function(addr, msg)
Expand Down
4 changes: 2 additions & 2 deletions src_code/gate/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
struct connection {
char * agent; // 转寄地址。接到每个连接的数据都会默认发往 watchdog ,如果设置了转寄地址则发往转寄地址
int connection_id; // skynet 内部的 socket 标号
int uid;
int uid; // id 标识,用户接入的时候会设置为一个大于0的值,断开的时候会重置为0.
};

struct gate {
Expand Down Expand Up @@ -104,7 +104,7 @@ _ctrl(struct skynet_context * ctx, struct gate * g, const void * msg, int sz) {
skynet_error(ctx, "[gate] Unkown command : %s", command);
}

// 从 ctx 发送消息到 watchdog
// 将连接和断开的消息发送到 watchdog
static void
_report(struct skynet_context * ctx, const char * data, ...) {
va_list ap;
Expand Down
4 changes: 4 additions & 0 deletions src_code/gate/mread.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ mread_poll(struct mread_pool * self , int timeout) {
return -1;
}
if (fd == self->listen_fd) { // 表示有新的连接
/*
这里有个问题:当用户连接进来但是没有发送数据, mread_poll 会返回 -1
所以在 gate/main.c 的 _cb 函数中调用的时候 _report open 不能得到立即响应
*/
struct sockaddr_in remote_addr;
socklen_t len = sizeof(struct sockaddr_in);
int client_fd = accept(self->listen_fd , (struct sockaddr *)&remote_addr , &len);
Expand Down
2 changes: 1 addition & 1 deletion src_code/lua-skynet.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _command(lua_State *L) {
result = skynet_command(context, cmd, NULL);
}
if (result) {
lua_pushstring(L, result);
lua_pushstring(L, result); // skynet.command 调用的返回结果
return 1;
}
return 0;
Expand Down
1 change: 1 addition & 0 deletions src_code/service_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ _load(lua_State *L, char ** filename) {
}

// 加载并执行 snlua XX.lua parms 中的 XX.lua 代码并传入 parms 参数
// snlua 服务的 init 接口中关于回调函数的设置放在 XX.lua 文件中。
int
snlua_init(lua_State *L, struct skynet_context *ctx, const char * args) {
lua_gc(L, LUA_GCSTOP, 0); // QUESTION: 这里以及后面的 lua_gc 作用
Expand Down
7 changes: 7 additions & 0 deletions src_code/skynet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ const char * skynet_command(struct skynet_context * context, const char * cmd ,
*/
void skynet_send(struct skynet_context * context, const char * addr , void * msg, size_t sz_session);

/*
context 是服务指针
ud 是 skynet_callback 设置的第二个参数
uid 是源服务地址
msg 是消息数据
sz_session 是数据大小或者一个约定号
*/
typedef void (*skynet_cb)(struct skynet_context * context, void *ud, const char * uid , const void * msg, size_t sz_session);
// 设置 ctx 的 回调函数接口以及传入回调函数的第二个参数
void skynet_callback(struct skynet_context * context, void *ud, skynet_cb cb);
Expand Down
2 changes: 1 addition & 1 deletion src_code/watchdog.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function command:open(parm)
local fd,addr = string.match(parm,"(%d+) ([^%s]+)")
fd = tonumber(fd)
print("[watchdog] open",self,fd,addr)
local agent = skynet.command("LAUNCH","snlua agent.lua ".. self)
local agent = skynet.command("LAUNCH","snlua agent.lua ".. self) -- 这里启动一个 snlua 服务,返回服务地址。并且因为 open 命令处理的是用户连接,所以经常在网上看到的说每个用户接入会启动一个 agent 。
if agent then
skynet.send(".gate","forward ".. self .. " " .. agent)
end
Expand Down

0 comments on commit 84ea71e

Please sign in to comment.