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 end
assert(server, "Can't get nameserver") assert(server, "Can't get nameserver")
end end
dns_server = socket.udp(function(data, sz, from) dns_server = socket.udp(function(str, from)
local str = skynet.tostring(data,sz)
skynet.trash(data,sz)
resolve(str) resolve(str)
end) end)
socket.udp_connect(dns_server, server, port or 53) socket.udp_connect(dns_server, server, port or 53)

View File

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

View File

@@ -3,16 +3,15 @@ local socket = require "socket"
local function server() local function server()
local host local host
host = socket.udp(function(data, sz, from) host = socket.udp(function(str, from)
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)) print("server recv", str, socket.udp_address(from))
socket.sendto(host, from, "OK " .. str) socket.sendto(host, from, "OK " .. str)
end , "127.0.0.1", 8765) -- bind an address end , "127.0.0.1", 8765) -- bind an address
end end
local function client() local function client()
local c = socket.udp(function(data, sz, from) local c = socket.udp(function(str, from)
print("client recv", skynet.tostring(data,sz), socket.udp_address(from)) print("client recv", str, socket.udp_address(from))
end) end)
socket.udp_connect(c, "127.0.0.1", 8765) socket.udp_connect(c, "127.0.0.1", 8765)
for i=1,20 do for i=1,20 do