Jump to content

[GUIA] Migração para mmocore assíncrono


KhayrusS

Recommended Posts

Esse guia tem como objetivo apresentar de maneira sucinta os passos necessários para utilizar o mmocore assíncrono. Não sabe o que é? dê uma olhada nesse tópico.

Estarei utilizando L2jServer Interlude para realizar a migração. Outras versões podem ter algumas diferenças, mas nada muito específico que este guia não possa cobrir.

  • Remover o mmocore da pasta lib. Nesse momento vários erros aparecerão na IDE, caso esteja usando

4U0b2Vq.png

  • Adicionar o mmocore-2.0 no projeto
    • A versão compilada está disponível aqui
    • Caso prefira compilar: github
  • Realizar as alterações em L2GameServerPacket
Index: L2J_Server/java/net/sf/l2j/gameserver/serverpackets/L2GameServerPacket.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/serverpackets/L2GameServerPacket.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/serverpackets/L2GameServerPacket.java	(date 1538483553000)
@@ -21,12 +21,12 @@
 
 import net.sf.l2j.gameserver.network.L2GameClient;
 
-import org.mmocore.network.SendablePacket;
+import org.l2j.mmocore.WritablePacket;
 
 /**
  * @author KenM
  */
-public abstract class L2GameServerPacket extends SendablePacket<L2GameClient>
+public abstract class L2GameServerPacket extends WritablePacket<L2GameClient>
 {
 	private static final Logger _log = Logger.getLogger(L2GameServerPacket.class.getName());

 

  • Realizar as alterações em L2GameClientPacket
Index: L2J_Server/java/net/sf/l2j/gameserver/clientpackets/L2GameClientPacket.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/clientpackets/L2GameClientPacket.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/clientpackets/L2GameClientPacket.java	(date 1538485300000)
@@ -24,13 +24,13 @@
 import net.sf.l2j.gameserver.serverpackets.ActionFailed;
 import net.sf.l2j.gameserver.serverpackets.L2GameServerPacket;
 
-import org.mmocore.network.ReceivablePacket;
+import org.l2j.mmocore.ReadablePacket;
 
 /**
  * Packets received by the game server from clients
  * @author KenM
  */
-public abstract class L2GameClientPacket extends ReceivablePacket<L2GameClient>
+public abstract class L2GameClientPacket extends ReadablePacket<L2GameClient>
 {
 	private static final Logger _log = Logger.getLogger(L2GameClientPacket.class.getName());

 

  •  Realizar as alterações no L2GameClient
Index: L2J_Server/java/net/sf/l2j/gameserver/network/L2GameClient.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/network/L2GameClient.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/network/L2GameClient.java	(date 1538486040000)
@@ -45,14 +45,14 @@
 import net.sf.l2j.gameserver.serverpackets.UserInfo;
 import net.sf.l2j.util.EventData;
 
-import org.mmocore.network.MMOClient;
-import org.mmocore.network.MMOConnection;
+import org.l2j.mmocore.Client;
+import org.l2j.mmocore.Connection;
 
 /**
  * Represents a client connected on Game Server
  * @author KenM
  */
-public final class L2GameClient extends MMOClient<MMOConnection<L2GameClient>>
+public final class L2GameClient extends Client<Connection<L2GameClient>>
 {
 	protected static final Logger _log = Logger.getLogger(L2GameClient.class.getName());
 	
@@ -89,7 +89,7 @@
 	public byte packetsSentInSec = 0;
 	public int packetsSentStartTick = 0;
 	
-	public L2GameClient(MMOConnection<L2GameClient> con)
+	public L2GameClient(Connection<L2GameClient> con)
 	{
 		super(con);
 		state = GameClientState.CONNECTED;
@@ -121,18 +121,17 @@
 	}
 	
 	@Override
-	public boolean decrypt(ByteBuffer buf, int size)
+	public boolean decrypt(byte[] buf, int offset, int size)
 	{
-		crypt.decrypt(buf.array(), buf.position(), size);
+		crypt.decrypt(buf, offset, size);
 		return true;
 	}
 	
 	@Override
-	public boolean encrypt(final ByteBuffer buf, final int size)
+	public int encrypt(final byte[] buf, final int offset, final int size)
 	{
-		crypt.encrypt(buf.array(), buf.position(), size);
-		buf.position(buf.position() + size);
-		return true;
+		crypt.encrypt(buf, offset, size);
+		return size;
 	}
 	
 	public L2PcInstance getActiveChar()
@@ -180,7 +179,7 @@
 	
 	public void sendPacket(L2GameServerPacket gsp)
 	{
-		getConnection().sendPacket(gsp);
+		writePacket(gsp);
 		gsp.runImpl();
 	}
 	
@@ -467,11 +466,6 @@
 		}
 	}
 	
-	public void close(L2GameServerPacket gsp)
-	{
-		getConnection().close(gsp);
-	}
-	
 	/**
 	 * @param charslot
 	 * @return
@@ -486,15 +480,15 @@
 		Integer objectId = _charSlotMapping.get(charslot);
 		return objectId.intValue();
 	}
-	
+
+
 	@Override
-	protected void onForcedDisconnection()
-	{
-		_log.info("Client " + toString() + " disconnected abnormally.");
+	public void onConnected() {
+
 	}
-	
+
 	/**
-	 * @see org.mmocore.network.MMOClient#onDisconnection()
+	 * @see Client#onDisconnection()
 	 */
 	@Override
 	protected void onDisconnection()
@@ -582,15 +576,15 @@
 	{
 		try
 		{
-			InetAddress address = getConnection().getInetAddress();
+			String address = getHostAddress();
 			switch (getState())
 			{
 				case CONNECTED:
-					return "[IP: " + (address == null ? "disconnected" : address.getHostAddress()) + "]";
+					return "[IP: " + (address.isEmpty() ? "disconnected" : address) + "]";
 				case AUTHED:
-					return "[Account: " + getAccountName() + " - IP: " + (address == null ? "disconnected" : address.getHostAddress()) + "]";
+					return "[Account: " + getAccountName() + " - IP: " + (address.isEmpty() ? "disconnected" : address) + "]";
 				case IN_GAME:
-					return "[Character: " + (getActiveChar() == null ? "disconnected" : getActiveChar().getName()) + " - Account: " + getAccountName() + " - IP: " + (address == null ? "disconnected" : address.getHostAddress()) + "]";
+					return "[Character: " + (getActiveChar() == null ? "disconnected" : getActiveChar().getName()) + " - Account: " + getAccountName() + " - IP: " + (address.isEmpty() ? "disconnected" : address) + "]";
 				default:
 					throw new IllegalStateException("Missing state on switch");
 			}

 

  •   Realizar as alterações em L2LoginServerPacket
Index: L2J_Server/java/net/sf/l2j/loginserver/serverpackets/L2LoginServerPacket.java
===================================================================
--- L2J_Server/java/net/sf/l2j/loginserver/serverpackets/L2LoginServerPacket.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/loginserver/serverpackets/L2LoginServerPacket.java	(date 1538486372000)
@@ -19,12 +19,12 @@
 
 import net.sf.l2j.loginserver.L2LoginClient;
 
-import org.mmocore.network.SendablePacket;
+import org.l2j.mmocore.WritablePacket;
 
 /**
  * @author KenM
  */
-public abstract class L2LoginServerPacket extends SendablePacket<L2LoginClient>
+public abstract class L2LoginServerPacket extends WritablePacket<L2LoginClient>
 {
 	
 }

 

  •   Realizar as alterações em L2LoginClientPacket
Index: L2J_Server/java/net/sf/l2j/loginserver/clientpackets/L2LoginClientPacket.java
===================================================================
--- L2J_Server/java/net/sf/l2j/loginserver/clientpackets/L2LoginClientPacket.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/loginserver/clientpackets/L2LoginClientPacket.java	(date 1538486503000)
@@ -21,12 +21,12 @@
 
 import net.sf.l2j.loginserver.L2LoginClient;
 
-import org.mmocore.network.ReceivablePacket;
+import org.l2j.mmocore.ReadablePacket;
 
 /**
  * @author KenM
  */
-public abstract class L2LoginClientPacket extends ReceivablePacket<L2LoginClient>
+public abstract class L2LoginClientPacket extends ReadablePacket<L2LoginClient>
 {
 	private static Logger _log = Logger.getLogger(L2LoginClientPacket.class.getName());
 	

 

  •   Realizar as alterações no L2LoginClient
Index: L2J_Server/java/net/sf/l2j/loginserver/L2LoginClient.java
===================================================================
--- L2J_Server/java/net/sf/l2j/loginserver/L2LoginClient.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/loginserver/L2LoginClient.java	(date 1538486988000)
@@ -18,14 +18,13 @@
 package net.sf.l2j.loginserver;
 
 import java.io.IOException;
-import java.net.InetAddress;
-import java.nio.ByteBuffer;
 import java.security.interfaces.RSAPrivateKey;
 import java.util.logging.Logger;
 
 import net.sf.l2j.Config;
 import net.sf.l2j.loginserver.crypt.LoginCrypt;
 import net.sf.l2j.loginserver.crypt.ScrambledKeyPair;
+import net.sf.l2j.loginserver.serverpackets.Init;
 import net.sf.l2j.loginserver.serverpackets.L2LoginServerPacket;
 import net.sf.l2j.loginserver.serverpackets.LoginFail;
 import net.sf.l2j.loginserver.serverpackets.LoginFail.LoginFailReason;
@@ -33,15 +32,15 @@
 import net.sf.l2j.loginserver.serverpackets.PlayFail.PlayFailReason;
 import net.sf.l2j.util.Rnd;
 
-import org.mmocore.network.MMOClient;
-import org.mmocore.network.MMOConnection;
-import org.mmocore.network.SendablePacket;
+import org.l2j.mmocore.Client;
+import org.l2j.mmocore.Connection;
+import org.l2j.mmocore.WritablePacket;
 
 /**
  * Represents a client connected into the LoginServer
  * @author KenM
  */
-public final class L2LoginClient extends MMOClient<MMOConnection<L2LoginClient>>
+public final class L2LoginClient extends Client<Connection<L2LoginClient>>
 {
 	private static Logger _log = Logger.getLogger(L2LoginClient.class.getName());
 	
@@ -72,11 +71,11 @@
 	/**
 	 * @param con
 	 */
-	public L2LoginClient(MMOConnection<L2LoginClient> con)
+	public L2LoginClient(Connection<L2LoginClient> con)
 	{
 		super(con);
 		_state = LoginClientState.CONNECTED;
-		String ip = getConnection().getInetAddress().getHostAddress();
+		String ip = getHostAddress();
 		
 		// TODO unhardcode this
 		if (ip.startsWith("192.168") || ip.startsWith("10.0") || ip.equals("127.0.0.1"))
@@ -98,53 +97,48 @@
 	}
 	
 	/**
-	 * @see #decrypt(java.nio.ByteBuffer, int)
+	 * @see Client#decrypt(byte[], int, int)
 	 */
 	@Override
-	public boolean decrypt(ByteBuffer buf, int size)
+	public boolean decrypt(byte[] buf, int offset, int size)
 	{
 		boolean ret = false;
 		try
 		{
-			ret = _loginCrypt.decrypt(buf.array(), buf.position(), size);
+			ret = _loginCrypt.decrypt(buf, offset, size);
 		}
 		catch (IOException e)
 		{
 			e.printStackTrace();
-			super.getConnection().close((SendablePacket<L2LoginClient>) null);
+			close((WritablePacket<L2LoginClient>) null);
 			return false;
 		}
 		
 		if (!ret)
 		{
-			byte[] dump = new byte[size];
-			System.arraycopy(buf.array(), buf.position(), dump, 0, size);
-			_log.warning("Wrong checksum from client: " + toString());
-			super.getConnection().close((SendablePacket<L2LoginClient>) null);
+			close((WritablePacket<L2LoginClient>) null);
 		}
 		
 		return ret;
 	}
 	
 	/**
-	 * @see #encrypt(java.nio.ByteBuffer, int)
+	 * @see Client#encrypt(byte[], int, int)
 	 */
 	@Override
-	public boolean encrypt(ByteBuffer buf, int size)
+	public int encrypt(byte[] buf, int offset, int size)
 	{
-		final int offset = buf.position();
+		int encryptedSize = -1;
 		try
 		{
-			size = _loginCrypt.encrypt(buf.array(), offset, size);
+			encryptedSize = _loginCrypt.encrypt(buf, offset, size);
 		}
 		catch (IOException e)
 		{
 			e.printStackTrace();
-			return false;
 		}
-		
-		buf.position(offset + size);
-		return true;
+
+		return encryptedSize;
 	}
 	
 	public LoginClientState getState()
@@ -234,26 +228,26 @@
 	
 	public void sendPacket(L2LoginServerPacket lsp)
 	{
-		getConnection().sendPacket(lsp);
+		writePacket(lsp);
 	}
 	
 	public void close(LoginFailReason reason)
 	{
-		getConnection().close(new LoginFail(reason));
+		super.close(new LoginFail(reason));
 	}
 	
 	public void close(PlayFailReason reason)
 	{
-		getConnection().close(new PlayFail(reason));
-	}
-	
-	public void close(L2LoginServerPacket lsp)
-	{
-		getConnection().close(lsp);
+		super.close(new PlayFail(reason));
 	}
-	
-	/**
-	 * @see org.mmocore.network.MMOClient#onDisconnection()
+
+	@Override
+	public void onConnected() {
+		sendPacket(new Init(this));
+	}
+
+	/**
+	 * @see Client#onDisconnection()
 	 */
 	@Override
 	protected void onDisconnection()
@@ -272,25 +266,16 @@
 			LoginController.getInstance().removeAuthedLoginClient(getAccount());
 		}
 	}
-	
-	/**
-	 * @see org.mmocore.network.MMOClient#onForcedDisconnection()
-	 */
-	@Override
-	protected void onForcedDisconnection()
-	{
-		// TODO Auto-generated method stub
-		
-	}
+
 	
 	@Override
 	public String toString()
 	{
-		InetAddress address = getConnection().getInetAddress();
+		String address = getHostAddress();
 		if (getState() == LoginClientState.AUTHED_LOGIN)
 		{
-			return "[" + getAccount() + " (" + (address == null ? "disconnected" : address.getHostAddress()) + ")]";
+			return "[" + getAccount() + " (" + (address.isEmpty() ? "disconnected" : address) + ")]";
 		}
-		return "[" + (address == null ? "disconnected" : address.getHostAddress()) + "]";
+		return "[" + (address.isEmpty() ? "disconnected" : address) + "]";
 	}
 }

 

  •   Realizar as alterações em SelectorHelper
Index: L2J_Server/java/net/sf/l2j/loginserver/SelectorHelper.java
===================================================================
--- L2J_Server/java/net/sf/l2j/loginserver/SelectorHelper.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/loginserver/SelectorHelper.java	(date 1538498336000)
@@ -17,23 +17,20 @@
  */
 package net.sf.l2j.loginserver;
 
-import java.nio.channels.SocketChannel;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.AsynchronousSocketChannel;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
-import net.sf.l2j.loginserver.serverpackets.Init;
 
-import org.mmocore.network.IAcceptFilter;
-import org.mmocore.network.IClientFactory;
-import org.mmocore.network.IMMOExecutor;
-import org.mmocore.network.MMOConnection;
-import org.mmocore.network.ReceivablePacket;
+import org.l2j.mmocore.*;
 
 /**
  * @author KenM
  */
-public class SelectorHelper implements IMMOExecutor<L2LoginClient>, IClientFactory<L2LoginClient>, IAcceptFilter
+public class SelectorHelper implements PacketExecutor<L2LoginClient>, ClientFactory<L2LoginClient>, ConnectionFilter
 {
 	private final ThreadPoolExecutor _generalPacketsThreadPool;
 	
@@ -43,22 +40,27 @@
 	}
 	
 	@Override
-	public void execute(ReceivablePacket<L2LoginClient> packet)
+	public void execute(ReadablePacket<L2LoginClient> packet)
 	{
 		_generalPacketsThreadPool.execute(packet);
 	}
 	
 	@Override
-	public L2LoginClient create(MMOConnection<L2LoginClient> con)
+	public L2LoginClient create(Connection<L2LoginClient> con)
 	{
 		L2LoginClient client = new L2LoginClient(con);
-		client.sendPacket(new Init(client));
 		return client;
 	}
 	
 	@Override
-	public boolean accept(SocketChannel sc)
+	public boolean accept(AsynchronousSocketChannel sc)
 	{
-		return !LoginController.getInstance().isBannedAddress(sc.socket().getInetAddress());
+		try {
+			InetSocketAddress socketAddress = (InetSocketAddress) sc.getRemoteAddress();
+			return !LoginController.getInstance().isBannedAddress(socketAddress.getAddress());
+		} catch (IOException e) {
+			return false;
+		}
+
 	}
 }

 

  • Realizar as alterações no LoginController
Index: L2J_Server/java/net/sf/l2j/loginserver/LoginController.java
===================================================================
--- L2J_Server/java/net/sf/l2j/loginserver/LoginController.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/loginserver/LoginController.java	(date 1538498717000)
@@ -68,7 +68,7 @@
 	
 	private final Map<InetAddress, BanInfo> _bannedIps = new FastMap<InetAddress, BanInfo>().shared();
 	
-	private final Map<InetAddress, FailedLoginAttempt> _hackProtection;
+	private final Map<String, FailedLoginAttempt> _hackProtection;
 	
 	protected ScrambledKeyPair[] _keyPairs;
 	
@@ -476,12 +476,12 @@
 	public boolean loginValid(String user, String password, L2LoginClient client)
 	{
 		boolean ok = false;
-		InetAddress address = client.getConnection().getInetAddress();
+		String address = client.getHostAddress();
 		// log it anyway
-		Log.add("'" + (user == null ? "null" : user) + "' " + (address == null ? "null" : address.getHostAddress()), "logins_ip");
+		Log.add("'" + (user == null ? "null" : user) + "' " + (address.isEmpty() ? "null" : address), "logins_ip");
 		
 		// player disconnected meanwhile
-		if (address == null)
+		if (address.isEmpty())
 		{
 			return false;
 		}
@@ -529,7 +529,7 @@
 							ps2.setString(2, Base64.encodeBytes(hash));
 							ps2.setLong(3, System.currentTimeMillis());
 							ps2.setInt(4, 0);
-							ps2.setString(5, address.getHostAddress());
+							ps2.setString(5, address);
 							ps2.execute();
 						}
 						
@@ -569,7 +569,7 @@
 				try (PreparedStatement ps3 = con.prepareStatement("UPDATE accounts SET lastactive=?, lastIP=? WHERE login=?"))
 				{
 					ps3.setLong(1, System.currentTimeMillis());
-					ps3.setString(2, address.getHostAddress());
+					ps3.setString(2, address);
 					ps3.setString(3, user);
 					ps3.execute();
 				}
@@ -583,13 +583,13 @@
 		
 		if (!ok)
 		{
-			Log.add("'" + user + "' " + address.getHostAddress(), "logins_ip_fails");
+			Log.add("'" + user + "' " + address, "logins_ip_fails");
 			
 			FailedLoginAttempt failedAttempt = _hackProtection.get(address);
 			int failedCount;
 			if (failedAttempt == null)
 			{
-				_hackProtection.put(address, new FailedLoginAttempt(address, password));
+				_hackProtection.put(address, new FailedLoginAttempt(password));
 				failedCount = 1;
 			}
 			else
@@ -600,14 +600,18 @@
 			
 			if (failedCount >= Config.LOGIN_TRY_BEFORE_BAN)
 			{
-				_log.info("Banning '" + address.getHostAddress() + "' for " + Config.LOGIN_BLOCK_AFTER_BAN + " seconds due to " + failedCount + " invalid user/pass attempts");
-				this.addBanForAddress(address, Config.LOGIN_BLOCK_AFTER_BAN * 1000);
+				_log.info("Banning '" + address + "' for " + Config.LOGIN_BLOCK_AFTER_BAN + " seconds due to " + failedCount + " invalid user/pass attempts");
+				try {
+					this.addBanForAddress(address, Config.LOGIN_BLOCK_AFTER_BAN * 1000);
+				} catch (UnknownHostException e) {
+					e.printStackTrace();
+				}
 			}
 		}
 		else
 		{
 			_hackProtection.remove(address);
-			Log.add("'" + user + "' " + address.getHostAddress(), "logins_ip");
+			Log.add("'" + user + "' " + address, "logins_ip");
 		}
 		
 		return ok;
@@ -649,7 +653,7 @@
 		private long _lastAttempTime;
 		private String _lastPassword;
 		
-		public FailedLoginAttempt(InetAddress address, String lastPassword)
+		public FailedLoginAttempt(String lastPassword)
 		{
 			// _ipAddress = address;
 			_count = 1;

 

  •   Realizar as alterações no ThreadPoolManager
Index: L2J_Server/java/net/sf/l2j/gameserver/ThreadPoolManager.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/ThreadPoolManager.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/ThreadPoolManager.java	(date 1538498840000)
@@ -31,7 +31,7 @@
 import net.sf.l2j.Config;
 import net.sf.l2j.gameserver.network.L2GameClient;
 
-import org.mmocore.network.ReceivablePacket;
+import org.l2j.mmocore.ReadablePacket;
 
 /**
  * <p>
@@ -219,12 +219,12 @@
 		}
 	}
 	
-	public void executePacket(ReceivablePacket<L2GameClient> pkt)
+	public void executePacket(ReadablePacket<L2GameClient> pkt)
 	{
 		_generalPacketsThreadPool.execute(pkt);
 	}
 	
-	public void executeIOPacket(ReceivablePacket<L2GameClient> pkt)
+	public void executeIOPacket(ReadablePacket<L2GameClient> pkt)
 	{
 		_ioPacketsThreadPool.execute(pkt);
 	}

 

  •   Realizar as alterações no Ipv4Filter
Index: L2J_Server/java/net/sf/l2j/gameserver/util/IPv4Filter.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/util/IPv4Filter.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/util/IPv4Filter.java	(date 1538499075000)
@@ -14,19 +14,20 @@
  */
 package net.sf.l2j.gameserver.util;
 
-import java.net.InetAddress;
-import java.nio.channels.SocketChannel;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.AsynchronousSocketChannel;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map.Entry;
 
-import org.mmocore.network.IAcceptFilter;
+import org.l2j.mmocore.ConnectionFilter;
 
 /**
  * Formatted Forsaiken's IPv4 filter [DrHouse]
  * @author Forsaiken
  */
-public class IPv4Filter implements IAcceptFilter, Runnable
+public class IPv4Filter implements ConnectionFilter, Runnable
 {
 	private final HashMap<Integer, Flood> _ipFloodMap;
 	private static final long SLEEP_TIME = 5000;
@@ -62,54 +63,47 @@
 	}
 	
 	@Override
-	public boolean accept(SocketChannel sc)
-	{
-		InetAddress addr = sc.socket().getInetAddress();
-		int h = hash(addr.getAddress());
-		
-		long current = System.currentTimeMillis();
-		Flood f;
-		synchronized (_ipFloodMap)
-		{
-			f = _ipFloodMap.get(h);
-		}
-		if (f != null)
-		{
-			if (f.trys == -1)
-			{
-				f.lastAccess = current;
-				return false;
-			}
-			
-			if ((f.lastAccess + 1000) > current)
-			{
-				f.lastAccess = current;
-				
-				if (f.trys >= 3)
-				{
-					f.trys = -1;
-					return false;
-				}
-				
-				f.trys++;
-			}
-			else
-			{
-				f.lastAccess = current;
-			}
-		}
-		else
-		{
-			synchronized (_ipFloodMap)
-			{
-				_ipFloodMap.put(h, new Flood());
+	public boolean accept(AsynchronousSocketChannel sc) {
+		try {
+			InetSocketAddress socketAddress = (InetSocketAddress) sc.getRemoteAddress();
+			int h = hash(socketAddress.getAddress().getAddress());
+
+			long current = System.currentTimeMillis();
+			Flood f;
+			synchronized (_ipFloodMap) {
+				f = _ipFloodMap.get(h);
+			}
+			if (f != null) {
+				if (f.trys == -1) {
+					f.lastAccess = current;
+					return false;
+				}
+
+				if ((f.lastAccess + 1000) > current) {
+					f.lastAccess = current;
+
+					if (f.trys >= 3) {
+						f.trys = -1;
+						return false;
+					}
+
+					f.trys++;
+				} else {
+					f.lastAccess = current;
+				}
+			} else {
+				synchronized (_ipFloodMap) {
+					_ipFloodMap.put(h, new Flood());
+				}
 			}
+		} catch (IOException e) {
+			e.printStackTrace();
 		}
-		
+
 		return true;
 	}

 

  •   Realizar as alterações no L2LoginPacketHandler
Index: L2J_Server/java/net/sf/l2j/loginserver/L2LoginPacketHandler.java
===================================================================
--- L2J_Server/java/net/sf/l2j/loginserver/L2LoginPacketHandler.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/loginserver/L2LoginPacketHandler.java	(date 1538499673000)
@@ -17,7 +17,6 @@
  */
 package net.sf.l2j.loginserver;
 
-import java.nio.ByteBuffer;
 
 import net.sf.l2j.loginserver.L2LoginClient.LoginClientState;
 import net.sf.l2j.loginserver.clientpackets.AuthGameGuard;
@@ -25,21 +24,22 @@
 import net.sf.l2j.loginserver.clientpackets.RequestServerList;
 import net.sf.l2j.loginserver.clientpackets.RequestServerLogin;
 
-import org.mmocore.network.IPacketHandler;
-import org.mmocore.network.ReceivablePacket;
+import org.l2j.mmocore.DataWrapper;
+import org.l2j.mmocore.PacketHandler;
+import org.l2j.mmocore.ReadablePacket;
 
 /**
  * Handler for packets received by Login Server
  * @author KenM
  */
-public final class L2LoginPacketHandler implements IPacketHandler<L2LoginClient>
+public final class L2LoginPacketHandler implements PacketHandler<L2LoginClient>
 {
 	@Override
-	public ReceivablePacket<L2LoginClient> handlePacket(ByteBuffer buf, L2LoginClient client)
+	public ReadablePacket<L2LoginClient> handlePacket(DataWrapper buf, L2LoginClient client)
 	{
 		int opcode = buf.get() & 0xFF;
 		
-		ReceivablePacket<L2LoginClient> packet = null;
+		ReadablePacket<L2LoginClient> packet = null;
 		LoginClientState state = client.getState();
 		
 		switch (state)

 

  • Realizar as alterações no L2GamePacketHandler
Index: L2J_Server/java/net/sf/l2j/gameserver/network/L2GamePacketHandler.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/network/L2GamePacketHandler.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/network/L2GamePacketHandler.java	(date 1538499928000)
@@ -17,7 +17,6 @@
  */
 package net.sf.l2j.gameserver.network;
 
-import java.nio.ByteBuffer;
 import java.util.concurrent.RejectedExecutionException;
 import java.util.logging.Logger;
 
@@ -27,11 +26,8 @@
 import net.sf.l2j.gameserver.network.L2GameClient.GameClientState;
 import net.sf.l2j.util.Util;
 
-import org.mmocore.network.IClientFactory;
-import org.mmocore.network.IMMOExecutor;
-import org.mmocore.network.IPacketHandler;
-import org.mmocore.network.MMOConnection;
-import org.mmocore.network.ReceivablePacket;
+import org.l2j.mmocore.*;
+
 
 /**
  * Stateful Packet Handler<BR>
@@ -41,17 +37,17 @@
  * Note: If for a given exception a packet needs to be handled on more then one state, then it should be added to all these states.
  * @author KenM
  */
-public final class L2GamePacketHandler implements IPacketHandler<L2GameClient>, IClientFactory<L2GameClient>, IMMOExecutor<L2GameClient>
+public final class L2GamePacketHandler implements PacketHandler<L2GameClient>, ClientFactory<L2GameClient>, PacketExecutor<L2GameClient>
 {
 	private static final Logger _log = Logger.getLogger(L2GamePacketHandler.class.getName());
 	
 	// implementation
 	@Override
-	public ReceivablePacket<L2GameClient> handlePacket(ByteBuffer buf, L2GameClient client)
+	public ReadablePacket<L2GameClient> handlePacket(DataWrapper buf, L2GameClient client)
 	{
 		int opcode = buf.get() & 0xFF;
 		
-		ReceivablePacket<L2GameClient> msg = null;
+		ReadablePacket<L2GameClient> msg = null;
 		GameClientState state = client.getState();
 		
 		switch (state)
@@ -644,7 +640,7 @@
 					
 					case 0xd0:
 						int id2 = -1;
-						if (buf.remaining() >= 2)
+						if (buf.available() >= 2)
 						{
 							id2 = buf.getShort() & 0xffff;
 						}
@@ -813,33 +809,27 @@
 		return msg;
 	}
 	
-	private void printDebug(int opcode, ByteBuffer buf, GameClientState state, L2GameClient client)
+	private void printDebug(int opcode, DataWrapper buf, GameClientState state, L2GameClient client)
 	{
-		int size = buf.remaining();
 		_log.warning("Unknown Packet: " + Integer.toHexString(opcode) + " on State: " + state.name() + " Client: " + client.toString());
-		byte[] array = new byte[size];
-		buf.get(array);
-		_log.warning(Util.printData(array, size));
+		_log.warning(Util.printData(buf.expose()));
 	}
 	
-	private void printDebugDoubleOpcode(int opcode, int id2, ByteBuffer buf, GameClientState state, L2GameClient client)
+	private void printDebugDoubleOpcode(int opcode, int id2, DataWrapper buf, GameClientState state, L2GameClient client)
 	{
-		int size = buf.remaining();
 		_log.warning("Unknown Packet: " + Integer.toHexString(opcode) + ":" + Integer.toHexString(id2) + " on State: " + state.name() + " Client: " + client.toString());
-		byte[] array = new byte[size];
-		buf.get(array);
-		_log.warning(Util.printData(array, size));
+		_log.warning(Util.printData(buf.expose()));
 	}
 	
 	// impl
 	@Override
-	public L2GameClient create(MMOConnection<L2GameClient> con)
+	public L2GameClient create(Connection<L2GameClient> con)
 	{
 		return new L2GameClient(con);
 	}
 	
 	@Override
-	public void execute(ReceivablePacket<L2GameClient> rp)
+	public void execute(ReadablePacket<L2GameClient> rp)
 	{
 		try
 		{

 

  • Realizar as alterações no GeoEditorThread
Index: L2J_Server/java/net/sf/l2j/gameserver/geoeditorcon/GeoEditorThread.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/geoeditorcon/GeoEditorThread.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/geoeditorcon/GeoEditorThread.java	(date 1538566418000)
@@ -87,13 +87,14 @@
 				{
 					for (L2PcInstance gm : _gms)
 					{
-						if (!gm.getClient().getConnection().isClosed())
-						{
-							sendGmPosition(gm);
-						}
-						else
+						if (!gm.getClient().isConnected())
 						{
 							_gms.remove(gm);
+						}
+						else
+						{
+							sendGmPosition(gm);
+
 						}
 					}
 					timer = 0;

 

  •   Realizar as alterações no Gameserver
Index: L2J_Server/java/net/sf/l2j/gameserver/GameServer.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/GameServer.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/GameServer.java	(date 1538566997000)
@@ -23,6 +23,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
 import java.util.Calendar;
 import java.util.logging.Level;
@@ -233,8 +234,8 @@
 import net.sf.l2j.gameserver.util.IPv4Filter;
 import net.sf.l2j.status.Status;
 
-import org.mmocore.network.SelectorConfig;
-import org.mmocore.network.SelectorThread;
+import org.l2j.mmocore.ConnectionBuilder;
+import org.l2j.mmocore.ConnectionHandler;
 
 /**
  * This class ...
@@ -243,7 +244,7 @@
 public class GameServer
 {
 	private static final Logger _log = Logger.getLogger(GameServer.class.getName());
-	private final SelectorThread<L2GameClient> _selectorThread;
+	private final ConnectionHandler<L2GameClient> connectionHandler;
 	private final SkillTable _skillTable;
 	private final ItemTable _itemTable;
 	private final NpcTable _npcTable;
@@ -269,15 +270,17 @@
 	private final ThreadPoolManager _threadpools;
 	
 	public static final Calendar dateTimeServerStarted = Calendar.getInstance();
-	
+
 	public long getUsedMemoryMB()
 	{
 		return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1048576; // 1024 * 1024 = 1048576;
 	}
-	
-	public SelectorThread<L2GameClient> getSelectorThread()
-	{
-		return _selectorThread;
+
+	public static void shutdown() {
+		if(gameServer != null) {
+			gameServer.connectionHandler.shutdown();
+			gameServer.connectionHandler.setDaemon(true);
+		}
 	}
 	
 	public ClanHallManager getCHManager()
@@ -665,41 +668,20 @@
 		
 		_loginThread = LoginServerThread.getInstance();
 		_loginThread.start();
-		
-		// TODO: Unhardcode this configuration options
-		final SelectorConfig sc = new SelectorConfig();
-		sc.MAX_READ_PER_PASS = 12; // Config.MMO_MAX_READ_PER_PASS;
-		sc.MAX_SEND_PER_PASS = 12; // Config.MMO_MAX_SEND_PER_PASS;
-		sc.SLEEP_TIME = 20; // Config.MMO_SELECTOR_SLEEP_TIME;
-		sc.HELPER_BUFFER_COUNT = 20; // Config.MMO_HELPER_BUFFER_COUNT;
-		sc.TCP_NODELAY = false; // Config.MMO_TCP_NODELAY;
-		
+
 		L2GamePacketHandler gph = new L2GamePacketHandler();
-		_selectorThread = new SelectorThread<>(sc, gph, gph, gph, new IPv4Filter());
-		InetAddress bindAddress = null;
-		if (!Config.GAMESERVER_HOSTNAME.equals("*"))
-		{
-			try
-			{
-				bindAddress = InetAddress.getByName(Config.GAMESERVER_HOSTNAME);
-			}
-			catch (UnknownHostException e1)
-			{
-				_log.log(Level.SEVERE, "WARNING: The GameServer bind address is invalid, using all avaliable IPs. Reason: " + e1.getMessage(), e1);
-			}
-		}
-		
-		try
-		{
-			_selectorThread.openServerSocket(bindAddress, Config.PORT_GAME);
+
+		InetSocketAddress bindAddress;
+		if (!Config.GAMESERVER_HOSTNAME.equals("*")) {
+			bindAddress =  new InetSocketAddress(Config.GAMESERVER_HOSTNAME, Config.PORT_GAME);
+		} else {
+			bindAddress = new InetSocketAddress(Config.PORT_GAME);
 		}
-		catch (IOException e)
-		{
-			_log.log(Level.SEVERE, "FATAL: Failed to open server socket. Reason: " + e.getMessage(), e);
-			System.exit(1);
-		}
+
+		connectionHandler = ConnectionBuilder.create(bindAddress, gph,gph,gph).filter(new IPv4Filter()).build();
+		connectionHandler.start();
 		
-		_selectorThread.start();
+
 		_log.config("Maximum Numbers of Connected Players: " + Config.MAXIMUM_ONLINE_USERS);
 	}
 	

 

  • Realizar as alterações no Shutdown
Index: L2J_Server/java/net/sf/l2j/gameserver/Shutdown.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/Shutdown.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/Shutdown.java	(date 1538567074000)
@@ -216,8 +216,7 @@
 			// saveData sends messages to exit players, so sgutdown selector after it
 			try
 			{
-				GameServer.gameServer.getSelectorThread().shutdown();
-				GameServer.gameServer.getSelectorThread().setDaemon(true);
+				GameServer.shutdown();
 			}
 			catch (Throwable t)
 			{

 

  • Realizar as alterações em L2LoginServer
Index: L2J_Server/java/net/sf/l2j/loginserver/L2LoginServer.java
===================================================================
--- L2J_Server/java/net/sf/l2j/loginserver/L2LoginServer.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/loginserver/L2LoginServer.java	(date 1538567848000)
@@ -24,11 +24,10 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.LineNumberReader;
-import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
 import java.security.GeneralSecurityException;
 import java.sql.SQLException;
-import java.util.logging.Level;
 import java.util.logging.LogManager;
 import java.util.logging.Logger;
 
@@ -36,9 +35,9 @@
 import net.sf.l2j.L2DatabaseFactory;
 import net.sf.l2j.Server;
 import net.sf.l2j.status.Status;
+import org.l2j.mmocore.ConnectionBuilder;
+import org.l2j.mmocore.ConnectionHandler;
 
-import org.mmocore.network.SelectorConfig;
-import org.mmocore.network.SelectorThread;
 
 /**
  * @author KenM
@@ -50,11 +49,10 @@
 	private static L2LoginServer _instance;
 	private final Logger _log = Logger.getLogger(L2LoginServer.class.getName());
 	private GameServerListener _gameServerListener;
-	private SelectorThread<L2LoginClient> _selectorThread;
+	private ConnectionHandler<L2LoginClient> connectionHandler;
 	private Status _statusServer;
 	
-	public static void main(String[] args)
-	{
+	public static void main(String[] args) throws IOException {
 		_instance = new L2LoginServer();
 	}
 	
@@ -63,8 +61,7 @@
 		return _instance;
 	}
 	
-	public L2LoginServer()
-	{
+	public L2LoginServer() throws IOException {
 		Server.serverMode = Server.MODE_LOGINSERVER;
 		// Local Constants
 		final String LOG_FOLDER = "log"; // Name of folder for log file
@@ -159,44 +156,8 @@
 		}
 		
 		loadBanFile();
-		
-		InetAddress bindAddress = null;
-		if (!Config.LOGIN_BIND_ADDRESS.equals("*"))
-		{
-			try
-			{
-				bindAddress = InetAddress.getByName(Config.LOGIN_BIND_ADDRESS);
-			}
-			catch (UnknownHostException e1)
-			{
-				_log.severe("WARNING: The LoginServer bind address is invalid, using all avaliable IPs. Reason: " + e1.getMessage());
-				if (Config.DEVELOPER)
-				{
-					e1.printStackTrace();
-				}
-			}
-		}
+
 		
-		// TODO: Unhardcode this configuration options
-		final SelectorConfig sc = new SelectorConfig();
-		sc.MAX_READ_PER_PASS = 12; // Config.MMO_MAX_READ_PER_PASS;
-		sc.MAX_SEND_PER_PASS = 12; // Config.MMO_MAX_SEND_PER_PASS;
-		sc.SLEEP_TIME = 20; // Config.MMO_SELECTOR_SLEEP_TIME;
-		sc.HELPER_BUFFER_COUNT = 20; // Config.MMO_HELPER_BUFFER_COUNT;
-		sc.TCP_NODELAY = false; // Config.MMO_TCP_NODELAY;
-		
-		final L2LoginPacketHandler lph = new L2LoginPacketHandler();
-		final SelectorHelper sh = new SelectorHelper();
-		try
-		{
-			_selectorThread = new SelectorThread<>(sc, sh, lph, sh, sh);
-		}
-		catch (IOException e)
-		{
-			_log.log(Level.SEVERE, "FATAL: Failed to open Selector. Reason: " + e.getMessage(), e);
-			System.exit(1);
-		}
-		
 		try
 		{
 			_gameServerListener = new GameServerListener();
@@ -233,18 +194,22 @@
 		{
 			System.out.println("Telnet server is currently disabled.");
 		}
-		
-		try
-		{
-			_selectorThread.openServerSocket(bindAddress, Config.PORT_LOGIN);
+
+		InetSocketAddress bindAddress;
+		if (!Config.LOGIN_BIND_ADDRESS.equals("*")) {
+			bindAddress =  new InetSocketAddress(Config.LOGIN_BIND_ADDRESS, Config.PORT_LOGIN);
+		} else {
+			bindAddress = new InetSocketAddress(Config.PORT_LOGIN);
 		}
-		catch (IOException e)
-		{
-			_log.log(Level.SEVERE, "FATAL: Failed to open server socket. Reason: " + e.getMessage(), e);
-			System.exit(1);
-		}
-		_selectorThread.start();
-		_log.info("Login Server ready on " + (bindAddress == null ? "*" : bindAddress.getHostAddress()) + ":" + Config.PORT_LOGIN);
+
+		final L2LoginPacketHandler lph = new L2LoginPacketHandler();
+		final SelectorHelper sh = new SelectorHelper();
+
+
+		connectionHandler = ConnectionBuilder.create(bindAddress, sh,lph,sh).filter(sh).build();
+		connectionHandler.start();
+
+		_log.info("Login Server ready on " + (bindAddress == null ? "*" : bindAddress.getHostString()) + ":" + Config.PORT_LOGIN);
 	}
 	
 	public Status getStatusServer()

 

  • Realizar as alterações em AdminEditChar
Index: L2J_Server/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminEditChar.java
===================================================================
--- L2J_Server/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminEditChar.java	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/java/net/sf/l2j/gameserver/handler/admincommandhandlers/AdminEditChar.java	(date 1538568122000)
@@ -749,7 +749,7 @@
 		adminReply.setFile("data/html/admin/ipfind.htm");
 		for (L2PcInstance player : players)
 		{
-			ip = player.getClient().getConnection().getInetAddress().getHostAddress();
+			ip = player.getClient().getHostAddress();
 			if (ip.equals(IpAdress))
 			{
 				name = player.getName();

 

  •   As seguintes substituições nas chamadas de método devem ser realizadas. Essa substituição pode ser feita em lote em algum editor de texto ou IDE


  readC() -> readUnsignedByte()
  readH() -> readShort()
  readD() -> readInt()
  readF() -> readDouble()
  readQ() -> readLong()
  readS() -> readString()
  readB() -> readBytes()
  buf.remaining() -> availableData()
.getConnection().sendPacket -> .sendPacket

bNTIOec.png

  writeC( -> writeByte(
  writeH( -> writeShort(
  writeD( -> writeInt(
  writeF( -> writeDouble(
  writeQ( -> writeLong(
  writeS( -> writeString(
  writeB( -> writeBytes(

o5OxaOT.png

 

  •   Realizar as alterações no build.xml
Index: L2J_Server/build.xml
===================================================================
--- L2J_Server/build.xml	(revision 21608dd4679422d6037c9683767493221807a2e7)
+++ L2J_Server/build.xml	(date 1538567213000)
@@ -37,7 +37,7 @@
 			<include name="javolution-5.5.1.jar" />
 			<include name="mchange-commons-0.2.jar" />
 			<include name="mysql-connector-java-5.1.18-bin.jar" />
-			<include name="mmocore.jar" />
+			<include name="mmocore-2.0.jar" />
 		</fileset>
 	</path>
 	<target name="init" depends="clean" description="Create the output directories.">

 

Depois desses passos você deverá ser capaz de compilar o source. Dependendo do projeto/revisão/versão que esteja usando, pode ser que precise editar outros arquivos além desses. Mas as alterações básicas são essas.

 

[ ]'s

  • Like 4
  • I love it 4
Link to comment
Share on other sites


  • Join now

    Be part of the largest and oldest communities about Lineage2 in Latin America.





×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.