mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +01:00
Safer not to create a new queue for each connection (race condition).
This commit is contained in:
@@ -32,12 +32,11 @@ class ModemImpl implements Modem, SerialPortEventListener {
|
|||||||
private final Callback callback;
|
private final Callback callback;
|
||||||
private final SerialPort port;
|
private final SerialPort port;
|
||||||
private final AtomicBoolean initialised, offHook, connected;
|
private final AtomicBoolean initialised, offHook, connected;
|
||||||
|
private final BlockingQueue<byte[]> received;
|
||||||
private final byte[] line;
|
private final byte[] line;
|
||||||
|
|
||||||
private int lineLen = 0;
|
private int lineLen = 0;
|
||||||
|
|
||||||
// A fresh queue is used for each connection
|
|
||||||
private volatile BlockingQueue<byte[]> received;
|
|
||||||
|
|
||||||
ModemImpl(Callback callback, String portName) {
|
ModemImpl(Callback callback, String portName) {
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
@@ -45,8 +44,8 @@ class ModemImpl implements Modem, SerialPortEventListener {
|
|||||||
initialised = new AtomicBoolean(false);
|
initialised = new AtomicBoolean(false);
|
||||||
offHook = new AtomicBoolean(false);
|
offHook = new AtomicBoolean(false);
|
||||||
connected = new AtomicBoolean(false);
|
connected = new AtomicBoolean(false);
|
||||||
line = new byte[MAX_LINE_LENGTH];
|
|
||||||
received = new LinkedBlockingQueue<byte[]>();
|
received = new LinkedBlockingQueue<byte[]>();
|
||||||
|
line = new byte[MAX_LINE_LENGTH];
|
||||||
}
|
}
|
||||||
|
|
||||||
public void init() throws IOException {
|
public void init() throws IOException {
|
||||||
@@ -121,7 +120,7 @@ class ModemImpl implements Modem, SerialPortEventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public InputStream getInputStream() {
|
public InputStream getInputStream() {
|
||||||
return new ModemInputStream(received);
|
return new ModemInputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
public OutputStream getOutputStream() {
|
public OutputStream getOutputStream() {
|
||||||
@@ -137,7 +136,6 @@ class ModemImpl implements Modem, SerialPortEventListener {
|
|||||||
throw new IOException(e.toString());
|
throw new IOException(e.toString());
|
||||||
}
|
}
|
||||||
received.add(new byte[0]); // Empty buffer indicates EOF
|
received.add(new byte[0]); // Empty buffer indicates EOF
|
||||||
received = new LinkedBlockingQueue<byte[]>();
|
|
||||||
connected.set(false);
|
connected.set(false);
|
||||||
offHook.set(false);
|
offHook.set(false);
|
||||||
}
|
}
|
||||||
@@ -236,15 +234,9 @@ class ModemImpl implements Modem, SerialPortEventListener {
|
|||||||
|
|
||||||
private class ModemInputStream extends InputStream {
|
private class ModemInputStream extends InputStream {
|
||||||
|
|
||||||
private final BlockingQueue<byte[]> received;
|
|
||||||
|
|
||||||
private byte[] buf = null;
|
private byte[] buf = null;
|
||||||
private int offset = 0;
|
private int offset = 0;
|
||||||
|
|
||||||
private ModemInputStream(BlockingQueue<byte[]> received) {
|
|
||||||
this.received = received;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
getBufferIfNecessary();
|
getBufferIfNecessary();
|
||||||
|
|||||||
Reference in New Issue
Block a user