mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 13:19:52 +01:00
Check for space before writing packets to simplex connections.
This commit is contained in:
@@ -58,8 +58,6 @@ class OutgoingSimplexConnection {
|
|||||||
transportId = ctx.getTransportId();
|
transportId = ctx.getTransportId();
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Write each packet to a buffer, check for capacity before writing
|
|
||||||
// it to the connection (except raw messages, which are already serialised)
|
|
||||||
void write() {
|
void write() {
|
||||||
connRegistry.registerConnection(contactId, transportId);
|
connRegistry.registerConnection(contactId, transportId);
|
||||||
try {
|
try {
|
||||||
@@ -72,26 +70,12 @@ class OutgoingSimplexConnection {
|
|||||||
PacketWriter writer = protoFactory.createPacketWriter(out,
|
PacketWriter writer = protoFactory.createPacketWriter(out,
|
||||||
transport.shouldFlush());
|
transport.shouldFlush());
|
||||||
// Send the initial packets: updates and acks
|
// Send the initial packets: updates and acks
|
||||||
Collection<TransportAck> transportAcks =
|
boolean hasSpace = writeTransportAcks(conn, writer);
|
||||||
db.generateTransportAcks(contactId);
|
if(hasSpace) hasSpace = writeTransportUpdates(conn, writer);
|
||||||
if(transportAcks != null) {
|
if(hasSpace) hasSpace = writeSubscriptionAck(conn, writer);
|
||||||
for(TransportAck ta : transportAcks)
|
if(hasSpace) hasSpace = writeSubscriptionUpdate(conn, writer);
|
||||||
writer.writeTransportAck(ta);
|
if(hasSpace) hasSpace = writeRetentionAck(conn, writer);
|
||||||
}
|
if(hasSpace) hasSpace = writeRetentionUpdate(conn, writer);
|
||||||
Collection<TransportUpdate> transportUpdates =
|
|
||||||
db.generateTransportUpdates(contactId);
|
|
||||||
if(transportUpdates != null) {
|
|
||||||
for(TransportUpdate tu : transportUpdates)
|
|
||||||
writer.writeTransportUpdate(tu);
|
|
||||||
}
|
|
||||||
SubscriptionAck sa = db.generateSubscriptionAck(contactId);
|
|
||||||
if(sa != null) writer.writeSubscriptionAck(sa);
|
|
||||||
SubscriptionUpdate su = db.generateSubscriptionUpdate(contactId);
|
|
||||||
if(su != null) writer.writeSubscriptionUpdate(su);
|
|
||||||
RetentionAck ra = db.generateRetentionAck(contactId);
|
|
||||||
if(ra != null) writer.writeRetentionAck(ra);
|
|
||||||
RetentionUpdate ru = db.generateRetentionUpdate(contactId);
|
|
||||||
if(ru != null) writer.writeRetentionUpdate(ru);
|
|
||||||
// Write acks until you can't write acks no more
|
// Write acks until you can't write acks no more
|
||||||
long capacity = conn.getRemainingCapacity();
|
long capacity = conn.getRemainingCapacity();
|
||||||
int maxMessages = writer.getMaxMessagesForAck(capacity);
|
int maxMessages = writer.getMaxMessagesForAck(capacity);
|
||||||
@@ -126,6 +110,67 @@ class OutgoingSimplexConnection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean writeTransportAcks(ConnectionWriter conn,
|
||||||
|
PacketWriter writer) throws DbException, IOException {
|
||||||
|
assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
Collection<TransportAck> acks = db.generateTransportAcks(contactId);
|
||||||
|
if(acks == null) return true;
|
||||||
|
for(TransportAck a : acks) {
|
||||||
|
writer.writeTransportAck(a);
|
||||||
|
if(conn.getRemainingCapacity() < MAX_PACKET_LENGTH) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean writeTransportUpdates(ConnectionWriter conn,
|
||||||
|
PacketWriter writer) throws DbException, IOException {
|
||||||
|
assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
Collection<TransportUpdate> updates =
|
||||||
|
db.generateTransportUpdates(contactId);
|
||||||
|
if(updates == null) return true;
|
||||||
|
for(TransportUpdate u : updates) {
|
||||||
|
writer.writeTransportUpdate(u);
|
||||||
|
if(conn.getRemainingCapacity() < MAX_PACKET_LENGTH) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean writeSubscriptionAck(ConnectionWriter conn,
|
||||||
|
PacketWriter writer) throws DbException, IOException {
|
||||||
|
assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
SubscriptionAck a = db.generateSubscriptionAck(contactId);
|
||||||
|
if(a == null) return true;
|
||||||
|
writer.writeSubscriptionAck(a);
|
||||||
|
return conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean writeSubscriptionUpdate(ConnectionWriter conn,
|
||||||
|
PacketWriter writer) throws DbException, IOException {
|
||||||
|
assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
SubscriptionUpdate u = db.generateSubscriptionUpdate(contactId);
|
||||||
|
if(u == null) return true;
|
||||||
|
writer.writeSubscriptionUpdate(u);
|
||||||
|
return conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean writeRetentionAck(ConnectionWriter conn,
|
||||||
|
PacketWriter writer) throws DbException, IOException {
|
||||||
|
assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
RetentionAck a = db.generateRetentionAck(contactId);
|
||||||
|
if(a == null) return true;
|
||||||
|
writer.writeRetentionAck(a);
|
||||||
|
return conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean writeRetentionUpdate(ConnectionWriter conn,
|
||||||
|
PacketWriter writer) throws DbException, IOException {
|
||||||
|
assert conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
RetentionUpdate u = db.generateRetentionUpdate(contactId);
|
||||||
|
if(u == null) return true;
|
||||||
|
writer.writeRetentionUpdate(u);
|
||||||
|
return conn.getRemainingCapacity() >= MAX_PACKET_LENGTH;
|
||||||
|
}
|
||||||
|
|
||||||
private void dispose(boolean exception) {
|
private void dispose(boolean exception) {
|
||||||
ByteUtils.erase(ctx.getSecret());
|
ByteUtils.erase(ctx.getSecret());
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user