SHARE
TWEET

Untitled

a guest Jun 19th, 2017 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Index: java/com/l2jserver/gameserver/instancemanager/AntiFeedManager.java
  2. ===================================================================
  3. --- java/com/l2jserver/gameserver/instancemanager/AntiFeedManager.java  (revision 5453)
  4. +++ java/com/l2jserver/gameserver/instancemanager/AntiFeedManager.java  (working copy)
  5. @@ -14,6 +14,9 @@
  6.   */
  7.  package com.l2jserver.gameserver.instancemanager;
  8.  
  9. +import gnu.trove.TIntObjectHashMap;
  10. +import gnu.trove.TObjectProcedure;
  11. +
  12.  import java.util.Map;
  13.  
  14.  import javolution.util.FastMap;
  15. @@ -26,7 +29,8 @@
  16.  public class AntiFeedManager
  17.  {
  18.     private Map<Integer,Long> _lastDeathTimes;
  19. -  
  20. +   private TIntObjectHashMap<Map<Integer, Integer>> _eventIPs;
  21. +
  22.     public static final AntiFeedManager getInstance()
  23.     {
  24.         return SingletonHolder._instance;
  25. @@ -35,6 +39,7 @@
  26.     private AntiFeedManager()
  27.     {
  28.         _lastDeathTimes = new FastMap<Integer,Long>().shared();
  29. +       _eventIPs = new TIntObjectHashMap<Map<Integer, Integer>>();
  30.     }
  31.    
  32.     /**
  33. @@ -96,7 +101,153 @@
  34.     {
  35.         _lastDeathTimes.clear();
  36.     }
  37. -  
  38. +
  39. +   /**
  40. +    * Register new event for dualbox check.
  41. +    * Should be called only once.
  42. +    * @param eventId
  43. +    */
  44. +   public final void registerEvent(int eventId)
  45. +   {
  46. +       if (!_eventIPs.containsKey(eventId))
  47. +           _eventIPs.put(eventId, new FastMap<Integer, Integer>());
  48. +   }
  49. +
  50. +   /**
  51. +    * If number of all simultaneous connections from player's IP address lower than max
  52. +    * then increment connection count and return true.
  53. +    * Returns false if number of all simultaneous connections from player's IP address
  54. +    * higher than max.
  55. +    * @param eventId
  56. +    * @param player
  57. +    * @param max
  58. +    * @return
  59. +    */
  60. +   public final boolean tryAddPlayer(int eventId, L2PcInstance player, int max)
  61. +   {
  62. +       if (_eventIPs.isEmpty())
  63. +           return false;
  64. +
  65. +       final Map<Integer, Integer> event = _eventIPs.get(eventId);
  66. +       if (event == null)
  67. +           return false; // no such event registered
  68. +
  69. +       final L2GameClient client = player.getClient();
  70. +       if (client == null || client.isDetached())
  71. +           return false; // unable to determine IP address
  72. +
  73. +       final Integer addrHash = Integer.valueOf(client.getConnection().getInetAddress().hashCode());
  74. +       Integer number;
  75. +       synchronized (event)
  76. +       {
  77. +           number = event.get(addrHash);
  78. +           if (number == null)
  79. +           {
  80. +               number = new Integer(0);
  81. +               event.put(addrHash, number);
  82. +           }
  83. +       }
  84. +
  85. +       synchronized (number)
  86. +       {
  87. +           if (number < max)
  88. +           {
  89. +               number++;
  90. +               return true;
  91. +           }
  92. +       }
  93. +       return false;
  94. +   }
  95. +
  96. +   /**
  97. +    * Decreasing number of active connection from player's IP address
  98. +    * Returns true if success and false if any problem detected.
  99. +    * @param eventId
  100. +    * @param player
  101. +    * @return
  102. +    */
  103. +   public final boolean removePlayer(int eventId, L2PcInstance player)
  104. +   {
  105. +       if (_eventIPs.isEmpty())
  106. +           return false;
  107. +
  108. +       final Map<Integer, Integer> event = _eventIPs.get(eventId);
  109. +       if (event == null)
  110. +           return false; // no such event registered
  111. +
  112. +       final L2GameClient client = player.getClient();
  113. +       if (client == null || client.isDetached())
  114. +           return false; // unable to determine IP address
  115. +
  116. +       final Integer addrHash = Integer.valueOf(client.getConnection().getInetAddress().hashCode());
  117. +       Integer number = event.get(addrHash);
  118. +       if (number == null)
  119. +           return false; // address not registered
  120. +
  121. +       synchronized (number)
  122. +       {
  123. +           if (number > 0)
  124. +           {
  125. +               number--;
  126. +               return true;
  127. +           }
  128. +           return false; // all boxes connected from this IP already removed
  129. +       }
  130. +   }
  131. +
  132. +   /**
  133. +    * Remove player connection IP address from all registered events lists.
  134. +    * @param player
  135. +    */
  136. +   public final void disconnectPlayer(L2PcInstance player)
  137. +   {
  138. +       if (_eventIPs.isEmpty())
  139. +           return;
  140. +
  141. +       final L2GameClient client = player.getClient();
  142. +       if (client == null || client.isDetached())
  143. +           return;
  144. +
  145. +       final Integer addrHash = Integer.valueOf(client.getConnection().getInetAddress().hashCode());
  146. +       _eventIPs.forEachValue(new DisconnectProcedure(addrHash));
  147. +   }
  148. +
  149. +   /**
  150. +    * Clear all entries for this eventId.
  151. +    * @param eventId
  152. +    */
  153. +   public final void clear(int eventId)
  154. +   {
  155. +       final Map<Integer, Integer> event = _eventIPs.get(eventId);
  156. +       if (event != null)
  157. +           event.clear();
  158. +   }
  159. +
  160. +   private final class DisconnectProcedure implements TObjectProcedure<Map<Integer, Integer>>
  161. +   {
  162. +       private final Integer _addrHash;
  163. +
  164. +       public DisconnectProcedure(Integer hash)
  165. +       {
  166. +           _addrHash = hash;
  167. +       }
  168. +
  169. +       @Override
  170. +       public final boolean execute(Map<Integer, Integer> event)
  171. +       {
  172. +           Integer number = event.get(_addrHash);
  173. +           if (number != null)
  174. +           {
  175. +               synchronized (number)
  176. +               {
  177. +                   if (number > 0)
  178. +                       number--;
  179. +               }
  180. +           }
  181. +           return true;
  182. +       }
  183. +   }
  184. +
  185.     @SuppressWarnings("synthetic-access")
  186.     private static class SingletonHolder
  187.     {
RAW Paste Data
Top