use of android.net.LocalSocket in project android_frameworks_base by AOSPA.
the class NetworkTest method testBindSocketOfLocalSocketThrows.
@SmallTest
public void testBindSocketOfLocalSocketThrows() throws Exception {
final LocalSocket mLocalClient = new LocalSocket();
mLocalClient.bind(new LocalSocketAddress("testClient"));
assertTrue(mLocalClient.getFileDescriptor().valid());
try {
mNetwork.bindSocket(mLocalClient.getFileDescriptor());
fail("SocketException not thrown");
} catch (SocketException expected) {
}
final LocalServerSocket mLocalServer = new LocalServerSocket("testServer");
mLocalClient.connect(mLocalServer.getLocalSocketAddress());
assertTrue(mLocalClient.isConnected());
try {
mNetwork.bindSocket(mLocalClient.getFileDescriptor());
fail("SocketException not thrown");
} catch (SocketException expected) {
}
}
use of android.net.LocalSocket in project android_frameworks_base by AOSPA.
the class BluetoothSocket method connect.
/**
* Attempt to connect to a remote device.
* <p>This method will block until a connection is made or the connection
* fails. If this method returns without an exception then this socket
* is now connected.
* <p>Creating new connections to
* remote Bluetooth devices should not be attempted while device discovery
* is in progress. Device discovery is a heavyweight procedure on the
* Bluetooth adapter and will significantly slow a device connection.
* Use {@link BluetoothAdapter#cancelDiscovery()} to cancel an ongoing
* discovery. Discovery is not managed by the Activity,
* but is run as a system service, so an application should always call
* {@link BluetoothAdapter#cancelDiscovery()} even if it
* did not directly request a discovery, just to be sure.
* <p>{@link #close} can be used to abort this call from another thread.
* @throws IOException on error, for example connection failure
*/
public void connect() throws IOException {
if (mDevice == null)
throw new IOException("Connect is called on null device");
try {
if (mSocketState == SocketState.CLOSED)
throw new IOException("socket closed");
IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null);
if (bluetoothProxy == null)
throw new IOException("Bluetooth is off");
mPfd = bluetoothProxy.connectSocket(mDevice, mType, mUuid, mPort, getSecurityFlags());
synchronized (this) {
if (DBG)
Log.d(TAG, "connect(), SocketState: " + mSocketState + ", mPfd: " + mPfd);
if (mSocketState == SocketState.CLOSED)
throw new IOException("socket closed");
if (mPfd == null)
throw new IOException("bt socket connect failed");
FileDescriptor fd = mPfd.getFileDescriptor();
mSocket = new LocalSocket(fd);
mSocketIS = mSocket.getInputStream();
mSocketOS = mSocket.getOutputStream();
}
int channel = readInt(mSocketIS);
if (channel <= 0)
throw new IOException("bt socket connect failed");
mPort = channel;
waitSocketSignal(mSocketIS);
synchronized (this) {
if (mSocketState == SocketState.CLOSED)
throw new IOException("bt socket closed");
mSocketState = SocketState.CONNECTED;
}
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
throw new IOException("unable to send RPC: " + e.getMessage());
}
}
use of android.net.LocalSocket in project android_frameworks_base by AOSPA.
the class BluetoothSocket method bindListen.
/**
* Currently returns unix errno instead of throwing IOException,
* so that BluetoothAdapter can check the error code for EADDRINUSE
*/
/*package*/
int bindListen() {
int ret;
if (mSocketState == SocketState.CLOSED)
return EBADFD;
IBluetooth bluetoothProxy = BluetoothAdapter.getDefaultAdapter().getBluetoothService(null);
if (bluetoothProxy == null) {
Log.e(TAG, "bindListen fail, reason: bluetooth is off");
return -1;
}
try {
mPfd = bluetoothProxy.createSocketChannel(mType, mServiceName, mUuid, mPort, getSecurityFlags());
} catch (RemoteException e) {
Log.e(TAG, Log.getStackTraceString(new Throwable()));
return -1;
}
// read out port number
try {
synchronized (this) {
if (DBG)
Log.d(TAG, "bindListen(), SocketState: " + mSocketState + ", mPfd: " + mPfd);
if (mSocketState != SocketState.INIT)
return EBADFD;
if (mPfd == null)
return -1;
FileDescriptor fd = mPfd.getFileDescriptor();
if (DBG)
Log.d(TAG, "bindListen(), new LocalSocket ");
mSocket = new LocalSocket(fd);
if (DBG)
Log.d(TAG, "bindListen(), new LocalSocket.getInputStream() ");
mSocketIS = mSocket.getInputStream();
mSocketOS = mSocket.getOutputStream();
}
if (DBG)
Log.d(TAG, "bindListen(), readInt mSocketIS: " + mSocketIS);
int channel = readInt(mSocketIS);
synchronized (this) {
if (mSocketState == SocketState.INIT)
mSocketState = SocketState.LISTENING;
}
if (DBG)
Log.d(TAG, "channel: " + channel);
if (mPort <= -1) {
mPort = channel;
}
// else ASSERT(mPort == channel)
ret = 0;
} catch (IOException e) {
if (mPfd != null) {
try {
mPfd.close();
} catch (IOException e1) {
Log.e(TAG, "bindListen, close mPfd: " + e1);
}
mPfd = null;
}
Log.e(TAG, "bindListen, fail to get port number, exception: " + e);
return -1;
}
return ret;
}
use of android.net.LocalSocket in project android_frameworks_base by ResurrectionRemix.
the class NativeDaemonConnector method listenToSocket.
private void listenToSocket() throws IOException {
LocalSocket socket = null;
try {
socket = new LocalSocket();
LocalSocketAddress address = determineSocketAddress();
socket.connect(address);
InputStream inputStream = socket.getInputStream();
synchronized (mDaemonLock) {
mOutputStream = socket.getOutputStream();
}
mCallbacks.onDaemonConnected();
FileDescriptor[] fdList = null;
byte[] buffer = new byte[BUFFER_SIZE];
int start = 0;
while (true) {
int count = inputStream.read(buffer, start, BUFFER_SIZE - start);
if (count < 0) {
loge("got " + count + " reading with start = " + start);
break;
}
fdList = socket.getAncillaryFileDescriptors();
// Add our starting point to the count and reset the start.
count += start;
start = 0;
for (int i = 0; i < count; i++) {
if (buffer[i] == 0) {
// Note - do not log this raw message since it may contain
// sensitive data
final String rawEvent = new String(buffer, start, i - start, StandardCharsets.UTF_8);
boolean releaseWl = false;
try {
final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent(rawEvent, fdList);
log("RCV <- {" + event + "}");
if (event.isClassUnsolicited()) {
// TODO: migrate to sending NativeDaemonEvent instances
if (mCallbacks.onCheckHoldWakeLock(event.getCode()) && mWakeLock != null) {
mWakeLock.acquire();
releaseWl = true;
}
Message msg = mCallbackHandler.obtainMessage(event.getCode(), uptimeMillisInt(), 0, event.getRawEvent());
if (mCallbackHandler.sendMessage(msg)) {
releaseWl = false;
}
} else {
mResponseQueue.add(event.getCmdNumber(), event);
}
} catch (IllegalArgumentException e) {
log("Problem parsing message " + e);
} finally {
if (releaseWl) {
mWakeLock.release();
}
}
start = i + 1;
}
}
if (start == 0) {
log("RCV incomplete");
}
// buffer and read again.
if (start != count) {
final int remaining = BUFFER_SIZE - start;
System.arraycopy(buffer, start, buffer, 0, remaining);
start = remaining;
} else {
start = 0;
}
}
} catch (IOException ex) {
loge("Communications error: " + ex);
throw ex;
} finally {
synchronized (mDaemonLock) {
if (mOutputStream != null) {
try {
loge("closing stream for " + mSocket);
mOutputStream.close();
} catch (IOException e) {
loge("Failed closing output stream: " + e);
}
mOutputStream = null;
}
}
try {
if (socket != null) {
socket.close();
}
} catch (IOException ex) {
loge("Failed closing socket: " + ex);
}
}
}
use of android.net.LocalSocket in project android_frameworks_base by ResurrectionRemix.
the class InstallerConnection method connect.
private boolean connect() {
if (mSocket != null) {
return true;
}
Slog.i(TAG, "connecting...");
try {
mSocket = new LocalSocket();
LocalSocketAddress address = new LocalSocketAddress("installd", LocalSocketAddress.Namespace.RESERVED);
mSocket.connect(address);
mIn = mSocket.getInputStream();
mOut = mSocket.getOutputStream();
} catch (IOException ex) {
disconnect();
return false;
}
return true;
}
Aggregations