comparison src/socket.lua @ 0:4b915342e2a8

LuaSocket 2.0.2 + CMake build description.
author Eric Wing <ewing . public |-at-| gmail . com>
date Tue, 26 Aug 2008 18:40:01 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4b915342e2a8
1 -----------------------------------------------------------------------------
2 -- LuaSocket helper module
3 -- Author: Diego Nehab
4 -- RCS ID: $Id: socket.lua,v 1.22 2005/11/22 08:33:29 diego Exp $
5 -----------------------------------------------------------------------------
6
7 -----------------------------------------------------------------------------
8 -- Declare module and import dependencies
9 -----------------------------------------------------------------------------
10 local base = _G
11 local string = require("string")
12 local math = require("math")
13 local socket = require("socket.core")
14 module("socket")
15
16 -----------------------------------------------------------------------------
17 -- Exported auxiliar functions
18 -----------------------------------------------------------------------------
19 function connect(address, port, laddress, lport)
20 local sock, err = socket.tcp()
21 if not sock then return nil, err end
22 if laddress then
23 local res, err = sock:bind(laddress, lport, -1)
24 if not res then return nil, err end
25 end
26 local res, err = sock:connect(address, port)
27 if not res then return nil, err end
28 return sock
29 end
30
31 function bind(host, port, backlog)
32 local sock, err = socket.tcp()
33 if not sock then return nil, err end
34 sock:setoption("reuseaddr", true)
35 local res, err = sock:bind(host, port)
36 if not res then return nil, err end
37 res, err = sock:listen(backlog)
38 if not res then return nil, err end
39 return sock
40 end
41
42 try = newtry()
43
44 function choose(table)
45 return function(name, opt1, opt2)
46 if base.type(name) ~= "string" then
47 name, opt1, opt2 = "default", name, opt1
48 end
49 local f = table[name or "nil"]
50 if not f then base.error("unknown key (".. base.tostring(name) ..")", 3)
51 else return f(opt1, opt2) end
52 end
53 end
54
55 -----------------------------------------------------------------------------
56 -- Socket sources and sinks, conforming to LTN12
57 -----------------------------------------------------------------------------
58 -- create namespaces inside LuaSocket namespace
59 sourcet = {}
60 sinkt = {}
61
62 BLOCKSIZE = 2048
63
64 sinkt["close-when-done"] = function(sock)
65 return base.setmetatable({
66 getfd = function() return sock:getfd() end,
67 dirty = function() return sock:dirty() end
68 }, {
69 __call = function(self, chunk, err)
70 if not chunk then
71 sock:close()
72 return 1
73 else return sock:send(chunk) end
74 end
75 })
76 end
77
78 sinkt["keep-open"] = function(sock)
79 return base.setmetatable({
80 getfd = function() return sock:getfd() end,
81 dirty = function() return sock:dirty() end
82 }, {
83 __call = function(self, chunk, err)
84 if chunk then return sock:send(chunk)
85 else return 1 end
86 end
87 })
88 end
89
90 sinkt["default"] = sinkt["keep-open"]
91
92 sink = choose(sinkt)
93
94 sourcet["by-length"] = function(sock, length)
95 return base.setmetatable({
96 getfd = function() return sock:getfd() end,
97 dirty = function() return sock:dirty() end
98 }, {
99 __call = function()
100 if length <= 0 then return nil end
101 local size = math.min(socket.BLOCKSIZE, length)
102 local chunk, err = sock:receive(size)
103 if err then return nil, err end
104 length = length - string.len(chunk)
105 return chunk
106 end
107 })
108 end
109
110 sourcet["until-closed"] = function(sock)
111 local done
112 return base.setmetatable({
113 getfd = function() return sock:getfd() end,
114 dirty = function() return sock:dirty() end
115 }, {
116 __call = function()
117 if done then return nil end
118 local chunk, err, partial = sock:receive(socket.BLOCKSIZE)
119 if not err then return chunk
120 elseif err == "closed" then
121 sock:close()
122 done = 1
123 return partial
124 else return nil, err end
125 end
126 })
127 end
128
129
130 sourcet["default"] = sourcet["until-closed"]
131
132 source = choose(sourcet)
133