aboutsummaryrefslogtreecommitdiff
path: root/src/socket.lua
diff options
context:
space:
mode:
authormoteus <mimir@newmail.ru>2013-05-27 12:45:09 +0400
committermoteus <mimir@newmail.ru>2013-05-27 12:45:09 +0400
commit920bc9762954454468d056a61391635cbd849406 (patch)
tree048c112f95fa578d239ae3e35b2aec0a482f0c23 /src/socket.lua
parentbd51d8c1a5bb30e6a358dee6e85963f777edfff4 (diff)
downloadluasocket-920bc9762954454468d056a61391635cbd849406.tar.gz
luasocket-920bc9762954454468d056a61391635cbd849406.tar.bz2
luasocket-920bc9762954454468d056a61391635cbd849406.zip
Build with Lua 5.2 without LUA_COMPAT_MODULE flag.
LUASOCKET_USE_GLOBAL flag enable create global variables when load socket/mime modules.
Diffstat (limited to 'src/socket.lua')
-rw-r--r--src/socket.lua25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/socket.lua b/src/socket.lua
index e8def75..f2fb8a3 100644
--- a/src/socket.lua
+++ b/src/socket.lua
@@ -10,20 +10,21 @@ local base = _G
10local string = require("string") 10local string = require("string")
11local math = require("math") 11local math = require("math")
12local socket = require("socket.core") 12local socket = require("socket.core")
13module("socket") 13
14local _M = socket
14 15
15----------------------------------------------------------------------------- 16-----------------------------------------------------------------------------
16-- Exported auxiliar functions 17-- Exported auxiliar functions
17----------------------------------------------------------------------------- 18-----------------------------------------------------------------------------
18function connect4(address, port, laddress, lport) 19function _M.connect4(address, port, laddress, lport)
19 return socket.connect(address, port, laddress, lport, "inet") 20 return socket.connect(address, port, laddress, lport, "inet")
20end 21end
21 22
22function connect6(address, port, laddress, lport) 23function _M.connect6(address, port, laddress, lport)
23 return socket.connect(address, port, laddress, lport, "inet6") 24 return socket.connect(address, port, laddress, lport, "inet6")
24end 25end
25 26
26function bind(host, port, backlog) 27function _M.bind(host, port, backlog)
27 if host == "*" then host = "0.0.0.0" end 28 if host == "*" then host = "0.0.0.0" end
28 local addrinfo, err = socket.dns.getaddrinfo(host); 29 local addrinfo, err = socket.dns.getaddrinfo(host);
29 if not addrinfo then return nil, err end 30 if not addrinfo then return nil, err end
@@ -52,9 +53,9 @@ function bind(host, port, backlog)
52 return nil, err 53 return nil, err
53end 54end
54 55
55try = newtry() 56_M.try = _M.newtry()
56 57
57function choose(table) 58function _M.choose(table)
58 return function(name, opt1, opt2) 59 return function(name, opt1, opt2)
59 if base.type(name) ~= "string" then 60 if base.type(name) ~= "string" then
60 name, opt1, opt2 = "default", name, opt1 61 name, opt1, opt2 = "default", name, opt1
@@ -69,10 +70,11 @@ end
69-- Socket sources and sinks, conforming to LTN12 70-- Socket sources and sinks, conforming to LTN12
70----------------------------------------------------------------------------- 71-----------------------------------------------------------------------------
71-- create namespaces inside LuaSocket namespace 72-- create namespaces inside LuaSocket namespace
72sourcet = {} 73local sourcet, sinkt = {}, {}
73sinkt = {} 74_M.sourcet = sourcet
75_M.sinkt = sinkt
74 76
75BLOCKSIZE = 2048 77_M.BLOCKSIZE = 2048
76 78
77sinkt["close-when-done"] = function(sock) 79sinkt["close-when-done"] = function(sock)
78 return base.setmetatable({ 80 return base.setmetatable({
@@ -102,7 +104,7 @@ end
102 104
103sinkt["default"] = sinkt["keep-open"] 105sinkt["default"] = sinkt["keep-open"]
104 106
105sink = choose(sinkt) 107_M.sink = _M.choose(sinkt)
106 108
107sourcet["by-length"] = function(sock, length) 109sourcet["by-length"] = function(sock, length)
108 return base.setmetatable({ 110 return base.setmetatable({
@@ -142,5 +144,6 @@ end
142 144
143sourcet["default"] = sourcet["until-closed"] 145sourcet["default"] = sourcet["until-closed"]
144 146
145source = choose(sourcet) 147_M.source = _M.choose(sourcet)
146 148
149return _M \ No newline at end of file