下面列出了怎么用io.netty.channel.socket.SocketChannelConfig的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void setSoLingerChannelOption() throws IOException {
startServer();
Map<ChannelOption<?>, Object> channelOptions = new HashMap<ChannelOption<?>, Object>();
// set SO_LINGER option
int soLinger = 123;
channelOptions.put(ChannelOption.SO_LINGER, soLinger);
NettyClientTransport transport = new NettyClientTransport(
address, NioSocketChannel.class, channelOptions, group, newNegotiator(),
DEFAULT_WINDOW_SIZE, DEFAULT_MAX_MESSAGE_SIZE, GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE,
KEEPALIVE_TIME_NANOS_DISABLED, 1L, false, authority, null /* user agent */,
tooManyPingsRunnable, new TransportTracer(), Attributes.EMPTY, new SocketPicker());
transports.add(transport);
callMeMaybe(transport.start(clientTransportListener));
// verify SO_LINGER has been set
ChannelConfig config = transport.channel().config();
assertTrue(config instanceof SocketChannelConfig);
assertEquals(soLinger, ((SocketChannelConfig) config).getSoLinger());
}
@Test
public void setSoLingerChannelOption() throws IOException {
startServer();
Map<ChannelOption<?>, Object> channelOptions = new HashMap<>();
// set SO_LINGER option
int soLinger = 123;
channelOptions.put(ChannelOption.SO_LINGER, soLinger);
NettyClientTransport transport = new NettyClientTransport(
address, new ReflectiveChannelFactory<>(NioSocketChannel.class), channelOptions, group,
newNegotiator(), false, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_MESSAGE_SIZE,
GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, KEEPALIVE_TIME_NANOS_DISABLED, 1L, false, authority,
null /* user agent */, tooManyPingsRunnable, new TransportTracer(), Attributes.EMPTY,
new SocketPicker(), new FakeChannelLogger(), false);
transports.add(transport);
callMeMaybe(transport.start(clientTransportListener));
// verify SO_LINGER has been set
ChannelConfig config = transport.channel().config();
assertTrue(config instanceof SocketChannelConfig);
assertEquals(soLinger, ((SocketChannelConfig) config).getSoLinger());
}
/**
* Template method for changing properties on the given {@link SocketChannelConfig}.
* <p>The default implementation sets the connect timeout based on the set property.
* @param config the channel configuration
*/
protected void configureChannel(SocketChannelConfig config) {
if (this.connectTimeout >= 0) {
config.setConnectTimeoutMillis(this.connectTimeout);
}
}
@Override
public SocketChannelConfig config() {
return config;
}
/**
* Template method for changing properties on the given {@link SocketChannelConfig}.
* <p>The default implementation sets the connect timeout based on the set property.
* @param config the channel configuration
*/
protected void configureChannel(SocketChannelConfig config) {
if (this.connectTimeout >= 0) {
config.setConnectTimeoutMillis(this.connectTimeout);
}
}
/**
* Simulates netty channel behavior, behavior verified by {@link ChannelBehavior} below.
*/
@Before
public void setup() {
// A description prefixed with "IGNORE " will not fail the suite!
assumeThat(desc, desc, not(startsWith("IGNORE ")));
ctx = mock(ChannelHandlerContext.class);
channel = mock(SocketChannel.class, "");
when(ctx.channel()).thenReturn(channel);
// Asserts
EventExecutor exec = mock(EventExecutor.class);
when(ctx.executor()).thenReturn(exec);
when(exec.inEventLoop()).thenReturn(true);
scc = mock(SocketChannelConfig.class);
when(channel.config()).thenReturn(scc);
EventLoop loop = mock(EventLoop.class);
when(channel.eventLoop()).thenReturn(loop);
when(loop.inEventLoop()).thenReturn(true);
when(scc.isAllowHalfClosure()).thenReturn(true);
when(channel.isOutputShutdown()).then(__ -> outputShutdown.get());
when(channel.isInputShutdown()).then(__ -> inputShutdown.get());
when(channel.isOpen()).then(__ -> !closed.get());
ChannelFuture future = mock(ChannelFuture.class);
when(channel.shutdownInput()).then(__ -> {
inputShutdown.set(true);
LOGGER.debug("channel.shutdownInput()");
h.channelClosedInbound(ctx); // OutputShutDownEvent observed from transport
return future;
});
when(channel.shutdownOutput()).then(__ -> {
outputShutdown.set(true);
LOGGER.debug("channel.shutdownOutput()");
h.channelClosedOutbound(ctx); // InputShutDownReadComplete observed from transport
return future;
});
when(channel.close()).then(__ -> {
closed.set(true);
LOGGER.debug("channel.close()");
return future;
});
when(scc.setSoLinger(0)).then(__ -> {
LOGGER.debug("channel.config().setSoLinger(0)");
if (inputShutdown.get() && outputShutdown.get()) {
fail("mock => setsockopt() failed - output already shutdown!");
}
return scc;
});
h = (RequestResponseCloseHandler) spy(mode == Mode.C ? forPipelinedRequestResponse(true, channel.config()) :
forPipelinedRequestResponse(false, channel.config()));
h.registerEventHandler(channel, e -> {
if (observedEvent == null) {
LOGGER.debug("Emitted: {}", e);
observedEvent = e;
}
});
}
/**
* Template method for changing properties on the given {@link SocketChannelConfig}.
* <p>The default implementation sets the connect timeout based on the set property.
* @param config the channel configuration
*/
protected void configureChannel(SocketChannelConfig config) {
if (this.connectTimeout >= 0) {
config.setConnectTimeoutMillis(this.connectTimeout);
}
}
/**
* Template method for changing properties on the given {@link SocketChannelConfig}.
* <p>The default implementation sets the connect timeout based on the set property.
* @param config the channel configuration
*/
protected void configureChannel(SocketChannelConfig config) {
if (this.connectTimeout >= 0) {
config.setConnectTimeoutMillis(this.connectTimeout);
}
}
/**
* Template method for changing properties on the given {@link SocketChannelConfig}.
* <p>The default implementation sets the connect timeout based on the set property.
* @param config the channel configuration
*/
protected void configureChannel(SocketChannelConfig config) {
if (this.connectTimeout >= 0) {
config.setConnectTimeoutMillis(this.connectTimeout);
}
}
@Override
public SocketChannelConfig config() {
return config;
}
@Override
public SocketChannelConfig config() {
return config;
}
@Override
public SocketChannelConfig setTcpNoDelay(boolean tcpNoDelay) {
channel.setOption(Options.TCP_NODELAY, tcpNoDelay);
return this;
}
@Override
public SocketChannelConfig setSoLinger(int soLinger) {
throw new UnsupportedOperationException();
}
@Override
public SocketChannelConfig setSendBufferSize(int sendBufferSize) {
channel.setOption(Options.SEND_BUFFER, sendBufferSize);
return this;
}
@Override
public SocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
channel.setOption(Options.RECEIVE_BUFFER, receiveBufferSize);
return this;
}
@Override
public SocketChannelConfig setKeepAlive(boolean keepAlive) {
channel.setOption(Options.KEEP_ALIVE, keepAlive);
return this;
}
@Override
public SocketChannelConfig setTrafficClass(int trafficClass) {
channel.setOption(Options.IP_TRAFFIC_CLASS, trafficClass);
return this;
}
@Override
public SocketChannelConfig setReuseAddress(boolean reuseAddress) {
channel.setOption(Options.REUSE_ADDRESSES, reuseAddress);
return this;
}
@Override
public SocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
throw new UnsupportedOperationException();
}
@Override
public SocketChannelConfig setAllowHalfClosure(boolean allowHalfClosure) {
throw new UnsupportedOperationException();
}
@Override
public SocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
super.setConnectTimeoutMillis(connectTimeoutMillis);
return this;
}
@Override
public SocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
super.setMaxMessagesPerRead(maxMessagesPerRead);
return this;
}
@Override
public SocketChannelConfig setWriteSpinCount(int writeSpinCount) {
super.setWriteSpinCount(writeSpinCount);
return this;
}
@Override
public SocketChannelConfig setAllocator(ByteBufAllocator allocator) {
super.setAllocator(allocator);
return this;
}
@Override
public SocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
super.setRecvByteBufAllocator(allocator);
return this;
}
@Override
public SocketChannelConfig setAutoRead(boolean autoRead) {
super.setAutoRead(autoRead);
return this;
}
@Override
public SocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
return this;
}
@Override
public SocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
super.setMessageSizeEstimator(estimator);
return this;
}
@Override
public SocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
return this;
}
@Override
public SocketChannelConfig setAutoClose(boolean autoClose) {
super.setAutoClose(autoClose);
return this;
}