SATDevices.cpp 17 KB

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