/mcp/src/minecraft_server/net/minecraft/src/NetworkListenThread.java

https://bitbucket.org/vkorolik/twoleanfour-client · Java · 153 lines · 113 code · 23 blank · 17 comment · 6 complexity · e9c38d2d3abda9d60e76d993cf3dd335 MD5 · raw file

  1. package net.minecraft.src;
  2. import java.io.IOException;
  3. import java.net.*;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import net.minecraft.server.MinecraftServer;
  9. public class NetworkListenThread
  10. {
  11. /** Reference to the logger. */
  12. public static Logger logger = Logger.getLogger("Minecraft");
  13. private ServerSocket serverSocket;
  14. private Thread networkAcceptThread;
  15. /** Whether the network listener object is listening. */
  16. public volatile boolean isListening;
  17. private int field_977_f;
  18. /** list of all people currently trying to connect to the server */
  19. private ArrayList pendingConnections;
  20. /** list of all currently connected players */
  21. private ArrayList playerList;
  22. /** Reference to the MinecraftServer object. */
  23. public MinecraftServer mcServer;
  24. private HashMap field_35506_i;
  25. public NetworkListenThread(MinecraftServer par1MinecraftServer, InetAddress par2InetAddress, int par3) throws IOException
  26. {
  27. isListening = false;
  28. field_977_f = 0;
  29. pendingConnections = new ArrayList();
  30. playerList = new ArrayList();
  31. field_35506_i = new HashMap();
  32. mcServer = par1MinecraftServer;
  33. serverSocket = new ServerSocket(par3, 0, par2InetAddress);
  34. serverSocket.setPerformancePreferences(0, 2, 1);
  35. isListening = true;
  36. networkAcceptThread = new NetworkAcceptThread(this, "Listen thread", par1MinecraftServer);
  37. networkAcceptThread.start();
  38. }
  39. public void func_35505_a(Socket par1Socket)
  40. {
  41. InetAddress inetaddress = par1Socket.getInetAddress();
  42. synchronized (field_35506_i)
  43. {
  44. field_35506_i.remove(inetaddress);
  45. }
  46. }
  47. /**
  48. * adds this connection to the list of currently connected players
  49. */
  50. public void addPlayer(NetServerHandler par1NetServerHandler)
  51. {
  52. playerList.add(par1NetServerHandler);
  53. }
  54. /**
  55. * adds a new pending connection to the waiting list
  56. */
  57. private void addPendingConnection(NetLoginHandler par1NetLoginHandler)
  58. {
  59. if (par1NetLoginHandler == null)
  60. {
  61. throw new IllegalArgumentException("Got null pendingconnection!");
  62. }
  63. else
  64. {
  65. pendingConnections.add(par1NetLoginHandler);
  66. return;
  67. }
  68. }
  69. /**
  70. * Handles all incoming connections and packets
  71. */
  72. public void handleNetworkListenThread()
  73. {
  74. for (int i = 0; i < pendingConnections.size(); i++)
  75. {
  76. NetLoginHandler netloginhandler = (NetLoginHandler)pendingConnections.get(i);
  77. try
  78. {
  79. netloginhandler.tryLogin();
  80. }
  81. catch (Exception exception)
  82. {
  83. netloginhandler.kickUser("Internal server error");
  84. logger.log(Level.WARNING, (new StringBuilder()).append("Failed to handle packet: ").append(exception).toString(), exception);
  85. }
  86. if (netloginhandler.finishedProcessing)
  87. {
  88. pendingConnections.remove(i--);
  89. }
  90. netloginhandler.netManager.wakeThreads();
  91. }
  92. for (int j = 0; j < playerList.size(); j++)
  93. {
  94. NetServerHandler netserverhandler = (NetServerHandler)playerList.get(j);
  95. try
  96. {
  97. netserverhandler.handlePackets();
  98. }
  99. catch (Exception exception1)
  100. {
  101. logger.log(Level.WARNING, (new StringBuilder()).append("Failed to handle packet: ").append(exception1).toString(), exception1);
  102. netserverhandler.kickPlayer("Internal server error");
  103. }
  104. if (netserverhandler.connectionClosed)
  105. {
  106. playerList.remove(j--);
  107. }
  108. netserverhandler.netManager.wakeThreads();
  109. }
  110. }
  111. /**
  112. * Gets the server socket.
  113. */
  114. static ServerSocket getServerSocket(NetworkListenThread par0NetworkListenThread)
  115. {
  116. return par0NetworkListenThread.serverSocket;
  117. }
  118. static HashMap func_35504_b(NetworkListenThread par0NetworkListenThread)
  119. {
  120. return par0NetworkListenThread.field_35506_i;
  121. }
  122. static int func_712_b(NetworkListenThread par0NetworkListenThread)
  123. {
  124. return par0NetworkListenThread.field_977_f++;
  125. }
  126. static void func_716_a(NetworkListenThread par0NetworkListenThread, NetLoginHandler par1NetLoginHandler)
  127. {
  128. par0NetworkListenThread.addPendingConnection(par1NetLoginHandler);
  129. }
  130. }