@Override public InetSocketAddress address() { return ((ServerSocketChannel) f.channel()).localAddress(); }
@Nullable public String extractRemoteAddressAndPort(Channel channel) { if (channel instanceof ServerSocketChannel) { val inetSocketAddress = ((ServerSocketChannel) channel).remoteAddress(); return computeAddressAndPort(inetSocketAddress); } else if (channel instanceof SocketChannel) { val inetSocketAddress = ((SocketChannel) channel).remoteAddress(); return computeAddressAndPort(inetSocketAddress); } else { return null; } }
private NettyHandshakeServer createHandshakeServer( SocketChannel channel ) { HandshakeServer handshakeServer = new HandshakeServer( applicationProtocolRepository, modifierProtocolRepository, new SimpleNettyChannel( channel, log ) ); handshakeServer.protocolStackFuture().whenComplete( ( protocolStack, failure ) -> onHandshakeComplete( protocolStack, channel, failure ) ); channel.closeFuture().addListener( f -> { try { channel.parent().pipeline().fireUserEventTriggered( new ServerHandshakeFinishedEvent.Closed( toSocketAddress( channel ) ) ); } catch ( RejectedExecutionException ignored ) { } } ); return new NettyHandshakeServer( handshakeServer ); }
@Override protected boolean isCompatible(EventLoop loop) { if (!(loop instanceof XnioEventLoop)) { return false; } ServerSocketChannel parent = parent(); if (parent != null) { // if this channel has a parent we need to ensure that both EventLoopGroups are the same for XNIO // to be sure it uses a Thread from the correct Worker. if (parent.eventLoop().parent() != loop.parent()) { return false; } } return true; }
private void onHandshakeComplete( ProtocolStack protocolStack, SocketChannel channel, Throwable failure ) { if ( failure != null ) { log.error( "Error when negotiating protocol stack", failure ); return; } try { protocolInstallerRepository.installerFor( protocolStack ).install( channel ); channel.parent().pipeline().fireUserEventTriggered( new ServerHandshakeFinishedEvent.Created( toSocketAddress( channel ), protocolStack ) ); } catch ( Throwable t ) { log.error( "Error installing protocol stack", t ); } }
/** * Return local server selector channel address. * * @return local {@link InetSocketAddress} */ default InetSocketAddress address(){ Channel c = channel(); if (c instanceof SocketChannel) { return ((SocketChannel) c).remoteAddress(); } if (c instanceof ServerSocketChannel) { return ((ServerSocketChannel) c).localAddress(); } if (c instanceof DatagramChannel) { InetSocketAddress a = ((DatagramChannel) c).remoteAddress(); return a != null ? a : ((DatagramChannel)c ).localAddress(); } throw new IllegalStateException("Does not have an InetSocketAddress"); }
@Test public void testValidAddressSocketServerChannel() throws Exception { val testPort = 123; val testAddress = "192.168.0.1"; val inetSocketAddress = new InetSocketAddress(testAddress, testPort); val expectedResult = testAddress + ":" + String.valueOf(testPort); when(serverSocketChannel.remoteAddress()).thenReturn(inetSocketAddress); val result = subject.extractRemoteAddressAndPort(serverSocketChannel); assertTrue(result.equals(expectedResult)); }
/** * Return local server selector channel address. * * @return local {@link InetSocketAddress} */ default InetSocketAddress address(){ Channel c = channel(); if (c instanceof SocketChannel) { return ((SocketChannel) c).remoteAddress(); } if (c instanceof ServerSocketChannel) { return ((ServerSocketChannel) c).localAddress(); } if (c instanceof DatagramChannel) { InetSocketAddress a = ((DatagramChannel) c).remoteAddress(); return a != null ? a : ((DatagramChannel)c ).localAddress(); } throw new IllegalStateException("Does not have an InetSocketAddress"); }
@Test public void testWithoutInetSocketAddressSocketServerChannel() throws Exception { when(serverSocketChannel.remoteAddress()).thenReturn(null); val result = subject.extractRemoteAddressAndPort(serverSocketChannel); assertTrue(result == null); }
/** * Return remote address if remote channel {@link NettyContext} otherwise local * address if server selector channel. * * @return remote or local {@link InetSocketAddress} */ default InetSocketAddress address(){ Channel c = channel(); if (c instanceof SocketChannel) { return ((SocketChannel) c).remoteAddress(); } if (c instanceof ServerSocketChannel) { return ((ServerSocketChannel) c).localAddress(); } if (c instanceof DatagramChannel) { InetSocketAddress a = ((DatagramChannel) c).remoteAddress(); return a != null ? a : ((DatagramChannel)c ).localAddress(); } throw new IllegalStateException("Does not have an InetSocketAddress"); }