mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-24 07:07:43 +01:00
Implemented SynchronizedProtoOutputStream
This commit is contained in:
parent
5fc7364973
commit
98eabf84fa
3 changed files with 56 additions and 5 deletions
|
@ -0,0 +1,7 @@
|
||||||
|
package io.bisq.network.p2p.network;
|
||||||
|
|
||||||
|
class BisqRuntimeException extends RuntimeException {
|
||||||
|
BisqRuntimeException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,23 +1,40 @@
|
||||||
package io.bisq.network.p2p.network;
|
package io.bisq.network.p2p.network;
|
||||||
|
|
||||||
import io.bisq.common.proto.network.NetworkEnvelope;
|
import io.bisq.common.proto.network.NetworkEnvelope;
|
||||||
|
import io.bisq.generated.protobuffer.PB;
|
||||||
import io.bisq.network.p2p.peers.keepalive.messages.KeepAliveMessage;
|
import io.bisq.network.p2p.peers.keepalive.messages.KeepAliveMessage;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.NotThreadSafe;
|
||||||
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
@NotThreadSafe
|
||||||
class ProtoOutputStream {
|
class ProtoOutputStream {
|
||||||
private final OutputStream deleage;
|
private static final Logger log = LoggerFactory.getLogger(ProtoOutputStream.class);
|
||||||
|
|
||||||
|
private final OutputStream delegate;
|
||||||
private final Statistic statistic;
|
private final Statistic statistic;
|
||||||
|
|
||||||
ProtoOutputStream(OutputStream deleage, Statistic statistic) {
|
ProtoOutputStream(OutputStream delegate, Statistic statistic) {
|
||||||
this.deleage = deleage;
|
this.delegate = delegate;
|
||||||
this.statistic = statistic;
|
this.statistic = statistic;
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeEnvelope(NetworkEnvelope envelope) {
|
void writeEnvelope(NetworkEnvelope envelope) {
|
||||||
|
try {
|
||||||
|
writeEnvelopeOrThrow(envelope);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Failed to write envelope", e);
|
||||||
|
throw new BisqRuntimeException("Failed to write envelope", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void writeEnvelopeOrThrow(NetworkEnvelope envelope) throws IOException {
|
||||||
PB.NetworkEnvelope proto = envelope.toProtoNetworkEnvelope();
|
PB.NetworkEnvelope proto = envelope.toProtoNetworkEnvelope();
|
||||||
proto.writeDelimitedTo(deleage);
|
proto.writeDelimitedTo(delegate);
|
||||||
deleage.flush();
|
delegate.flush();
|
||||||
|
|
||||||
statistic.addSentBytes(proto.getSerializedSize());
|
statistic.addSentBytes(proto.getSerializedSize());
|
||||||
statistic.addSentMessage(envelope);
|
statistic.addSentMessage(envelope);
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package io.bisq.network.p2p.network;
|
||||||
|
|
||||||
|
import io.bisq.common.proto.network.NetworkEnvelope;
|
||||||
|
|
||||||
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
@ThreadSafe
|
||||||
|
class SynchronizedProtoOutputStream extends ProtoOutputStream {
|
||||||
|
private final ExecutorService executorService;
|
||||||
|
|
||||||
|
SynchronizedProtoOutputStream(OutputStream delegate, Statistic statistic) {
|
||||||
|
super(delegate, statistic);
|
||||||
|
this.executorService = Executors.newSingleThreadExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void writeEnvelope(NetworkEnvelope envelope) {
|
||||||
|
executorService.submit(() -> super.writeEnvelope(envelope));
|
||||||
|
}
|
||||||
|
|
||||||
|
void onConnectionShutdown() {
|
||||||
|
executorService.shutdownNow();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue