SATDevices.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. #include "StdAfx.h"
  2. #include "SATDevices.h"
  3. #include "SATExecutor.h"
  4. bool CSATDevices::s_bEnableAndroid = true;
  5. ULONGLONG CSATDevices::s_ulEraseDuration = 30000;
  6. ThreadSection CSATDevices::s_ThreadSection;
  7. std::vector<SATDEV::STDevice> CSATDevices::s_vtDevices;
  8. bool CSATDevices::s_bAutoAddAdbThreadFinished = true;
  9. void Split(vector<std::string>& vtLine, std::string strLines, const std::string strSplit)
  10. {
  11. if (strLines.size() == 0 || strSplit.size() == 0)
  12. return;
  13. int nIndex = 0;
  14. vtLine.clear();
  15. std::string strtmp;
  16. while (std::string::npos != (nIndex = strLines.find(strSplit))) {
  17. strtmp = strLines.substr(0, nIndex);
  18. if (strtmp.size())vtLine.push_back(strtmp);
  19. strLines = strLines.substr(nIndex + strSplit.size());
  20. }
  21. if (strLines.size())
  22. vtLine.push_back(strLines);
  23. }
  24. std::string ExecuteCMD(std::string cmd)
  25. {
  26. SECURITY_ATTRIBUTES sa = {0};
  27. HANDLE hRead = NULL, hWrite = NULL;
  28. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  29. sa.lpSecurityDescriptor = NULL;
  30. sa.bInheritHandle = TRUE;
  31. // 创建管道;
  32. if ( !CreatePipe(&hRead, &hWrite, &sa, 1024) )
  33. {
  34. return "";
  35. }
  36. STARTUPINFO si = {0};
  37. PROCESS_INFORMATION pi = {0};
  38. si.cb = sizeof(STARTUPINFO);
  39. GetStartupInfo(&si);
  40. si.hStdError = hWrite;
  41. si.hStdOutput = hWrite;
  42. si.wShowWindow = SW_HIDE;
  43. si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  44. // 创建子进程;
  45. TCHAR szCommand[MAX_PATH] = {0};
  46. _tcscpy_s(szCommand,cmd.c_str());
  47. if ( !CreateProcess(NULL, szCommand, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi) )
  48. {
  49. CloseHandle(hRead);
  50. CloseHandle(hWrite);
  51. hWrite = hRead = NULL;
  52. }
  53. // 等待进程结束;
  54. WaitForSingleObject(pi.hProcess, INFINITE);
  55. // 释放所有句柄;
  56. CloseHandle(pi.hProcess);
  57. CloseHandle(pi.hThread);
  58. if ( hWrite ) {
  59. CloseHandle(hWrite);
  60. hWrite = NULL;
  61. }
  62. // 等待缓冲写入;
  63. Sleep(200);
  64. // 读取内容;
  65. DWORD dwBytesRead;
  66. TCHAR szBuffer[1024] = {0};
  67. ReadFile(hRead, szBuffer, 1024, &dwBytesRead, NULL);
  68. CloseHandle(hRead);
  69. // 返回结果;
  70. return std::string(szBuffer);
  71. }
  72. CSATDevices::CSATDevices(void)
  73. {
  74. m_hEvent = NULL;
  75. m_hWorkThread = NULL;
  76. // 添加一个虚拟设备;
  77. AddVirtualDevices("192.168.1.999:5555");
  78. }
  79. CSATDevices::~CSATDevices(void)
  80. {
  81. EndofWork();
  82. }
  83. void CSATDevices::StartWork()
  84. {
  85. m_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  86. if ( m_hEvent == NULL ) {
  87. return;
  88. }
  89. m_hWorkThread = CreateThread(NULL, 0, WorkThread, this, 0, NULL);
  90. if ( m_hWorkThread == NULL) {
  91. SetEvent(m_hEvent);
  92. CloseHandle(m_hEvent);
  93. CloseHandle(m_hWorkThread);
  94. m_hEvent = NULL;
  95. m_hWorkThread = NULL;
  96. return;
  97. }
  98. }
  99. void CSATDevices::EndofWork()
  100. {
  101. // 设置事件有信号;
  102. if ( m_hEvent )
  103. SetEvent(m_hEvent);
  104. // 等待线程结束;
  105. if ( m_hWorkThread ) {
  106. WaitForSingleObject(m_hWorkThread, INFINITE);
  107. CloseHandle(m_hWorkThread);
  108. m_hWorkThread = NULL;
  109. }
  110. // 关闭事件句柄;
  111. if ( m_hEvent ) {
  112. CloseHandle(m_hEvent);
  113. m_hEvent = NULL;
  114. }
  115. }
  116. DWORD CSATDevices::WorkThread(LPVOID lpVoid)
  117. {
  118. CSATDevices *that = (CSATDevices*)lpVoid;
  119. do
  120. {
  121. if ( !s_bEnableAndroid )
  122. continue;
  123. // 刷新当前设备列表;
  124. std::vector<SATDEV::STDevice> vtDevices;
  125. // 获取当前设备列表;
  126. GetCurrentDevices(vtDevices);
  127. AutoThreadSection ats(&s_ThreadSection);
  128. #ifdef _DEBUG
  129. //TRACE(_T("\t=================start==================\n"));
  130. #endif
  131. // 遍历设备列表是否有下线设备;
  132. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  133. for ( ;it != s_vtDevices.end(); ) {
  134. #ifdef _DEBUG
  135. //TRACE3(_T("\t当前设备类型:%d, 设备名称:%s, 设备状态:%d\n"), it->nType, it->strName.c_str(), it->nStatus);
  136. #endif
  137. if ( it->nType == SATDEV::Virtual ) {
  138. it++;
  139. continue;
  140. }
  141. if ( IsDevicesOffline(*it, vtDevices) ) {
  142. #ifdef _DEBUG
  143. printf("IsDevicesOffline\n");
  144. #endif
  145. if ( (GetTickCount64() - it->ulOfflineTime) > GLOBAL::g_stSATConfig.dwAdbTimeout ) {
  146. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV, "移除设备->设备类型:%d, 设备名称:%s, 状态:%d", it->nType, it->strName.c_str(), it->nStatus);
  147. // 移除设备(需要线程加锁);
  148. it = s_vtDevices.erase(it);
  149. } else {
  150. if ( it->nType == SATDEV::Reticle ) {
  151. if ( it->nStatus == SATDEV::Offline ) {
  152. // offline只能reconnect才能重连;
  153. std::string str = "adb reconnect offline";
  154. WinExec(str.c_str(), SW_HIDE);
  155. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV, "offline重连->设备类型:%d, 设备名称:%s, 状态:%d, 命令:%s", it->nType, it->strName.c_str(), it->nStatus, str.c_str());
  156. }
  157. else if ( it->nStatus == SATDEV::Dropline ) {
  158. std::string str = "adb connect ";
  159. str.append(it->strName);
  160. WinExec(str.c_str(), SW_HIDE);
  161. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV, "dropline重连->设备类型:%d, 设备名称:%s, 状态:%d, 命令:%s", it->nType, it->strName.c_str(), it->nStatus, str.c_str());
  162. }
  163. // 等待adb重连完成;
  164. Sleep(2500);
  165. } else if ( it->nType == SATDEV::Usb ) {
  166. #if 1
  167. if ( it->nStatus == SATDEV::Offline ) {
  168. // offline需要reconnect才能重连;
  169. std::string str = "adb reconnect offline";
  170. WinExec(str.c_str(), SW_HIDE);
  171. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV, "usb重连->设备类型:%d, 设备名称:%s, 状态:%d, 命令:%s", it->nType, it->strName.c_str(), it->nStatus, str.c_str());
  172. // 等待adb启动;
  173. Sleep(2500);
  174. }
  175. // 不管状态是Offline还是Dropline,只要重连超过指定时长, 执行kill;
  176. if ( it->ulOfflineTime != 0 && (GetTickCount64() - it->ulOfflineTime > GLOBAL::g_stSATConfig.dwAdbKillTime) ) { // 1/2掉线时长太久,固定9秒时间,大概3次重连;
  177. std::string str = "adb kill-server";
  178. // 如果是usb的话,可能要kill-server,再全部重连接;
  179. WinExec(str.c_str(), SW_HIDE);
  180. // 重连所有;
  181. ReConnectAllDevices();
  182. // 重连后,break此次循环;
  183. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV, "usb重连->设备类型:%d, 设备名称:%s, 状态:%d, 命令:%s", it->nType, it->strName.c_str(), it->nStatus, str.c_str());
  184. // 等待adb启动;
  185. Sleep(3500);
  186. // kill后,需要重新GetCurrentDevices查询;
  187. break;
  188. }
  189. #else
  190. std::string str = "adb kill-server";
  191. // 如果是usb的话,可能要kill-server,再全部重连接;
  192. WinExec(str.c_str(), SW_HIDE);
  193. // 重连所有;
  194. ReConnectAllDevices();
  195. // 重连后,break此次循环;
  196. GLOBAL::WriteTextLog("usb重连->设备类型:%d, 设备名称:%s, 状态:%d, 命令:%s", it->nType, it->strName.c_str(), it->nStatus, str.c_str());
  197. // 等待adb启动;
  198. Sleep(3500);
  199. // kill后,需要重新GetCurrentDevices查询;
  200. break;
  201. #endif
  202. }
  203. it++;
  204. }
  205. }
  206. else
  207. it++;
  208. }
  209. // 保存全部设备状态;
  210. SaveAllDevicesStatus2Config();
  211. #ifdef _DEBUG
  212. //TRACE(_T("\t=================end==================\n"));
  213. #endif
  214. } while (WaitForSingleObject(that->m_hEvent, 2500) == WAIT_TIMEOUT);
  215. printf("end thread\n");
  216. return 0;
  217. }
  218. void CSATDevices::DelDevices(std::string name)
  219. {
  220. std::string str;
  221. AutoThreadSection ats(&s_ThreadSection);
  222. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  223. for(; it != s_vtDevices.end(); it++ ) {
  224. if ( _tcscmp(it->strName.c_str(), name.c_str()) == 0 ) {
  225. // usb设备没办法删除,此处忽略;
  226. if ( it->nType != SATDEV::Usb) {
  227. // 只有网络设备才能删除;
  228. str = "adb disconnect ";
  229. str.append(name);
  230. WinExec(str.c_str(), SW_HIDE);
  231. CSATExecutor::GetInstance()->DelDevices(*it);
  232. s_vtDevices.erase(it);
  233. #ifdef _DEBUG
  234. TRACE(_T("\t删除设备:%s\n"), name.c_str());
  235. #endif
  236. }
  237. break;
  238. }
  239. }
  240. }
  241. void CSATDevices::AddReticleDevices(std::string ip)
  242. {
  243. // 处理自动添加adb;
  244. if ( ip.compare("0.0.0.0") == 0 ) {
  245. if (s_bAutoAddAdbThreadFinished) {
  246. // 启动adb设备自动添加线程;
  247. HANDLE h = CreateThread(NULL, 0, AddAdbThread, NULL, 0, NULL);
  248. CloseHandle(h);
  249. }
  250. return;
  251. }
  252. SATDEV::STDevice stDevice;
  253. stDevice.nType = SATDEV::Reticle;
  254. stDevice.strName = ip;
  255. stDevice.ulOfflineTime = 0;
  256. stDevice.nStatus = SATDEV::Online;
  257. stDevice.nUsageState = SATDEV::Idle;
  258. #ifdef _DEBUG
  259. TRACE1(_T("\t添加新设备:%s\n"), ip.c_str());
  260. #endif
  261. AutoThreadSection ats(&s_ThreadSection);
  262. if (!IsDeviceExist(stDevice)) {
  263. s_vtDevices.push_back(stDevice);
  264. CSATExecutor::GetInstance()->AddDevices(stDevice);
  265. }
  266. }
  267. void CSATDevices::AddVirtualDevices(std::string name)
  268. {
  269. SATDEV::STDevice stDevice;
  270. stDevice.nType = SATDEV::Virtual;
  271. stDevice.strName = name;
  272. stDevice.ulOfflineTime = 0;
  273. stDevice.nStatus = SATDEV::Online;
  274. stDevice.nUsageState = SATDEV::Idle;
  275. AutoThreadSection ats(&s_ThreadSection);
  276. if (!IsDeviceExist(stDevice)) {
  277. s_vtDevices.push_back(stDevice);
  278. CSATExecutor::GetInstance()->AddDevices(stDevice);
  279. }
  280. }
  281. bool CSATDevices::IsDeviceExist(SATDEV::STDevice &stDevice)
  282. {
  283. bool bExist = false;
  284. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  285. for(; it != s_vtDevices.end(); it++ ) {
  286. if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
  287. bExist = true;
  288. break;
  289. }
  290. }
  291. return bExist;
  292. }
  293. bool CSATDevices::IsDevicesOffline(SATDEV::STDevice &stDevice, std::vector<SATDEV::STDevice> &vtDevices )
  294. {
  295. bool bFound = false;
  296. bool bOffline = true;
  297. // 在当前设备列表查询;
  298. std::vector<SATDEV::STDevice>::iterator it = vtDevices.begin();
  299. for(; it != vtDevices.end(); it++ ) {
  300. if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
  301. // 找到设备;
  302. bFound = true;
  303. // 同步状态;
  304. stDevice.nStatus = it->nStatus;
  305. // 设备是否offline或dropline;
  306. if ( it->nStatus != SATDEV::Offline && it->nStatus != SATDEV::Dropline)
  307. bOffline = false;
  308. break;
  309. }
  310. }
  311. if ( bOffline ) {
  312. if ( stDevice.ulOfflineTime == 0 ) {
  313. // 首次检测到离线;
  314. if ( stDevice.nStatus != SATDEV::Offline )
  315. stDevice.nStatus = SATDEV::Dropline;
  316. stDevice.ulOfflineTime = GetTickCount64();
  317. }
  318. // 如果没找到设备,彻底离线;
  319. if ( !bFound )
  320. stDevice.nStatus = SATDEV::Dropline;
  321. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV, "离线->设备类型:%d, 设备名称:%s, 状态:%d", stDevice.nType, stDevice.strName.c_str(), stDevice.nStatus);
  322. } else {
  323. // 若重新连接,重置离线时间;
  324. if ( stDevice.ulOfflineTime ) {
  325. stDevice.nStatus = SATDEV::Online;
  326. stDevice.ulOfflineTime = 0;
  327. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV, "重连成功->设备类型:%d, 设备名称:%s, 状态:%d", stDevice.nType, stDevice.strName.c_str(), stDevice.nStatus);
  328. }
  329. }
  330. return bOffline;
  331. }
  332. bool CSATDevices::IsNewDevices(SATDEV::STDevice &stDevice)
  333. {
  334. bool bNewDevices = true;
  335. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  336. for(; it != s_vtDevices.end(); it++ ) {
  337. if ( _tcscmp(it->strName.c_str(), stDevice.strName.c_str()) == 0 ) {
  338. bNewDevices = false;
  339. break;
  340. }
  341. }
  342. // 如果是新设备(一般是usb连接)
  343. if ( bNewDevices && stDevice.nType != SATDEV::Reticle ) {
  344. AutoThreadSection ats(&s_ThreadSection);
  345. s_vtDevices.push_back(stDevice);
  346. CSATExecutor::GetInstance()->AddDevices(stDevice);
  347. }
  348. return bNewDevices;
  349. }
  350. void CSATDevices::GetCurrentDevices(std::vector<SATDEV::STDevice> &vtDevices)
  351. {
  352. std::string strLines = ExecuteCMD("adb devices");
  353. std::vector<std::string> vtLine;
  354. Split(vtLine, strLines, "\r\n");
  355. int npos = -1;
  356. // offline设备也要加入,不区分usb或reticle;
  357. for ( std::vector<std::string>::iterator it = vtLine.begin(); it != vtLine.end(); it++ ) {
  358. //if ( _tcsicmp("List of devices attached ", it->c_str()) == 0 )
  359. if ( it->find("List of devices attached") != std::string::npos ||
  360. it->find("* daemon started successfully") != std::string::npos ||
  361. it->find("* daemon not running") != std::string::npos )
  362. continue;
  363. SATDEV::STDevice stDevice;
  364. // 设备类型;
  365. if ( it->find(":5555") != std::string::npos )
  366. stDevice.nType = SATDEV::Reticle;
  367. else if ( it->find(":5555") == std::string::npos )
  368. stDevice.nType = SATDEV::Usb;
  369. stDevice.ulOfflineTime = 0;
  370. // 设备状态;
  371. if ( (npos = it->find(" device")) != std::string::npos )
  372. stDevice.nStatus = SATDEV::Online;
  373. else if ( (npos = it->find(" offline")) != std::string::npos )
  374. stDevice.nStatus = SATDEV::Offline;
  375. else if ( (npos = it->find("unauthorized")) != std::string::npos ) // 未认证也做为离线的一种;
  376. {
  377. stDevice.nStatus = SATDEV::Unauthorized;
  378. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV,"找到unauthorized状态的设备");
  379. }
  380. // 获取设备名;
  381. stDevice.strName = it->substr(0, npos);
  382. // 如果是网络,去掉5555;
  383. if ( stDevice.nType == SATDEV::Reticle ) {
  384. npos = stDevice.strName.find(":5555");
  385. stDevice.strName = stDevice.strName.substr(0, npos);
  386. }
  387. // 新设备否(一般用于usb设备)
  388. IsNewDevices(stDevice);
  389. // 压入容器保存;
  390. vtDevices.push_back(stDevice);
  391. }
  392. }
  393. void CSATDevices::ReConnectAllDevices()
  394. {
  395. std::string str;
  396. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  397. for ( ;it != s_vtDevices.end(); it++ ) {
  398. if ( it->nType == SATDEV::Reticle ) {
  399. str = "adb connect ";
  400. str.append(it->strName);
  401. WinExec(str.c_str(), SW_HIDE);
  402. }
  403. }
  404. }
  405. void CSATDevices::SetDeviceUsageStatus(std::string strDevName, SATDEV::DEVICE_USAGE_STATUS status)
  406. {
  407. AutoThreadSection ats(&s_ThreadSection);
  408. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  409. for ( ;it != s_vtDevices.end(); it++ ) {
  410. if ( _tcsicmp(it->strName.c_str(), strDevName.c_str() ) == 0) {
  411. it->nUsageState = status;
  412. break;
  413. }
  414. }
  415. }
  416. int CSATDevices::AttachDeviceName2Buffer(SATPROTO::Device (&pbuff)[SATPROTO::MAX_DEVS])
  417. {
  418. int count = 0;
  419. if ( pbuff ) {
  420. std::string str;
  421. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  422. for ( ;it != s_vtDevices.end(); it++ ) {
  423. pbuff[count].nType = it->nType;
  424. pbuff[count].nStatus = it->nStatus;
  425. pbuff[count].nUsageState = it->nUsageState;
  426. memcpy_s(pbuff[count++].szName, MAX_PATH, it->strName.c_str(), it->strName.size());
  427. // 超过MAX_DEVS退出;
  428. if ( count == SATPROTO::MAX_DEVS )
  429. break;
  430. }
  431. }
  432. return count;
  433. }
  434. void CSATDevices::SaveDeviceStatus2Config(SATDEV::STDevice &stDevice)
  435. {
  436. if ( stDevice.nStatus == SATDEV::Online )
  437. WritePrivateProfileString(_T("ADBSTATUS"), stDevice.strName.c_str(), _T("Online"), GLOBAL::g_szPython27ServerConfig);
  438. else if ( stDevice.nStatus == SATDEV::Offline )
  439. WritePrivateProfileString(_T("ADBSTATUS"), stDevice.strName.c_str(), _T("Offline"), GLOBAL::g_szPython27ServerConfig);
  440. else if ( stDevice.nStatus == SATDEV::Dropline )
  441. WritePrivateProfileString(_T("ADBSTATUS"), stDevice.strName.c_str(), _T("Dropline"), GLOBAL::g_szPython27ServerConfig);
  442. else if ( stDevice.nStatus == SATDEV::Unauthorized)
  443. WritePrivateProfileString(_T("ADBSTATUS"), stDevice.strName.c_str(), _T("Unauthorized"), GLOBAL::g_szPython27ServerConfig);
  444. }
  445. void CSATDevices::SaveAllDevicesStatus2Config()
  446. {
  447. // 先清空段内容;
  448. WritePrivateProfileString(_T("ADBSTATUS"), NULL, NULL, GLOBAL::g_szPython27ServerConfig);
  449. std::vector<SATDEV::STDevice>::iterator it = s_vtDevices.begin();
  450. for ( ; it != s_vtDevices.end(); it++ ) {
  451. if ( it->nType != SATDEV::Virtual )
  452. SaveDeviceStatus2Config(*it);
  453. }
  454. }
  455. SOCKET CSATDevices::ConnectAdbDevice(std::string ip, int port)
  456. {
  457. // 创建套接字;
  458. SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
  459. if( sock == INVALID_SOCKET )
  460. return INVALID_SOCKET;
  461. // 设置为非阻塞;
  462. DWORD ul = 1;
  463. if( 0 != ioctlsocket( sock, FIONBIO, &ul) )
  464. return INVALID_SOCKET;
  465. // 连接服务器;
  466. SOCKADDR_IN sin;
  467. sin.sin_family = AF_INET;
  468. sin.sin_port = htons(port);
  469. sin.sin_addr.s_addr = inet_addr( ip.c_str() );
  470. if( ! ( SOCKET_ERROR == connect( sock, (SOCKADDR *)&sin, sizeof(sin)) && WSAGetLastError() == WSAEWOULDBLOCK ) )
  471. return INVALID_SOCKET;
  472. return sock;
  473. }
  474. bool CSATDevices::CheckAdbSocket(SOCKET sock)
  475. {
  476. // 由于异步connect直接返回;
  477. // 需要用select另外判断;
  478. fd_set fs_read;
  479. FD_ZERO( &fs_read );
  480. FD_SET( sock, &fs_read );
  481. fd_set fs_write;
  482. fs_write.fd_count = 1;
  483. fs_write.fd_array[0] = sock;
  484. fd_set fs_error;
  485. fs_error.fd_count = 1;
  486. fs_error.fd_array[0] = sock;
  487. timeval tv;
  488. tv.tv_sec = GLOBAL::g_stSATConfig.ulAsynConnectTimeout/1000000; // 秒;
  489. tv.tv_usec = GLOBAL::g_stSATConfig.ulAsynConnectTimeout%1000000; // 微秒(百万分之一秒);
  490. // 必须要设置超时值,否则默认值
  491. int ret = select( 0, &fs_read, &fs_write, &fs_error, &tv );
  492. if( ret == SOCKET_ERROR )
  493. return false;
  494. // 判断socket句柄是否可写;
  495. if( !FD_ISSET( sock, &fs_write ) )
  496. return false;
  497. int optval = -1;
  498. int optlen = sizeof(optval);
  499. ret = getsockopt( sock, SOL_SOCKET,SO_ERROR, (char*)(&optval), &optlen );
  500. if( ret != 0 || optval != 0)
  501. return false;
  502. return true;
  503. }
  504. void CSATDevices::CloseAdbSocket(SOCKET sock)
  505. {
  506. shutdown(sock, SD_BOTH);
  507. closesocket(sock);
  508. }
  509. DWORD CSATDevices::AddAdbThread(LPVOID lpVoid)
  510. {
  511. s_bAutoAddAdbThreadFinished = false;
  512. int a = 0, b = 0, c = 0, d = 0;
  513. sscanf_s(GLOBAL::g_stSATConfig.szAdbRouteAddress, _T("%d.%d.%d.%d"), &a, &b, &c, &d);
  514. char szIPAddress[MAX_PATH] = {0};
  515. for ( int i = d+1; i < 255; i++ )
  516. {
  517. sprintf_s(szIPAddress, "%d.%d.%d.%d", a,b,c,i);
  518. // TODO: 在此处为应用程序的行为编写代码。
  519. SOCKET sock = ConnectAdbDevice(szIPAddress, 5555);
  520. if ( sock != INVALID_SOCKET ) {
  521. if ( CheckAdbSocket(sock) ) {
  522. AddReticleDevices(szIPAddress);
  523. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV,"连接IP=%s:5555成功", szIPAddress);
  524. }
  525. else
  526. GLOBAL::WriteTextLog(GLOBAL::SAT_DEV,"连接IP=%s:5555失败", szIPAddress);
  527. CloseAdbSocket(sock);
  528. }
  529. }
  530. s_bAutoAddAdbThreadFinished = true;
  531. return 0;
  532. }