Don't create empty packets.

This commit is contained in:
akwizgran
2011-09-21 13:30:04 +01:00
parent 3e60233ae0
commit 77d61e0aea
3 changed files with 25 additions and 20 deletions

View File

@@ -14,7 +14,7 @@ import net.sf.briar.api.serial.WriterFactory;
class AckWriterImpl implements AckWriter { class AckWriterImpl implements AckWriter {
private final OutputStream out; private final OutputStream out;
private final SerialComponent serial; private final int headerLength, idLength, footerLength;
private final Writer w; private final Writer w;
private boolean started = false; private boolean started = false;
@@ -23,17 +23,19 @@ class AckWriterImpl implements AckWriter {
AckWriterImpl(OutputStream out, SerialComponent serial, AckWriterImpl(OutputStream out, SerialComponent serial,
WriterFactory writerFactory) { WriterFactory writerFactory) {
this.out = out; this.out = out;
this.serial = serial; headerLength = serial.getSerialisedUserDefinedIdLength(Types.ACK)
+ serial.getSerialisedListStartLength();
idLength = serial.getSerialisedUniqueIdLength(Types.BATCH_ID);
footerLength = serial.getSerialisedListEndLength();
w = writerFactory.createWriter(out); w = writerFactory.createWriter(out);
} }
public boolean writeBatchId(BatchId b) throws IOException { public boolean writeBatchId(BatchId b) throws IOException {
int overhead = started ? footerLength : headerLength + footerLength;
if(capacity < idLength + overhead) return false;
if(!started) start(); if(!started) start();
int length = serial.getSerialisedUniqueIdLength(Types.BATCH_ID);
if(capacity < length + serial.getSerialisedListEndLength())
return false;
b.writeTo(w); b.writeTo(w);
capacity -= length; capacity -= idLength;
return true; return true;
} }
@@ -47,9 +49,8 @@ class AckWriterImpl implements AckWriter {
private void start() throws IOException { private void start() throws IOException {
w.writeUserDefinedTag(Types.ACK); w.writeUserDefinedTag(Types.ACK);
capacity -= serial.getSerialisedUserDefinedIdLength(Types.ACK);
w.writeListStart(); w.writeListStart();
capacity -= serial.getSerialisedListStartLength(); capacity -= headerLength;
started = true; started = true;
} }
} }

View File

@@ -16,7 +16,7 @@ import net.sf.briar.api.serial.WriterFactory;
class BatchWriterImpl implements BatchWriter { class BatchWriterImpl implements BatchWriter {
private final DigestOutputStream out; private final DigestOutputStream out;
private final SerialComponent serial; private final int headerLength, footerLength;
private final Writer w; private final Writer w;
private final MessageDigest messageDigest; private final MessageDigest messageDigest;
@@ -26,15 +26,17 @@ class BatchWriterImpl implements BatchWriter {
BatchWriterImpl(OutputStream out, SerialComponent serial, BatchWriterImpl(OutputStream out, SerialComponent serial,
WriterFactory writerFactory, MessageDigest messageDigest) { WriterFactory writerFactory, MessageDigest messageDigest) {
this.out = new DigestOutputStream(out, messageDigest); this.out = new DigestOutputStream(out, messageDigest);
this.serial = serial; headerLength = serial.getSerialisedUserDefinedIdLength(Types.BATCH)
+ serial.getSerialisedListStartLength();
footerLength = serial.getSerialisedListEndLength();
w = writerFactory.createWriter(this.out); w = writerFactory.createWriter(this.out);
this.messageDigest = messageDigest; this.messageDigest = messageDigest;
} }
public boolean writeMessage(byte[] message) throws IOException { public boolean writeMessage(byte[] message) throws IOException {
int overhead = started ? footerLength : headerLength + footerLength;
if(capacity < message.length + overhead) return false;
if(!started) start(); if(!started) start();
if(capacity < message.length + serial.getSerialisedListEndLength())
return false;
// Bypass the writer and write the raw message directly // Bypass the writer and write the raw message directly
out.write(message); out.write(message);
capacity -= message.length; capacity -= message.length;
@@ -53,9 +55,8 @@ class BatchWriterImpl implements BatchWriter {
private void start() throws IOException { private void start() throws IOException {
messageDigest.reset(); messageDigest.reset();
w.writeUserDefinedTag(Types.BATCH); w.writeUserDefinedTag(Types.BATCH);
capacity -= serial.getSerialisedUserDefinedIdLength(Types.BATCH);
w.writeListStart(); w.writeListStart();
capacity -= serial.getSerialisedListStartLength(); capacity -= headerLength;
started = true; started = true;
} }
} }

View File

@@ -14,7 +14,7 @@ import net.sf.briar.api.serial.WriterFactory;
class OfferWriterImpl implements OfferWriter { class OfferWriterImpl implements OfferWriter {
private final OutputStream out; private final OutputStream out;
private final SerialComponent serial; private final int headerLength, idLength, footerLength;
private final Writer w; private final Writer w;
private boolean started = false; private boolean started = false;
@@ -23,17 +23,19 @@ class OfferWriterImpl implements OfferWriter {
OfferWriterImpl(OutputStream out, SerialComponent serial, OfferWriterImpl(OutputStream out, SerialComponent serial,
WriterFactory writerFactory) { WriterFactory writerFactory) {
this.out = out; this.out = out;
this.serial = serial; headerLength = serial.getSerialisedUserDefinedIdLength(Types.OFFER)
+ serial.getSerialisedListStartLength();
idLength = serial.getSerialisedUniqueIdLength(Types.MESSAGE_ID);
footerLength = serial.getSerialisedListEndLength();
w = writerFactory.createWriter(out); w = writerFactory.createWriter(out);
} }
public boolean writeMessageId(MessageId m) throws IOException { public boolean writeMessageId(MessageId m) throws IOException {
int overhead = started ? footerLength : headerLength + footerLength;
if(capacity < idLength + overhead) return false;
if(!started) start(); if(!started) start();
int length = serial.getSerialisedUniqueIdLength(Types.MESSAGE_ID);
if(capacity < length + serial.getSerialisedListEndLength())
return false;
m.writeTo(w); m.writeTo(w);
capacity -= length; capacity -= idLength;
return true; return true;
} }
@@ -48,6 +50,7 @@ class OfferWriterImpl implements OfferWriter {
private void start() throws IOException { private void start() throws IOException {
w.writeUserDefinedTag(Types.OFFER); w.writeUserDefinedTag(Types.OFFER);
w.writeListStart(); w.writeListStart();
capacity -= headerLength;
started = true; started = true;
} }
} }