前面我们分析到WifiDisplaySource会调用ANetworkSession的接口去创建一个socket,并在这个socket上监听是否有客户端的连接请求。先来看看Wifi Display规范的一些流程图:
从之前的一篇文章中,当ANetworkSession创建好RTSP的listen socket后,就会把它加入到selelct中等待对方的连接,那我们首先来看ANetworkSession的threadLoop方法:
[java] view plain copy 1.
void ANetworkSession::threadLoop() {
2.
3. int res = select(maxFd + 1, &rs, &ws, NULL, NULL /* tv */); 4.
5. {
6. Mutex::Autolock autoLock(mLock); 7.
8. List
10. for (size_t i = mSessions.size(); res > 0 && i-- > 0;) { 11. const sp
13. int s = session->socket(); 14.
15. if (s < 0) { 16. continue; 17. } 18.
19. if (FD_ISSET(s, &rs) || FD_ISSET(s, &ws)) { 20. --res; 21. } 22.
23. if (FD_ISSET(s, &rs)) {
24. if (session->isRTSPServer() || session->isTCPDatagramServer(
)) {
25. struct sockaddr_in remoteAddr;
26. socklen_t remoteAddrLen = sizeof(remoteAddr); 27.
28. int clientSocket = accept(
29. s, (struct sockaddr *)&remoteAddr, &remoteAddrLe
n);
30.
31. if (clientSocket >= 0) {
32. status_t err = MakeSocketNonBlocking(clientSocket);
33.
34. if (err != OK) { 35.
36. } else {
37. in_addr_t addr = ntohl(remoteAddr.sin_addr.s_add
r);
38.
39. ALOGI(\
40. \,
41. (addr >> 24),
42. (addr >> 16) & 0xff, 43. (addr >> 8) & 0xff, 44. addr & 0xff,
45. ntohs(remoteAddr.sin_port), 46. clientSocket); 47.
48. sp
50. mNextSessionID++, 51. Session::CONNECTED, 52. clientSocket,
53. session->getNotificationMessage());
54.
55. clientSession->setMode(
56. session->isRTSPServer() 57. ? Session::MODE_RTSP
58. : Session::MODE_DATAGRAM); 59.
60. sessionsToAdd.push_back(clientSession); 61. } 62. } else {
63. ALOGE(\, 64. errno, strerror(errno)); 65. } 66. } 67. } 68.
69. while (!sessionsToAdd.empty()) {
70. sp
73. mSessions.add(session->sessionID(), session); 74.
75. ALOGI(\, session->sessionID()); 76. } 77. }
上面在selelct循环中,首先只有刚创建的RTSP的listen socket,接着如果有客户端的连接请求,就会跳出select语句,然后调用accept去接收对方的连接。接着会去创建一个新的Session会话,我们去看它的构造函数:
[java] view plain copy 1. ANetworkSession::Session::Session( 2. int32_t sessionID, 3. State state, 4. int s,
5. const sp ?ify) 6. : mSessionID(sessionID), 7. mState(state),
8. mMode(MODE_DATAGRAM), 9. mSocket(s),
10. mNotify(notify),
11. mSawReceiveFailure(false), 12. mSawSendFailure(false),
13. mUDPRetries(kMaxUDPRetries), 14. mLastStallReportUs(-1ll) { 15. if (mState == CONNECTED) {
16. struct sockaddr_in localAddr;
17. socklen_t localAddrLen = sizeof(localAddr); 18.
19. int res = getsockname(
20. mSocket, (struct sockaddr *)&localAddr, &localAddrLen); 21. CHECK_GE(res, 0); 22.
23. struct sockaddr_in remoteAddr;
24. socklen_t remoteAddrLen = sizeof(remoteAddr); 25.
26. res = getpeername(
27. mSocket, (struct sockaddr *)&remoteAddr, &remoteAddrLen); 28. CHECK_GE(res, 0); 29.
30. sp msg = mNotify->dup();
31. msg->setInt32(\, mSessionID);
32. msg->setInt32(\, kWhatClientConnected);
33. msg->setString(\, localAddrString.c_str()); 34. msg->setInt32(\, ntohs(localAddr.sin_port)); 35. msg->setString(\, remoteAddrString.c_str()); 36. msg->setInt32(\, ntohs(remoteAddr.sin_port)); 37. msg->post(); 38. } 39. }
这里的状态mState为CONNECTED,所以会构建一个AMessage并post出去,由前一章的知识,我们知道这里的mNotify是一个kWhatRTSPNotify消息,会在WifiDisplaySource调用createRTSPServer时传进来的一个参数,所以这里创建的AMessage最终还是会被
WifiDisplaySource去处理,跳过中间AMessage、ALooperRoster、ALooper的调用关系,我们直接到WifiDisplaySource的onMessageReceived去看如何处理:
[java] view plain copy 1. case kWhatRTSPNotify: 2. {
3. int32_t reason;
4. CHECK(msg->findInt32(\, &reason)); 5.
6. switch (reason) {
7. case ANetworkSession::kWhatError: 8. { 9.
10. break; 11. } 12.
13. case ANetworkSession::kWhatClientConnected: 14. {
15. int32_t sessionID;
16. CHECK(msg->findInt32(\, &sessionID)); 17.
18. if (mClientSessionID > 0) {
19. ALOGW(\ 20. \); 21.
22. mNetSession->destroySession(sessionID); 23. break; 24. } 25.
26. CHECK_EQ(mState, AWAITING_CLIENT_CONNECTION); 27.
28. CHECK(msg->findString(\, &mClientInfo.mRemoteIP)); 29. CHECK(msg->findString(\, &mClientInfo.mLocalIP)); 30.
31. if (mClientInfo.mRemoteIP == mClientInfo.mLocalIP) { 32. // Disallow connections from the local interface 33. // for security reasons.
34. mNetSession->destroySession(sessionID); 35. break; 36. } 37.
38. CHECK(msg->findInt32(
39. \, &mClientInfo.mLocalPort)); 40. mClientInfo.mPlaybackSessionID = -1;