run the message loop asynchronously
This commit is contained in:
parent
af47f1a825
commit
2ddc8e1d79
3 changed files with 24 additions and 22 deletions
|
@ -260,7 +260,8 @@ final class RpcState<VatId> {
|
|||
private final CompletableFuture<java.lang.Void> onDisconnect;
|
||||
private Throwable disconnected = null;
|
||||
private CompletableFuture<java.lang.Void> messageReady = CompletableFuture.completedFuture(null);
|
||||
private final CompletableFuture<java.lang.Void> messageLoop;
|
||||
private final CompletableFuture<java.lang.Void> messageLoop = new CompletableFuture<>();
|
||||
// completes when the message loop exits
|
||||
private final ReferenceQueue<Question> questionRefs = new ReferenceQueue<>();
|
||||
private final ReferenceQueue<ImportClient> importRefs = new ReferenceQueue<>();
|
||||
|
||||
|
@ -270,7 +271,7 @@ final class RpcState<VatId> {
|
|||
this.bootstrapFactory = bootstrapFactory;
|
||||
this.connection = connection;
|
||||
this.onDisconnect = onDisconnect;
|
||||
this.messageLoop = this.doMessageLoop();
|
||||
startMessageLoop();
|
||||
}
|
||||
|
||||
public CompletableFuture<java.lang.Void> getMessageLoop() {
|
||||
|
@ -397,24 +398,30 @@ final class RpcState<VatId> {
|
|||
return pipeline.getPipelinedCap(new PipelineOp[0]);
|
||||
}
|
||||
|
||||
private CompletableFuture<java.lang.Void> doMessageLoop() {
|
||||
private void startMessageLoop() {
|
||||
if (isDisconnected()) {
|
||||
return CompletableFuture.failedFuture(this.disconnected);
|
||||
this.messageLoop.completeExceptionally(this.disconnected);
|
||||
return;
|
||||
}
|
||||
|
||||
return connection.receiveIncomingMessage().thenCompose(message -> {
|
||||
var messageReader = this.connection.receiveIncomingMessage()
|
||||
.thenAccept(message -> {
|
||||
if (message == null) {
|
||||
this.messageLoop.complete(null);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
this.handleMessage(message);
|
||||
} catch (Exception rpcExc) {
|
||||
// either we received an Abort message from peer
|
||||
// or internal RpcState is bad.
|
||||
return this.disconnect(rpcExc);
|
||||
this.disconnect(rpcExc);
|
||||
}
|
||||
this.cleanupImports();
|
||||
this.cleanupQuestions();
|
||||
return this.doMessageLoop();
|
||||
});
|
||||
|
||||
}).exceptionallyCompose(exc -> this.disconnect(exc));
|
||||
messageReader.thenRunAsync(this::startMessageLoop);
|
||||
}
|
||||
|
||||
private void handleMessage(IncomingRpcMessage message) throws RpcException {
|
||||
|
|
|
@ -77,11 +77,7 @@ public class TwoPartyVatNetwork
|
|||
public CompletableFuture<IncomingRpcMessage> receiveIncomingMessage() {
|
||||
var message = Serialize.readAsync(channel)
|
||||
.thenApply(reader -> (IncomingRpcMessage) new IncomingMessage(reader))
|
||||
.whenComplete((msg, exc) -> {
|
||||
if (exc != null) {
|
||||
this.peerDisconnected.complete(null);
|
||||
}
|
||||
});
|
||||
.exceptionally(exc -> null);
|
||||
|
||||
// send to message tap
|
||||
if (this.tap != null) {
|
||||
|
|
|
@ -110,7 +110,6 @@ public class RpcStateTest {
|
|||
var msg = new TestMessage();
|
||||
msg.builder.getRoot(RpcProtocol.Message.factory).initUnimplemented();
|
||||
this.connection.setNextIncomingMessage(msg);
|
||||
Assert.assertFalse(sent.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Reference in a new issue