Android开发进阶之NIO非阻塞包

 

Android开发进阶之NIO非阻塞包 这个系列转载于http://www.android123.com.cn/androidkaifa/695.html,特此说明

 

对于Android的网络通讯性能的提高,我们可以使用Java上高性能的NIO (New I/O) 技术进行处理,NIO是从JDK 1.4开始引入的,NIO的N我们可以理解为Noblocking即非阻塞的意思,相对应传统的I/O,比如Socket的accpet()、read()这些方法而言都是阻塞的。

  NIO主要使用了Channel和Selector来实现,Java的Selector类似Winsock的Select模式,是一种基于事件驱动的,整个处理方法使用了轮训的状态机,如果你过去开发过Symbian应用的话这种方式有点像活动对象,好处就是单线程更节省系统开销,NIO的好处可以很好的处理并发,对于Android网游开发来说比较关键,对于多点Socket连接而言使用NIO可以大大减少线程使用,降低了线程死锁的概率,毕竟手机游戏有UI线程,音乐线程,网络线程,管理的难度可想而知,同时I/O这种低速设备将影响游戏的体验。

  NIO作为一种中高负载的I/O模型,相对于传统的BIO (Blocking I/O)来说有了很大的提高,处理并发不用太多的线程,省去了创建销毁的时间,如果线程过多调度是问题,同时很多线程可能处于空闲状态,大大浪费了CPU时间,同时过多的线程可能是性能大幅下降,一般的解决方案中可能使用线程池来管理调度但这种方法治标不治本。使用NIO可以使并发的效率大大提高。当然NIO和JDK 7中的AIO还存在一些区别,AIO作为一种更新的当然这是对于Java而言,如果你开发过Winsock服务器,那么IOCP这样的I/O完成端口可以解决更高级的负载,当然了今天Android123主要给大家讲解下为什么使用NIO在Android中有哪些用处。

   NIO我们分为几个类型分别描述,作为Java的特性之一,我们需要了解一些新的概念,比如ByteBuffer类,Channel,SocketChannel,ServerSocketChannel,Selector和SelectionKey。有关具体的使用,Android开发网将在明天详细讲解。网友可以在Android SDK文档中看下java.nio和java.nio.channels两个包了解。

有关Android NIO我们主要分为三大类,ByteBufferFileChannelSocketChannel。由于篇幅原因今天Android123只对前两个做说明。NIO和传统的I/O比较大的区别在于传输方式非阻塞,一种基于事件驱动的模式,将会使方法执行完后立即返回,传统I/O主要使用了流Stream的方式,而在New I/O中,使用了字节缓存ByteBuffer来承载数据。

   ByteBuffer位于java.nio包中,目前提供了Java基本类型中除Boolean外其他类型的缓冲类型,比如ByteBufferDoubleBufferFloatBufferIntBufferLongBufferShortBuffer  。同时还提供了一种更特殊的映射字节缓冲类型MappedByteBuffer。在传统IO的输入输出流中,InputStream中只提供了字节型或字节数组的访问对应NIO就是ByteBuffer,但是处理传统的DataInputStreamint等类型,就是IntBuffer,但是缓冲类型并没有提供UTF这样的类型处理,所以我们仍然需要使用ByteBuffer处理字符串,但是NIO提供了一个封装的类在java.nio.charset包中,通过字符的编码CharsetEncoder和解码CharsetDecoder类来处理字符串,同时这些类可以方便转换编码比如GBKUTF等等。

  一、ByteBuffer

  1) 实例化

  直接使用ByteBuffer类的静态方法static ByteBuffer allocate(int capacity)  static ByteBuffer allocateDirect(int capacity)  这两个方法来分配内存空间,两种方法的区别主要是后者更适用于繁复分配的字节数组。而 put(ByteBuffer src) 可以从另一个ByteBuffer中构造,也可以通过wrap方法从byte[]中构造,具体参考下面的类型转化内容。

  2) 类型转化

   ByteBuffer可以很好的和字节数组byte[]转换类型,通过执行ByteBuffer类的final byte[]  array() 方法就可以将ByteBuffer转为byte[]。从byte[]来构造ByteBuffer可以使用wrap方法,目前Android或者说Java提供了两种重写方法,比如为static ByteBuffer  wrap(byte[] array)   static ByteBuffer  wrap(byte[] array, int start, int len)  ,第二个重载方法中第二个参数为从array这个字节数组的起初位置,第三个参数为array这个字节数组的长度。

  3) ByteBuffer中添加元素

  目前ByteBuffer提供了多种put重写类型来添加,比如put(byte b) putChar(char value) putFloat(float value) 等等,需要注意的是,按照Java的类型长度,一个byte1字节,一个char类型是2字节,一个floatint4字节,一个long则为8字节,和传统的C++有些区别。所以内部的相关位置也会发生变化,同时每种方法还提供了定位的方法比如ByteBuffer  put(int index, byte b) 

  4) ByteBuffer中获取元素

  同上面的添加想法,各种put被换成了get,比如byte  get()  float  getFloat()  ,当然了还提供了一种定位的方式,比如double  getDouble(int index) 

  5) ByteBuffer中字节顺序

  对于Java来说默认使用了BIG_ENDIAN方式存储,和C正好相反的,通过

  final ByteOrder  order() 返回当前的字节顺序。

  final ByteBuffer  order(ByteOrder byteOrder)  设置字节顺序,ByteOrder类的值有两个定义,比如LITTLE_ENDIANBIG_ENDIAN,如果使用当前平台则为ByteOrder.nativeOrder()Android中则为 BIG_ENDIAN,当然如果设置为order(null) 则使用LITTLE_ENDIAN

  二、FileChannel

   NIO中除了Socket外,还提供了File设备的通道类,FileChannel位于java.nio.channels.FileChannel包中,在Android SDK文档中我们可以方便的找到,对于文件复制我们可以使用ByteBuffer方式作为缓冲,比如

  String infile = "/sdcard/cwj.dat";
  String outfile = "/sdcard/android123-test.dat";

    FileInputStream fin = new FileInputStream( infile );
    FileOutputStream fout = new FileOutputStream( outfile );

    FileChannel fcin = fin.getChannel();
    FileChannel fcout = fout.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate( 1024 ); //分配1KB作为缓冲区

    while (true) {
    buffer.clear(); //
每次使用必须置空缓冲区

      int r = fcin.read( buffer );

      if (r==-1) {
        break;
      }

   buffer.flip(); //写入前使用flip这个方法

      fcout.write( buffer );
    }

   flipclear这两个方法是java.nio.Buffer包中,ByteBuffer的父类是从Buffer类继承而来的,这点Android123要提醒大家看Android SDK文档时注意Inherited Methods,而JDK的文档就比较直接了,同时复制文件使用FileChanneltransferTo(long position, long count, WritableByteChannel target) 这个方法可以快速的复制文件,无需自己管理ByteBuffer缓冲区。明天Android开发网介绍NIO主要的Socket相关的内容。


