/** * Adds a channel that listens locally */ public SocketAddress addLocalEndpoint() { ChannelFuture channelfuture; synchronized (this.endpoints) { channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer<Channel>() { protected void initChannel(Channel p_initChannel_1_) throws Exception { NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND); networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager)); NetworkSystem.this.networkManagers.add(networkmanager); p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager); } }).group((EventLoopGroup)eventLoops.getValue()).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly(); this.endpoints.add(channelfuture); } return channelfuture.channel().localAddress(); }
/** * Adds a channel that listens locally */ public SocketAddress addLocalEndpoint() { ChannelFuture channelfuture; synchronized (this.endpoints) { channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer<Channel>() { protected void initChannel(Channel p_initChannel_1_) throws Exception { NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND); networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager)); NetworkSystem.this.networkManagers.add(networkmanager); p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager); } }).group((EventLoopGroup)SERVER_NIO_EVENTLOOP.getValue()).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly(); this.endpoints.add(channelfuture); } return channelfuture.channel().localAddress(); }
/** * Adds a channel that listens locally */ @SideOnly(Side.CLIENT) public SocketAddress addLocalEndpoint() { ChannelFuture channelfuture; synchronized (this.endpoints) { channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer<Channel>() { protected void initChannel(Channel p_initChannel_1_) throws Exception { NetworkManager networkmanager = new NetworkManager(EnumPacketDirection.SERVERBOUND); networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager)); NetworkSystem.this.networkManagers.add(networkmanager); p_initChannel_1_.pipeline().addLast((String)"packet_handler", (ChannelHandler)networkmanager); } }).group((EventLoopGroup)SERVER_NIO_EVENTLOOP.getValue()).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly(); this.endpoints.add(channelfuture); } return channelfuture.channel().localAddress(); }
/** * Sets up a server channel bound to the given local address. * * @return the event loop group used to process incoming connections. */ static EventLoopGroup setUpServer( ChannelInitializer<LocalChannel> serverInitializer, LocalAddress localAddress) throws Exception { // Only use one thread in the event loop group. The same event loop group will be used to // register client channels during setUpClient as well. This ensures that all I/O activities // in both channels happen in the same thread, making debugging easier (i. e. no need to jump // between threads when debugging, everything happens synchronously within the only I/O // effectively). Note that the main thread is still separate from the I/O thread and // synchronization (using the lock field) is still needed when the main thread needs to verify // properties calculated by the I/O thread. EventLoopGroup eventLoopGroup = new NioEventLoopGroup(1); ServerBootstrap sb = new ServerBootstrap() .group(eventLoopGroup) .channel(LocalServerChannel.class) .childHandler(serverInitializer); ChannelFuture unusedFuture = sb.bind(localAddress).syncUninterruptibly(); return eventLoopGroup; }
@SideOnly(Side.CLIENT) public SocketAddress addLocalEndpoint() { List list = this.endpoints; ChannelFuture channelfuture; synchronized (this.endpoints) { channelfuture = ((ServerBootstrap)((ServerBootstrap)(new ServerBootstrap()).channel(LocalServerChannel.class)).childHandler(new ChannelInitializer() { private static final String __OBFID = "CL_00001449"; protected void initChannel(Channel p_initChannel_1_) { NetworkManager networkmanager = new NetworkManager(false); networkmanager.setNetHandler(new NetHandlerHandshakeMemory(NetworkSystem.this.mcServer, networkmanager)); NetworkSystem.this.networkManagers.add(networkmanager); p_initChannel_1_.pipeline().addLast("packet_handler", networkmanager); } }).group(eventLoops).localAddress(LocalAddress.ANY)).bind().syncUninterruptibly(); this.endpoints.add(channelfuture); } return channelfuture.channel().localAddress(); }
@Test public void testTryWithResourcesShouldCloseAllClustersButNotEventLoopIfProvided() throws Exception { EventLoopGroup eventLoop = new DefaultEventLoopGroup(); BoundCluster cluster; MockClient client; try (Server server = Server.builder() .withAddressResolver(localAddressResolver) .withEventLoopGroup(eventLoop, LocalServerChannel.class) .build()) { cluster = server.register(ClusterSpec.builder().withNodes(5)); BoundNode node = cluster.node(0); SocketAddress address = node.getAddress(); client = new MockClient(eventLoop); client.connect(address); } // event loop should not have been closed. assertThat(eventLoop.isShutdown()).isFalse(); // timer should have since a custom one was not provided. try { cluster .getServer() .timer .newTimeout( timeout -> { // noop }, 1, TimeUnit.SECONDS); fail("Expected IllegalStateException"); } catch (IllegalStateException ise) { // expected } eventLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS); }
@Test public void testTryWithResourcesShouldCloseAllClustersButNotEventLoopAndTimerIfProvided() throws Exception { EventLoopGroup eventLoop = new DefaultEventLoopGroup(); Timer timer = new HashedWheelTimer(); BoundCluster cluster; MockClient client; try (Server server = Server.builder() .withAddressResolver(localAddressResolver) .withTimer(timer) .withEventLoopGroup(eventLoop, LocalServerChannel.class) .build()) { cluster = server.register(ClusterSpec.builder().withNodes(5)); BoundNode node = cluster.node(0); SocketAddress address = node.getAddress(); client = new MockClient(eventLoop); client.connect(address); } // event loop should not have been closed. assertThat(eventLoop.isShutdown()).isFalse(); // timer should not have since a custom one was not provided. cluster .getServer() .timer .newTimeout( timeout -> { // noop }, 1, TimeUnit.SECONDS); eventLoop.shutdownGracefully(0, 0, TimeUnit.SECONDS); timer.stop(); }
ServerBootstrap getLocalServerBootstrap() { EventLoopGroup serverGroup = new LocalEventLoopGroup(); ServerBootstrap sb = new ServerBootstrap(); sb.group(serverGroup); sb.channel(LocalServerChannel.class); sb.childHandler(new ChannelInitializer<LocalChannel>() { @Override public void initChannel(LocalChannel ch) throws Exception { } }); return sb; }
ServerBootstrap getLocalServerBootstrap() { EventLoopGroup serverGroup = new DefaultEventLoopGroup(); ServerBootstrap sb = new ServerBootstrap(); sb.group(serverGroup); sb.channel(LocalServerChannel.class); sb.childHandler(new ChannelInitializer<LocalChannel>() { @Override public void initChannel(LocalChannel ch) throws Exception { } }); return sb; }
@Override protected AbstractServerImplBuilder<?> getServerBuilder() { return NettyServerBuilder .forAddress(new LocalAddress("in-process-1")) .flowControlWindow(65 * 1024) .maxMessageSize(AbstractInteropTest.MAX_MESSAGE_SIZE) .channelType(LocalServerChannel.class); }
@Before public void setUp() throws Exception { ServerBootstrap b = new ServerBootstrap(); LocalAddress address = LocalAddress.ANY; EventLoopGroup workerGroup = new NioEventLoopGroup(); final FixAcceptorChannelInitializer<Channel> channelInitializer = new FixAcceptorChannelInitializer<>( workerGroup, new FixApplicationAdapter(), authenticator, new InMemorySessionRepository() ); serverChannel = (LocalServerChannel) b.group(new NioEventLoopGroup()) .channel(LocalServerChannel.class) .handler(channelInitializer) .childHandler(new FixApplicationAdapter()) .validate() .bind(address) .sync() .channel(); pipeline = serverChannel.pipeline(); when(authenticator.authenticate(any(FixMessage.class))).thenReturn(true); }
@Test public void testRegisterClusterFailsWhenBindTimesOut() throws Exception { // Designated address to be slow to bind. SocketAddress slowAddr = localAddressResolver.get(); // create a bootstrap with a handler that delays binding by 1 second for designated address. ServerBootstrap serverBootstrap = new ServerBootstrap() .group(eventLoop) .channel(LocalServerChannel.class) .handler(new SlowBindHandler(slowAddr)) .childHandler(new Server.Initializer()); // Define server with 500ms timeout, which should cause binding of slow address to timeout and // fail register. Server flakyServer = new Server( localAddressResolver, eventLoop, true, new HashedWheelTimer(), false, TimeUnit.NANOSECONDS.convert(500, TimeUnit.MILLISECONDS), new StubStore(), false, serverBootstrap); // Create a 2 node cluster with 1 node having the slow address. ClusterSpec cluster = ClusterSpec.builder().build(); DataCenterSpec dc = cluster.addDataCenter().build(); dc.addNode().withAddress(slowAddr).build(); dc.addNode().build(); // Attempt to register which should fail. try { flakyServer.register(cluster); fail(); } catch (Exception e) { // Expect a timeout exception. assertThat(e.getCause()).isInstanceOf(TimeoutException.class); } }
/** * True if this NetworkManager uses a memory connection (single player game). False may imply both an active TCP * connection or simply no active connection at all */ public boolean isLocalChannel() { return this.channel instanceof LocalChannel || this.channel instanceof LocalServerChannel; }
public boolean isLocalChannel() { return this.channel instanceof LocalChannel || this.channel instanceof LocalServerChannel; }
public boolean isLocal() { return this.channel instanceof LocalChannel || this.channel instanceof LocalServerChannel; }
@Override protected Class<? extends ServerChannel> channelClass() { return LocalServerChannel.class; }