Menu

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

Download this file

107 lines (84 with data), 3.0 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
105
/**
* <strong>启动RServer连接</strong>
* <p>
*
* </p>
*
* @author: 汉娱网络.技术部.yangxf
* @date: 2008-3-3
* @version: 1.1
**/
package com.handjoys.socket;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.net.Socket;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import com.handjoys.conf.ConfigReader;
import com.handjoys.conf.ConfigParam;
import com.handjoys.logger.FileLogger;
import com.handjoys.Broadcaster;
import com.handjoys.GameServer;
public class RServerFactory {
private static Map<String, GSession> rserver=new HashMap<String, GSession>();
private static GameServer gs_instance;
private RServerFactory() {
}
public static boolean init(GameServer gs) {
gs_instance=gs;
FileLogger.info("\n\n--- [ RServer Starting ] ---\n");
Map rserverM=(HashMap)ConfigReader.getParam(ConfigParam.RSERVER);
String hostIp=(String)ConfigReader.getParam(ConfigParam.SERVERIP);
int hostPort=((Integer)(ConfigReader.getParam(ConfigParam.SERVERPORT))).intValue();
String hostName=(String)ConfigReader.getParam(ConfigParam.SERVERNAME);
int index=0;
for(Iterator ir=rserverM.keySet().iterator(); ir.hasNext(); ) {
String name=(String)ir.next();
String address=(String)rserverM.get(name);
String s[]=address.split(":");
String ip=s[0];
int port=Integer.parseInt(s[1]);
// 如果是本地地址, 则忽略
if(ip.equals(hostIp) && (port == hostPort))
continue;
index++;
try {
InetSocketAddress isa=new InetSocketAddress(ip, port);
SocketChannel sc=SocketChannel.open(isa);
// 发送session验证报文
String sessionID=hostName + "#" + index + "#" + hostIp + ":" + hostPort;
GSession gSession=new GSession(sessionID, sc);
String msg="<msg t='sys' s='" + sessionID + "'><body action='50000.20000' r='1'></body></msg>";
gs_instance.initRServer(msg, gSession);
rserver.put(name, gSession);
}catch(Exception e) {
//e.printStackTrace();
FileLogger.info("RServer " + address + " has been connect failed......");
continue;
}
FileLogger.info("RServer " + address + " has been connect success......");
}
return true;
}
public static GSession getRServer(String serverName) {
return (GSession)rserver.get(serverName);
}
public static boolean addRServer(String serverName, GSession session) {
Map rserverM=(HashMap)ConfigReader.getParam(ConfigParam.RSERVER);
if(rserverM.get(serverName) == null)
return false;
rserver.put(serverName, session);
return true;
}
public static boolean removeRServer(SocketChannel sc) {
for(Iterator ir=rserver.keySet().iterator(); ir.hasNext(); ) {
String name=(String)ir.next();
GSession gSession=(GSession)rserver.get(name);
if(sc == gSession.channel) {
ir.remove();
}
}
return true;
}
}
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.