有关Android NIO的精髓主要用于高负载的Socket网络传输,相对于传统I/O模型的Socket传输方式的优势,我们已经在 Android开发进阶之NIO非阻塞包(一) 中讲到了,这里不再赘述,一起来看看Android NIO有关Socket操作提供的类吧:

 

  一、ServerSocketChannel 服务器套接字通道在Android SDK中查找package名为  java.nio.channels.ServerSocketChannel

 

   在Java的NIO中,ServerSocketChannel对应的是传统IO中的ServerSocket,通过ServerSocketChannel类的socket() 方法可以获得一个传统的ServerSocket对象,同时从ServerSocket对象的getChannel() 方法,可以获得一个ServerSocketChannel()对象,这点说明NIO的ServerSocketChannel和传统IO的ServerSocket是有关联的,实例化ServerSocketChannel 只需要直接调用ServerSocketChannel 类的静态方法open()即可。

 

  二、 SocketChannel 套接字通道 java.nio.channels.SocketChannel   

 

  在Java的New I/O中,处理Socket类对应的东西,我们可以看做是SocketChannel,套接字通道关联了一个Socket类,这一点使用SocketChannel类的socket() 方法可以返回一个传统IO的Socket类。SocketChannel()对象在Server中一般通过Socket类的getChannel()方法获得。

 

 三、SelectionKey 选择键 java.nio.channels.SelectionKey

 

  在NIO中SelectionKey和Selector是最关键的地方,SelectionKey类中描述了NIO中比较重要的事件,比如OP_ACCEPT(用于服务器端)、OP_CONNECT(用于客户端)、OP_READ和OP_WRITE。

 

 四、Selector 选择器 java.nio.channels.Selector

 

  在NIO中注册各种事件的方法主要使用Selector来实现的,构造一个Selector对象,使用Selector类的静态方法open()来实例化。

 

  对于Android平台上我们实现一个非阻塞的服务器,过程如下:

 

   1. 通过Selector类的open()静态方法实例化一个Selector对象。

 

   2. 通过ServerSocketChannel类的open()静态方法实例化一个ServerSocketChannel对象。

 

   3. 显示的调用ServerSocketChannel对象的configureBlocking(false);方法,设置为非阻塞模式,Android123提示网友这一步十分重要。

 

   4. 使用ServerSocketChannel对象的socket()方法返回一个ServerSocket对象,使用ServerSocket对象的bind()方法绑定一个IP地址和端口号

 

   5. 调用ServerSocketChannel对象的register方法注册感兴趣的网络事件,很多开发者可能发现Android SDK文档中没有看到register方法,这里Android开发网给大家一个ServerSocketChannel类的继承关系  

 

