Advertisement
toko214

LoginServer

Nov 20th, 2017
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.03 KB | None | 0 0
  1. /*
  2.  This file is part of the OdinMS Maple Story Server
  3.  Copyright (C) 2008 ~ 2010 Patrick Huy <patrick.huy@frz.cc>
  4.  Matthias Butz <matze@odinms.de>
  5.  Jan Christian Meyer <vimes@odinms.de>
  6.  
  7.  This program is free software: you can redistribute it and/or modify
  8.  it under the terms of the GNU Affero General Public License version 3
  9.  as published by the Free Software Foundation. You may not use, modify
  10.  or distribute this program under any other version of the
  11.  GNU Affero General Public License.
  12.  
  13.  This program is distributed in the hope that it will be useful,
  14.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  GNU Affero General Public License for more details.
  17.  
  18.  You should have received a copy of the GNU Affero General Public License
  19.  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20.  */
  21. package handling.login;
  22.  
  23. import constants.GameConstants;
  24. import constants.ServerConfig;
  25. import handling.MapleServerHandler;
  26. import handling.mina.MapleCodecFactory;
  27. import java.io.IOException;
  28. import java.net.InetSocketAddress;
  29. import java.util.HashMap;
  30. import java.util.HashSet;
  31. import java.util.Map;
  32. import org.apache.mina.common.ByteBuffer;
  33. import org.apache.mina.common.IoAcceptor;
  34. import org.apache.mina.common.SimpleByteBufferAllocator;
  35. import org.apache.mina.filter.codec.ProtocolCodecFilter;
  36. import org.apache.mina.transport.socket.nio.SocketAcceptor;
  37. import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;
  38. import tools.Triple;
  39.  
  40. public class LoginServer {
  41.  
  42.     public static final int PORT = 8484;
  43.     private static InetSocketAddress InetSocketadd;
  44.     private static IoAcceptor acceptor;
  45.     private static Map<Integer, Integer> load = new HashMap<>();
  46.     private static String serverName, eventMessage;
  47.     private static byte flag;
  48.     private static int maxCharacters, userLimit, usersOn = 0;
  49.     private static boolean finishedShutdown = true, adminOnly = false;
  50.     private static final HashMap<Integer, Triple<String, String, Integer>> loginAuth = new HashMap<>();
  51.     private static final HashSet<String> loginIPAuth = new HashSet<>();
  52.  
  53.     public static void putLoginAuth(int chrid, String ip, String tempIP, int channel) {
  54.         Triple<String, String, Integer> put = loginAuth.put(chrid, new Triple<>(ip, tempIP, channel));
  55.         loginIPAuth.add(ip);
  56.     }
  57.  
  58.     public static Triple<String, String, Integer> getLoginAuth(int chrid) {
  59.         return loginAuth.remove(chrid);
  60.     }
  61.  
  62.     public static boolean containsIPAuth(String ip) {
  63.         return loginIPAuth.contains(ip);
  64.     }
  65.  
  66.     public static void removeIPAuth(String ip) {
  67.         loginIPAuth.remove(ip);
  68.     }
  69.  
  70.     public static void addIPAuth(String ip) {
  71.         loginIPAuth.add(ip);
  72.     }
  73.  
  74.     public static final void addChannel(final int channel) {
  75.         load.put(channel, 0);
  76.     }
  77.  
  78.     public static final void removeChannel(final int channel) {
  79.         load.remove(channel);
  80.     }
  81.  
  82.     public static final void run_startup_configurations() {
  83.         System.out.print("Loading Login Server...");
  84.         userLimit = ServerConfig.userLimit;
  85.         serverName = ServerConfig.serverName;
  86.         eventMessage = ServerConfig.eventMessage;
  87.         flag = ServerConfig.flag;
  88.         adminOnly = ServerConfig.adminOnly;
  89.         maxCharacters = ServerConfig.maxCharacters;
  90.  
  91.         ByteBuffer.setUseDirectBuffers(false);
  92.         ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
  93.  
  94.         acceptor = new SocketAcceptor();
  95.         final SocketAcceptorConfig cfg = new SocketAcceptorConfig();
  96.         cfg.getSessionConfig().setTcpNoDelay(true);
  97.         cfg.setDisconnectOnUnbind(true);
  98.         cfg.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MapleCodecFactory()));
  99.  
  100.         try {
  101.             InetSocketadd = new InetSocketAddress(PORT);
  102.             acceptor.bind(InetSocketadd, new MapleServerHandler(), cfg);
  103.             System.out.println(" Complete!");
  104.             System.out.println("Login Server is listening on port " + PORT + ".");
  105.         } catch (IOException e) {
  106.             System.out.println(" Failed!");
  107.             System.err.println("Could not bind to port " + PORT + ": " + e);
  108.         }
  109.     }
  110.  
  111.     public static final void shutdown() {
  112.         if (finishedShutdown) {
  113.             return;
  114.         }
  115.         System.out.println("Shutting down login...");
  116.         acceptor.unbindAll();
  117.         finishedShutdown = true; //nothing. lol
  118.     }
  119.  
  120.     public static final String getServerName() {
  121.         return serverName;
  122.     }
  123.  
  124.     public static final String getTrueServerName() {
  125.         return serverName.substring(0, serverName.length() - (GameConstants.GMS ? 2 : 3));
  126.     }
  127.  
  128.     public static String getEventMessage() {
  129.         return eventMessage;
  130.     }
  131.  
  132.     public static int getMaxCharacters() {
  133.         return maxCharacters;
  134.     }
  135.  
  136.     public static Map<Integer, Integer> getLoad() {
  137.         return load;
  138.     }
  139.  
  140.     public static void setLoad(final Map<Integer, Integer> load_, final int usersOn_) {
  141.         load = load_;
  142.         usersOn = usersOn_;
  143.     }
  144.  
  145.     public static String getEventMessage(int world) { //TODO: Finish this
  146.         switch (world) {
  147.             case 0:
  148.                 return null;
  149.         }
  150.         return null;
  151.     }
  152.  
  153.     public static final void setFlag(final byte newflag) {
  154.         flag = newflag;
  155.     }
  156.  
  157.     public static final int getUserLimit() {
  158.         return userLimit;
  159.     }
  160.  
  161.     public static final int getUsersOn() {
  162.         return usersOn;
  163.     }
  164.  
  165.     public static final void setUserLimit(final int newLimit) {
  166.         userLimit = newLimit;
  167.     }
  168.  
  169.     public static final int getNumberOfSessions() {
  170.         return acceptor.getManagedSessions(InetSocketadd).size();
  171.     }
  172.  
  173.     public static final boolean isAdminOnly() {
  174.         return adminOnly;
  175.     }
  176.  
  177.     public static final boolean isShutdown() {
  178.         return finishedShutdown;
  179.     }
  180.  
  181.     public static final void setOn() {
  182.         finishedShutdown = false;
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement