001 package net.minecraft.src;
002
003 import cpw.mods.fml.common.Side;
004 import cpw.mods.fml.common.asm.SideOnly;
005 import java.io.IOException;
006 import java.net.InetSocketAddress;
007 import java.net.SocketAddress;
008 import java.util.ArrayList;
009 import java.util.Collections;
010 import java.util.List;
011
012 import cpw.mods.fml.common.network.FMLNetworkHandler;
013
014 @SideOnly(Side.CLIENT)
015 public class MemoryConnection implements INetworkManager
016 {
017 private static final SocketAddress mySocketAddress = new InetSocketAddress("127.0.0.1", 0);
018 private final List readPacketCache = Collections.synchronizedList(new ArrayList());
019 private MemoryConnection pairedConnection;
020 private NetHandler myNetHandler;
021
022 /** set to true by {server,network}Shutdown */
023 private boolean shuttingDown = false;
024 private String shutdownReason = "";
025 private Object[] field_74439_g;
026 private boolean gamePaused = false;
027
028 public MemoryConnection(NetHandler par1NetHandler) throws IOException
029 {
030 this.myNetHandler = par1NetHandler;
031 }
032
033 /**
034 * Sets the NetHandler for this NetworkManager. Server-only.
035 */
036 public void setNetHandler(NetHandler par1NetHandler)
037 {
038 this.myNetHandler = par1NetHandler;
039 }
040
041 /**
042 * Adds the packet to the correct send queue (chunk data packets go to a separate queue).
043 */
044 public void addToSendQueue(Packet par1Packet)
045 {
046 if (!this.shuttingDown)
047 {
048 this.pairedConnection.processOrCachePacket(par1Packet);
049 }
050 }
051
052 public void closeConnections()
053 {
054 this.pairedConnection = null;
055 this.myNetHandler = null;
056 }
057
058 public boolean isConnectionActive()
059 {
060 return !this.shuttingDown && this.pairedConnection != null;
061 }
062
063 /**
064 * Wakes reader and writer threads
065 */
066 public void wakeThreads() {}
067
068 /**
069 * Checks timeouts and processes all pending read packets.
070 */
071 public void processReadPackets()
072 {
073 int var1 = 2500;
074
075 while (var1-- >= 0 && !this.readPacketCache.isEmpty())
076 {
077 Packet var2 = (Packet)this.readPacketCache.remove(0);
078 var2.processPacket(this.myNetHandler);
079 }
080
081 if (this.readPacketCache.size() > var1)
082 {
083 System.out.println("Memory connection overburdened; after processing 2500 packets, we still have " + this.readPacketCache.size() + " to go!");
084 }
085
086 if (this.shuttingDown && this.readPacketCache.isEmpty())
087 {
088 this.myNetHandler.handleErrorMessage(this.shutdownReason, this.field_74439_g);
089 FMLNetworkHandler.onConnectionClosed(this, this.myNetHandler.getPlayer());
090 }
091 }
092
093 /**
094 * Return the InetSocketAddress of the remote endpoint
095 */
096 public SocketAddress getSocketAddress()
097 {
098 return mySocketAddress;
099 }
100
101 /**
102 * Shuts down the server. (Only actually used on the server)
103 */
104 public void serverShutdown()
105 {
106 this.shuttingDown = true;
107 }
108
109 /**
110 * Shuts down the network with the specified reason. Closes all streams and sockets, spawns NetworkMasterThread to
111 * stop reading and writing threads.
112 */
113 public void networkShutdown(String par1Str, Object ... par2ArrayOfObj)
114 {
115 this.shuttingDown = true;
116 this.shutdownReason = par1Str;
117 this.field_74439_g = par2ArrayOfObj;
118 }
119
120 /**
121 * returns 0 for memoryConnections
122 */
123 public int packetSize()
124 {
125 return 0;
126 }
127
128 public void pairWith(MemoryConnection par1MemoryConnection)
129 {
130 this.pairedConnection = par1MemoryConnection;
131 par1MemoryConnection.pairedConnection = this;
132 }
133
134 public boolean isGamePaused()
135 {
136 return this.gamePaused;
137 }
138
139 public void setGamePaused(boolean par1)
140 {
141 this.gamePaused = par1;
142 }
143
144 public MemoryConnection getPairedConnection()
145 {
146 return this.pairedConnection;
147 }
148
149 /**
150 * acts immiditally if isWritePacket, otherwise adds it to the readCache to be processed next tick
151 */
152 public void processOrCachePacket(Packet par1Packet)
153 {
154 String var2 = this.myNetHandler.isServerHandler() ? ">" : "<";
155
156 if (par1Packet.isWritePacket() && this.myNetHandler.canProcessPackets())
157 {
158 par1Packet.processPacket(this.myNetHandler);
159 }
160 else
161 {
162 this.readPacketCache.add(par1Packet);
163 }
164 }
165 }