java.lang.Object

   ↳ java.nio.channels.spi.AbstractInterruptibleChannel

     ↳ java.nio.channels.SelectableChannel

       ↳ java.nio.channels.spi.AbstractSelectableChannel

         ↳ java.nio.channels.ServerSocketChannel

 

 

   这里我们使用的register方法其实来自ServerSocketChannel的父类java.nio.channels.SelectableChannel,该方法原型为 final SelectionKey  register(Selector selector, int operations)  ,参数为我们执行第1步时的selector对象,参数二为需要注册的事件,作为服务器,我们当然是接受客户端发来的请求,所以这里使用SelectionKey.OP_ACCEPT了。

 

  6. 通过Selector对象的select() 方法判断是否有我们感兴趣的事件发生,这里就是OP_ACCEPT事件了。我们通过一个死循环获取Selector对象执行select()方法的值,SDK中的原始描述为the number of channels that are ready for operation.,就是到底有多少个通道返回。

 

  7. 如果 Selector对象的select()方法返回的结果数大于0,则通过selector对象的selectedKeys()方法获取一个SelectionKey类型的Set集合,我们使用Java的迭代器Iterator类来遍历这个Set集合,注意判断SelectionKey对象,

 

  8. 为了表示我们处理了SelectionKey对象,需要先移除这个SelectionKey对象从Set集合中。这句很关键Android 123提醒网友注意这个地方。

 

  9. 接下来判断SelectionKey对象的事件,因为我们注册的感兴趣的是SelectionKey.OP_ACCEPT事件,我们使用SelectionKey对象的isAcceptable()方法判断,如果是我们创建一个临时SocketChannel对象类似上面的方法继续处理,不过这时这个SocketChannel对象主要处理读写操作,我们注册SelectionKey.OP_READ和SelectionKey.OP_WRITE分配ByteBuffer缓冲区,进行网络数据传输。

 

  有关具体的示例和解释上面的流畅,由于篇幅原因我们明天Android开发网给出源代码,做详细的分析。


今天我们通过一个实例详细讲解下Android下NIO非阻塞服务器的开发,对于客户端而言Android123不推荐使用NIO,毕竟NIO相对于传统IO较为复杂,最重要的NIO是为了解决多线程并发问题而解决的技术,可能会因为管理和复杂性降低最终的结果,毕竟NIO是Java的,相关的类型比较难控制,对于客户端而言我们可以使用C++、Java、C#甚至Flash Action Script来编写。

 

    下面我们以一个简单的Echo Server为例子来分析

 

 import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.CharBuffer;

import java.nio.channels.SelectionKey;

import java.nio.channels.Selector;

import java.nio.channels.ServerSocketChannel;

import java.nio.channels.SocketChannel;

import java.nio.charset.Charset;

import java.nio.charset.CharsetDecoder;

import java.nio.charset.CharsetEncoder;

import java.util.Iterator;

 

public class Server {

 

 public static void main(String[] args) {

  Selector selector = null;

  ServerSocketChannel ssc = null;

  try {

      selector = Selector.open(); //实例化selector

      ssc = ServerSocketChannel.open(); //实例化ServerSocketChannel 对象

 

      ssc.socket().bind(new InetSocketAddress(1987)); //绑定端口为1987

 

      ssc.configureBlocking(false); //设置为非阻塞模式

      ssc.register(selector, SelectionKey.OP_ACCEPT); //注册关心的事件,对于Server来说主要是accpet了

 

 

   while (true) {

   int n= selector.select(); //获取感兴趣的selector数量

   if(n<1)

          continue; //如果没有则一直轮训检查

    Iterator<SelectionKey> it = selector.selectedKeys().iterator(); //有新的链接,我们返回一个SelectionKey集合

    while (it.hasNext()) {

     SelectionKey key = it.next(); //使用迭代器遍历

     it.remove(); //删除迭代器

 

     if (key.isAcceptable()) { //如果是我们注册的OP_ACCEPT事件

      ServerSocketChannel ssc2 = (ServerSocketChannel) key.channel();

      SocketChannel channel = ssc2.accept();

      channel.configureBlocking(false); //同样是非阻塞

      channel.register(selector, SelectionKey.OP_READ); //本次注册的是read事件,即receive接受

 

      System.out.println("CWJ Client :" + channel.socket().getInetAddress().getHostName() + ":"  + channel.socket().getPort());

     }

 

    else if (key.isReadable()) { //如果为读事件

 

      SocketChannel channel = (SocketChannel) key.channel();

 

      ByteBuffer buffer = ByteBuffer.allocate(1024); //1KB的缓冲区

      channel.read(buffer); //读取到缓冲区

      buffer.flip(); //准备写入

      System.out.println("android123 receive info:" + buffer.toString());

 

      channel.write(CharBuffer.wrap("it works".getBytes())); //返回给客户端

     }

    }

   }

  } catch (IOException e) {

   e.printStackTrace();

  } finally {

   try {

    selector.close();

    server.close();

   } catch (IOException e) {

   }

  }

 }

}

 

 上面是比较简单的框架,里面存在很多问题,Android123将在下次详细阐述下,上面或者说国内有关NIO资料中的通病,如果你看过Mina或GlassFish的源码,你可能就知道上面的问题大于10种,有关框架的bug占了大多数,作为服务器而言很容易CPU超过100%



