    001package net.minecraft.network;
    002
    003import java.io.IOException;
    004import java.util.ArrayList;
    005import java.util.Collections;
    006import java.util.List;
    007import java.util.logging.Level;
    008import java.util.logging.Logger;
    009
    010import cpw.mods.fml.common.FMLLog;
    011import net.minecraft.crash.CrashReport;
    012import net.minecraft.server.MinecraftServer;
    013import net.minecraft.util.ReportedException;
    014
    015public abstract class NetworkListenThread
    016{
    017    /** Reference to the logger. */
    018    public static Logger logger = Logger.getLogger("Minecraft");
    019
    020    /** Reference to the MinecraftServer object. */
    021    private final MinecraftServer mcServer;
    022    private final List connections = Collections.synchronizedList(new ArrayList());
    023
    024    /** Whether the network listener object is listening. */
    025    public volatile boolean isListening = false;
    026
    027    public NetworkListenThread(MinecraftServer par1MinecraftServer) throws IOException
    028    {
    029        this.mcServer = par1MinecraftServer;
    030        this.isListening = true;
    031    }
    032
    033    /**
    034     * adds this connection to the list of currently connected players
    035     */
    036    public void addPlayer(NetServerHandler par1NetServerHandler)
    037    {
    038        this.connections.add(par1NetServerHandler);
    039    }
    040
    041    public void stopListening()
    042    {
    043        this.isListening = false;
    044    }
    045
    046    /**
    047     * processes packets and pending connections
    048     */
    049    public void networkTick()
    050    {
    051        for (int var1 = 0; var1 < this.connections.size(); ++var1)
    052        {
    053            NetServerHandler var2 = (NetServerHandler)this.connections.get(var1);
    054
    055            try
    056            {
    057                var2.networkTick();
    058            }
    059            catch (Exception var5)
    060            {
    061                if (var2.netManager instanceof MemoryConnection)
    062                {
    063                    CrashReport var4 = CrashReport.makeCrashReport(var5, "Ticking memory connection");
    064                    throw new ReportedException(var4);
    065                }
    066
    067                FMLLog.log(Level.SEVERE, var5, "A critical server error occured handling a packet, kicking %s", var2.getPlayer().entityId);
    068                logger.log(Level.WARNING, "Failed to handle packet for " + var2.playerEntity.getEntityName() + "/" + var2.playerEntity.func_71114_r() + ": " + var5, var5);
    069                var2.kickPlayerFromServer("Internal server error");
    070            }
    071
    072            if (var2.connectionClosed)
    073            {
    074                this.connections.remove(var1--);
    075            }
    076
    077            var2.netManager.wakeThreads();
    078        }
    079    }
    080
    081    public MinecraftServer getServer()
    082    {
    083        return this.mcServer;
    084    }
    085}