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