summaryrefslogtreecommitdiff
path: root/xmake/core/base/socket.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2019-11-04 22:19:43 +0800
committerruki <[email protected]>2019-11-04 07:58:23 +0800
commit860571059e3d30da620110e646be3b9a537333fe (patch)
treefeb1365b2bfbeea516949b07a1662a6ab9cd411d /xmake/core/base/socket.lua
parent7a63343afae6bf51e25ec4b7d2d8cdf704de3c1a (diff)
fix socket send
Diffstat (limited to 'xmake/core/base/socket.lua')
-rw-r--r--xmake/core/base/socket.lua24
1 files changed, 23 insertions, 1 deletions
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua
index 06820522c..4a2dfdcdc 100644
--- a/xmake/core/base/socket.lua
+++ b/xmake/core/base/socket.lua
@@ -178,10 +178,17 @@ function _instance:send(data, opt)
return -1, errors
end
+ -- data is bytes? unpack the raw address
+ local datasize = #data
+ if type(data) == "table" and data.caddr then
+ datasize = data:size()
+ data = {data = data:caddr(), size = data:size()}
+ end
+
-- init start and last
opt = opt or {}
local start = opt.start or 1
- local last = opt.last or #data
+ local last = opt.last or datasize
-- check start and last
if start > last or start < 1 then
@@ -355,11 +362,21 @@ function _instance:sendto(data, addr, port, opt)
return -1, errors
end
+ -- only for udp
+ if self:type() ~= socket.UDP then
+ return -1, string.format("%s: sendto() only for udp socket!", self)
+ end
+
-- check address
if not addr or not port then
return -1, string.format("%s: sendto empty address!", self)
end
+ -- data is bytes? unpack the raw address
+ if type(data) == "table" and data.caddr then
+ data = {data = data:caddr(), size = data:size()}
+ end
+
-- send it
opt = opt or {}
local send = 0
@@ -398,6 +415,11 @@ function _instance:recvfrom(size, opt)
return -1, errors
end
+ -- only for udp
+ if self:type() ~= socket.UDP then
+ return -1, string.format("%s: sendto() only for udp socket!", self)
+ end
+
-- check size
if size == 0 then
return 0