有关Android NIO的注意点和重点今天Android123着重分析下上次 Android开发进阶之NIO非阻塞包() 一文中提到的不足地方改进。由于目前国内很多人资料书籍编写人员没有通过NIO实现服务器的经验,导致了很多例子中存在严重的错误,由于大多数例子为Echo这样的单次交互以及数据量较小所以反映不出问题的所在。

   1. 读和写应该分开,NIO使用的是异步的方法但不等于说不会阻塞,在上面的例子中我们可以看到 判断  key.isReadable() 时,对于这个SelectionKey关联的SocketChannel尽量不要使用写入数据量过多时ByteBuffer使用hasRemaining这样的方法,NIO每次读写不一定全部要把数据读完在一次Selector时。

  2. 对于上面的解决方法我们可以继续关注感兴趣的事件,比如说使用interestOps方法,而很多资料中均使用了错误的继续用Selectorregister方法继续注册事件,这样没有发生错误的原因是每次注册时会替换上次的这个key注册的事件,比较侥幸,从效率上讲还会判断这个key上次注册的是什么事件,并不是一种正统的方法。

  3. 我们可以继续判断写入事件,比如key.isWritable,在写入时来处理发送数据。这样可以应对很多低速网络时产生的异常。

  有关的细节还有很多,下一次Android开发网总结出常见的问题,并给大家一个较科学完善的框架,减少不必要的麻烦产生。


有关Android NIO的相关内容,本次Android123整理并归类如下,为了让大家感觉NIOAndroid平台联系的紧密,这里我们结合ADT插件的重要开发工具DDMS中的源码进行分析。在android git中的sdk.git文件中,可以找到ddmlib这个文件夹。有关PC和手机的互通内核在这里使用了Java来完全实现。这里Android开发网一起帮助大家了解下PC同步软件的开发原理同时学习下Java中的New I/O技术。

   比较重要的代码段我们贴出,逐一分析,其他的网友可以直接预读源码:

   AdbHelper.java文件中

   public static SocketChannel open(InetSocketAddress adbSockAddr,
            Device device, int devicePort) //
这是一个重载版本,主要是关联Device实例。
            throws IOException, TimeoutException, AdbCommandRejectedException {

        SocketChannel adbChan = SocketChannel.open(adbSockAddr); //构造SocketChannel对象,使用常规的open方法创建
        try {
            adbChan.socket().setTcpNoDelay(true); //
设置TCP非延迟
            adbChan.configureBlocking(false); //
非阻塞

            setDevice(adbChan, device); //本句和NIO没有多大关系,这句是指定具体的设备,比如模拟器,或Android手机的厂家代号,比如宏达电的以HTXXXXX这样的方式

            byte[] req = createAdbForwardRequest(null, devicePort); //设置端口转发,这句很关键,否则PC和手机通过USB是无法互通的。
            write(adbChan, req); //
发送数据

            AdbResponse resp = readAdbResponse(adbChan, false); //读取收到的内容
            if (resp.okay == false) {
                throw new AdbCommandRejectedException(resp.message);
            }

            adbChan.configureBlocking(true);
        } catch (TimeoutException e) { //
一般要处理超时异常
            adbChan.close(); //
释放channel句柄
            throw e;
        } catch (IOException e) { //
处理常规的IO异常
            adbChan.close();
            throw e;
        }

        return adbChan;
    }

   有关读取ADB返回的报文方法

  static AdbResponse readAdbResponse(SocketChannel chan, boolean readDiagString)
            throws TimeoutException, IOException {

        AdbResponse resp = new AdbResponse();

        byte[] reply = new byte[4]; //创建4字节数组,主要检测成功与否,adb的协议是成功返回 okay,失败fail,等等。
        read(chan, reply); //
读取具体的返回

        if (isOkay(reply)) { //判断是否成功
            resp.okay = true;
        } else {
            readDiagString = true; // look for a reason after the FAIL
            resp.okay = false;
        }

        // not a loop -- use "while" so we can use "break"
        try {
            while (readDiagString) {
                // length string is in next 4 bytes
                byte[] lenBuf = new byte[4];
                read(chan, lenBuf); //
读取一个字节数组,最终为了转为一个整形

                String lenStr = replyToString(lenBuf); //字节数组转为String

                int len;
                try {
                    len = Integer.parseInt(lenStr, 16); //String
转为整形,这里Android123提示,这种写法可能比较愚蠢,但是下面为Log输出提供了一点点的便利。
                } catch (NumberFormatException nfe) {
                    Log.w("ddms", "Expected digits, got '" + lenStr + "': "
                            + lenBuf[0] + " " + lenBuf[1] + " " + lenBuf[2] + " "
                            + lenBuf[3]);
                    Log.w("ddms", "reply was " + replyToString(reply));
                    break;
                }

                byte[] msg = new byte[len];
                read(chan, msg);

                resp.message = replyToString(msg);
                Log.v("ddms", "Got reply '" + replyToString(reply) + "', diag='"
                        + resp.message + "'");

                break;
            }
        } catch (Exception e) {
            // ignore those, since it's just reading the diagnose string, the response will
            // contain okay==false anyway.
        }

        return resp;
    }

   有关PC上对Android手机屏幕截图的方法之一:

   static RawImage getFrameBuffer(InetSocketAddress adbSockAddr, Device device)
            throws TimeoutException, AdbCommandRejectedException, IOException {

        RawImage imageParams = new RawImage();
        byte[] request = formAdbRequest("framebuffer:"); // 
读取手机端adbd服务器的framebuffer调用返回的数组

        byte[] nudge = {
            0
        };
        byte[] reply;

        SocketChannel adbChan = null;
        try {
            adbChan = SocketChannel.open(adbSockAddr);
            adbChan.configureBlocking(false); //
非阻塞

            setDevice(adbChan, device); //设置我们关系的设备

            write(adbChan, request); //发送framebuffer这个请求了

            AdbResponse resp = readAdbResponse(adbChan, false /* readDiagString */);
            if (resp.okay == false) {   //
判断返回是否ok
                throw new AdbCommandRejectedException(resp.message);
            }

            reply = new byte[4];
            read(adbChan, reply); //
首先返回的是一个协议,目前分为两个版本,主要是兼容模式和标准的模式,兼容模式比较少见,在2.0以后几乎看不到了。部分早期的1.6或更老的T-Mobile G1会使用兼容模式,模式不同,输出的截图中的颜色编码方式略有不同。

            ByteBuffer buf = ByteBuffer.wrap(reply);
            buf.order(ByteOrder.LITTLE_ENDIAN); //
小头字节顺序

            int version = buf.getInt(); //ByteBuffer直接转int的方法,比较方便不用自己从字节数组中构造,按位计算

            int headerSize = RawImage.getHeaderSize(version); //根据返回的adb截图协议版本判断将收到的字节大小

            reply = new byte[headerSize * 4]; //分配空间,具体大小需要看协议版本
            read(adbChan, reply);

            buf = ByteBuffer.wrap(reply); //reply数组实例化ByteBuffer
            buf.order(ByteOrder.LITTLE_ENDIAN); //
注意字节序列,毕竟远端的adbd是工作在linux系统的手机上。

            if (imageParams.readHeader(version, buf) == false) { //判断是否有效,兼容这种截图协议。
                Log.e("Screenshot", "Unsupported protocol: " + version);
                return null;
            }

            Log.d("ddms", "image params: bpp=" + imageParams.bpp + ", size="
                    + imageParams.size + ", width=" + imageParams.width
                    + ", height=" + imageParams.height); //
打印下截图的基本信息,比如bpp代表色深,size是需要分配dib图像的字节数组。比较原始,

            write(adbChan, nudge); //发送一个字节,代表准备接收字节数组了

            reply = new byte[imageParams.size]; //分配和图像大小一样的字节数组
            read(adbChan, reply); //
接收图像字节数组,这里Android开发网提示大家对于Android 1.x可能为RGB565,分配大小为 wxhx2xsize ,而2.x以后基本上为32位的RGB8888,分配大小为wxhx4xsize

            imageParams.data = reply;
        } finally {
            if (adbChan != null) {
                adbChan.close();
            }
        }

        return imageParams;
    }

  有关Android平台PC通过USBADB方式和手机同步原理和NIO相关技术,Android123明天继续讲解。


