Skip to content

Commit

Permalink
http2 support context
Browse files Browse the repository at this point in the history
  • Loading branch information
dinstone committed Feb 1, 2024
1 parent 716e25d commit b9e7ab6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.dinstone.focus.transport.http2;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;

import com.dinstone.focus.compress.Compressor;
Expand Down Expand Up @@ -129,6 +130,14 @@ private byte[] encodeContent(Invocation invocation, ServiceConfig serviceConfig,
return content;
}

public InetSocketAddress getRemoteAddress() {
return (InetSocketAddress) channel.remoteAddress();
}

public InetSocketAddress getLocalAddress() {
return (InetSocketAddress) channel.localAddress();
}

public static class StreamChannelHandler extends SimpleChannelInboundHandler<Http2StreamFrame> {

private final CompletableFuture<Object> future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ public CompletableFuture<Object> send(Invocation invocation, ServiceInstance ins
// create connection
if (instance.isEnableSsl()) {
Http2Channel http2Channel = secureChannelFactory.create(instance.getInstanceAddress());
invocation.context().setRemoteAddress(http2Channel.getRemoteAddress());
invocation.context().setLocalAddress(http2Channel.getLocalAddress());
return http2Channel.send(invocation);
} else {
Http2Channel http2Channel = commonChannelFactory.create(instance.getInstanceAddress());
invocation.context().setRemoteAddress(http2Channel.getRemoteAddress());
invocation.context().setLocalAddress(http2Channel.getLocalAddress());
return http2Channel.send(invocation);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.dinstone.focus.transport.http2;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
Expand Down Expand Up @@ -73,6 +74,9 @@ private void invoke(Channel channel, Http2HeadersFrame headersFrame, Http2DataFr
ByteBuf dataBuf = dataFrame == null ? null : dataFrame.content();
Invocation invocation = decode(headers, dataBuf, serviceConfig, methodConfig);

invocation.context().setRemoteAddress((InetSocketAddress) channel.remoteAddress());
invocation.context().setLocalAddress((InetSocketAddress) channel.localAddress());

// invoke invocation
CompletableFuture<Object> replyFuture = serviceConfig.getHandler().handle(invocation);
replyFuture.whenComplete((reply, error) -> {
Expand Down

0 comments on commit b9e7ab6

Please sign in to comment.