plugins/mod_bosh.lua
changeset 7509 8cca24bea3e0
parent 7394 c381106173d0
child 7657 b40776ee2aef
equal deleted inserted replaced
7508:3d950ee0de35 7509:8cca24bea3e0
    61 
    61 
    62 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
    62 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
    63 local os_time = os.time;
    63 local os_time = os.time;
    64 
    64 
    65 -- All sessions, and sessions that have no requests open
    65 -- All sessions, and sessions that have no requests open
    66 local sessions, inactive_sessions = module:shared("sessions", "inactive_sessions");
    66 local sessions = module:shared("sessions");
    67 
    67 
    68 -- Used to respond to idle sessions (those with waiting requests)
    68 -- Used to respond to idle sessions (those with waiting requests)
    69 local waiting_requests = module:shared("waiting_requests");
       
    70 function on_destroy_request(request)
    69 function on_destroy_request(request)
    71 	log("debug", "Request destroyed: %s", tostring(request));
    70 	log("debug", "Request destroyed: %s", tostring(request));
    72 	waiting_requests[request] = nil;
       
    73 	local session = sessions[request.context.sid];
    71 	local session = sessions[request.context.sid];
    74 	if session then
    72 	if session then
    75 		local requests = session.requests;
    73 		local requests = session.requests;
    76 		for i, r in ipairs(requests) do
    74 		for i, r in ipairs(requests) do
    77 			if r == request then
    75 			if r == request then
    81 		end
    79 		end
    82 
    80 
    83 		-- If this session now has no requests open, mark it as inactive
    81 		-- If this session now has no requests open, mark it as inactive
    84 		local max_inactive = session.bosh_max_inactive;
    82 		local max_inactive = session.bosh_max_inactive;
    85 		if max_inactive and #requests == 0 then
    83 		if max_inactive and #requests == 0 then
    86 			inactive_sessions[session] = os_time() + max_inactive;
    84 			if session.inactive_timer then
       
    85 				session.inactive_timer:stop();
       
    86 			end
       
    87 			session.inactive_timer = module:add_timer(max_inactive, check_inactive, session, request.context,
       
    88 				"BOSH client silent for over "..max_inactive.." seconds");
    87 			(session.log or log)("debug", "BOSH session marked as inactive (for %ds)", max_inactive);
    89 			(session.log or log)("debug", "BOSH session marked as inactive (for %ds)", max_inactive);
    88 		end
    90 		end
       
    91 		if session.bosh_wait_timer then
       
    92 			session.bosh_wait_timer:stop();
       
    93 			session.bosh_wait_timer = nil;
       
    94 		end
       
    95 	end
       
    96 end
       
    97 
       
    98 function check_inactive(now, session, context, reason)
       
    99 	if not sessions.destroyed then
       
   100 		sessions[context.sid] = nil;
       
   101 		sm_destroy_session(session, reason);
    89 	end
   102 	end
    90 end
   103 end
    91 
   104 
    92 local function set_cross_domain_headers(response)
   105 local function set_cross_domain_headers(response)
    93 	local headers = response.headers;
   106 	local headers = response.headers;
   117 	response.context = context;
   130 	response.context = context;
   118 
   131 
   119 	local headers = response.headers;
   132 	local headers = response.headers;
   120 	headers.content_type = "text/xml; charset=utf-8";
   133 	headers.content_type = "text/xml; charset=utf-8";
   121 
   134 
   122 	if cross_domain and event.request.headers.origin then
   135 	if cross_domain and request.headers.origin then
   123 		set_cross_domain_headers(response);
   136 		set_cross_domain_headers(response);
   124 	end
   137 	end
   125 
   138 
   126 	-- stream:feed() calls the stream_callbacks, so all stanzas in
   139 	-- stream:feed() calls the stream_callbacks, so all stanzas in
   127 	-- the body are processed in this next line before it returns.
   140 	-- the body are processed in this next line before it returns.