在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
本篇主要介绍在Lua服务里调用skynet网络层底层接口的流程,Lua层的api主要在lualib/skynet/socket.lua,可参考官方wiki https://github.com/cloudwu/skynet/wiki/Socket。 通过一个简单的例子说明Lua服务是如何最终调用到网络层底层接口的: 1 local socket = require “socket” 2 local skynet = require "skynet" 3 4 local function loop(fd) 5 socket.start(fd) 6 while true do 7 local data = socket.readline('\n') 8 print(data, #data) 9 end 10 end 11 12 skynet.start(function() 13 local listen_fd = socket.listen(ip, hort) 14 socket.start(listen_fd, function(fd, addr) 15 print("connect fd[%d], addr[%s]", fd, addr) 16 skynet.fork(loop, fd) 17 end) 18 end) 1. api调用流程概述在服务启动时,调用socket.listen监听。调用流程是:driver.listen(第7行)——>skynet_socket_listen(第17行)——>socket_server_listen(第29行)——>send_request(第47行),最后向发送管道写数据。Lua接口执行流程是:socket.lua -> lua-socket.c ->skynet_socket.c -> socket_server.c 注:第34行,do_listen依次调用了unix网络系统接口socket,bind,listen。 1 // lualib/skynet/socket.lua 2 function socket.listen(host, port, backlog) 3 if port == nil then 4 host, port = string.match(host, "([^:]+):(.+)$") 5 port = tonumber(port) 6 end 7 return driver.listen(host, port, backlog) 8 end 9 10 // lualib-src/lua-socket.c 11 static int 12 llisten(lua_State *L) { 13 const char * host = luaL_checkstring(L,1); 14 int port = luaL_checkinteger(L,2); 15 int backlog = luaL_optinteger(L,3,BACKLOG); 16 struct skynet_context * ctx = lua_touserdata(L, lua_upvalueindex(1)); 17 int id = skynet_socket_listen(ctx, host,port,backlog); 18 if (id < 0) { 19 return luaL_error(L, "Listen error"); 20 } 21 22 lua_pushinteger(L,id); 23 return 1; 24 } 25 26 // skynet-src/skynet_socket.c 27 skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog) { 28 uint32_t source = skynet_context_handle(ctx); 29 return socket_server_listen(SOCKET_SERVER, source, host, port, backlog); 30 } 31 32 // skynet-src/socket_server.c 33 socket_server_listen(struct socket_server *ss, uintptr_t opaque, const char * addr, int port, int backlog) { 34 int fd = do_listen(addr, port, backlog); 35 if (fd < 0) { 36 return -1; 37 } 38 struct request_package request; 39 int id = reserve_id(ss); 40 if (id < 0) { 41 close(fd); 42 return id; 43 } 44 request.u.listen.opaque = opaque; 45 request.u.listen.id = id; 46 request.u.listen.fd = fd; 47 send_request(ss, &request, 'L', sizeof(request.u.listen)); 48 return id; 49 } 2. socket连接过程skynet里的socket结构有几种状态: 1 #define SOCKET_TYPE_INVALID 0 //可使用 2 #define SOCKET_TYPE_RESERVE 1 //已占用 3 #define SOCKET_TYPE_PLISTEN 2 //等待监听(监听套接字拥有) 4 #define SOCKET_TYPE_LISTEN 3 //监听,可接受客户端的连接(监听套接字才拥有) 5 #define SOCKET_TYPE_CONNECTING 4 //正在连接(connect失败时状态,tcp会尝试重新connect) 6 #define SOCKET_TYPE_CONNECTED 5 //已连接,可以收发数据 7 #define SOCKET_TYPE_HALFCLOSE 6 8 #define SOCKET_TYPE_PACCEPT 7 //等待连接(连接套接字才拥有) 9 #define SOCKET_TYPE_BIND 8 当工作线程执行socket.listen后,socket线程从接收管道读取数据,执行ctrl_cmd,调用listen_socket(第6行),此时该socket状态是SOCKET_TYPE_PLISTEN(第18行) 1 // skynet-src/socket_server.c 2 static int 3 ctrl_cmd(struct socket_server *ss, struct socket_message *result) { 4 ... 5 case 'L': 6 return listen_socket(ss,(struct request_listen *)buffer, result); 7 ... 8 } 9 10 static int 11 listen_socket(struct socket_server *ss, struct request_listen * request, struct socket_message *result) { 12 int id = request->id; 13 int listen_fd = request->fd; 14 struct socket *s = new_fd(ss, id, listen_fd, PROTOCOL_TCP, request->opaque, false); 15 if (s == NULL) { 16 goto _failed; 17 } 18 s->type = SOCKET_TYPE_PLISTEN; 19 return -1; 20 ... 21 } 接着,Lua服务调用socket.start,最终socket线程执行start_socket,此时socket状态是SOCKET_TYPE_LISTEN,等待客户端的连接请求。 1 // skynet-src/socket_server.c 2 static int 3 start_socket(struct socket_server *ss, struct request_start *request, struct socket_message *result) { 4 ... 5 if (s->type == SOCKET_TYPE_PACCEPT || s->type == SOCKET_TYPE_PLISTEN) { 6 if (sp_add(ss->event_fd, s->fd, s)) { 7 force_close(ss, s, &l, result); 8 result->data = strerror(errno); 9 return SOCKET_ERR; 10 } 11 s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN; 12 s->opaque = request->opaque; 13 result->data = "start"; 14 return SOCKET_OPEN; 15 } 16 ... 17 } 当客户端发起连接请求后,epoll事件返回,调用report_accept(第5行) 第14行,调用unix网络系统接口accept,接受客户端的请求。由于客户端已发起连接,所以不会阻塞。 第16行,从socket池中获取可用的socket id 17-22行,初始化该socket,此时socket状态是SOCKET_TYPE_PACCEPT 1 int 2 socket_server_poll(struct socket_server *ss, struct socket_message * result, int * more) { 3 ... 4 case SOCKET_TYPE_LISTEN: { 5 int ok = report_accept(ss, s, result); 6 ... 7 } 8 9 // return 0 when failed, or -1 when file limit 10 static int 11 report_accept(struct socket_server *ss, struct socket *s, struct socket_message *result) { 12 union sockaddr_all u; 13 socklen_t len = sizeof(u); 14 int client_fd = accept(s->fd, &u.s, &len); 15 ... 16 int id = reserve_id(ss); 17 struct socket *ns = new_fd(ss, id, client_fd, PROTOCOL_TCP, s->opaque, false); 18 ns->type = SOCKET_TYPE_PACCEPT; 19 result->opaque = s->opaque; 20 result->id = s->id; 21 result->ud = id; 22 result->data = NULL; 23 24 ... 25 return 1; 26 } 接着,Lua服务再次调用socket.start(id),此时id是连接的socket,而不是监听的socket。此时,socket状态是SOCKET_TYPE_CONNECTED,连接已经建立,可以收发数据。这就是整个socket连接过程。 至于怎么通知到 Lua服务稍后分析。 1 // skynet-src/socket_server.c 2 static int 3 start_socket(struct socket_server *ss, struct request_start *request, struct socket_message *result) { 4 ... 5 s->type = (s->type == SOCKET_TYPE_PACCEPT) ? SOCKET_TYPE_CONNECTED : SOCKET_TYPE_LISTEN; 6 ... 7 } 关闭socket,socket.close 发送数据有两个api,正常发送socket.write, 低优先级发送socket.lwrite。 3. 网络层如何通知给Lua服务socket线程在运行过程(socket_server_poll)中,当收到网络数据会调用forward_message_tcp 第19行,调用unix系统接口读取socket上的数据 21-24行,采用args-value形式构造result,opaque是Lua服务的地址,id是该socket在池中的索引,ud是实际读取到的字节数,data是数据 第25行,返回SOCKET_DATA,表示接收到数据。 1 // skynet-src/socket_server.c 2 int 3 socket_server_poll(struct socket_server *ss, struct socket_message * result, int * more) { 4 ... 5 default: 6 if (e->read) { 7 int type; 8 if (s->protocol == PROTOCOL_TCP) { 9 type = forward_message_tcp(ss, s, &l, result); 10 ... 11 return type 12 } 13 14 static int 15 forward_message_tcp(struct socket_server *ss, struct socket *s, struct socket_lock *l, struct socket_message * resu 16 lt) { 17 int sz = s->p.size; 18 char * buffer = MALLOC(sz); 19 int n = (int)read(s->fd, buffer, sz); 20 ... 21 result->opaque = s->opaque; 22 result->id = s->id; 23 result->ud = n; 24 result->data = buffer; 25 return SOCKET_DATA; 26 } 由于socket_server_poll返回的是SOCKET_DATA,调用forward_message(第11行), 23-26行,构造即将要发送的消息数据,用到了上面返回的result 28-32行,构造skynet消息结构,因为是在网络层发送的,不是具体的某个服务,所以source,session字段都设置成0即可 第34行,把消息发送给与socket对应的服务地址。 至此,网络消息通知给具体的Lua服务。 1 // skynet-src/skynet_socket.c 2 int 3 skynet_socket_poll() { 4 struct socket_server *ss = SOCKET_SERVER; 5 assert(ss); 6 struct socket_message result; 7 int more = 1; 8 int type = socket_server_poll(ss, &result, &more); 9 switch (type) { 10 case SOCKET_DATA: 11 forward_message(SKYNET_SOCKET_TYPE_DATA, false, &result); 12 break; 13 ... 14 return 1; 15 } 16 17 // mainloop thread 18 static void 19 forward_message(int type, bool padding, struct socket_message * result) { 20 struct skynet_socket_message *sm; 21 size_t sz = sizeof(*sm); 22 ... 23 sm = (struct skynet_socket_message *)skynet_malloc(sz); 24 sm->type = type; 25 sm->id = result->id; 26 sm->ud = result->ud; 27 ... 28 struct skynet_message message; 29 message.source = 0; 30 message.session = 0; 31 message.data = sm; 32 message.sz = sz | ((size_t)PTYPE_SOCKET << MESSAGE_TYPE_SHIFT); 33 34 if (skynet_context_push((uint32_t)result->opaque, &message)) { 35 // todo: report somewhere to close socket 36 // don't call skynet_socket_close here (It will block mainloop) 37 skynet_free(sm->buffer); 38 skynet_free(sm); 39 } 40 } 4. Lua服务处理流程当网络数据到达Lua服务时,lualib/skynet/socket.lua中提供了相应的处理方案。调用消息分发函数socket_message,网络数据类型包含正常数据传输(DATA),连接(CONNECT),关闭(CLOSE),错误(ERROR)等。 第15行,把客户端发过来的数据push到该socket的缓冲池中。 1 -- lualib/skynet/socket.lua 2 skynet.register_protocol { 3 name = "socket", 4 id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6 5 unpack = driver.unpack, 6 dispatch = function (_, _, t, ...) 7 socket_message[t](...) 8 end 9 } 10 11 -- SKYNET_SOCKET_TYPE_DATA = 1 12 socket_message[1] = function(id, size, data) 13 local s = socket_pool[id] 14 ... 15 local sz = driver.push(s.buffer, buffer_pool, data, size) 16 ... 17 } socket.read(id, sz),从一个socket上读sz指定的字节数,如果缓冲池里有足够多的数据,从缓冲池里pop出直接返回(第5行),否则,暂停当前协程(第15行),当数据够或者连接断开时重启协程。 1 -- lualib/skynet/socket.lua 2 function socket.read(id, sz) 3 local s = socket_pool[id] 4 assert(s) 5 ... 6 local ret = driver.pop(s.buffer, buffer_pool, sz) 7 if ret then 8 return ret 9 end 10 if not s.connected then 11 return false, driver.readall(s.buffer, buffer_pool) 12 end 13 14 assert(not s.read_required) 15 s.read_required = sz 16 suspend(s) 17 ret = driver.pop(s.buffer, buffer_pool, sz) 18 if ret then 19 return ret 20 else 21 return false, driver.readall(s.buffer, buffer_pool) 22 end 23 end socket.readline(id, sep),从一个socket上读以sep分割的数据,默认是"\n",即读一行数据。注:该api可以指定分隔符,不单单是一行数据。 socket.abandon(id),清除socket id在本服务内的数据结构,但不并关闭这个socket,用于把id转给其他服务控制。通常,会设计一个master服务接收外部连接,等连接上后再将socket分配给一个slave服务控制,减少master服务的压力。 5. 总结socket库的使用流程一般是: -- master服务 local listen_fd = socket.listen(ip, port) //监听一个地址 socket.start(listen_fd, function(fd, addr) slave.post.start(fd) //客户端连接上,转交给slave socket.abandon(fd) end) -- slave服务 function accept.start(fd) socket.start(fd) //接管socket ... end |
请发表评论