use of io.grpc.internal.ServerTransportListener in project grpc-java by grpc.
the class NettyServerTest method getPort.
@Test
public void getPort() throws Exception {
InetSocketAddress addr = new InetSocketAddress(0);
NettyServer ns = new NettyServer(addr, NioServerSocketChannel.class, // no boss group
null, // no event group
null, new ProtocolNegotiators.PlaintextNegotiator(), // ignore
1, // ignore
1, // ignore
1, // ignore
1);
ns.start(new ServerListener() {
@Override
public ServerTransportListener transportCreated(ServerTransport transport) {
return null;
}
@Override
public void serverShutdown() {
}
});
// Check that we got an actual port.
assertThat(ns.getPort()).isGreaterThan(0);
// Cleanup
ns.shutdown();
}
use of io.grpc.internal.ServerTransportListener in project grpc-java by grpc.
the class NettyServer method start.
@Override
public void start(ServerListener serverListener) throws IOException {
listener = checkNotNull(serverListener, "serverListener");
// If using the shared groups, get references to them.
allocateSharedGroups();
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(channelType);
if (NioServerSocketChannel.class.isAssignableFrom(channelType)) {
b.option(SO_BACKLOG, 128);
b.childOption(SO_KEEPALIVE, true);
}
b.childHandler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel ch) throws Exception {
NettyServerTransport transport = new NettyServerTransport(ch, protocolNegotiator, maxStreamsPerConnection, flowControlWindow, maxMessageSize, maxHeaderListSize);
ServerTransportListener transportListener;
// This is to order callbacks on the listener, not to guard access to channel.
synchronized (NettyServer.this) {
if (channel != null && !channel.isOpen()) {
// Server already shutdown.
ch.close();
return;
}
// `channel` shutdown can race with `ch` initialization, so this is only safe to increment
// inside the lock.
eventLoopReferenceCounter.retain();
transportListener = listener.transportCreated(transport);
}
transport.start(transportListener);
ch.closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
eventLoopReferenceCounter.release();
}
});
}
});
// Bind and start to accept incoming connections.
ChannelFuture future = b.bind(address);
try {
future.await();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted waiting for bind");
}
if (!future.isSuccess()) {
throw new IOException("Failed to bind", future.cause());
}
channel = future.channel();
}
Aggregations