今天我们继续就Android DDMS源码一起分析NIO非阻塞通讯方式,Android123也会给大家分享下手机和PC互通中的一些技术。在NIO中有关SocketChannelByteBuffer的使用细节,可以在今天文章中

   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的框架。


在整个DDMS中体现Android NIO主要框架的要数MonitorThread.java这个文件了,有关PCAndroid手机同步以及NIO非阻塞编程的精髓可以在下面的文件中充分体现出来。

  final class MonitorThread extends Thread {

    private static final int CLIENT_READY = 2;

    private static final int CLIENT_DISCONNECTED = 3;

    private volatile boolean mQuit = false;

    private ArrayList<Client> mClientList; //用一个数组保存客户端信息

    private Selector mSelector;

    private HashMap<Integer, ChunkHandler> mHandlerMap; //这里Android123提示大家,由于在多线程中concurrentHashMap效率比HashMap更安全高效,推荐使用并发库的这个替代版本。

    private ServerSocketChannel mDebugSelectedChan; //一个用于调试的服务器通道

    private int mNewDebugSelectedPort;

    private int mDebugSelectedPort = -1;

    private Client mSelectedClient = null;

    private static MonitorThread mInstance;

    private MonitorThread() {
        super("Monitor");
        mClientList = new ArrayList<Client>();
        mHandlerMap = new HashMap<Integer, ChunkHandler>();

        mNewDebugSelectedPort = DdmPreferences.getSelectedDebugPort();
    }

    static MonitorThread createInstance() {  //创建实例
        return mInstance = new MonitorThread();
    }

    static MonitorThread getInstance() { //获取实例
        return mInstance;
    }

    synchronized void setDebugSelectedPort(int port) throws IllegalStateException { //设置调试端口号
        if (mInstance == null) {
            return;
        }

        if (AndroidDebugBridge.getClientSupport() == false) {
            return;
        }

        if (mDebugSelectedChan != null) {
            Log.d("ddms", "Changing debug-selected port to " + port);
            mNewDebugSelectedPort = port;
            wakeup(); //
这里用来唤醒所有的Selector
        } else {
            // we set mNewDebugSelectedPort instead of mDebugSelectedPort so that it's automatically
            mNewDebugSelectedPort = port;
        }
    }

    synchronized void setSelectedClient(Client selectedClient) {
        if (mInstance == null) {
            return;
        }

        if (mSelectedClient != selectedClient) {
            Client oldClient = mSelectedClient;
            mSelectedClient = selectedClient;

            if (oldClient != null) {
                oldClient.update(Client.CHANGE_PORT);
            }

            if (mSelectedClient != null) {
                mSelectedClient.update(Client.CHANGE_PORT);
            }
        }
    }

    Client getSelectedClient() {
        return mSelectedClient;
    }

    boolean getRetryOnBadHandshake() {
        return true; // TODO? make configurable
    }

    Client[] getClients() {
        synchronized (mClientList) {
            return mClientList.toArray(new Client[0]);
        }
    }

    synchronized void registerChunkHandler(int type, ChunkHandler handler) {
        if (mInstance == null) {
            return;
        }

        synchronized (mHandlerMap) {
            if (mHandlerMap.get(type) == null) {
                mHandlerMap.put(type, handler);
            }
        }
    }

    @Override
    public void run() { //
本类的主要线程
        Log.d("ddms", "Monitor is up");

        try {
            mSelector = Selector.open();
        } catch (IOException ioe) {
            Log.logAndDisplay(LogLevel.ERROR, "ddms",
                    "Failed to initialize Monitor Thread: " + ioe.getMessage());
            return;
        }

        while (!mQuit) {

            try {
                synchronized (mClientList) {
                }

                try {
                    if (AndroidDebugBridge.getClientSupport()) {
                        if ((mDebugSelectedChan == null ||
                                mNewDebugSelectedPort != mDebugSelectedPort) &&
                                mNewDebugSelectedPort != -1) {
                            if (reopenDebugSelectedPort()) {
                                mDebugSelectedPort = mNewDebugSelectedPort;
                            }
                        }
                    }
                } catch (IOException ioe) {
                    Log.e("ddms",
                            "Failed to reopen debug port for Selected Client to: " + mNewDebugSelectedPort);
                    Log.e("ddms", ioe);
                    mNewDebugSelectedPort = mDebugSelectedPort; // no retry
                }

                int count;
                try {
                    count = mSelector.select();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                    continue;
                } catch (CancelledKeyException cke) {
                    continue;
                }

                if (count == 0) {
                    continue;
                } //
这里代码写的不是很好,Android开发网提示大家因为这个NIODDMS工作在PC端的还不明显,这样轮训的在一个while中,效率不是很高,CPU很容易占用率很高。

                Set<SelectionKey> keys = mSelector.selectedKeys();
                Iterator<SelectionKey> iter = keys.iterator(); //
使用迭代器获取这个选择键

                while (iter.hasNext()) {
                    SelectionKey key = iter.next();
                    iter.remove();

                    try {
                        if (key.attachment() instanceof Client) { //
判断收到的key的附件是否是Client的实例
                            processClientActivity(key);
                        }
                        else if (key.attachment() instanceof Debugger) { //
如果是Debug实例
                            processDebuggerActivity(key);
                        }
                        else if (key.attachment() instanceof MonitorThread) {
                            processDebugSelectedActivity(key);
                        }
                        else {
                            Log.e("ddms", "unknown activity key");
                        }
                    } catch (Exception e) {
                        Log.e("ddms", "Exception during activity from Selector.");
                        Log.e("ddms", e);
                    }
                }
            } catch (Exception e) {
                Log.e("ddms", "Exception MonitorThread.run()");
                Log.e("ddms", e);
            }
        }
    }

    int getDebugSelectedPort() {
        return mDebugSelectedPort;
    }

    private void processClientActivity(SelectionKey key) {
        Client client = (Client)key.attachment();

        try {
            if (key.isReadable() == false || key.isValid() == false) {
                Log.d("ddms", "Invalid key from " + client + ". Dropping client.");
                dropClient(client, true /* notify */);
                return;
            }

            client.read();

            JdwpPacket packet = client.getJdwpPacket();
            while (packet != null) {
                if (packet.isDdmPacket()) {
                    // unsolicited DDM request - hand it off
                    assert !packet.isReply();
                    callHandler(client, packet, null);
                    packet.consume();
                } else if (packet.isReply()
                        && client.isResponseToUs(packet.getId()) != null) {
                    // reply to earlier DDM request
                    ChunkHandler handler = client
                            .isResponseToUs(packet.getId());
                    if (packet.isError())
                        client.packetFailed(packet);
                    else if (packet.isEmpty())
                        Log.d("ddms", "Got empty reply for 0x"
                                + Integer.toHexString(packet.getId())
                                + " from " + client);
                    else
                        callHandler(client, packet, handler);
                    packet.consume();
                    client.removeRequestId(packet.getId());
                } else {
                    Log.v("ddms", "Forwarding client "
                            + (packet.isReply() ? "reply" : "event") + " 0x"
                            + Integer.toHexString(packet.getId()) + " to "
                            + client.getDebugger());
                    client.forwardPacketToDebugger(packet);
                }

                packet = client.getJdwpPacket();
            }
        } catch (CancelledKeyException e) { //
注意正确处理这个异常
            dropClient(client, true /* notify */);
        } catch (IOException ex) {
            dropClient(client, true /* notify */);
        } catch (Exception ex) {
            Log.e("ddms", ex);

            dropClient(client, true /* notify */);

            if (ex instanceof BufferOverflowException) { //可能存在缓冲区异常
                Log.w("ddms",
                        "Client data packet exceeded maximum buffer size "
                                + client);
            } else {
                // don't know what this is, display it
                Log.e("ddms", ex);
            }
        }
    }

    private void callHandler(Client client, JdwpPacket packet,
            ChunkHandler handler) {

        // on first DDM packet received, broadcast a "ready" message
        if (!client.ddmSeen())
            broadcast(CLIENT_READY, client);

        ByteBuffer buf = packet.getPayload();
        int type, length;
        boolean reply = true;

        type = buf.getInt();
        length = buf.getInt();

        if (handler == null) {
            // not a reply, figure out who wants it
            synchronized (mHandlerMap) {
                handler = mHandlerMap.get(type);
                reply = false;
            }
        }

        if (handler == null) {
            Log.w("ddms", "Received unsupported chunk type "
                    + ChunkHandler.name(type) + " (len=" + length + ")");
        } else {
            Log.d("ddms", "Calling handler for " + ChunkHandler.name(type)
                    + " [" + handler + "] (len=" + length + ")");
            ByteBuffer ibuf = buf.slice();
            ByteBuffer roBuf = ibuf.asReadOnlyBuffer(); // enforce R/O
            roBuf.order(ChunkHandler.CHUNK_ORDER);
            synchronized (mClientList) {
                handler.handleChunk(client, type, roBuf, reply, packet.getId());
            }
        }
    }

    synchronized void dropClient(Client client, boolean notify) {
        if (mInstance == null) {
            return;
        }

        synchronized (mClientList) {
            if (mClientList.remove(client) == false) {
                return;
            }
        }
        client.close(notify);
        broadcast(CLIENT_DISCONNECTED, client);

        /*
         * http://forum.java.sun.com/thread.jspa?threadID=726715&start=0
         * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5073504
         */
        wakeup();
    }

    /*
     * Process activity from one of the debugger sockets. This could be a new
     * connection or a data packet.
     */
    private void processDebuggerActivity(SelectionKey key) {
        Debugger dbg = (Debugger)key.attachment();

        try {
            if (key.isAcceptable()) { //
处理Server响应这个事件
                try {
                    acceptNewDebugger(dbg, null); 
                } catch (IOException ioe) {
                    Log.w("ddms", "debugger accept() failed");
                    ioe.printStackTrace();
                }
            } else if (key.isReadable()) { //
如果是收到的数据,则可读取
                processDebuggerData(key);
            } else {
                Log.d("ddm-debugger", "key in unknown state");
            }
        } catch (CancelledKeyException cke) { //
记住,NIO处理这个异常,很多入门的开发者很容易忘记
            // key has been cancelled we can ignore that.
        }
    }

     private void acceptNewDebugger(Debugger dbg, ServerSocketChannel acceptChan) //这里用到了阻塞方式
            throws IOException {

        synchronized (mClientList) {
            SocketChannel chan;

            if (acceptChan == null)
                chan = dbg.accept(); 
            else
                chan = dbg.accept(acceptChan);

            if (chan != null) {
                chan.socket().setTcpNoDelay(true);

                wakeup();

                try {
                    chan.register(mSelector, SelectionKey.OP_READ, dbg);
                } catch (IOException ioe) {
                    // failed, drop the connection
                    dbg.closeData();
                    throw ioe;
                } catch (RuntimeException re) {
                    // failed, drop the connection
                    dbg.closeData();
                    throw re;
                }
            } else {
                Log.w("ddms", "ignoring duplicate debugger");
            }
        }
    }

    private void processDebuggerData(SelectionKey key) {
        Debugger dbg = (Debugger)key.attachment();

        try {
            dbg.read();

            JdwpPacket packet = dbg.getJdwpPacket();
            while (packet != null) {
                Log.v("ddms", "Forwarding dbg req 0x"
                        + Integer.toHexString(packet.getId()) + " to "
                        + dbg.getClient());

                dbg.forwardPacketToClient(packet);

                packet = dbg.getJdwpPacket();
            }
        } catch (IOException ioe) {
            Log.d("ddms", "Closing connection to debugger " + dbg);
            dbg.closeData();
            Client client = dbg.getClient();
            if (client.isDdmAware()) {
                   Log.d("ddms", " (recycling client connection as well)");

                    client.getDeviceImpl().getMonitor().addClientToDropAndReopen(client,
                        IDebugPortProvider.NO_STATIC_PORT);
            } else {
                Log.d("ddms", " (recycling client connection as well)");
                // we should drop the client, but also attempt to reopen it.
                // This is done by the DeviceMonitor.
                client.getDeviceImpl().getMonitor().addClientToDropAndReopen(client,
                        IDebugPortProvider.NO_STATIC_PORT);
            }
        }
    }

    private void wakeup() {
        mSelector.wakeup();
    }

    synchronized void quit() {
        mQuit = true;
        wakeup();
        Log.d("ddms", "Waiting for Monitor thread");
        try {
            this.join();
            // since we're quitting, lets drop all the client and disconnect
            // the DebugSelectedPort
            synchronized (mClientList) {
                for (Client c : mClientList) {
                    c.close(false /* notify */);
                    broadcast(CLIENT_DISCONNECTED, c);
                }
                mClientList.clear();
            }

            if (mDebugSelectedChan != null) {
                mDebugSelectedChan.close();
                mDebugSelectedChan.socket().close();
                mDebugSelectedChan = null;
            }
            mSelector.close();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        mInstance = null;
    }

    synchronized void addClient(Client client) {
        if (mInstance == null) {
            return;
        }

        Log.d("ddms", "Adding new client " + client);

        synchronized (mClientList) {
            mClientList.add(client);

            try {
                wakeup();

                client.register(mSelector);

                Debugger dbg = client.getDebugger();
                if (dbg != null) {
                    dbg.registerListener(mSelector);
                }
            } catch (IOException ioe) {
                // not really expecting this to happen
                ioe.printStackTrace();
            }
        }
    }

    /*
     * Broadcast an event to all message handlers.
     */
    private void broadcast(int event, Client client) {
        Log.d("ddms", "broadcast " + event + ": " + client);

        /*
         * The handler objects appear once in mHandlerMap for each message they
         * handle. We want to notify them once each, so we convert the HashMap
         * to a HashSet before we iterate.
         */
        HashSet<ChunkHandler> set;
        synchronized (mHandlerMap) {
            Collection<ChunkHandler> values = mHandlerMap.values();
            set = new HashSet<ChunkHandler>(values);
        }

        Iterator<ChunkHandler> iter = set.iterator();
        while (iter.hasNext()) {
            ChunkHandler handler = iter.next();
            switch (event) {
                case CLIENT_READY:
                    try {
                        handler.clientReady(client);
                    } catch (IOException ioe) {
                        // Something failed with the client. It should
                        // fall out of the list the next time we try to
                        // do something with it, so we discard the
                        // exception here and assume cleanup will happen
                        // later. May need to propagate farther. The
                        // trouble is that not all values for "event" may
                        // actually throw an exception.
                        Log.w("ddms",
                                "Got exception while broadcasting 'ready'");
                        return;
                    }
                    break;
                case CLIENT_DISCONNECTED:
                    handler.clientDisconnected(client);
                    break;
                default:
                    throw new UnsupportedOperationException();
            }
        }

    }

    /**
     * Opens (or reopens) the "debug selected" port and listen for connections.
     * @return true if the port was opened successfully.
     * @throws IOException
     */
    private boolean reopenDebugSelectedPort() throws IOException {

        Log.d("ddms", "reopen debug-selected port: " + mNewDebugSelectedPort);
        if (mDebugSelectedChan != null) {
            mDebugSelectedChan.close();
        }

        mDebugSelectedChan = ServerSocketChannel.open();
        mDebugSelectedChan.configureBlocking(false); // required for Selector

        InetSocketAddress addr = new InetSocketAddress(
                InetAddress.getByName("localhost"), //$NON-NLS-1$
                mNewDebugSelectedPort);
        mDebugSelectedChan.socket().setReuseAddress(true); // enable SO_REUSEADDR

        try {
            mDebugSelectedChan.socket().bind(addr);
            if (mSelectedClient != null) {
                mSelectedClient.update(Client.CHANGE_PORT);
            }

            mDebugSelectedChan.register(mSelector, SelectionKey.OP_ACCEPT, this);

            return true;
        } catch (java.net.BindException e) {
            displayDebugSelectedBindError(mNewDebugSelectedPort);

            // do not attempt to reopen it.
            mDebugSelectedChan = null;
            mNewDebugSelectedPort = -1;

            return false;
        }
    }

    /*
     * We have some activity on the "debug selected" port. Handle it.
     */
    private void processDebugSelectedActivity(SelectionKey key) {
        assert key.isAcceptable();

        ServerSocketChannel acceptChan = (ServerSocketChannel)key.channel();

        /*
         * Find the debugger associated with the currently-selected client.
         */
        if (mSelectedClient != null) {
            Debugger dbg = mSelectedClient.getDebugger();

            if (dbg != null) {
                Log.d("ddms", "Accepting connection on 'debug selected' port");
                try {
                    acceptNewDebugger(dbg, acceptChan);
                } catch (IOException ioe) {
                    // client should be gone, keep going
                }

                return;
            }
        }

        Log.w("ddms",
                "Connection on 'debug selected' port, but none selected");
        try {
            SocketChannel chan = acceptChan.accept();
            chan.close();
        } catch (IOException ioe) {
            // not expected; client should be gone, keep going
        } catch (NotYetBoundException e) {
            displayDebugSelectedBindError(mDebugSelectedPort);
        }
    }

    private void displayDebugSelectedBindError(int port) {
        String message = String.format(
                "Could not open Selected VM debug port (%1$d). Make sure you do not have another instance of DDMS or of the eclipse plugin running. If it's being used by something else, choose a new port number in the preferences.",
                port);

        Log.logAndDisplay(LogLevel.ERROR, "ddms", message);
    }
}

  从上面来看Android的开源代码有关PC上的写的不是很好,很多实现的地方都是用了严重的缝缝补补方式解决,有些习惯不是很到位,有关本NIO例子由于涉及的项目对象多,理解需要网友深入分析DDMS源码中的每个对象。细节写的不是很理想,Android123推荐大家,画出UML后再分析更清晰。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值