Software / code / prosody
Comparison
core/sessionmanager.lua @ 123:ebd65feb188c
Fix for not destroying sessions when connection closed.
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 23 Oct 2008 16:07:40 +0100 |
| parent | 118:76ac96c53ee5 |
| child | 124:7fee6b63abca |
comparison
equal
deleted
inserted
replaced
| 122:21f8d2175393 | 123:ebd65feb188c |
|---|---|
| 17 local newproxy = newproxy; | 17 local newproxy = newproxy; |
| 18 local getmetatable = getmetatable; | 18 local getmetatable = getmetatable; |
| 19 | 19 |
| 20 module "sessionmanager" | 20 module "sessionmanager" |
| 21 | 21 |
| 22 local open_sessions = 0; | |
| 23 | |
| 22 function new_session(conn) | 24 function new_session(conn) |
| 23 local session = { conn = conn, priority = 0, type = "c2s_unauthed" }; | 25 local session = { conn = conn, priority = 0, type = "c2s_unauthed" }; |
| 24 if true then | 26 if true then |
| 25 session.trace = newproxy(true); | 27 session.trace = newproxy(true); |
| 26 getmetatable(session.trace).__gc = function () print("Session got collected") end; | 28 getmetatable(session.trace).__gc = function () open_sessions = open_sessions - 1; print("Session got collected, now "..open_sessions.." sessions are allocated") end; |
| 27 end | 29 end |
| 30 open_sessions = open_sessions + 1; | |
| 28 local w = conn.write; | 31 local w = conn.write; |
| 29 session.send = function (t) w(tostring(t)); end | 32 session.send = function (t) w(tostring(t)); end |
| 30 return session; | 33 return session; |
| 31 end | 34 end |
| 32 | 35 |
| 33 function destroy_session(session) | 36 function destroy_session(session) |
| 34 if not (session and session.disconnect) then return; end | 37 session.log("info", "Destroying session"); |
| 35 log("debug", "Destroying session..."); | |
| 36 session.disconnect(); | |
| 37 if session.username then | 38 if session.username then |
| 38 if session.resource then | 39 if session.resource then |
| 39 hosts[session.host].sessions[session.username].sessions[session.resource] = nil; | 40 hosts[session.host].sessions[session.username].sessions[session.resource] = nil; |
| 40 end | 41 end |
| 41 local nomore = true; | 42 local nomore = true; |
| 51 for k in pairs(session) do | 52 for k in pairs(session) do |
| 52 if k ~= "trace" then | 53 if k ~= "trace" then |
| 53 session[k] = nil; | 54 session[k] = nil; |
| 54 end | 55 end |
| 55 end | 56 end |
| 56 collectgarbage("collect"); | |
| 57 collectgarbage("collect"); | |
| 58 collectgarbage("collect"); | |
| 59 collectgarbage("collect"); | |
| 60 collectgarbage("collect"); | |
| 61 end | 57 end |
| 62 | 58 |
| 63 function send_to_session(session, data) | 59 function send_to_session(session, data) |
| 64 log("debug", "Sending: %s", tostring(data)); | 60 log("debug", "Sending: %s", tostring(data)); |
| 65 session.conn.write(tostring(data)); | 61 session.conn.write(tostring(data)); |