If recv one udp message, try read next immediately rather than until next epoll_wait

This commit is contained in:
Cloud Wu
2014-11-13 11:44:46 +08:00
parent cfe9506a5a
commit 20b181c56d
2 changed files with 19 additions and 4 deletions

View File

@@ -1205,7 +1205,17 @@ socket_server_poll(struct socket_server *ss, struct socket_message * result, int
break;
default:
if (e->read) {
int type = (s->protocol == PROTOCOL_TCP ? forward_message_tcp : forward_message_udp)(ss, s, result);
int type;
if (s->protocol == PROTOCOL_TCP) {
type = forward_message_tcp(ss, s, result);
} else {
type = forward_message_udp(ss, s, result);
if (type == SOCKET_UDP) {
// try read again
--ss->event_index;
return SOCKET_UDP;
}
}
if (e->write) {
// Try to dispatch write message next step if write flag set.
e->read = false;
@@ -1452,6 +1462,8 @@ socket_server_udp(struct socket_server *ss, uintptr_t opaque, const char * addr,
return -1;
}
}
sp_nonblocking(fd);
int id = reserve_id(ss);
if (id < 0) {
close(fd);

View File

@@ -4,8 +4,9 @@ local socket = require "socket"
local function server()
local host
host = socket.udp(function(data, sz, from)
print("server recv", skynet.tostring(data,sz), socket.udp_address(from))
socket.sendto(host, from, "OK")
local str = skynet.tostring(data,sz) -- skynet.tostring should call only once, because it will free the pointer data
print("server recv", str, socket.udp_address(from))
socket.sendto(host, from, "OK " .. str)
end , "127.0.0.1", 8765) -- bind an address
end
@@ -14,7 +15,9 @@ local function client()
print("client recv", skynet.tostring(data,sz), socket.udp_address(from))
end)
socket.udp_connect(c, "127.0.0.1", 8765)
socket.write(c, "hello") -- write to the address by udp_connect binding
for i=1,20 do
socket.write(c, "hello " .. i) -- write to the address by udp_connect binding
end
end
skynet.start(function()