Android開發進階之NIO非阻塞包(七)

來源:互聯網
上載者:User

今天我們繼續就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的架構。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.