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