summaryrefslogtreecommitdiff
path: root/xmake/core/base/socket.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2019-12-18 22:43:48 +0800
committerruki <[email protected]>2019-12-18 10:22:52 +0800
commit9e01c1f9affa69b23a78ee1c04ba7cf8302bdfb2 (patch)
tree180a2f86b80f81335df8afb698e29e36d8fd44a7 /xmake/core/base/socket.lua
parent656f546055712e694754211bbd99ff18d144d856 (diff)
improve socket recv
Diffstat (limited to 'xmake/core/base/socket.lua')
-rw-r--r--xmake/core/base/socket.lua42
1 files changed, 32 insertions, 10 deletions
diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua
index a01dde5dd..455b42b0a 100644
--- a/xmake/core/base/socket.lua
+++ b/xmake/core/base/socket.lua
@@ -48,7 +48,7 @@ socket.EV_ACPT = socket.EV_RECV
function _instance.new(socktype, family, sock)
local instance = table.inherit(_instance)
instance._SOCK = sock
- instance._TYPE = socktype
+ instance._TYPE = socktype
instance._FAMILY = family
setmetatable(instance, _instance)
return instance
@@ -322,15 +322,16 @@ function _instance:recv(size, opt)
local real = 0
local wait = false
local data_or_errors = nil
- local data = bytes(8192)
if opt.block then
local results = {}
while recv < size do
- real, data_or_errors = io.socket_recv(self:csock(), data:caddr(), math.min(8192, size - recv))
+ local buff = self:_recvbuff()
+ real, data_or_errors = io.socket_recv(self:csock(), buff:caddr(), math.min(buff:size(), size - recv))
if real > 0 then
recv = recv + real
wait = false
- table.insert(results, bytes(data, 1, real))
+ table.insert(results, bytes(buff, 1, real))
+ self:_recvbuff_clear()
elseif real == 0 and not wait then
local events, waiterrs = self:wait(socket.EV_RECV, opt.timeout or -1)
if events == socket.EV_RECV then
@@ -349,9 +350,11 @@ function _instance:recv(size, opt)
recv = -1
end
else
- recv, data_or_errors = io.socket_recv(self:csock(), data:caddr(), math.min(8192, size))
+ local buff = self:_recvbuff()
+ recv, data_or_errors = io.socket_recv(self:csock(), buff:caddr(), math.min(buff:size(), size))
if recv > 0 then
- data_or_errors = bytes(data, 1, recv)
+ data_or_errors = bytes(buff, 1, recv)
+ self:_recvbuff_clear()
end
end
if recv < 0 and data_or_errors then
@@ -441,9 +444,11 @@ function _instance:recvfrom(size, opt)
local data_or_errors = nil
if opt.block then
while true do
- recv, data_or_errors, addr, port = io.socket_recvfrom(self:csock(), size)
+ local buff = self:_recvbuff()
+ recv, data_or_errors, addr, port = io.socket_recvfrom(self:csock(), buff:caddr(), math.min(buff:size(), size))
if recv > 0 then
- data_or_errors = bytes(data_or_errors)
+ data_or_errors = bytes(buff, 1, recv)
+ self:_recvbuff_clear()
break
elseif recv == 0 and not wait then
local events, waiterrs = self:wait(socket.EV_RECV, opt.timeout or -1)
@@ -459,9 +464,11 @@ function _instance:recvfrom(size, opt)
end
end
else
- recv, data_or_errors, addr, port = io.socket_recvfrom(self:csock(), size)
+ local buff = self:_recvbuff()
+ recv, data_or_errors, addr, port = io.socket_recvfrom(self:csock(), buff:caddr(), math.min(buff:size(), size))
if recv > 0 then
- data_or_errors = bytes(data_or_errors)
+ data_or_errors = bytes(buff, 1, recv)
+ self:_recvbuff_clear()
end
end
if recv < 0 and data_or_errors then
@@ -518,6 +525,21 @@ function _instance:close()
return ok
end
+-- get the recv buffer
+function _instance:_recvbuff()
+ local recvbuff = self._RECVBUFF
+ if not recvbuff then
+ recvbuff = bytes(8192)
+ self._RECVBUFF = recvbuff
+ end
+ return recvbuff
+end
+
+-- clear the recv buffer
+function _instance:_recvbuff_clear()
+ self._RECVBUFF = nil
+end
+
-- ensure the socket is opened
function _instance:_ensure_opened()
if not self:csock() then