summaryrefslogtreecommitdiff
path: root/xmake/core/base/io.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2019-10-12 22:43:00 +0800
committerruki <[email protected]>2019-10-12 22:43:00 +0800
commit3dcbba4360d7a7ab7082117711566a5bfc6857dc (patch)
treea7e8cd6345c6a655e87d6220f169d898f451139a /xmake/core/base/io.lua
parentb0aa3b69139ffc540968dd8b37d04e9647253d8b (diff)
add opensock
Diffstat (limited to 'xmake/core/base/io.lua')
-rw-r--r--xmake/core/base/io.lua54
1 files changed, 53 insertions, 1 deletions
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index f6c931970..5aaebc1af 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -22,6 +22,7 @@
local io = io or {}
local _file = _file or {}
local _filelock = _filelock or {}
+local _socket = _socket or {}
-- load modules
local path = require("base/path")
@@ -31,6 +32,7 @@ local string = require("base/string")
-- save metatable and builtin functions
io._file = _file
io._filelock = _filelock
+io._socket = _socket
io._stdfile = io._stdfile or io.stdfile
-- new an file
@@ -212,7 +214,7 @@ function _file:load()
end
end
--- new an filelock
+-- new a filelock
function _filelock.new(lockpath, lock)
local filelock = table.inherit(_filelock)
filelock._NAME = path.filename(lockpath)
@@ -317,6 +319,44 @@ function _filelock:__gc()
end
end
+-- new a socket
+function _socket.new(sock)
+ local socket = table.inherit(_socket)
+ socket._SOCK = sock
+ socket._NAME = ""
+ setmetatable(socket, _socket)
+ return socket
+end
+
+-- get the socket name
+function _socket:name()
+ return self._NAME
+end
+
+-- close socket
+function _socket:close()
+ if not self._SOCK then
+ return false, string.format("socket(%s) has been closed!", self:name())
+ end
+ local ok = io.socket_close(self._SOCK)
+ if ok then
+ self._SOCK = nil
+ end
+ return ok
+end
+
+-- tostring(socket)
+function _socket:__tostring()
+ return "socket: " .. self:name()
+end
+
+-- gc(socket)
+function _socket:__gc()
+ if self._SOCK and io.socket_close(self._SOCK) then
+ self._SOCK = nil
+ end
+end
+
-- read all lines from file
function io.lines(filepath, opt)
@@ -456,6 +496,18 @@ function io.openlock(filepath)
end
end
+-- open a socket
+function io.opensock()
+
+ -- open it
+ local sock = io.socket_open()
+ if sock then
+ return _socket.new(filepath, sock)
+ else
+ return nil, string.format("failed to open socket!")
+ end
+end
+
-- close file
function io.close(file)
return (file or io.stdout):close()