diff --git a/connection/connection.c b/connection/connection.c index 21d31352..318ca373 100644 --- a/connection/connection.c +++ b/connection/connection.c @@ -108,7 +108,6 @@ connection_del(struct connection_pool * pool, int fd) { EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL); kevent(pool->kqueue_fd, &ke, 1, NULL, 0, NULL); #endif - close(fd); } static int diff --git a/connection/main.c b/connection/main.c index 4eedd6c4..0f6c0d38 100644 --- a/connection/main.c +++ b/connection/main.c @@ -87,11 +87,12 @@ _del(struct connection_server * server, int fd) { if (c->fd == fd) { if (c->close == 0) { skynet_send(server->ctx, 0, c->address, PTYPE_CLIENT | PTYPE_TAG_DONTCOPY, 0, NULL, 0); + connection_del(server->pool, fd); } c->address = 0; c->fd = 0; c->close = 0; - connection_del(server->pool, fd); + close(fd); return; } } @@ -125,6 +126,7 @@ _poll(struct connection_server * server) { if (c->close == 0) { c->close = 1; skynet_send(server->ctx, 0, c->address, PTYPE_CLIENT | PTYPE_TAG_DONTCOPY, 0, NULL, 0); + connection_del(server->pool, c->fd); } } else { skynet_send(server->ctx, 0, c->address, PTYPE_CLIENT | PTYPE_TAG_DONTCOPY, 0, buffer, size); diff --git a/service/testconnection.lua b/service/testconnection.lua new file mode 100644 index 00000000..d5bd87f8 --- /dev/null +++ b/service/testconnection.lua @@ -0,0 +1,34 @@ +local skynet = require "skynet" +local socket = require "socket" + +local server = "127.0.0.1:8866" + +local function reconnect() + while socket.connect(server) do + skynet.sleep(1000) + print("reconnect to", server) + end + print(server, "connected") +end + +skynet.register_protocol { + name = "client", + id = 3, + pack = function(...) return ... end, + unpack = function(msg,sz) + if sz == 0 then + skynet.timeout(0, reconnect) + return + end + print("sz=",sz,skynet.tostring(msg,sz)) + end, + dispatch = function () end +} + +skynet.start(function() + if socket.connect(server) then + print "use 'nc -l 8866' first to listen" + skynet.exit() + end +end) +