luaevent/luaevent.lua

changeset 10
88ce07d62597
parent 3
5999243fab1d
child 11
8339f6236a3c
equal deleted inserted replaced
9:1f3b72ba96c9 10:88ce07d62597
8 local EV_READ = luaevent.core.EV_READ 8 local EV_READ = luaevent.core.EV_READ
9 local EV_WRITE = luaevent.core.EV_WRITE 9 local EV_WRITE = luaevent.core.EV_WRITE
10 local fair = false 10 local fair = false
11 11
12 local hookedObjectMt = false 12 local hookedObjectMt = false
13
14 -- Weak keys.. the keys are the client sockets
15 local clientTable = setmetatable({}, {'__mode', 'k'})
16 13
17 function send(sock, data, start, stop) 14 function send(sock, data, start, stop)
18 local s, err 15 local s, err
19 local from = start or 1 16 local from = start or 1
20 local sent = 0 17 local sent = 0
77 local client = sock:accept() 74 local client = sock:accept()
78 if client then 75 if client then
79 --cl[#cl + 1] = client 76 --cl[#cl + 1] = client
80 client:settimeout(0) 77 client:settimeout(0)
81 local coFunc = coroutine.wrap(clientCoroutine) 78 local coFunc = coroutine.wrap(clientCoroutine)
82 clientTable[client] = luaevent.core.addevent(client, coFunc, client, callback) 79 luaevent.core.addevent(client, coFunc, client, callback)
83 end 80 end
84 until false 81 until false
85 end 82 end
86 83
87 local oldAddEvent = luaevent.core.addevent 84 local oldAddEvent = luaevent.core.addevent
101 return item 98 return item
102 end 99 end
103 100
104 function addserver(sock, callback) 101 function addserver(sock, callback)
105 local coFunc = coroutine.wrap(serverCoroutine) 102 local coFunc = coroutine.wrap(serverCoroutine)
106 clientTable[sock] = luaevent.core.addevent(sock, coFunc, sock, callback) 103 luaevent.core.addevent(sock, coFunc, sock, callback)
107 end 104 end
108 function addthread(sock, func, ...) 105 function addthread(sock, func, ...)
109 local coFunc = coroutine.wrap(func) 106 local coFunc = coroutine.wrap(func)
110 clientTable[sock] = luaevent.core.addevent(sock, coFunc, ...) 107 luaevent.core.addevent(sock, coFunc, ...)
111 end 108 end
112 local _skt_mt = {__index = { 109 local _skt_mt = {__index = {
113 connect = function(self, ...) 110 connect = function(self, ...)
114 return connect(self.socket, ...) 111 return connect(self.socket, ...)
115 end, 112 end,
130 127
131 settimeout = function (self,time) 128 settimeout = function (self,time)
132 self.timeout=time 129 self.timeout=time
133 return 130 return
134 end, 131 end,
132 close = function(self)
133 self.socket:close()
134 end
135 }} 135 }}
136 function wrap(sock) 136 function wrap(sock)
137 return setmetatable({socket = sock}, _skt_mt) 137 return setmetatable({socket = sock}, _skt_mt)
138 end 138 end
139 loop = luaevent.core.loop 139 loop = luaevent.core.loop

mercurial