mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-22 02:53:09 +00:00
udp api changed, callback recv the string
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user