mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
socket.listen returns addr,port
This commit is contained in:
@@ -103,13 +103,17 @@ socket_message[1] = function(id, size, data)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- SKYNET_SOCKET_TYPE_CONNECT = 2
|
-- SKYNET_SOCKET_TYPE_CONNECT = 2
|
||||||
socket_message[2] = function(id, _ , addr)
|
socket_message[2] = function(id, ud , addr)
|
||||||
local s = socket_pool[id]
|
local s = socket_pool[id]
|
||||||
if s == nil then
|
if s == nil then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
-- log remote addr
|
-- log remote addr
|
||||||
if not s.connected then -- resume may also post connect message
|
if not s.connected then -- resume may also post connect message
|
||||||
|
if s.listen then
|
||||||
|
s.addr = addr
|
||||||
|
s.port = ud
|
||||||
|
end
|
||||||
s.connected = true
|
s.connected = true
|
||||||
wakeup(s)
|
wakeup(s)
|
||||||
end
|
end
|
||||||
@@ -220,7 +224,10 @@ local function connect(id, func)
|
|||||||
protocol = "TCP",
|
protocol = "TCP",
|
||||||
}
|
}
|
||||||
assert(not socket_onclose[id], "socket has onclose callback")
|
assert(not socket_onclose[id], "socket has onclose callback")
|
||||||
assert(not socket_pool[id], "socket is not closed")
|
local s2 = socket_pool[id]
|
||||||
|
if s2 and not s2.listen then
|
||||||
|
error("socket is not closed")
|
||||||
|
end
|
||||||
socket_pool[id] = s
|
socket_pool[id] = s
|
||||||
suspend(s)
|
suspend(s)
|
||||||
local err = s.connecting
|
local err = s.connecting
|
||||||
@@ -404,7 +411,15 @@ function socket.listen(host, port, backlog)
|
|||||||
host, port = string.match(host, "([^:]+):(.+)$")
|
host, port = string.match(host, "([^:]+):(.+)$")
|
||||||
port = tonumber(port)
|
port = tonumber(port)
|
||||||
end
|
end
|
||||||
return driver.listen(host, port, backlog)
|
local id = driver.listen(host, port, backlog)
|
||||||
|
local s = {
|
||||||
|
id = id,
|
||||||
|
connected = false,
|
||||||
|
listen = true,
|
||||||
|
}
|
||||||
|
socket_pool[id] = s
|
||||||
|
suspend(s)
|
||||||
|
return id, s.addr, s.port
|
||||||
end
|
end
|
||||||
|
|
||||||
-- abandon use to forward socket id to other service
|
-- abandon use to forward socket id to other service
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ local function launch_master(conf)
|
|||||||
skynet.error(string.format("invalid client (fd = %d) error = %s", fd, err))
|
skynet.error(string.format("invalid client (fd = %d) error = %s", fd, err))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
socket.close_fd(fd) -- We haven't call socket.start, so use socket.close_fd rather than socket.close.
|
socket.close(fd)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -263,11 +263,8 @@ skynet.start(function()
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
skynet.wait()
|
skynet.wait()
|
||||||
socket.close(slave_fd)
|
|
||||||
else
|
|
||||||
-- slave_fd does not start, so use close_fd.
|
|
||||||
socket.close_fd(slave_fd)
|
|
||||||
end
|
end
|
||||||
|
socket.close(slave_fd)
|
||||||
skynet.error("Shakehand ready")
|
skynet.error("Shakehand ready")
|
||||||
skynet.fork(ready)
|
skynet.fork(ready)
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ local function console_main_loop(stdin, print, addr)
|
|||||||
end
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
local listen_socket = socket.listen (ip, port)
|
local listen_socket, ip, port = socket.listen (ip, port)
|
||||||
skynet.error("Start debug console at " .. ip .. ":" .. port)
|
skynet.error("Start debug console at " .. ip .. ":" .. port)
|
||||||
socket.start(listen_socket , function(id, addr)
|
socket.start(listen_socket , function(id, addr)
|
||||||
local function print(...)
|
local function print(...)
|
||||||
|
|||||||
@@ -1089,7 +1089,28 @@ listen_socket(struct socket_server *ss, struct request_listen * request, struct
|
|||||||
goto _failed;
|
goto _failed;
|
||||||
}
|
}
|
||||||
ATOM_STORE(&s->type , SOCKET_TYPE_PLISTEN);
|
ATOM_STORE(&s->type , SOCKET_TYPE_PLISTEN);
|
||||||
return -1;
|
result->opaque = request->opaque;
|
||||||
|
result->id = id;
|
||||||
|
result->ud = 0;
|
||||||
|
result->data = "listen";
|
||||||
|
|
||||||
|
union sockaddr_all u;
|
||||||
|
socklen_t slen = sizeof(u);
|
||||||
|
if (getsockname(listen_fd, &u.s, &slen) == 0) {
|
||||||
|
void * sin_addr = (u.s.sa_family == AF_INET) ? (void*)&u.v4.sin_addr : (void *)&u.v6.sin6_addr;
|
||||||
|
if (inet_ntop(u.s.sa_family, sin_addr, ss->buffer, sizeof(ss->buffer)) == 0) {
|
||||||
|
result->data = strerror(errno);
|
||||||
|
return SOCKET_ERR;
|
||||||
|
}
|
||||||
|
int sin_port = ntohs((u.s.sa_family == AF_INET) ? u.v4.sin_port : u.v6.sin6_port);
|
||||||
|
result->data = ss->buffer;
|
||||||
|
result->ud = sin_port;
|
||||||
|
} else {
|
||||||
|
result->data = strerror(errno);
|
||||||
|
return SOCKET_ERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SOCKET_OPEN;
|
||||||
_failed:
|
_failed:
|
||||||
close(listen_fd);
|
close(listen_fd);
|
||||||
result->opaque = request->opaque;
|
result->opaque = request->opaque;
|
||||||
|
|||||||
Reference in New Issue
Block a user