udp api changed, callback recv the string

This commit is contained in:
Cloud Wu
2015-04-09 12:49:40 +08:00
parent 22364bac89
commit 03399e86a9
3 changed files with 8 additions and 8 deletions

View File

@@ -249,9 +249,7 @@ function dns.server(server, port)
end
assert(server, "Can't get nameserver")
end
dns_server = socket.udp(function(data, sz, from)
local str = skynet.tostring(data,sz)
skynet.trash(data,sz)
dns_server = socket.udp(function(str, from)
resolve(str)
end)
socket.udp_connect(dns_server, server, port or 53)

View File

@@ -1,5 +1,6 @@
local driver = require "socketdriver"
local skynet = require "skynet"
local skynet_core = require "skynet.core"
local assert = assert
local socket = {} -- api
@@ -127,7 +128,9 @@ socket_message[6] = function(id, size, data, address)
driver.drop(data, size)
return
end
s.callback(data, size, address)
local str = skynet.tostring(data, size)
skynet_core.trash(data, size)
s.callback(str, address)
end
skynet.register_protocol {

View File

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