今天我們繼續就Android DDMS源碼一起分析NIO非阻塞通訊方式,Android123也會給大家分享下手機和PC互連中的一些技術。在NIO中有關SocketChannel和ByteBuffer的使用細節,可以在今天文章中
static void read(SocketChannel chan, byte[] data, int length, int timeout)
throws TimeoutException, IOException {
ByteBuffer buf = ByteBuffer.wrap(data, 0, length != -1 ? length : data.length); //從位元組數組中執行個體化ByteBuffer
int numWaits = 0;
while (buf.position() != buf.limit()) { //迴圈接收資料
int count;
count = chan.read(buf);
if (count < 0) {
throw new IOException("EOF"); //讀到末尾
} else if (count == 0) {
if (timeout != 0 && numWaits * WAIT_TIME > timeout) {
throw new TimeoutException();
}
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException ie) {
}
numWaits++;
} else {
numWaits = 0;
}
}
}
有關SocketChannel的寫操作,就是發送資料代碼如下:
static void write(SocketChannel chan, byte[] data, int length, int timeout)
throws TimeoutException, IOException {
ByteBuffer buf = ByteBuffer.wrap(data, 0, length != -1 ? length : data.length);
int numWaits = 0;
while (buf.position() != buf.limit()) {
int count;
count = chan.write(buf); //發送資料從ByteBuffer中
if (count < 0) {
throw new IOException("channel EOF");
} else if (count == 0) {
if (timeout != 0 && numWaits * WAIT_TIME > timeout) {
throw new TimeoutException();
}
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException ie) {
}
numWaits++;
} else {
numWaits = 0;
}
}
}
有關ADB如何選擇一個具體的裝置,可以使用 setDevice 方法,這樣當電腦中有模擬器或連結了多個手機,可以通過裝置序號,選擇需要通訊的裝置。
static void setDevice(SocketChannel adbChan, IDevice device)
throws TimeoutException, AdbCommandRejectedException, IOException {
// if the device is not -1, then we first tell adb we're looking to talk
// to a specific device
if (device != null) {
String msg = "host:transport:" + device.getSerialNumber(); // 最後的擷取序號,android123提示大家在adb命令中是adb get-serialno
byte[] device_query = formAdbRequest(msg);
write(adbChan, device_query);
AdbResponse resp = readAdbResponse(adbChan, false /* readDiagString */);
if (resp.okay == false) {
throw new AdbCommandRejectedException(resp.message,
true/*errorDuringDeviceSelection*/);
}
}
}
通過PC控制手機重啟的代碼,當然這裡需要Root許可權才能執行
public static void reboot(String into, InetSocketAddress adbSockAddr,
Device device) throws TimeoutException, AdbCommandRejectedException, IOException {
byte[] request;
if (into == null) {
request = formAdbRequest("reboot:"); //$NON-NLS-1$
} else {
request = formAdbRequest("reboot:" + into); //$NON-NLS-1$
}
SocketChannel adbChan = null;
try {
adbChan = SocketChannel.open(adbSockAddr);
adbChan.configureBlocking(false);
// if the device is not -1, then we first tell adb we're looking to talk
// to a specific device
setDevice(adbChan, device);
write(adbChan, request);
} finally {
if (adbChan != null) {
adbChan.close();
}
}
}
我們可以看到基本上,每個命令的執行,都是用了單獨SocketChannel通過非阻塞方式執行,這樣大大加強了並發,所以DDMS可以一邊處理Logcat列印,顯示堆資訊,處理檔案管理等等,有關NIO伺服器的內容,Android開發網將著重分析MonitorThread.java這個檔案,一起說下NIO的架構。