[Android输入系统] Socket连接的建立

265 阅读3分钟

前一篇文章,我们讲了Input ANR是怎么产生的[ANR]Input ANR是怎样产生的,着重讲的Input ANR的触发和判定原理。主要分析了system_server进程中的InputDispatcher线程的运行流程。这个线程主要负责事件的分发,通过socket将事件发送给App端进行处理。

system_server进程的InputDispatcher线程,需要与App端的UI线程进行通信,需要先建立socket连接。这篇文章,我们讲讲socket连接的建立过程。

socketpair使用

socketpair方法,主要用于创建一对无名的、相互连接的套接字。

#include <sys/types.h> 
#include <sys/socket.h>

int socketpair(int domain, int type, int protocol, int sv[2]);

参数:

  • domain:协议家族
    • AF_LOCAL
    • AF_UNIX
  • type:套接字类型
    • SOCKET_STREAM:基于TCP
    • SOCKET_DGRAM:基于UDP
    • SOCKET_SEQPACKET:序列化包,提供一个序列化的、可靠的、双向的基本连接的数据传输通道,数据长度定长。
  • protocol:协议类型,只能是0
  • sv:返回的套接字对

返回值:

  • 0:成功
  • 1:失败

read方法

ssize_t read(int fd, void * buf, size_t count);

作用:将fd所指的文件传送count个字节到buf指针所指的内存中 返回值:

  • 返回实际读到的字节数
  • 0:表示读到文件尾,无刻度数据
  • 小于0:读取出错,需要看具体的errno

write方法

ssize_t write (int fd, const void * buf, size_t count); 

作用:将buf所指的内存写入count个字节到参数fd所指的文件中。 返回值:

  • 返回实际写入的字节数
  • -1:发生错误,需要看具体的errno。

这里先简单介绍一下read和write方法,之后再出文章详细介绍UNIX域套接字。

App跨进程调用WMS

主线程创建Activity的时候,会调用setContentView,最后会调用到ViewRootImplsetView方法。

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
    synchronized (this) {
      ...
      if ((mWindowAttributes.inputFeatures
          & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
          mInputChannel = new InputChannel(); //创建InputChannel对象
      }
      //通过Binder调用,进入system进程的Session
      res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
                  getHostVisibility(), mDisplay.getDisplayId(),
                  mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                  mAttachInfo.mOutsets, mInputChannel);
      ...
      if (mInputChannel != null) {
          if (mInputQueueCallback != null) {
              mInputQueue = new InputQueue();
              mInputQueueCallback.onInputQueueCreated(mInputQueue);
          }
          //创建WindowInputEventReceiver对象,并且传入刚刚创建好的mInputChannel
          mInputEventReceiver = new WindowInputEventReceiver(mInputChannel,  Looper.myLooper());
      }
    }
}

这个方法,主要是一个跨进程的Binder调用,最后调用的WMS中的addWinow方法。

WMS生成两个InputChannel

public int addWindow(Session session, IWindow client, int seq, WindowManager.LayoutParams attrs, int viewVisibility, int displayId, Rect outContentInsets, Rect outStableInsets, Rect outOutsets, InputChannel outInputChannel) {
        //创建一对InputChannel
        InputChannel[] inputChannels = InputChannel.openInputChannelPair(name);
        //将socket服务端保存到WindowState的mInputChannel
        win.setInputChannel(inputChannels[0]);
        //socket客户端传递给outInputChannel,最后会作为跨进程调用的返回值,传递给App端
        inputChannels[1].transferTo(outInputChannel);
        //利用socket服务端作为参数,注册到system_server的IMS中
        mInputManager.registerInputChannel(win.mInputChannel, win.mInputWindowHandle);
    }
    //设置当前聚焦窗口
    mInputMonitor.updateInputWindowsLw(false /*force*/);
}

这个方法,主要调用了InputChannelopenInputChannePair,生成一对InputChannel

  • InputChannel[0] 保存到WindowStatemInputChannel
  • InputChannel[1] 传递给客户端,即ViewRootImpl中的mInputChannel,最后会和WindowInputEventReceiver关联。InputChannel可以跨进程通信。
status_t InputChannel::openInputChannelPair(const String8& name,
        sp<InputChannel>& outServerChannel, sp<InputChannel>& outClientChannel) {
    int sockets[2];
    //真正创建socket对的地方【核心】
    if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, sockets)) {
        ...
        return result;
    }

    int bufferSize = SOCKET_BUFFER_SIZE; //32k
    setsockopt(sockets[0], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
    setsockopt(sockets[0], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));
    setsockopt(sockets[1], SOL_SOCKET, SO_SNDBUF, &bufferSize, sizeof(bufferSize));
    setsockopt(sockets[1], SOL_SOCKET, SO_RCVBUF, &bufferSize, sizeof(bufferSize));

    String8 serverChannelName = name;
    serverChannelName.append(" (server)");
    //创建InputChannel对象
    outServerChannel = new InputChannel(serverChannelName, sockets[0]);

    String8 clientChannelName = name;
    clientChannelName.append(" (client)");
    //创建InputChannel对象
    outClientChannel = new InputChannel(clientChannelName, sockets[1]);
    return OK;
}

在这个方法里,主要做了以下事情:

  • 创建了一个socket pair,生成一对无名的,相互连接的套接字
  • 设两个套接口的缓冲区大小为32kb
  • 分别创建服务端和客户端的InputChannel对象
    • socket[0]对应的是server
    • socket[1]对应的是client

到这里,socket的创建就完成了。

总结

Android输入系统,system_server和app之间socket的创建流程:

  • 首先App端在初始化view的时候,会通过跨进程Binder调用WMS的addWindow方法
  • WMS在addWindow中会创建socket连接,生成两个inputChannel对象,一个设置给IMS,一个通过Binder传回给App。

拓展阅读