下面列出了io.netty.channel.Channel#alloc ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
NettyClientStream(
TransportState state,
MethodDescriptor<?, ?> method,
Metadata headers,
Channel channel,
AsciiString authority,
AsciiString scheme,
AsciiString userAgent,
StatsTraceContext statsTraceCtx,
TransportTracer transportTracer,
CallOptions callOptions) {
super(
new NettyWritableBufferAllocator(channel.alloc()),
statsTraceCtx,
transportTracer,
headers,
callOptions,
useGet(method));
this.state = checkNotNull(state, "transportState");
this.writeQueue = state.handler.getWriteQueue();
this.method = checkNotNull(method, "method");
this.channel = checkNotNull(channel, "channel");
this.authority = checkNotNull(authority, "authority");
this.scheme = checkNotNull(scheme, "scheme");
this.userAgent = userAgent;
}
NettyClientStream(
TransportState state,
MethodDescriptor<?, ?> method,
Metadata headers,
Channel channel,
AsciiString authority,
AsciiString scheme,
AsciiString userAgent,
StatsTraceContext statsTraceCtx,
TransportTracer transportTracer,
CallOptions callOptions,
boolean useGetForSafeMethods) {
super(
new NettyWritableBufferAllocator(channel.alloc()),
statsTraceCtx,
transportTracer,
headers,
callOptions,
useGetForSafeMethods && method.isSafe());
this.state = checkNotNull(state, "transportState");
this.writeQueue = state.handler.getWriteQueue();
this.method = checkNotNull(method, "method");
this.authority = checkNotNull(authority, "authority");
this.scheme = checkNotNull(scheme, "scheme");
this.userAgent = userAgent;
}
public NettyServerStream(
Channel channel,
TransportState state,
Attributes transportAttrs,
String authority,
StatsTraceContext statsTraceCtx,
TransportTracer transportTracer) {
super(new NettyWritableBufferAllocator(channel.alloc()), statsTraceCtx);
this.state = checkNotNull(state, "transportState");
this.writeQueue = state.handler.getWriteQueue();
this.attributes = checkNotNull(transportAttrs);
this.authority = authority;
this.transportTracer = checkNotNull(transportTracer, "transportTracer");
// Read the id early to avoid reading transportState later.
this.streamId = transportState().id();
}
public NettyServerStream(
Channel channel,
TransportState state,
Attributes transportAttrs,
String authority,
StatsTraceContext statsTraceCtx,
TransportTracer transportTracer) {
super(new NettyWritableBufferAllocator(channel.alloc()), statsTraceCtx);
this.state = checkNotNull(state, "transportState");
this.channel = checkNotNull(channel, "channel");
this.writeQueue = state.handler.getWriteQueue();
this.attributes = checkNotNull(transportAttrs);
this.authority = authority;
this.transportTracer = checkNotNull(transportTracer, "transportTracer");
}
@SuppressWarnings("SubscriberImplementation")
@Override
public reactor.core.publisher.Flux<DataBuffer> getBody() {
final Optional<Channel> opt = channelResolver.resolveChannel(request);
if (opt.isPresent()) {
final Channel channel = opt.get();
final NettyDataBufferFactory nettyDataBufferFactory = new NettyDataBufferFactory(channel.alloc());
final Optional<HttpContentProcessor<ByteBufHolder>> httpContentProcessor = channelResolver.resolveContentProcessor(request);
if (httpContentProcessor.isPresent()) {
final HttpContentProcessor<ByteBufHolder> processor = httpContentProcessor.get();
return Flux.from(subscriber -> processor.subscribe(new Subscriber<ByteBufHolder>() {
@Override
public void onSubscribe(Subscription s) {
subscriber.onSubscribe(s);
}
@Override
public void onNext(ByteBufHolder byteBufHolder) {
subscriber.onNext(nettyDataBufferFactory.wrap(byteBufHolder.content()));
}
@Override
public void onError(Throwable t) {
subscriber.onError(t);
}
@Override
public void onComplete() {
subscriber.onComplete();
}
}));
}
}
return Flux.empty();
}
public XrpcRequest(
FullHttpRequest request,
ServerContext connectionContext,
Map<String, String> groups,
Channel channel) {
this.h1Request = request;
this.h2Headers = null;
this.connectionContext = connectionContext;
this.groups = groups;
this.upstreamChannel = channel;
this.alloc = channel.alloc();
this.eventLoop = channel.eventLoop();
this.h2Data = null;
}
public XrpcRequest(
Http2Headers headers,
ServerContext connectionContext,
Map<String, String> groups,
Channel channel) {
this.h1Request = null;
this.h2Headers = headers;
this.connectionContext = connectionContext;
this.groups = groups;
this.upstreamChannel = channel;
this.alloc = channel.alloc();
this.eventLoop = channel.eventLoop();
this.h2Data = alloc.compositeBuffer();
}
@Override
protected void initChannel(Channel channel) {
ChannelPipeline pipeline = channel.pipeline();
if (config.metricsRecorder != null) {
ChannelOperations.addMetricsHandler(channel,
Objects.requireNonNull(config.metricsRecorder.get(), "Metrics recorder supplier returned null"),
remoteAddress,
onServer);
ByteBufAllocator alloc = channel.alloc();
if (alloc instanceof PooledByteBufAllocator) {
ByteBufAllocatorMetrics.INSTANCE.registerMetrics("pooled", ((PooledByteBufAllocator) alloc).metric());
}
else if (alloc instanceof UnpooledByteBufAllocator) {
ByteBufAllocatorMetrics.INSTANCE.registerMetrics("unpooled", ((UnpooledByteBufAllocator) alloc).metric());
}
}
if (config.loggingHandler != null) {
pipeline.addFirst(NettyPipeline.LoggingHandler, config.loggingHandler);
}
ChannelOperations.addReactiveBridge(channel, config.channelOperationsProvider(), connectionObserver);
config.defaultOnChannelInit()
.then(config.doOnChannelInit)
.onChannelInit(connectionObserver, channel, remoteAddress);
pipeline.remove(this);
if (log.isDebugEnabled()) {
log.debug(format(channel, "Initialized pipeline {}"), pipeline.toString());
}
}
@Override
public ByteBufAllocator alloc() {
final Channel channel = channel();
return channel != null ? channel.alloc() : PooledByteBufAllocator.DEFAULT;
}