Menu

[r2]: / socket / ConnectionCleanerTask.java  Maximize  Restore  History

Download this file

105 lines (90 with data), 3.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
/**
* <strong>清除长时间不活动的session</strong>
* <p>
*
*
* </p>
*
*
* @author: 汉娱网络.技术部.yangxf
* @date: 2007-12-24
* @version: 1.1
**/
package com.handjoys.socket;
import java.net.Socket;
import java.nio.channels.SocketChannel;
import java.util.Map;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.TimerTask;
import com.handjoys.Broadcaster;
import com.handjoys.logger.FileLogger;
import com.handjoys.conf.ConfigParam;
import com.handjoys.conf.ConfigReader;
import com.handjoys.fastdb.FastDB;
import com.handjoys.socket.GSession;
import com.handjoys.console.GameState;
import com.handjoys.console.MovableObject;
import com.handjoys.account.AccountState;
import com.handjoys.dbpool.DBProxy;
public class ConnectionCleanerTask extends TimerTask {
private int maxIdleTime=((Integer)(ConfigReader.getParam(ConfigParam.MAXUSERIDLETIME))).intValue();
private LinkedList<GSession> killableChannels;
private long lastTime;
private long currTime;
public ConnectionCleanerTask() {
}
public void run() {
killableChannels = new LinkedList<GSession>();
Map<String, MovableObject> channelList=GameState.getChannelList();
FileLogger.info("SessionCleaner starting......, channelList size=" + channelList.size());
Iterator it = channelList.keySet().iterator();
currTime = System.currentTimeMillis();
while(it.hasNext()) {
try {
String sessionID=(String)it.next();
MovableObject mo = (MovableObject)channelList.get(sessionID);
SocketChannel client = mo.client;
if(client != null && client.isConnected()){
// 已经加入房间了, 如果maxIdleTime时间内都没有发送消息, 则kill掉
// 该段代码只会在世界服务器运行
lastTime = mo.lastTime;
FileLogger.debug("lastTime=" + lastTime);
FileLogger.debug("currTime=" + currTime);
FileLogger.debug("maxIdleTime=" + maxIdleTime);
if( ((currTime - lastTime) > (long)maxIdleTime) && !mo.isAdmin ) {
addKillable(new GSession(sessionID, client));
}
}else {
// 与接入服务器已经断开, 无法通知IMServer
GameState.exitRoom(mo);
AccountState.logout(new GSession(sessionID, client));
}
}catch(Exception e){
e.printStackTrace();
}
}
killEmAll();
// 执行SQL语句, 防止数据库连接失效
FastDB.maintanceDBConnection();
DBProxy.checkConncetions();
}
private void addKillable(GSession session) {
killableChannels.add(session);
}
private void killEmAll() {
if(killableChannels.size() > 0) {
for(Iterator i = killableChannels.iterator(); i.hasNext();) {
GSession session = (GSession)i.next();
if(session != null) {
FileLogger.info("Session: " + session.sessionID + " was found idle and was disconnected.");
// 发断开连接的报文到接入服务器
String msg="<msg t='sls' s='@" + session.sessionID + "'><body action='0.0' r='1'></body></msg>";
Broadcaster.write(msg, session);
}
i.remove();
}
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.