SelectorHelper.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under the terms of the
  3. * GNU General Public License as published by the Free Software Foundation, either version 3 of the
  4. * License, or (at your option) any later version.
  5. *
  6. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  7. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. * General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with this program. If
  11. * not, see <http://www.gnu.org/licenses/>.
  12. */
  13. package com.l2jserver.loginserver;
  14. import java.nio.channels.SocketChannel;
  15. import java.util.concurrent.LinkedBlockingQueue;
  16. import java.util.concurrent.ThreadPoolExecutor;
  17. import java.util.concurrent.TimeUnit;
  18. import org.mmocore.network.IAcceptFilter;
  19. import org.mmocore.network.IClientFactory;
  20. import org.mmocore.network.IMMOExecutor;
  21. import org.mmocore.network.MMOConnection;
  22. import org.mmocore.network.ReceivablePacket;
  23. import com.l2jserver.loginserver.network.L2LoginClient;
  24. import com.l2jserver.loginserver.network.serverpackets.Init;
  25. import com.l2jserver.util.IPv4Filter;
  26. /**
  27. *
  28. * @author KenM
  29. */
  30. public class SelectorHelper implements IMMOExecutor<L2LoginClient>,
  31. IClientFactory<L2LoginClient>, IAcceptFilter
  32. {
  33. private ThreadPoolExecutor _generalPacketsThreadPool;
  34. private IPv4Filter _ipv4filter;
  35. public SelectorHelper()
  36. {
  37. _generalPacketsThreadPool = new ThreadPoolExecutor(4, 6, 15L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
  38. _ipv4filter = new IPv4Filter();
  39. }
  40. /**
  41. *
  42. * @see org.mmocore.network.IMMOExecutor#execute(org.mmocore.network.ReceivablePacket)
  43. */
  44. public void execute(ReceivablePacket<L2LoginClient> packet)
  45. {
  46. _generalPacketsThreadPool.execute(packet);
  47. }
  48. /**
  49. *
  50. * @see org.mmocore.network.IClientFactory#create(org.mmocore.network.MMOConnection)
  51. */
  52. public L2LoginClient create(MMOConnection<L2LoginClient> con)
  53. {
  54. L2LoginClient client = new L2LoginClient(con);
  55. client.sendPacket(new Init(client));
  56. return client;
  57. }
  58. /**
  59. *
  60. * @see org.mmocore.network.IAcceptFilter#accept(java.nio.channels.SocketChannel)
  61. */
  62. public boolean accept(SocketChannel sc)
  63. {
  64. return _ipv4filter.accept(sc) && !LoginController.getInstance().isBannedAddress(sc.socket().getInetAddress());
  65. }
  66. }