mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-17 05:09:53 +01:00
Don't try to make multiple Bluetooth connections on Motorola devices.
This commit is contained in:
@@ -33,6 +33,7 @@ abstract class DuplexSyncConnection extends SyncConnection
|
||||
|
||||
final Executor ioExecutor;
|
||||
final TransportId transportId;
|
||||
final DuplexTransportConnection connection;
|
||||
final TransportConnectionReader reader;
|
||||
final TransportConnectionWriter writer;
|
||||
final TransportProperties remote;
|
||||
@@ -80,6 +81,7 @@ abstract class DuplexSyncConnection extends SyncConnection
|
||||
transportPropertyManager);
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.transportId = transportId;
|
||||
this.connection = connection;
|
||||
reader = connection.getReader();
|
||||
writer = connection.getWriter();
|
||||
remote = connection.getRemoteProperties();
|
||||
@@ -96,6 +98,13 @@ abstract class DuplexSyncConnection extends SyncConnection
|
||||
disposeOnError(writer);
|
||||
}
|
||||
|
||||
void closeOutgoingStream(StreamContext ctx, TransportConnectionWriter w)
|
||||
throws IOException {
|
||||
StreamWriter streamWriter = streamWriterFactory.createStreamWriter(
|
||||
w.getOutputStream(), ctx);
|
||||
streamWriter.sendEndOfStream();
|
||||
}
|
||||
|
||||
SyncSession createDuplexOutgoingSession(StreamContext ctx,
|
||||
TransportConnectionWriter w, @Nullable Priority priority)
|
||||
throws IOException {
|
||||
|
||||
@@ -93,10 +93,16 @@ class IncomingDuplexSyncConnection extends DuplexSyncConnection
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// Create and run the outgoing session
|
||||
SyncSession out = createDuplexOutgoingSession(ctx, writer, null);
|
||||
setOutgoingSession(out);
|
||||
out.run();
|
||||
if (connection.isMarkedForClose()) {
|
||||
// Close the outgoing stream without sending anything
|
||||
closeOutgoingStream(ctx, writer);
|
||||
} else {
|
||||
// Create and run the outgoing session
|
||||
SyncSession out =
|
||||
createDuplexOutgoingSession(ctx, writer, null);
|
||||
setOutgoingSession(out);
|
||||
out.run();
|
||||
}
|
||||
writer.dispose(false);
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
|
||||
@@ -65,11 +65,16 @@ class OutgoingDuplexSyncConnection extends DuplexSyncConnection
|
||||
Priority priority = generatePriority();
|
||||
ioExecutor.execute(() -> runIncomingSession(priority));
|
||||
try {
|
||||
// Create and run the outgoing session
|
||||
SyncSession out =
|
||||
createDuplexOutgoingSession(ctx, writer, priority);
|
||||
setOutgoingSession(out);
|
||||
out.run();
|
||||
if (connection.isMarkedForClose()) {
|
||||
// Close the outgoing stream without sending anything
|
||||
closeOutgoingStream(ctx, writer);
|
||||
} else {
|
||||
// Create and run the outgoing session
|
||||
SyncSession out =
|
||||
createDuplexOutgoingSession(ctx, writer, priority);
|
||||
setOutgoingSession(out);
|
||||
out.run();
|
||||
}
|
||||
writer.dispose(false);
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
|
||||
@@ -23,7 +23,9 @@ interface BluetoothConnectionLimiter {
|
||||
boolean canOpenContactConnection();
|
||||
|
||||
/**
|
||||
* Informs the limiter that the given connection has been opened.
|
||||
* Informs the limiter that the given connection has been opened. If the
|
||||
* connection is above the limit it will be
|
||||
* {@link DuplexTransportConnection#markForClose() marked for close}.
|
||||
*/
|
||||
void connectionOpened(DuplexTransportConnection conn);
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
||||
getLogger(BluetoothConnectionLimiterImpl.class.getName());
|
||||
|
||||
private final EventBus eventBus;
|
||||
private final boolean singleConnection;
|
||||
|
||||
private final Object lock = new Object();
|
||||
@GuardedBy("lock")
|
||||
@@ -32,8 +33,10 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
||||
@GuardedBy("lock")
|
||||
private boolean keyAgreementInProgress = false;
|
||||
|
||||
BluetoothConnectionLimiterImpl(EventBus eventBus) {
|
||||
BluetoothConnectionLimiterImpl(EventBus eventBus,
|
||||
boolean singleConnection) {
|
||||
this.eventBus = eventBus;
|
||||
this.singleConnection = singleConnection;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -59,6 +62,9 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
||||
if (keyAgreementInProgress) {
|
||||
LOG.info("Can't open contact connection during key agreement");
|
||||
return false;
|
||||
} else if (singleConnection && !connections.isEmpty()) {
|
||||
LOG.info("Can't open contact connection due to limit");
|
||||
return false;
|
||||
} else {
|
||||
LOG.info("Can open contact connection");
|
||||
return true;
|
||||
@@ -68,12 +74,18 @@ class BluetoothConnectionLimiterImpl implements BluetoothConnectionLimiter {
|
||||
|
||||
@Override
|
||||
public void connectionOpened(DuplexTransportConnection conn) {
|
||||
boolean shouldClose = false;
|
||||
synchronized (lock) {
|
||||
connections.add(conn);
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Connection opened, " + connections.size() + " open");
|
||||
}
|
||||
if (singleConnection && connections.size() > 1) {
|
||||
LOG.info("Marking connection for close");
|
||||
shouldClose = true;
|
||||
}
|
||||
}
|
||||
if (shouldClose) conn.markForClose();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user