From 03399e86a9f5886e4227cbbf6a5508263b1fa7a3 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 9 Apr 2015 12:49:40 +0800 Subject: [PATCH] udp api changed, callback recv the string --- lualib/dns.lua | 4 +--- lualib/socket.lua | 5 ++++- test/testudp.lua | 7 +++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lualib/dns.lua b/lualib/dns.lua index 26a7bcd5..a38c00e6 100644 --- a/lualib/dns.lua +++ b/lualib/dns.lua @@ -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) diff --git a/lualib/socket.lua b/lualib/socket.lua index dd2df1c1..8d98e5a3 100644 --- a/lualib/socket.lua +++ b/lualib/socket.lua @@ -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 { diff --git a/test/testudp.lua b/test/testudp.lua index d8387e29..fb8f7109 100644 --- a/test/testudp.lua +++ b/test/testudp.lua @@ -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