LoginServer.java
资源名称:src.rar [点击查看]
上传用户:gwt600
上传日期:2021-06-03
资源大小:704k
文件大小:14k
源码类别:
游戏
开发平台:
Java
- /*
- This file is part of the OdinMS Maple Story Server
- Copyright (C) 2008 Patrick Huy <patrick.huy@frz.cc>
- Matthias Butz <matze@odinms.de>
- Jan Christian Meyer <vimes@odinms.de>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License version 3
- as published by the Free Software Foundation. You may not use, modify
- or distribute this program under any other version of the
- GNU Affero General Public License.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package net.sf.odinms.net.login;
- import java.io.FileReader;
- import java.io.IOException;
- import java.lang.management.ManagementFactory;
- import java.net.InetSocketAddress;
- import java.rmi.RemoteException;
- import java.rmi.registry.LocateRegistry;
- import java.rmi.registry.Registry;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Properties;
- import java.util.Set;
- import javax.management.MBeanServer;
- import javax.management.ObjectName;
- import javax.rmi.ssl.SslRMIClientSocketFactory;
- import net.sf.odinms.database.DatabaseConnection;
- import net.sf.odinms.net.MapleServerHandler;
- import net.sf.odinms.net.PacketProcessor;
- import net.sf.odinms.net.login.remote.LoginWorldInterface;
- import net.sf.odinms.net.mina.MapleCodecFactory;
- import net.sf.odinms.net.world.remote.WorldLoginInterface;
- import net.sf.odinms.net.world.remote.WorldRegistry;
- import net.sf.odinms.server.TimerManager;
- import org.apache.mina.common.ByteBuffer;
- import org.apache.mina.common.IoAcceptor;
- import org.apache.mina.common.SimpleByteBufferAllocator;
- import org.apache.mina.filter.codec.ProtocolCodecFilter;
- import org.apache.mina.transport.socket.nio.SocketAcceptor;
- import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class LoginServer implements Runnable, LoginServerMBean {
- public static int PORT = 8484;
- private IoAcceptor acceptor;
- static final Logger log = LoggerFactory.getLogger(LoginServer.class);
- private static WorldRegistry worldRegistry = null;
- private Map<Integer, String> channelServer = new HashMap<Integer, String>();
- private LoginWorldInterface lwi;
- private WorldLoginInterface wli;
- private Properties prop = new Properties();
- private Properties initialProp = new Properties();
- private Boolean worldReady = Boolean.TRUE;
- private Properties subnetInfo = new Properties();
- private Map<Integer, Integer> load = new HashMap<Integer, Integer>();
- private String serverName;
- private String eventMessage;
- int flag;
- int maxCharacters;
- int userLimit;
- int loginInterval;
- private long rankingInterval;
- private boolean AutoReg;
- private boolean serverCheck;
- private static LoginServer instance = new LoginServer();
- private int worlds;
- private boolean autoReg, resetStats;
- static {
- MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
- try {
- mBeanServer.registerMBean(instance, new ObjectName("net.sf.odinms.net.login:type=LoginServer,name=LoginServer"));
- } catch (Exception e) {
- log.error("MBEAN ERROR", e);
- // not taht bad...
- }
- }
- private LoginServer() {
- }
- public static LoginServer getInstance() {
- return instance;
- }
- public Set<Integer> getChannels() {
- return channelServer.keySet();
- }
- public void addChannel(int channel, String ip) {
- channelServer.put(channel, ip);
- load.put(channel, 0);
- }
- public void removeChannel(int channel) {
- channelServer.remove(channel);
- load.remove(channel);
- }
- public String getIP(int channel) {
- return channelServer.get(channel);
- }
- public int numberOfWorlds() {
- return worlds;
- }
- public int getPossibleLogins() {
- int ret = 0;
- try {
- Connection con = DatabaseConnection.getConnection();
- PreparedStatement limitCheck = con.prepareStatement("SELECT COUNT(*) FROM accounts WHERE loggedin > 1 AND gm=0");
- ResultSet rs = limitCheck.executeQuery();
- if (rs.next()) {
- int usersOn = rs.getInt(1);
- // log.info("userson: " + usersOn + ", limit: " + userLimit);
- if (usersOn < userLimit) {
- ret = userLimit - usersOn;
- }
- }
- rs.close();
- limitCheck.close();
- } catch (Exception ex) {
- log.error("登陆限制错误:", ex);
- }
- return ret;
- }
- public void reconnectWorld() {
- // check if the connection is really gone
- try {
- wli.isAvailable();
- } catch (RemoteException ex) {
- synchronized (worldReady) {
- worldReady = Boolean.FALSE;
- }
- synchronized (lwi) {
- synchronized (worldReady) {
- if (worldReady) {
- return;
- }
- }
- System.err.println("重新连接世界的服务器");
- synchronized (wli) {
- // completely re-establish the rmi connection
- try {
- FileReader fileReader = new FileReader(System.getProperty("net.sf.odinms.login.config"));
- initialProp.load(fileReader);
- fileReader.close();
- Registry registry = LocateRegistry.getRegistry(initialProp.getProperty("net.sf.odinms.world.host"),
- Registry.REGISTRY_PORT, new SslRMIClientSocketFactory());
- worldRegistry = (WorldRegistry) registry.lookup("WorldRegistry");
- lwi = new LoginWorldInterfaceImpl();
- wli = worldRegistry.registerLoginServer(initialProp.getProperty("net.sf.odinms.login.key"), lwi);
- Properties dbProp = new Properties();
- fileReader = new FileReader("db.properties");
- dbProp.load(fileReader);
- fileReader.close();
- DatabaseConnection.setProps(dbProp);
- DatabaseConnection.getConnection();
- prop = wli.getWorldProperties();
- userLimit = Integer.parseInt(prop.getProperty("net.sf.odinms.login.userlimit"));
- serverName = prop.getProperty("net.sf.odinms.world.serverName");
- eventMessage = prop.getProperty("net.sf.odinms.login.eventMessage");
- flag = Integer.parseInt(prop.getProperty("net.sf.odinms.login.flag"));
- maxCharacters = Integer.parseInt(prop.getProperty("net.sf.odinms.login.maxCharacters"));
- worlds = Integer.parseInt(prop.getProperty("net.sf.odinms.world.numberOfWorlds"));
- resetStats = Boolean.parseBoolean(prop.getProperty("net.sf.odinms.login.resetStats", "false"));
- serverCheck = Boolean.parseBoolean(prop.getProperty("net.sf.odinms.login.serverCheck", "false"));
- try {
- fileReader = new FileReader("subnet.properties");
- subnetInfo.load(fileReader);
- fileReader.close();
- } catch (Exception e) {
- log.info("无法加载的子网配置,回落至世界默认", e);
- }
- } catch (Exception e) {
- System.err.println("重新连接失败:"+ e);
- }
- worldReady = Boolean.TRUE;
- }
- }
- synchronized (worldReady) {
- worldReady.notifyAll();
- }
- }
- }
- public boolean getAutoRegister() {
- return autoReg;
- }
- @Override
- public void run() {
- try {
- FileReader fileReader = new FileReader(System.getProperty("net.sf.odinms.login.config"));
- initialProp.load(fileReader);
- fileReader.close();
- Registry registry = LocateRegistry.getRegistry(initialProp.getProperty("net.sf.odinms.world.host"),
- Registry.REGISTRY_PORT, new SslRMIClientSocketFactory());
- worldRegistry = (WorldRegistry) registry.lookup("WorldRegistry");
- lwi = new LoginWorldInterfaceImpl();
- wli = worldRegistry.registerLoginServer(initialProp.getProperty("net.sf.odinms.login.key"), lwi);
- Properties dbProp = new Properties();
- fileReader = new FileReader("db.properties");
- dbProp.load(fileReader);
- fileReader.close();
- DatabaseConnection.setProps(dbProp);
- DatabaseConnection.getConnection();
- prop = wli.getWorldProperties();
- userLimit = Integer.parseInt(prop.getProperty("net.sf.odinms.login.userlimit"));
- serverName = prop.getProperty("net.sf.odinms.world.serverName");
- eventMessage = prop.getProperty("net.sf.odinms.login.eventMessage");
- flag = Integer.parseInt(prop.getProperty("net.sf.odinms.login.flag"));
- maxCharacters = Integer.parseInt(prop.getProperty("net.sf.odinms.login.maxCharacters"));
- serverCheck = Boolean.parseBoolean(prop.getProperty("net.sf.odinms.login.serverCheck", "false"));
- worlds = Integer.parseInt(prop.getProperty("net.sf.odinms.world.numberOfWorlds"));
- resetStats = Boolean.parseBoolean(prop.getProperty("net.sf.odinms.login.resetStats", "false"));
- try {
- fileReader = new FileReader("subnet.properties");
- subnetInfo.load(fileReader);
- fileReader.close();
- } catch (Exception e) {
- log.trace("无法加载的子网配置,回落至世界默认", e);
- }
- } catch (Exception e) {
- throw new RuntimeException("无法连接到世界服务器.", e);
- }
- ByteBuffer.setUseDirectBuffers(false);
- ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
- acceptor = new SocketAcceptor();
- SocketAcceptorConfig cfg = new SocketAcceptorConfig();
- // cfg.setThreadModel(ThreadModel.MANUAL);
- // cfg.getFilterChain().addLast("logger", new LoggingFilter());
- // Loginserver is still on the default threadmodel so no executor filter here...
- // ExecutorService executor = new ThreadPoolExecutor(4, 8, 60, TimeUnit.SECONDS,
- // new LinkedBlockingQueue<Runnable>());
- // cfg.getFilterChain().addLast("executor", new ExecutorFilter(executor));
- cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MapleCodecFactory()));
- TimerManager tMan = TimerManager.getInstance();
- tMan.start();
- loginInterval = Integer.parseInt(prop.getProperty("net.sf.odinms.login.interval"));
- PORT = Integer.parseInt(prop.getProperty("net.sf.odinms.login.port"));
- tMan.register(LoginWorker.getInstance(), loginInterval);
- rankingInterval = Long.parseLong(prop.getProperty("net.sf.odinms.login.ranking.interval"));
- tMan.register(new RankingWorker(), rankingInterval);
- try {
- acceptor.bind(new InetSocketAddress(PORT), new MapleServerHandler(PacketProcessor.getProcessor(PacketProcessor.Mode.LOGINSERVER)), cfg);
- System.out.println("游戏监听端口: " + PORT );
- } catch (IOException e) {
- System.out.println("访问游戏端口: "+ PORT +" 失败"+ e);
- }
- }
- public void shutdown() {
- System.out.println("关闭中...");
- try {
- worldRegistry.deregisterLoginServer(lwi);
- } catch (RemoteException e) {
- // doesn't matter we're shutting down anyway
- }
- TimerManager.getInstance().stop();
- System.exit(0);
- }
- public WorldLoginInterface getWorldInterface() {
- synchronized (worldReady) {
- while (!worldReady) {
- try {
- worldReady.wait();
- } catch (InterruptedException e) {
- }
- }
- }
- return wli;
- }
- public static void main(String args[]) {
- try {
- LoginServer.getInstance().run();
- } catch (Exception ex) {
- log.error("Error initializing loginserver", ex);
- }
- }
- public int getLoginInterval() {
- return loginInterval;
- }
- public Properties getSubnetInfo() {
- return subnetInfo;
- }
- public int getUserLimit() {
- return userLimit;
- }
- public String getServerName() {
- return serverName;
- }
- @Override
- public String getEventMessage() {
- return eventMessage;
- }
- @Override
- public int getFlag() {
- return flag;
- }
- public int getMaxCharacters() {
- return maxCharacters;
- }
- public Map<Integer, Integer> getLoad() {
- return load;
- }
- public void setLoad(Map<Integer, Integer> load) {
- this.load = load;
- }
- @Override
- public void setEventMessage(String newMessage) {
- this.eventMessage = newMessage;
- }
- @Override
- public void setFlag(int newflag) {
- flag = newflag;
- }
- @Override
- public int getNumberOfSessions() {
- return acceptor.getManagedSessions(new InetSocketAddress(PORT)).size();
- }
- @Override
- public void setUserLimit(int newLimit) {
- userLimit = newLimit;
- }
- public void setServerCheck(boolean set) {
- serverCheck = set;
- }
- public boolean isServerCheck() {
- return serverCheck;
- }
- }