add UDP support, and TCP listen support ipv6 now

This commit is contained in:
Cloud Wu
2014-11-12 23:00:44 +08:00
parent fa78623b1c
commit cfe9506a5a
7 changed files with 773 additions and 98 deletions

View File

@@ -119,12 +119,23 @@ socket_message[5] = function(id)
wakeup(s)
end
-- SKYNET_SOCKET_TYPE_UDP = 6
socket_message[6] = function(id, size, data, address)
local s = socket_pool[id]
if s == nil or s.callback == nil then
skynet.error("socket: drop udp package from " .. id)
driver.drop(data, size)
return
end
s.callback(data, size, address)
end
skynet.register_protocol {
name = "socket",
id = skynet.PTYPE_SOCKET, -- PTYPE_SOCKET = 6
unpack = driver.unpack,
dispatch = function (_, _, t, n1, n2, data)
socket_message[t](n1,n2,data)
dispatch = function (_, _, t, ...)
socket_message[t](...)
end
}
@@ -140,6 +151,7 @@ local function connect(id, func)
read_require = false,
co = false,
callback = func,
protocol = "TCP",
}
socket_pool[id] = s
suspend(s)
@@ -354,4 +366,41 @@ function socket.limit(id, limit)
s.buffer_limit = limit
end
---------------------- UDP
local udp_socket = {}
local function create_udp_object(id, cb)
socket_pool[id] = {
id = id,
read_require = false,
co = false,
connected = true,
protocol = "UDP",
callback = cb,
}
end
function socket.udp(callback, host, port)
local id = driver.udp(host, port)
create_udp_object(id, callback)
return id
end
function socket.udp_connect(id, addr, port, callback)
local obj = socket_pool[id]
if obj then
assert(obj.protocol == "UDP")
if callback then
obj.callback = callback
end
else
create_udp_object(id, callback)
end
driver.udp_connect(id, addr, port)
end
socket.sendto = assert(driver.udp_send)
socket.udp_address = assert(driver.udp_address)
return socket