| 1 | package net.minecraft.network;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
| 5 | import io.netty.bootstrap.ServerBootstrap;
|
| 6 | import io.netty.channel.Channel;
|
| 7 | import io.netty.channel.ChannelException;
|
| 8 | import io.netty.channel.ChannelFuture;
|
| 9 | import io.netty.channel.ChannelHandler;
|
| 10 | import io.netty.channel.ChannelInitializer;
|
| 11 | import io.netty.channel.ChannelOption;
|
| 12 | import io.netty.channel.EventLoopGroup;
|
| 13 | import io.netty.channel.epoll.Epoll;
|
| 14 | import io.netty.channel.epoll.EpollEventLoopGroup;
|
| 15 | import io.netty.channel.epoll.EpollServerSocketChannel;
|
| 16 | import io.netty.channel.local.LocalAddress;
|
| 17 | import io.netty.channel.local.LocalEventLoopGroup;
|
| 18 | import io.netty.channel.local.LocalServerChannel;
|
| 19 | import io.netty.channel.nio.NioEventLoopGroup;
|
| 20 | import io.netty.channel.socket.ServerSocketChannel;
|
| 21 | import io.netty.channel.socket.nio.NioServerSocketChannel;
|
| 22 | import io.netty.handler.timeout.ReadTimeoutHandler;
|
| 23 | import io.netty.util.concurrent.Future;
|
| 24 | import io.netty.util.concurrent.GenericFutureListener;
|
| 25 | import java.io.IOException;
|
| 26 | import java.net.InetAddress;
|
| 27 | import java.net.SocketAddress;
|
| 28 | import java.util.Collections;
|
| 29 | import java.util.Iterator;
|
| 30 | import java.util.List;
|
| 31 | import java.util.concurrent.Callable;
|
| 32 | import net.minecraft.client.network.NetHandlerHandshakeMemory;
|
| 33 | import net.minecraft.crash.CrashReport;
|
| 34 | import net.minecraft.crash.CrashReportCategory;
|
| 35 | import net.minecraft.network.play.server.S40PacketDisconnect;
|
| 36 | import net.minecraft.server.MinecraftServer;
|
| 37 | import net.minecraft.server.network.NetHandlerHandshakeTCP;
|
| 38 | import net.minecraft.util.ChatComponentText;
|
| 39 | import net.minecraft.util.LazyLoadBase;
|
| 40 | import net.minecraft.util.MessageDeserializer;
|
| 41 | import net.minecraft.util.MessageDeserializer2;
|
| 42 | import net.minecraft.util.MessageSerializer;
|
| 43 | import net.minecraft.util.MessageSerializer2;
|
| 44 | import net.minecraft.util.ReportedException;
|
| 45 | import org.apache.logging.log4j.LogManager;
|
| 46 | import org.apache.logging.log4j.Logger;
|
| 47 |
|
| 48 | public class NetworkSystem
|
| 49 | {
|
| 50 | private static final Logger logger = LogManager.getLogger();
|
| 51 | public static final LazyLoadBase<NioEventLoopGroup> eventLoops = new LazyLoadBase<NioEventLoopGroup>()
|
| 52 | {
|
| 53 | protected NioEventLoopGroup load()
|
| 54 | {
|
| 55 | return new NioEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Server IO #%d").setDaemon(true).build());
|
| 56 | }
|
| 57 | };
|
| 58 | public static final LazyLoadBase<EpollEventLoopGroup> SERVER_EPOLL_EVENTLOOP = new LazyLoadBase<EpollEventLoopGroup>()
|
| 59 | {
|
| 60 | protected EpollEventLoopGroup load()
|
| 61 | {
|
| 62 | return new EpollEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Epoll Server IO #%d").setDaemon(true).build());
|
| 63 | }
|
| 64 | };
|
| 65 | public static final LazyLoadBase<LocalEventLoopGroup> SERVER_LOCAL_EVENTLOOP = new LazyLoadBase<LocalEventLoopGroup>()
|
| 66 | {
|
| 67 | protected LocalEventLoopGroup load()
|
| 68 | {
|
| 69 | return new LocalEventLoopGroup(0, (new ThreadFactoryBuilder()).setNameFormat("Netty Local Server IO #%d").setDaemon(true).build());
|
| 70 | }
|
| 71 | };
|
| 72 |
|
| 73 | /** Reference to the MinecraftServer object. */
|
| 74 | private final MinecraftServer mcServer;
|
| 75 |
|
| 76 | /** True if this NetworkSystem has never had his endpoints terminated */
|
| 77 | public volatile boolean isAlive;
|
| 78 | private final List<ChannelFuture> endpoints = Collections.<ChannelFuture>synchronizedList(Lists.<ChannelFuture>newArrayList());
|
| 79 | private final List<NetworkManager> networkManagers = Collections.<NetworkManager>synchronizedList(Lists.<NetworkManager>newArrayList());
|
| 80 |
|
| 81 | public NetworkSystem(MinecraftServer server)
|
| 82 | {
|
| 83 | this.mcServer = server;
|
| 84 | this.isAlive = true;
|
| 85 | }
|
| 86 |
|
| 87 | /**
|
| 88 | * Adds a channel that listens on publicly accessible network ports
|
| 89 | */
|
| 90 | public void addLanEndpoint(InetAddress address, int port) throws IOException
|
| 91 | {
|
| 92 | synchronized (this.endpoints)
|
| 93 | {
|
| 94 | Class <? extends ServerSocketChannel > oclass;
|
| 95 | LazyLoadBase <? extends EventLoopGroup > lazyloadbase;
|
| 96 |
|
| 97 | if (Epoll.isAvailable() && this.mcServer.shouldUseNativeTransport())
|
| 98 | {
|
| 99 | oclass = EpollServerSocketChannel.class;
|
| 100 | lazyloadbase = SERVER_EPOLL_EVENTLOOP;
|
| 101 | logger.info("Using epoll channel type");
|
| 102 | }
|
| 103 | else
|
| 104 | {
|
| 105 | oclass = NioServerSocketChannel.class;
|
| 106 | lazyloadbase = eventLoops;
|
| 107 | logger.info("Using default channel type");
|
| 108 | }
|
| 109 |
|
| 110 | this.endpoints.add(((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(oclass)).childHandler(new ChannelInitializer<Channel>()
|
| 111 | {
|
| 112 | protected void initChannel(Channel p_initChannel_1_) throws Exception
|
| 113 | {
|
| 114 | try
|
| 115 | {
|
| 116 | p_initChannel_1_.config().setOption(ChannelOption.TCP_NODELAY, Boolean.valueOf(true));
|
| 117 | }
|
| 118 | catch (ChannelException var3)
|
| 119 | {
|
| 120 | ;
|
| 121 | }
|
| 122 |
|
| 123 | p_initChannel_1_.pipeline().addLast((String)"timeout", (ChannelHandler)(new ReadTimeoutHandler(30))).addLast((String)"legacy_query", (ChannelHandler)(new PingResponseHandler(NetworkSystem.this))).addLast((String)"splitter", (ChannelHandler)(new MessageDeserializer2())).addLast((String)"decoder", (ChannelHandler)(new MessageDeserializer(EnumPacketDirection.SERVERBOUND))).addLast((String)"prepender", (ChannelHandler)(new MessageSerializer2())).addLast((String)"encoder", (ChannelHandler)(new MessageSerializer(EnumPacketDirection.CLIENTBOUND)));
|
| 124 | NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND);
|
| 125 | NetworkSystem.this.networkManagers.add(networkmanager);
|
| 126 | p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager);
|
| 127 | networkmanager.setNetHandler(new NetHandlerHandshakeTCP(NetworkSystem.this.mcServer, networkmanager));
|
| 128 | }
|
| 129 | }).group((EventLoopGroup)lazyloadbase.getValue()).localAddress(address, port)).bind().syncUninterruptibly());
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | /**
|
| 134 | * Adds a channel that listens locally
|
| 135 | */
|
| 136 | public SocketAddress addLocalEndpoint()
|
| 137 | {
|
| 138 | ChannelFuture channelfuture;
|
| 139 |
|
| 140 | synchronized (this.endpoints)
|
| 141 | {
|
| 142 | channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer<Channel>()
|
| 143 | {
|
| 144 | protected void initChannel(Channel p_initChannel_1_) throws Exception
|
| 145 | {
|
| 146 | NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND);
|
| 147 | networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager));
|
| 148 | NetworkSystem.this.networkManagers.add(networkmanager);
|
| 149 | p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager);
|
| 150 | }
|
| 151 | }).group((EventLoopGroup)eventLoops.getValue()).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly();
|
| 152 | this.endpoints.add(channelfuture);
|
| 153 | }
|
| 154 |
|
| 155 | return channelfuture.channel().localAddress();
|
| 156 | }
|
| 157 |
|
| 158 | /**
|
| 159 | * Shuts down all open endpoints (with immediate effect?)
|
| 160 | */
|
| 161 | public void terminateEndpoints()
|
| 162 | {
|
| 163 | this.isAlive = false;
|
| 164 |
|
| 165 | for (ChannelFuture channelfuture : this.endpoints)
|
| 166 | {
|
| 167 | try
|
| 168 | {
|
| 169 | channelfuture.channel().close().sync();
|
| 170 | }
|
| 171 | catch (InterruptedException var4)
|
| 172 | {
|
| 173 | logger.error("Interrupted whilst closing channel");
|
| 174 | }
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | /**
|
| 179 | * Will try to process the packets received by each NetworkManager, gracefully manage processing failures and cleans
|
| 180 | * up dead connections
|
| 181 | */
|
| 182 | public void networkTick()
|
| 183 | {
|
| 184 | synchronized (this.networkManagers)
|
| 185 | {
|
| 186 | Iterator<NetworkManager> iterator = this.networkManagers.iterator();
|
| 187 |
|
| 188 | while (iterator.hasNext())
|
| 189 | {
|
| 190 | final NetworkManager networkmanager = (NetworkManager)iterator.next();
|
| 191 |
|
| 192 | if (!networkmanager.hasNoChannel())
|
| 193 | {
|
| 194 | if (!networkmanager.isChannelOpen())
|
| 195 | {
|
| 196 | iterator.remove();
|
| 197 | networkmanager.checkDisconnected();
|
| 198 | }
|
| 199 | else
|
| 200 | {
|
| 201 | try
|
| 202 | {
|
| 203 | networkmanager.processReceivedPackets();
|
| 204 | }
|
| 205 | catch (Exception exception)
|
| 206 | {
|
| 207 | if (networkmanager.isLocalChannel())
|
| 208 | {
|
| 209 | CrashReport crashreport = CrashReport.makeCrashReport(exception, "Ticking memory connection");
|
| 210 | CrashReportCategory crashreportcategory = crashreport.makeCategory("Ticking connection");
|
| 211 | crashreportcategory.addCrashSectionCallable("Connection", new Callable<String>()
|
| 212 | {
|
| 213 | public String call() throws Exception
|
| 214 | {
|
| 215 | return networkmanager.toString();
|
| 216 | }
|
| 217 | });
|
| 218 | throw new ReportedException(crashreport);
|
| 219 | }
|
| 220 |
|
| 221 | logger.warn((String)("Failed to handle packet for " + networkmanager.getRemoteAddress()), (Throwable)exception);
|
| 222 | final ChatComponentText chatcomponenttext = new ChatComponentText("Internal server error");
|
| 223 | networkmanager.sendPacket(new S40PacketDisconnect(chatcomponenttext), new GenericFutureListener < Future <? super Void >> ()
|
| 224 | {
|
| 225 | public void operationComplete(Future <? super Void > p_operationComplete_1_) throws Exception
|
| 226 | {
|
| 227 | networkmanager.closeChannel(chatcomponenttext);
|
| 228 | }
|
| 229 | }, new GenericFutureListener[0]);
|
| 230 | networkmanager.disableAutoRead();
|
| 231 | }
|
| 232 | }
|
| 233 | }
|
| 234 | }
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | public MinecraftServer getServer()
|
| 239 | {
|
| 240 | return this.mcServer;
|
| 241 | }
|
| 242 | }
|
| 243 |
|