zoukankan      html  css  js  c++  java
  • android adb源码分析(5)【转】

    本文转载自:http://blog.csdn.net/xgbing/article/details/52096880

    本篇以“adb devices"命令为例,跟踪代码的执行流程。

    (1) main()->
    (2)adb_commandline()->
    adb_commandline()中的相关源码:

    [cpp] view plain copy
     
    1. if(!strcmp(argv[0], "devices")) {  
    2.     char *tmp;  
    3.     char *listopt;  
    4.     if (argc < 2)  
    5.         listopt = "";  
    6.     else if (argc == 2 && !strcmp(argv[1], "-l"))  
    7.         listopt = argv[1];  
    8.     else {  
    9.         fprintf(stderr, "Usage: adb devices [-l] ");  
    10.         return 1;  
    11.     }  
    12.     snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt);  
    13.     tmp = adb_query(buf); //传递的buf=”host:devices”  
    14.     if(tmp) { //命令执行成功,打印出信息  
    15.         printf("List of devices attached  ");  
    16.         printf("%s ", tmp);  
    17.         return 0;  
    18.     } else {///失败  
    19.         return 1;  
    20.     }  
    21. }  

    (3) adb_query():adb_query: host:devices ->

    [cpp] view plain copy
     
    1. char *adb_query(const char *service) //函数返回设备信息字符串  
    2. {  
    3.     char buf[5];  
    4.     unsigned n;  
    5.     char *tmp;  
    6.   
    7.     D("adb_query: %s ", service);  
    8.     int fd = adb_connect(service);  //连接adbserver,返回fd  
    9.     if(fd < 0) {  
    10.         fprintf(stderr,"error: %s ", __adb_error);  
    11.         return 0;  
    12.     }  
    13.   
    14.     if(readx(fd, buf, 4)) goto oops;   //读取数据长度,如果失败则返回错误  
    15.   
    16.     buf[4] = 0;  
    17.     n = strtoul(buf, 0, 16);      //转换成数值  
    18.     if(n > 1024) goto oops;  
    19.   
    20.     tmp = malloc(n + 1);     //申请空间  
    21.     if(tmp == 0) goto oops;  
    22.   
    23.     if(readx(fd, tmp, n) == 0) {   //读取信息并返回  
    24.         tmp[n] = 0;  
    25.         adb_close(fd);  
    26.         return tmp;  
    27.     }  
    28.     free(tmp);  
    29.   
    30. oops:  
    31.     adb_close(fd);  
    32.     return 0;  
    33. }  


    (4) adb_connect()->
    这个函数的作用是连接adb server,如果adb server没有启动则先启动它。

    [cpp] view plain copy
     
    1. int adb_connect(const char *service)  
    2. {  
    3.     // first query the adb server's version  
    4.     int fd = _adb_connect("host:version");  //查询adb server的版本信息,用来判断它是否启动。  
    5.   
    6.     D("adb_connect: service %s ", service);  
    7.     if(fd == -2) { //查询不到adb server  
    8.         fprintf(stdout,"* daemon not running. starting it now on port %d * ",  
    9.                 __adb_server_port);  
    10.     start_server: //启动adb server  
    11.         if(launch_server(__adb_server_port)) { //启动adb server失败  
    12.             fprintf(stderr,"* failed to start daemon * ");  
    13.             return -1;  
    14.         } else {  
    15.             fprintf(stdout,"* daemon started successfully * ");  
    16.         }  
    17.         /* give the server some time to start properly and detect devices */  
    18.         adb_sleep_ms(3000);  
    19.         // fall through to _adb_connect  
    20.     } else {//查询到adb server版本信息,说明adb server 已经启动  
    21.         // if server was running, check its version to make sure it is not out of date  
    22.         char buf[100];  
    23.         int n;  
    24.         int version = ADB_SERVER_VERSION - 1;  
    25.   
    26.         // if we have a file descriptor, then parse version result  
    27.         if(fd >= 0) {  
    28.             if(readx(fd, buf, 4)) goto error; //读取版本信息的长度  
    29.   
    30.             buf[4] = 0;  
    31.             n = strtoul(buf, 0, 16);  
    32.             if(n > (int)sizeof(buf)) goto error;  
    33.             if(readx(fd, buf, n)) goto error;//读取版本信息  
    34.             adb_close(fd);  
    35.   
    36.             if (sscanf(buf, "%04x", &version) != 1) goto error;//转换字符串的版本为数值型  
    37.         } else { //fd返回的值表示adb server可能不支持读取版本信息  
    38.             // if fd is -1, then check for "unknown host service",  
    39.             // which would indicate a version of adb that does not support the version command  
    40.             if (strcmp(__adb_error, "unknown host service") != 0)  
    41.                 return fd;  //返回错误。  
    42.         }  
    43.   
    44.         if(version != ADB_SERVER_VERSION) {  
    45.             printf("adb server is out of date.  killing... ");  
    46.             fd = _adb_connect("host:kill");  
    47.             adb_close(fd);  
    48.   
    49.             /* XXX can we better detect its death? */  
    50.             adb_sleep_ms(2000);  
    51.             goto start_server; //版本信息过期则关闭adb server并重新启动。  
    52.         }  
    53.     }  
    54.   
    55.     // if the command is start-server, we are done.  
    56.     if (!strcmp(service, "host:start-server"))  //如果命令是start-server,执行到这就可以了  
    57.         return 0;  
    58.   
    59.    //下面的代码连接adb server并返回fd。  
    60.     fd = _adb_connect(service); //连接adb server 并返回fd。  
    61.     if(fd == -2) {  
    62.         fprintf(stderr,"** daemon still not running");  
    63.     }  
    64.     D("adb_connect: return fd %d ", fd);  
    65.   
    66.     return fd;  
    67. error:  
    68.     adb_close(fd);  
    69.     return -1;  
    70. }  


    (5) launch_server()

    [cpp] view plain copy
     
    1. #if ADB_HOST  
    2. int launch_server(int server_port)  
    3. {  
    4. #ifdef HAVE_WIN32_PROC  
    5.     /* we need to start the server in the background                    */  
    6.     /* we create a PIPE that will be used to wait for the server's "OK" */  
    7.     /* message since the pipe handles must be inheritable, we use a     */  
    8.     /* security attribute                                               */  
    9.     HANDLE                pipe_read, pipe_write;  
    10.     SECURITY_ATTRIBUTES   sa;  
    11.     STARTUPINFO           startup;  
    12.     PROCESS_INFORMATION   pinfo;  
    13.     char                  program_path[ MAX_PATH ];  
    14.     int                   ret;  
    15.   
    16.     sa.nLength = sizeof(sa);  
    17.     sa.lpSecurityDescriptor = NULL;  
    18.     sa.bInheritHandle = TRUE;  
    19.   
    20.     /* create pipe, and ensure its read handle isn't inheritable */  
    21.     ret = CreatePipe( &pipe_read, &pipe_write, &sa, 0 );  
    22.     if (!ret) {  
    23.         fprintf(stderr, "CreatePipe() failure, error %ld ", GetLastError() );  
    24.         return -1;  
    25.     }  
    26.   
    27.     SetHandleInformation( pipe_read, HANDLE_FLAG_INHERIT, 0 );  
    28.   
    29.     ZeroMemory( &startup, sizeof(startup) );  
    30.     startup.cb = sizeof(startup);  
    31.     startup.hStdInput  = GetStdHandle( STD_INPUT_HANDLE );  
    32.     startup.hStdOutput = pipe_write;  
    33.     startup.hStdError  = GetStdHandle( STD_ERROR_HANDLE );  
    34.     startup.dwFlags    = STARTF_USESTDHANDLES;  
    35.   
    36.     ZeroMemory( &pinfo, sizeof(pinfo) );  
    37.   
    38.     /* get path of current program */  
    39.     GetModuleFileName( NULL, program_path, sizeof(program_path) );  
    40.   
    41. //创建进程“adb fork-server server”,并把startup信息传给新进程。  
    42.     ret = CreateProcess(  
    43.             program_path,                              /* program path  */  
    44.             "adb fork-server server",  
    45.                                     /* the fork-server argument will set the 
    46.                                        debug = 2 in the child           */  
    47.             NULL,                   /* process handle is not inheritable */  
    48.             NULL,                    /* thread handle is not inheritable */  
    49.             TRUE,                          /* yes, inherit some handles */  
    50.             DETACHED_PROCESS, /* the new process doesn't have a console */  
    51.             NULL,                     /* use parent's environment block */  
    52.             NULL,                    /* use parent's starting directory */  
    53.             &startup,                 /* startup info, i.e. std handles */  
    54.             &pinfo );  
    55.   
    56.     CloseHandle( pipe_write );  
    57.   
    58.     if (!ret) {  
    59.         fprintf(stderr, "CreateProcess failure, error %ld ", GetLastError() );  
    60.         CloseHandle( pipe_read );  
    61.         return -1;  
    62.     }  
    63.   
    64.     CloseHandle( pinfo.hProcess );  
    65.     CloseHandle( pinfo.hThread );  
    66.   
    67.     /* wait for the "OK " message */  
    68.     {  
    69.         char  temp[3];  
    70.         DWORD  count;  
    71.   
    72. //等待新进程发送“OK”到pipe_read管道。  
    73.         ret = ReadFile( pipe_read, temp, 3, &count, NULL );  
    74.         CloseHandle( pipe_read );  
    75.         if ( !ret ) {  
    76.             fprintf(stderr, "could not read ok from ADB Server, error = %ld ", GetLastError() );  
    77.             return -1;  
    78.         }  
    79.         if (count != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != ' ') {  
    80.             fprintf(stderr, "ADB server didn't ACK " );  
    81.             return -1;  
    82.         }  
    83.     }  
    84. #elif defined(HAVE_FORKEXEC)  
    85.     char    path[PATH_MAX];  
    86.     int     fd[2];  
    87.   
    88.     // set up a pipe so the child can tell us when it is ready.  
    89.     // fd[0] will be parent's end, and fd[1] will get mapped to stderr in the child.  
    90.     if (pipe(fd)) {  
    91.         fprintf(stderr, "pipe failed in launch_server, errno: %d ", errno);  
    92.         return -1;  
    93.     }  
    94.     get_my_path(path, PATH_MAX);  
    95.     pid_t pid = fork();  
    96.     if(pid < 0) return -1;  
    97.   
    98.     if (pid == 0) {  //下面的代码在子进程中运行  
    99.         // child side of the fork  
    100.   
    101.         // redirect stderr to the pipe  
    102.         // we use stderr instead of stdout due to stdout's buffering behavior.  
    103.         adb_close(fd[0]);  
    104.         dup2(fd[1], STDERR_FILENO);  //重定向新进程的错误信息给fd[1]  
    105.         adb_close(fd[1]);  
    106.   
    107.         // child process 运行”adb fok-server server”程序。  
    108.         int result = execl(path, "adb", "fork-server", "server", NULL);  
    109.         // this should not return  
    110.         fprintf(stderr, "OOPS! execl returned %d, errno: %d ", result, errno);  
    111.     } else  {//下面的代码还是在这个进程中执行  
    112.         // parent side of the fork  
    113.   
    114.         char  temp[3];  
    115.   
    116.         temp[0] = 'A'; temp[1] = 'B'; temp[2] = 'C';  
    117.         // wait for the "OK " message  
    118.         adb_close(fd[1]);  
    119.         int ret = adb_read(fd[0], temp, 3); //等待新进程发送“OK”字符串。  
    120.         int saved_errno = errno;  
    121.         adb_close(fd[0]);  
    122.         if (ret < 0) {  
    123.             fprintf(stderr, "could not read ok from ADB Server, errno = %d ", saved_errno);  
    124.             return -1;  
    125.         }  
    126.         if (ret != 3 || temp[0] != 'O' || temp[1] != 'K' || temp[2] != ' ') {//接收字符错误。  
    127.             fprintf(stderr, "ADB server didn't ACK " );  
    128.             return -1;  
    129.         }  
    130.   
    131.         setsid();  
    132.     }  
    133. #else  
    134. #error "cannot implement background server start on this platform"  
    135. #endif  
    136.     return 0;  
    137. }  
    138. #endif  


    再来看看”adb fork-server server”的运行。
    (1)    main()->adb_commandline();

    [cpp] view plain copy
     
    1. /* modifiers and flags */  
    2. while(argc > 0) {  
    3.     if(!strcmp(argv[0],"server")) {  
    4.         is_server = 1;  
    5.     } else if(!strcmp(argv[0],"nodaemon")) {  
    6.         no_daemon = 1;  
    7.     } else if (!strcmp(argv[0], "fork-server")) {  
    8.         /* this is a special flag used only when the ADB client launches the ADB Server */  
    9.         is_daemon = 1;  
    10.     } else if(!strcmp(argv[0],"persist")) {  
    11.         persist = 1;  
    12.     } else if(!strncmp(argv[0], "-p", 2)) {  
    13.         const char *product = NULL;  
    14.         if (argv[0][2] == '') {  
    15.             if (argc < 2) return usage();  
    16.             product = argv[1];  
    17.             argc--;  
    18.             argv++;  
    19.         } else {  
    20.             product = argv[0] + 2;  
    21.         }  
    22.         gProductOutPath = find_product_out_path(product);  
    23.         if (gProductOutPath == NULL) {  
    24.             fprintf(stderr, "adb: could not resolve "-p %s" ",  
    25.                     product);  
    26.             return usage();  
    27.         }  
    28.     } else if (argv[0][0]=='-' && argv[0][1]=='s') {  
    29.         if (isdigit(argv[0][2])) {  
    30.             serial = argv[0] + 2;  
    31.         } else {  
    32.             if(argc < 2 || argv[0][2] != '') return usage();  
    33.             serial = argv[1];  
    34.             argc--;  
    35.             argv++;  
    36.         }  
    37.     } else if (!strcmp(argv[0],"-d")) {  
    38.         ttype = kTransportUsb;  
    39.     } else if (!strcmp(argv[0],"-e")) {  
    40.         ttype = kTransportLocal;  
    41.     } else {  
    42.             /* out of recognized modifiers and flags */  
    43.         break;  
    44.     }  
    45.     argc--;  
    46.     argv++;  
    47. }  
    48.   
    49. adb_set_transport(ttype, serial);  
    50. adb_set_tcp_specifics(server_port);  
    51.   
    52. if (is_server) {  
    53.     if (no_daemon || is_daemon) {  
    54.         r = adb_main(is_daemon, server_port);  
    55.     } else {  
    56.         r = launch_server(server_port);  
    57.     }  
    58.     if(r) {  
    59.         fprintf(stderr,"* could not start server * ");  
    60.     }  
    61.     return r;  
    62. }  

    这里将is_daemon和is_server都置为1,并调用adb_main(1, 5037);

    (2)    adb_main()

    [cpp] view plain copy
     
    1. int adb_main(int is_daemon, int server_port)  
    2. {  
    3.   
    4. #if ADB_HOST  
    5.     HOST = 1;  
    6.     usb_vendors_init();  
    7.     usb_init();  //监听USB端口数据  
    8.     local_init(DEFAULT_ADB_LOCAL_TRANSPORT_PORT); //初始化建立5555网口信道  
    9.     adb_auth_init();  
    10.   
    11.     char local_name[30];  
    12.     build_local_name(local_name, sizeof(local_name), server_port); //监听5037端口  
    13.     if(install_listener(local_name, "*smartsocket*", NULL)) {  
    14.         exit(1);  
    15.     }  
    16. #else  
    17. …  
    18. #endif  
    19.   
    20.     if (is_daemon)  
    21.     {  
    22.         // inform our parent that we are up and running.  
    23. //发送“OK”给父进程  
    24. #ifdef HAVE_WIN32_PROC  
    25.         DWORD  count;  
    26.         WriteFile( GetStdHandle( STD_OUTPUT_HANDLE ), "OK ", 3, &count, NULL );  
    27. #elif defined(HAVE_FORKEXEC)  
    28.         fprintf(stderr, "OK ");  
    29. #endif  
    30.         start_logging();  
    31.     }  
    32.     D("Event loop starting ");  
    33.   
    34.     fdevent_loop();  
    35.   
    36.     usb_cleanup();  
    37.   
    38.     return 0;  
    39.   
    40. }  


    (3)    usb_linux.c中的usb_init()

    [cpp] view plain copy
     
    1. void usb_init()  
    2. {  
    3.     adb_thread_t tid;  
    4.     struct sigaction    actions;  
    5.   
    6.     memset(&actions, 0, sizeof(actions));  
    7.     sigemptyset(&actions.sa_mask);  
    8.     actions.sa_flags = 0;  
    9.     actions.sa_handler = sigalrm_handler;  
    10.     sigaction(SIGALRM,& actions, NULL);  
    11.   
    12.     if(adb_thread_create(&tid, device_poll_thread, NULL)){  
    13.         fatal_errno("cannot create input thread");  
    14.     }  
    15. }  
    16.   
    17. void* device_poll_thread(void* unused)  
    18. {  
    19.     D("Created device thread ");  
    20.     for(;;) {  
    21.             /* XXX use inotify */  
    22.         find_usb_device("/dev/bus/usb", register_device);  
    23.         kick_disconnected_devices();  
    24.         sleep(1);  
    25.     }  
    26.     return NULL;  
    27. }  

    在register_device()函数中最终会调用register_usb_transport()。

    (4)    install_listener(local_name, "*smartsocket*", NULL)

    [cpp] view plain copy
     
    1. if(!strcmp(l->connect_to, "*smartsocket*")) {  
    2.         fdevent_install(&l->fde, l->fd, ss_listener_event_func, l);  
    3.     } else {  
    4.         fdevent_install(&l->fde, l->fd, listener_event_func, l);  
    5.     }  
    6.     fdevent_set(&l->fde, FDE_READ);  


    (5)    ss_listener_event_func分析

    [cpp] view plain copy
     
    1. static void ss_listener_event_func(int _fd, unsigned ev, void *_l)  
    2. {  
    3.     asocket *s;  
    4.   
    5.     if(ev & FDE_READ) {  
    6.         struct sockaddr addr;  
    7.         socklen_t alen;  
    8.         int fd;  
    9.   
    10.         alen = sizeof(addr);  
    11.         fd = adb_socket_accept(_fd, &addr, &alen);  //接受客户端的连接  
    12.         if(fd < 0) return;  
    13.   
    14.         adb_socket_setbufsize(fd, CHUNK_SIZE);  
    15.   
    16.         s = create_local_socket(fd);  
    17.         if(s) {  
    18.             connect_to_smartsocket(s);  
    19.             return;  
    20.         }  
    21.   
    22.         adb_close(fd);  
    23.     }  
    24. }  


    (6)    执行connect_to_smartsocket(s)

    [cpp] view plain copy
     
    1. void connect_to_smartsocket(asocket *s)  
    2. {  
    3.     D("Connecting to smart socket  ");  
    4.     asocket *ss = create_smart_socket(smart_socket_action);  
    5.     s->peer = ss;  
    6.     ss->peer = s;  
    7.     s->ready(s);  
    8. }  


    (7)    执行create_smart_socket

    [cpp] view plain copy
     
    1. static int smart_socket_enqueue(asocket *s, apacket *p)  
    2. {  
    3.     unsigned len;  
    4. #if ADB_HOST  
    5.     char *service = NULL;  
    6.     char* serial = NULL;  
    7.     transport_type ttype = kTransportAny;  
    8. #endif  
    9.   
    10.     D("SS(%d): enqueue %d ", s->id, p->len);  
    11.   
    12.     if(s->pkt_first == 0) {  
    13.         s->pkt_first = p;  
    14.         s->pkt_last = p;  
    15.     } else {  
    16.         if((s->pkt_first->len + p->len) > MAX_PAYLOAD) {  
    17.             D("SS(%d): overflow ", s->id);  
    18.             put_apacket(p);  
    19.             goto fail;  
    20.         }  
    21.   
    22.         memcpy(s->pkt_first->data + s->pkt_first->len,  
    23.                p->data, p->len);  
    24.         s->pkt_first->len += p->len;  
    25.         put_apacket(p);  
    26.   
    27.         p = s->pkt_first;  
    28.     }  
    29.   
    30.         /* don't bother if we can't decode the length */  
    31.     if(p->len < 4) return 0;  
    32.   
    33.     len = unhex(p->data, 4);  
    34.     if((len < 1) ||  (len > 1024)) {  
    35.         D("SS(%d): bad size (%d) ", s->id, len);  
    36.         goto fail;  
    37.     }  
    38.   
    39.     D("SS(%d): len is %d ", s->id, len );  
    40.         /* can't do anything until we have the full header */  
    41.     if((len + 4) > p->len) {  
    42.         D("SS(%d): waiting for %d more bytes ", s->id, len+4 - p->len);  
    43.         return 0;  
    44.     }  
    45.   
    46.     p->data[len + 4] = 0;  
    47.   
    48.     D("SS(%d): '%s' ", s->id, (char*) (p->data + 4));  
    49.   
    50. #if ADB_HOST  
    51.     service = (char *)p->data + 4;  
    52.     if(!strncmp(service, "host-serial:", strlen("host-serial:"))) {  
    53.         char* serial_end;  
    54.         service += strlen("host-serial:");  
    55.   
    56.         // serial number should follow "host:" and could be a host:port string.  
    57.         serial_end = skip_host_serial(service);  
    58.         if (serial_end) {  
    59.             *serial_end = 0; // terminate string  
    60.             serial = service;  
    61.             service = serial_end + 1;  
    62.         }  
    63.     } else if (!strncmp(service, "host-usb:", strlen("host-usb:"))) {  
    64.         ttype = kTransportUsb;  
    65.         service += strlen("host-usb:");  
    66.     } else if (!strncmp(service, "host-local:", strlen("host-local:"))) {  
    67.         ttype = kTransportLocal;  
    68.         service += strlen("host-local:");  
    69.     } else if (!strncmp(service, "host:", strlen("host:"))) {  
    70.         ttype = kTransportAny;  
    71.         service += strlen("host:");  
    72.     } else {  
    73.         service = NULL;  
    74.     }  
    75.   
    76.     if (service) {  
    77.         asocket *s2;  
    78.   
    79.             /* some requests are handled immediately -- in that 
    80.             ** case the handle_host_request() routine has sent 
    81.             ** the OKAY or FAIL message and all we have to do 
    82.             ** is clean up. 
    83.             */  
    84.         if(handle_host_request(service, ttype, serial, s->peer->fd, s) == 0) {  
    85.                 /* XXX fail message? */  
    86.             D( "SS(%d): handled host service '%s' ", s->id, service );  
    87.             goto fail;  
    88.         }  
    89.         if (!strncmp(service, "transport", strlen("transport"))) {  
    90.             D( "SS(%d): okay transport ", s->id );  
    91.             p->len = 0;  
    92.             return 0;  
    93.         }  
    94.   
    95.             /* try to find a local service with this name. 
    96.             ** if no such service exists, we'll fail out 
    97.             ** and tear down here. 
    98.             */  
    99.         s2 = create_host_service_socket(service, serial);  
    100.         if(s2 == 0) {  
    101.             D( "SS(%d): couldn't create host service '%s' ", s->id, service );  
    102.             sendfailmsg(s->peer->fd, "unknown host service");  
    103.             goto fail;  
    104.         }  
    105.   
    106.             /* we've connected to a local host service, 
    107.             ** so we make our peer back into a regular 
    108.             ** local socket and bind it to the new local 
    109.             ** service socket, acknowledge the successful 
    110.             ** connection, and close this smart socket now 
    111.             ** that its work is done. 
    112.             */  
    113.         adb_write(s->peer->fd, "OKAY", 4);  
    114.   
    115.         s->peer->ready = local_socket_ready;  
    116.         s->peer->close = local_socket_close;  
    117.         s->peer->peer = s2;  
    118.         s2->peer = s->peer;  
    119.         s->peer = 0;  
    120.         D( "SS(%d): okay ", s->id );  
    121.         s->close(s);  
    122.   
    123.             /* initial state is "ready" */  
    124.         s2->ready(s2);  
    125.         return 0;  
    126.     }  
    127. #else /* !ADB_HOST */  
    128.     if (s->transport == NULL) {  
    129.         char* error_string = "unknown failure";  
    130.         s->transport = acquire_one_transport (CS_ANY,  
    131.                 kTransportAny, NULL, &error_string);  
    132.   
    133.         if (s->transport == NULL) {  
    134.             sendfailmsg(s->peer->fd, error_string);  
    135.             goto fail;  
    136.         }  
    137.     }  
    138. #endif  
    139.   
    140.     if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) {  
    141.            /* if there's no remote we fail the connection 
    142.             ** right here and terminate it 
    143.             */  
    144.         sendfailmsg(s->peer->fd, "device offline (x)");  
    145.         goto fail;  
    146.     }  
    147.   
    148.   
    149.         /* instrument our peer to pass the success or fail 
    150.         ** message back once it connects or closes, then 
    151.         ** detach from it, request the connection, and 
    152.         ** tear down 
    153.         */  
    154.     s->peer->ready = local_socket_ready_notify;  
    155.     s->peer->close = local_socket_close_notify;  
    156.     s->peer->peer = 0;  
    157.         /* give him our transport and upref it */  
    158.     s->peer->transport = s->transport;  
    159.   
    160.     connect_to_remote(s->peer, (char*) (p->data + 4));  
    161.     s->peer = 0;  
    162.     s->close(s);  
    163.     return 1;  
    164.   
    165. fail:  
    166.         /* we're going to close our peer as a side-effect, so 
    167.         ** return -1 to signal that state to the local socket 
    168.         ** who is enqueueing against us 
    169.         */  
    170.     s->close(s);  
    171.     return -1;  
    172. }  


    (8)    执行handle_host_request

    [cpp] view plain copy
     
      1. int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s)  
      2. {  
      3.   
      4.     // return a list of all connected devices  
      5.     if (!strncmp(service, "devices", 7)) {   //执行adb devices,返回结果  
      6.         char buffer[4096];  
      7.         int use_long = !strcmp(service+7, "-l");  
      8.         if (use_long || service[7] == 0) {  
      9.             memset(buf, 0, sizeof(buf));  
      10.             memset(buffer, 0, sizeof(buffer));  
      11.             D("Getting device list  ");  
      12.             list_transports(buffer, sizeof(buffer), use_long);  
      13.             snprintf(buf, sizeof(buf), "OKAY%04x%s",(unsigned)strlen(buffer),buffer);  
      14.             D("Wrote device list  ");  
      15.             writex(reply_fd, buf, strlen(buf));  
      16.             return 0;  
      17.         }  
      18. }  
      19.   
      20. }  
  • 相关阅读:
    YC创始人格雷厄姆为何不喜欢大学生创业?
    在创业的道路上,让公司生存下去是做重要的!
    农村90后李传帅的创业故事
    创业公司的出头之日在哪里?
    创业者不能盲目的跟风,不然结局很凄凉
    小程序到底适不适合创业者
    共享经济的涨潮与退潮就在一瞬间
    互联网行业进入焦虑时代,如何才能做到攻守有道?
    88
    JZOJ.5331【NOIP2017模拟8.23】壕游戏
  • 原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/8175845.html
Copyright © 2011-2022 走看看