bus.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. #include "precompile.h"
  2. #include "bus.h"
  3. #include "sockutil.h"
  4. #include "url.h"
  5. #include "spinlock.h"
  6. #include "list.h"
  7. #include "bus_internal.h"
  8. #include "dbgutil.h"
  9. #include <winpr/file.h>
  10. #include <winpr/pipe.h>
  11. #include <winpr/synch.h>
  12. #include <winpr/string.h>
  13. #include <winpr/wlog.h>
  14. #define TAG TOOLKIT_TAG("bus")
  15. #include "memutil.h"
  16. #define BUS_RESULT_DATA 1 // ==BUS_TYPE_PACKET, callback: callback.on_pkt, no use
  17. #define BUS_RESULT_INFO 2 // ==BUS_TYPE_INFO, callback: callback.on_inf
  18. #define BUS_RESULT_EVENT 3 // ==BUS_TYPE_EVENT, callback: callback.on_evt , no use
  19. #define BUS_RESULT_SYSTEM 4 // ==BUS_TYPE_SYSTEM, callback: callback.on_sys
  20. #define BUS_RESULT_MSG 5 // send package msg, callback: callback.on_msg
  21. #define BUS_RESULT_UNKNOWN 6
  22. typedef struct msg_t {
  23. struct list_head entry;
  24. int type;
  25. int nparam;
  26. int* params;
  27. HANDLE evt;
  28. int evt_result;
  29. }msg_t;
  30. struct bus_endpt_t {
  31. int type;
  32. int epid;
  33. union {
  34. HANDLE pipe_handle;
  35. SOCKET sock_handle;
  36. };
  37. char* url;
  38. bus_endpt_callback callback;
  39. struct list_head msg_list;
  40. spinlock_t msg_lock;
  41. HANDLE msg_sem;
  42. HANDLE tx_evt;
  43. HANDLE rx_evt;
  44. OVERLAPPED rx_overlapped;
  45. int rx_pending;
  46. int rx_pending_pkt_len;
  47. iobuffer_queue_t* rx_buf_queue;
  48. volatile int quit_flag;
  49. };
  50. static void free_msg(msg_t* msg)
  51. {
  52. free(msg->params);
  53. free(msg);
  54. }
  55. static int to_result(int pkt_type)
  56. {
  57. switch (pkt_type) {
  58. case BUS_TYPE_EVENT:
  59. return BUS_RESULT_EVENT;
  60. case BUS_TYPE_SYSTEM:
  61. return BUS_RESULT_SYSTEM;
  62. case BUS_TYPE_PACKET:
  63. return BUS_RESULT_DATA;
  64. case BUS_TYPE_INFO:
  65. return BUS_RESULT_INFO;
  66. default:
  67. break;
  68. }
  69. return BUS_RESULT_UNKNOWN;
  70. }
  71. static HANDLE create_pipe_handle(const char* name)
  72. {
  73. char tmp[MAX_PATH];
  74. HANDLE pipe = INVALID_HANDLE_VALUE;
  75. sprintf(tmp, "\\\\.\\pipe\\%s", name);
  76. for (;;) {
  77. pipe = CreateFileA(tmp,
  78. GENERIC_READ | GENERIC_WRITE,
  79. 0,
  80. NULL,
  81. OPEN_EXISTING,
  82. FILE_FLAG_OVERLAPPED,
  83. NULL);
  84. if (pipe == INVALID_HANDLE_VALUE) {
  85. if (GetLastError() == ERROR_PIPE_BUSY) {
  86. if (WaitNamedPipeA(name, 20000))
  87. continue;
  88. }
  89. }
  90. break;
  91. }
  92. return pipe;
  93. }
  94. static SOCKET create_socket_handle(const char* ip, int port)
  95. {
  96. SOCKET fd;
  97. struct sockaddr_in addr;
  98. fd = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
  99. if (fd == INVALID_SOCKET)
  100. return fd;
  101. {
  102. BOOL f = TRUE;
  103. u_long l = TRUE;
  104. setsockopt(fd, SOL_SOCKET, SO_DONTLINGER, (char*)&f, sizeof(f));
  105. //setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&f, sizeof(f));
  106. _ioctlsocket(fd, FIONBIO, &l);
  107. }
  108. memset(&addr, 0, sizeof(addr));
  109. addr.sin_family = AF_INET;
  110. addr.sin_port = htons(port);
  111. addr.sin_addr.s_addr = inet_addr(ip);
  112. if (connect(fd, (struct sockaddr*) & addr, sizeof(addr)) != 0) {
  113. if (WSAGetLastError() == WSAEWOULDBLOCK) {
  114. fd_set wr_set, ex_set;
  115. FD_ZERO(&wr_set);
  116. FD_ZERO(&ex_set);
  117. FD_SET(fd, &wr_set);
  118. FD_SET(fd, &ex_set);
  119. if (select(fd, NULL, &wr_set, &ex_set, NULL) > 0 && FD_ISSET(fd, &wr_set))
  120. return fd;
  121. }
  122. }
  123. if (fd != INVALID_SOCKET)
  124. closesocket(fd);
  125. //return INVALID_SOCKET;//{bug}
  126. return fd;
  127. }
  128. static int tcp_send_buf(bus_endpt_t* endpt, const char* buf, int n)
  129. {
  130. DWORD left = n;
  131. DWORD offset = 0;
  132. while (left > 0) {
  133. BOOL ret;
  134. WSABUF wsabuf;
  135. DWORD dwBytesTransfer;
  136. OVERLAPPED overlapped;
  137. memset(&overlapped, 0, sizeof(overlapped));
  138. overlapped.hEvent = endpt->tx_evt;
  139. ResetEvent(endpt->tx_evt);
  140. wsabuf.buf = (char*)buf + offset;
  141. wsabuf.len = left;
  142. ret = WSASend(endpt->sock_handle, &wsabuf, 1, &dwBytesTransfer, 0, &overlapped, NULL) == 0;
  143. if (!ret && WSAGetLastError() == WSA_IO_PENDING) {
  144. DWORD dwFlags = 0;
  145. ret = WSAGetOverlappedResult(endpt->sock_handle, &overlapped, &dwBytesTransfer, TRUE, &dwFlags);
  146. }
  147. if (ret && dwBytesTransfer) {
  148. offset += dwBytesTransfer;
  149. left -= dwBytesTransfer;
  150. }
  151. else {
  152. return -1;
  153. }
  154. }
  155. return 0;
  156. }
  157. static int pipe_send_buf(bus_endpt_t* endpt, const char* buf, int n)
  158. {
  159. DWORD left = n;
  160. DWORD offset = 0;
  161. while (left > 0) {
  162. BOOL ret;
  163. DWORD dwBytesTransfer;
  164. OVERLAPPED overlapped;
  165. memset(&overlapped, 0, sizeof(overlapped));
  166. overlapped.hEvent = endpt->tx_evt;
  167. ResetEvent(endpt->tx_evt);
  168. ret = WriteFile(endpt->pipe_handle, buf + offset, left, &dwBytesTransfer, &overlapped);
  169. if (!ret && GetLastError() == ERROR_IO_PENDING) {
  170. ret = GetOverlappedResult(endpt->pipe_handle, &overlapped, &dwBytesTransfer, TRUE);
  171. }
  172. if (ret && dwBytesTransfer) {
  173. offset += dwBytesTransfer;
  174. left -= dwBytesTransfer;
  175. }
  176. else {
  177. return -1;
  178. }
  179. }
  180. return 0;
  181. }
  182. static int send_buf(bus_endpt_t* endpt, const char* buf, int n)
  183. {
  184. if (endpt->type == TYPE_PIPE) {
  185. return pipe_send_buf(endpt, buf, n);
  186. }
  187. else if (endpt->type == TYPE_TCP) {
  188. return tcp_send_buf(endpt, buf, n);
  189. }
  190. else {
  191. return -1;
  192. }
  193. }
  194. static int send_pkt_raw(bus_endpt_t* endpt, iobuffer_t* pkt)
  195. {
  196. int pkt_len = iobuffer_get_length(pkt);
  197. int rc;
  198. iobuffer_write_head(pkt, IOBUF_T_I4, &pkt_len, 0);
  199. rc = send_buf(endpt, iobuffer_data(pkt, 0), iobuffer_get_length(pkt));
  200. return rc;
  201. }
  202. static int pipe_recv_buf(bus_endpt_t* endpt, char* buf, DWORD n)
  203. {
  204. DWORD left = n;
  205. DWORD offset = 0;
  206. while (left > 0) {
  207. BOOL ret;
  208. DWORD dwBytesTransfer;
  209. OVERLAPPED overlapped;
  210. memset(&overlapped, 0, sizeof(overlapped));
  211. overlapped.hEvent = endpt->rx_evt;
  212. ResetEvent(overlapped.hEvent);
  213. ret = ReadFile(endpt->pipe_handle, buf + offset, left, &dwBytesTransfer, &overlapped);
  214. if (!ret && GetLastError() == ERROR_IO_PENDING) {
  215. ret = GetOverlappedResult(endpt->pipe_handle, &overlapped, &dwBytesTransfer, TRUE);
  216. }
  217. if (ret && dwBytesTransfer) {
  218. offset += dwBytesTransfer;
  219. left -= dwBytesTransfer;
  220. }
  221. else {
  222. return -1;
  223. }
  224. }
  225. return 0;
  226. }
  227. static int tcp_recv_buf(bus_endpt_t* endpt, char* buf, DWORD n)
  228. {
  229. DWORD left = n;
  230. DWORD offset = 0;
  231. while (left > 0) {
  232. BOOL ret;
  233. DWORD dwFlags = 0;
  234. WSABUF wsabuf;
  235. DWORD dwBytesTransfer;
  236. OVERLAPPED overlapped;
  237. memset(&overlapped, 0, sizeof(overlapped));
  238. overlapped.hEvent = endpt->rx_evt;
  239. wsabuf.buf = buf + offset;
  240. wsabuf.len = left;
  241. ResetEvent(overlapped.hEvent);
  242. ret = WSARecv(endpt->sock_handle, &wsabuf, 1, &dwBytesTransfer, &dwFlags, &overlapped, NULL) == 0;
  243. if (!ret && WSAGetLastError() == WSA_IO_PENDING) {
  244. ret = WSAGetOverlappedResult(endpt->sock_handle, &overlapped, &dwBytesTransfer, TRUE, &dwFlags);
  245. }
  246. if (ret && dwBytesTransfer) {
  247. offset += dwBytesTransfer;
  248. left -= dwBytesTransfer;
  249. }
  250. else {
  251. return -1;
  252. }
  253. }
  254. return 0;
  255. }
  256. static int recv_buf(bus_endpt_t* endpt, char* buf, DWORD n)
  257. {
  258. if (endpt->type == TYPE_PIPE) {
  259. return pipe_recv_buf(endpt, buf, n);
  260. }
  261. else if (endpt->type == TYPE_TCP) {
  262. return tcp_recv_buf(endpt, buf, n);
  263. }
  264. else {
  265. return -1;
  266. }
  267. }
  268. static int recv_pkt_raw(bus_endpt_t* endpt, iobuffer_t** pkt)
  269. {
  270. int pkt_len;
  271. int rc = -1;
  272. rc = recv_buf(endpt, (char*)&pkt_len, 4);
  273. if (rc != 0)
  274. return rc;
  275. *pkt = iobuffer_create(-1, pkt_len);
  276. iobuffer_push_count(*pkt, pkt_len);
  277. if (pkt_len > 0) {
  278. rc = recv_buf(endpt, iobuffer_data(*pkt, 0), pkt_len);
  279. }
  280. if (rc < 0) {
  281. iobuffer_destroy(*pkt);
  282. *pkt = NULL;
  283. }
  284. return rc;
  285. }
  286. static int start_read_pkt(bus_endpt_t* endpt, iobuffer_t** p_pkt)
  287. {
  288. DWORD dwBytesTransferred;
  289. BOOL ret;
  290. int rc = 0;
  291. iobuffer_t* pkt = NULL;
  292. *p_pkt = NULL;
  293. ResetEvent(endpt->rx_evt);
  294. memset(&endpt->rx_overlapped, 0, sizeof(OVERLAPPED));
  295. endpt->rx_overlapped.hEvent = endpt->rx_evt;
  296. if (endpt->type == TYPE_PIPE) {
  297. ret = ReadFile(endpt->pipe_handle,
  298. &endpt->rx_pending_pkt_len,
  299. 4,
  300. &dwBytesTransferred,
  301. &endpt->rx_overlapped);
  302. }
  303. else if (endpt->type == TYPE_TCP) {
  304. DWORD dwFlags = 0;
  305. WSABUF wsabuf;
  306. wsabuf.buf = (char*)&endpt->rx_pending_pkt_len;
  307. wsabuf.len = 4;
  308. ret = WSARecv(endpt->sock_handle,
  309. &wsabuf,
  310. 1,
  311. &dwBytesTransferred,
  312. &dwFlags,
  313. &endpt->rx_overlapped,
  314. NULL) == 0;
  315. }
  316. else {
  317. return -1;
  318. }
  319. if (ret) {
  320. if (dwBytesTransferred == 0)
  321. return -1;
  322. if (dwBytesTransferred < 4) {
  323. rc = recv_buf(endpt, (char*)&endpt->rx_pending_pkt_len + dwBytesTransferred, 4 - dwBytesTransferred);
  324. if (rc < 0)
  325. return rc;
  326. }
  327. pkt = iobuffer_create(0, endpt->rx_pending_pkt_len);
  328. if (endpt->rx_pending_pkt_len > 0) {
  329. rc = recv_buf(endpt, iobuffer_data(pkt, 0), endpt->rx_pending_pkt_len);
  330. if (rc < 0) {
  331. iobuffer_destroy(pkt);
  332. return rc;
  333. }
  334. iobuffer_push_count(pkt, endpt->rx_pending_pkt_len);
  335. }
  336. *p_pkt = pkt;
  337. }
  338. else {
  339. if (WSAGetLastError() == WSA_IO_PENDING) {
  340. endpt->rx_pending = 1;
  341. }
  342. else {
  343. return -1;
  344. }
  345. }
  346. return 0;
  347. }
  348. static int read_left_pkt(bus_endpt_t* endpt, iobuffer_t** p_pkt)
  349. {
  350. BOOL ret = 0;
  351. int rc;
  352. DWORD dwBytesTransferred = 0;
  353. iobuffer_t* pkt = NULL;
  354. if (endpt->type == TYPE_PIPE) {
  355. ret = GetOverlappedResult(endpt->pipe_handle, &endpt->rx_overlapped, &dwBytesTransferred, TRUE);
  356. }
  357. else if (endpt->type == TYPE_TCP) {
  358. DWORD dwFlags = 0;
  359. ret = WSAGetOverlappedResult(endpt->sock_handle, &endpt->rx_overlapped, &dwBytesTransferred, TRUE, &dwFlags);
  360. }
  361. else {
  362. TOOLKIT_ASSERT(0);
  363. }
  364. if (!ret || dwBytesTransferred == 0) {
  365. WLog_ERR(TAG, "(WSA)GetOverlappedResult failed: %d", GetLastError());
  366. return -1;
  367. }
  368. if (dwBytesTransferred < 4) {
  369. rc = recv_buf(endpt, (char*)&endpt->rx_pending_pkt_len + dwBytesTransferred, 4 - dwBytesTransferred);
  370. if (rc < 0) {
  371. WLog_ERR(TAG, "recv buf failed.");
  372. return rc;
  373. }
  374. }
  375. pkt = iobuffer_create(-1, endpt->rx_pending_pkt_len);
  376. rc = recv_buf(endpt, iobuffer_data(pkt, 0), endpt->rx_pending_pkt_len);
  377. if (rc < 0) {
  378. WLog_ERR(TAG, "recv buf failed and destroy buffer.");
  379. iobuffer_destroy(pkt);
  380. return rc;
  381. }
  382. iobuffer_push_count(pkt, endpt->rx_pending_pkt_len);
  383. *p_pkt = pkt;
  384. endpt->rx_pending = 0;
  385. return 0;
  386. }
  387. static int append_rx_pkt(bus_endpt_t* endpt, iobuffer_t* pkt)
  388. {
  389. int type;
  390. int read_state;
  391. read_state = iobuffer_get_read_state(pkt);
  392. iobuffer_read(pkt, IOBUF_T_I4, &type, 0);
  393. iobuffer_restore_read_state(pkt, read_state);
  394. if (type == BUS_TYPE_PACKET || type == BUS_TYPE_INFO || type == BUS_TYPE_EVENT || type == BUS_TYPE_SYSTEM) {
  395. iobuffer_queue_enqueue(endpt->rx_buf_queue, pkt);
  396. return 1;
  397. }
  398. else {
  399. return -1;
  400. }
  401. }
  402. TOOLKIT_API int bus_endpt_create(const char* url, int epid, const bus_endpt_callback* callback, bus_endpt_t** p_endpt)
  403. {
  404. bus_endpt_t* endpt = NULL;
  405. char* tmp_url;
  406. url_fields uf;
  407. int rc;
  408. int v;
  409. iobuffer_t* buf = NULL;
  410. iobuffer_t* ans_buf = NULL;
  411. if (!url)
  412. return -1;
  413. tmp_url = _strdup(url);
  414. if (url_parse(tmp_url, &uf) < 0) {
  415. free(tmp_url);
  416. return -1;
  417. }
  418. endpt = ZALLOC_T(bus_endpt_t);
  419. endpt->sock_handle = -1;
  420. endpt->url = tmp_url;
  421. if (_stricmp(uf.scheme, "tcp") == 0) {
  422. endpt->type = TYPE_TCP;
  423. endpt->sock_handle = create_socket_handle(uf.host, uf.port);
  424. if (endpt->sock_handle == INVALID_SOCKET)
  425. goto on_error;
  426. }
  427. else if (_stricmp(uf.scheme, "pipe") == 0) {
  428. endpt->type = TYPE_PIPE;
  429. endpt->pipe_handle = create_pipe_handle(uf.host);
  430. if (endpt->pipe_handle == INVALID_HANDLE_VALUE)
  431. goto on_error;
  432. }
  433. else {
  434. goto on_error;
  435. }
  436. endpt->epid = epid;
  437. endpt->tx_evt = CreateEventA(NULL, TRUE, FALSE, NULL);
  438. endpt->rx_evt = CreateEventA(NULL, TRUE, FALSE, NULL);
  439. endpt->rx_buf_queue = iobuffer_queue_create();
  440. endpt->msg_sem = CreateSemaphoreA(NULL, 0, 0x7fffffff, NULL);
  441. INIT_LIST_HEAD(&endpt->msg_list);
  442. spinlock_init(&endpt->msg_lock);
  443. memcpy(&endpt->callback, callback, sizeof(bus_endpt_callback));
  444. buf = iobuffer_create(-1, -1);
  445. v = BUS_TYPE_ENDPT_REGISTER;
  446. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  447. v = endpt->epid;
  448. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  449. rc = send_pkt_raw(endpt, buf);
  450. if (rc != 0)
  451. goto on_error;
  452. rc = recv_pkt_raw(endpt, &ans_buf);
  453. if (rc != 0) {
  454. goto on_error;
  455. }
  456. iobuffer_read(ans_buf, IOBUF_T_I4, &v, 0);
  457. iobuffer_read(ans_buf, IOBUF_T_I4, &rc, 0);
  458. if (rc != 0)
  459. goto on_error;
  460. url_free_fields(&uf);
  461. if (buf)
  462. iobuffer_destroy(buf);
  463. if (ans_buf)
  464. iobuffer_destroy(ans_buf);
  465. *p_endpt = endpt;
  466. return 0;
  467. on_error:
  468. if (endpt->type == TYPE_TCP) {
  469. closesocket(endpt->sock_handle);
  470. }
  471. else if (endpt->type == TYPE_PIPE) {
  472. CloseHandle(endpt->pipe_handle);
  473. }
  474. if (endpt->msg_sem)
  475. CloseHandle(endpt->msg_sem);
  476. if (endpt->tx_evt)
  477. CloseHandle(endpt->tx_evt);
  478. if (endpt->rx_evt)
  479. CloseHandle(endpt->rx_evt);
  480. if (endpt->rx_buf_queue)
  481. iobuffer_queue_destroy(endpt->rx_buf_queue);
  482. if (endpt->url)
  483. free(endpt->url);
  484. free(endpt);
  485. url_free_fields(&uf);
  486. if (buf)
  487. iobuffer_destroy(buf);
  488. if (ans_buf)
  489. iobuffer_destroy(ans_buf);
  490. return -1;
  491. }
  492. TOOLKIT_API void bus_endpt_destroy(bus_endpt_t* endpt)
  493. {
  494. int rc = -1;
  495. iobuffer_t* buf = NULL;
  496. iobuffer_t* ans_buf = NULL;
  497. int v;
  498. TOOLKIT_ASSERT(endpt);
  499. buf = iobuffer_create(-1, -1);
  500. v = BUS_TYPE_ENDPT_UNREGISTER;
  501. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  502. v = endpt->epid;
  503. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  504. rc = send_pkt_raw(endpt, buf);
  505. if (rc != 0)
  506. goto on_error;
  507. rc = recv_pkt_raw(endpt, &ans_buf);
  508. if (rc != 0)
  509. goto on_error;
  510. iobuffer_read(ans_buf, IOBUF_T_I4, &v, 0);
  511. iobuffer_read(ans_buf, IOBUF_T_I4, &rc, 0);
  512. on_error:
  513. /** try to release memory as possible as i can [5/22/2020 Gifur] */
  514. spinlock_enter(&endpt->msg_lock, -1);
  515. if (!list_empty(&endpt->msg_list))
  516. {
  517. msg_t* msg, * t;
  518. list_for_each_entry_safe(msg, t, &endpt->msg_list, msg_t, entry) {
  519. list_del(&msg->entry);
  520. if (msg->evt)
  521. CloseHandle(msg->evt);
  522. if (msg->type == 2/*IOM_T_SEND_INFO*/) {
  523. iobuffer_t* pkt = (iobuffer_t*)msg->params[5];
  524. if (pkt)
  525. iobuffer_dec_ref(pkt);
  526. }
  527. free_msg(msg);
  528. }
  529. }
  530. spinlock_leave(&endpt->msg_lock);
  531. if (buf)
  532. iobuffer_destroy(buf);
  533. if (ans_buf)
  534. iobuffer_destroy(ans_buf);
  535. if (endpt->type == TYPE_TCP) {
  536. closesocket(endpt->sock_handle);
  537. }
  538. else if (endpt->type == TYPE_PIPE) {
  539. CloseHandle(endpt->pipe_handle);
  540. }
  541. if (endpt->msg_sem)
  542. CloseHandle(endpt->msg_sem);
  543. if (endpt->tx_evt)
  544. CloseHandle(endpt->tx_evt);
  545. if (endpt->rx_evt)
  546. CloseHandle(endpt->rx_evt);
  547. if (endpt->rx_buf_queue)
  548. iobuffer_queue_destroy(endpt->rx_buf_queue);
  549. if (endpt->url)
  550. free(endpt->url);
  551. free(endpt);
  552. }
  553. // 1 : recv ok
  554. // 0 : time out
  555. // <0 : error
  556. static int bus_endpt_poll_internal(bus_endpt_t* endpt, int* result, int timeout)
  557. {
  558. iobuffer_t* pkt = NULL;
  559. int rc;
  560. BOOL ret;
  561. TOOLKIT_ASSERT(endpt);
  562. // peek first packge type
  563. if (iobuffer_queue_count(endpt->rx_buf_queue) > 0) {
  564. iobuffer_t* pkt = iobuffer_queue_head(endpt->rx_buf_queue);
  565. int pkt_type;
  566. int read_state = iobuffer_get_read_state(pkt);
  567. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, NULL);
  568. iobuffer_restore_read_state(pkt, read_state);
  569. *result = to_result(pkt_type);
  570. if (*result == BUS_RESULT_UNKNOWN) {
  571. WLog_ERR(TAG, "bug: unknown pkt type!");
  572. return -1;
  573. }
  574. return 1;
  575. }
  576. // no received package, try to receive one
  577. if (!endpt->rx_pending) {
  578. rc = start_read_pkt(endpt, &pkt);
  579. if (rc < 0)
  580. return rc;
  581. if (pkt) {
  582. WLog_DBG(TAG, "pkt has read");
  583. rc = append_rx_pkt(endpt, pkt);
  584. if (rc < 0) {
  585. iobuffer_destroy(pkt);
  586. return -1;
  587. }
  588. }
  589. else {
  590. WLog_DBG(TAG, "pending");
  591. }
  592. }
  593. // if receive is pending, wait for send or receive complete event
  594. if (!pkt) {
  595. HANDLE hs[] = { endpt->msg_sem, endpt->rx_evt };
  596. ret = WaitForMultipleObjects(array_size(hs), &hs[0], FALSE, (DWORD)timeout);
  597. if (ret == WAIT_TIMEOUT) {
  598. return 0;
  599. }
  600. else if (ret == WAIT_OBJECT_0) {
  601. *result = BUS_RESULT_MSG; // indicate send package event
  602. return 1;
  603. }
  604. else if (ret == WAIT_OBJECT_0 + 1) {
  605. rc = read_left_pkt(endpt, &pkt);
  606. if (rc < 0) {
  607. WLog_ERR(TAG, "read_left_pkt failed: %d", rc);
  608. return rc;
  609. }
  610. if (pkt) {
  611. rc = append_rx_pkt(endpt, pkt);
  612. if (rc < 0) {
  613. iobuffer_destroy(pkt);
  614. WLog_ERR(TAG, "append_rx_pkt failed: %d", rc);
  615. return -1;
  616. }
  617. }
  618. }
  619. }
  620. else {
  621. WLog_ERR(TAG, "pkt has readed");
  622. }
  623. if (pkt) {
  624. int type;
  625. int read_state = iobuffer_get_read_state(pkt);
  626. iobuffer_read(pkt, IOBUF_T_I4, &type, 0);
  627. iobuffer_restore_read_state(pkt, read_state);
  628. *result = to_result(type);
  629. if (*result == BUS_RESULT_UNKNOWN) {
  630. WLog_ERR(TAG, "bug: unknown pkt type!");
  631. return -1;
  632. }
  633. }
  634. else {
  635. WLog_ERR(TAG, "not pkt");
  636. return -1;
  637. }
  638. return 1;
  639. }
  640. static int recv_until(bus_endpt_t* endpt, int type, iobuffer_t** p_ansbuf)
  641. {
  642. int rc;
  643. iobuffer_t* ans_pkt = NULL;
  644. int ans_type;
  645. for (;;) {
  646. if (!endpt->rx_pending) {
  647. rc = start_read_pkt(endpt, &ans_pkt);
  648. if (rc < 0) {
  649. break;
  650. }
  651. }
  652. if (!ans_pkt) {
  653. DWORD ret = WaitForSingleObject(endpt->rx_evt, INFINITE);
  654. if (ret != WAIT_OBJECT_0)
  655. return -1;
  656. rc = read_left_pkt(endpt, &ans_pkt);
  657. if (rc < 0)
  658. break;
  659. }
  660. if (ans_pkt) {
  661. int read_state = iobuffer_get_read_state(ans_pkt);
  662. iobuffer_read(ans_pkt, IOBUF_T_I4, &ans_type, 0);
  663. iobuffer_restore_read_state(ans_pkt, read_state);
  664. if (ans_type == type) {
  665. *p_ansbuf = ans_pkt;
  666. break;
  667. }
  668. else {
  669. rc = append_rx_pkt(endpt, ans_pkt);
  670. if (rc < 0) {
  671. iobuffer_destroy(ans_pkt);
  672. break;
  673. }
  674. else {
  675. ans_pkt = NULL;
  676. }
  677. }
  678. }
  679. }
  680. return rc;
  681. }
  682. static int recv_until_result(bus_endpt_t* endpt, int* p_result)
  683. {
  684. int rc;
  685. iobuffer_t* ans_pkt = NULL;
  686. int type, error;
  687. rc = recv_until(endpt, BUS_TYPE_ERROR, &ans_pkt);
  688. if (rc < 0)
  689. return rc;
  690. iobuffer_read(ans_pkt, IOBUF_T_I4, &type, 0);
  691. iobuffer_read(ans_pkt, IOBUF_T_I4, &error, 0);
  692. iobuffer_destroy(ans_pkt);
  693. *p_result = error;
  694. return rc;
  695. }
  696. static int recv_until_state(bus_endpt_t* endpt, int* p_state)
  697. {
  698. int rc;
  699. iobuffer_t* ans_pkt = NULL;
  700. int type, epid, state;
  701. rc = recv_until(endpt, BUS_TYPE_ENDPT_GET_STATE, &ans_pkt);
  702. if (rc < 0)
  703. return rc;
  704. iobuffer_read(ans_pkt, IOBUF_T_I4, &type, 0);
  705. iobuffer_read(ans_pkt, IOBUF_T_I4, &epid, 0);
  706. iobuffer_read(ans_pkt, IOBUF_T_I4, &state, 0);
  707. iobuffer_destroy(ans_pkt);
  708. *p_state = state;
  709. return rc;
  710. }
  711. TOOLKIT_API int bus_endpt_send_pkt(bus_endpt_t* endpt, int epid, int type, iobuffer_t* pkt)
  712. {
  713. int t;
  714. int rc;
  715. int read_state;
  716. int write_state;
  717. int error;
  718. char bussinessId[LINKINFO_BUSSID_LEN];
  719. char traceId[LINKINFO_TRACEID_LEN];
  720. char spanId[LINKINFO_SPANID_LEN];
  721. char parentSpanId[LINKINFO_PARENTSPANID_LEN];
  722. TOOLKIT_ASSERT(endpt);
  723. read_state = iobuffer_get_read_state(pkt);
  724. write_state = iobuffer_get_write_state(pkt);
  725. /*
  726. t = iobuffer_get_linkId(pkt);
  727. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  728. */
  729. iobuffer_get_linkInfo(pkt, bussinessId, traceId, spanId, parentSpanId);
  730. iobuffer_write_head(pkt, IOBUF_T_BUF, parentSpanId, sizeof(parentSpanId));
  731. iobuffer_write_head(pkt, IOBUF_T_BUF, spanId, sizeof(spanId));
  732. iobuffer_write_head(pkt, IOBUF_T_BUF, traceId, sizeof(traceId));
  733. iobuffer_write_head(pkt, IOBUF_T_BUF, bussinessId, sizeof(bussinessId));
  734. t = epid; // remote epid
  735. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  736. t = endpt->epid; // local epid
  737. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  738. t = type; // user type
  739. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  740. t = BUS_TYPE_PACKET; // type
  741. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  742. rc = send_pkt_raw(endpt, pkt);
  743. iobuffer_restore_read_state(pkt, read_state);
  744. iobuffer_restore_write_state(pkt, write_state);
  745. if (rc < 0)
  746. return rc;
  747. rc = recv_until_result(endpt, &error);
  748. if (rc == 0 && error != 0)
  749. rc = error;
  750. return rc;
  751. }
  752. TOOLKIT_API int bus_endpt_send_info(bus_endpt_t* endpt, int epid, int type, iobuffer_t* pkt)
  753. {
  754. int t;
  755. int rc;
  756. int read_state;
  757. int write_state;
  758. char bussinessId[LINKINFO_BUSSID_LEN];
  759. char traceId[LINKINFO_TRACEID_LEN];
  760. char spanId[LINKINFO_SPANID_LEN];
  761. char parentSpanId[LINKINFO_PARENTSPANID_LEN];
  762. TOOLKIT_ASSERT(endpt);
  763. read_state = iobuffer_get_read_state(pkt);
  764. write_state = iobuffer_get_write_state(pkt);
  765. /*
  766. t = iobuffer_get_linkId(pkt);
  767. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  768. */
  769. iobuffer_get_linkInfo(pkt, bussinessId, traceId, spanId, parentSpanId);
  770. iobuffer_write_head(pkt, IOBUF_T_BUF, parentSpanId, sizeof(parentSpanId));
  771. iobuffer_write_head(pkt, IOBUF_T_BUF, spanId, sizeof(spanId));
  772. iobuffer_write_head(pkt, IOBUF_T_BUF, traceId, sizeof(traceId));
  773. iobuffer_write_head(pkt, IOBUF_T_BUF, bussinessId, sizeof(bussinessId));
  774. t = epid; // remote epid
  775. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  776. t = endpt->epid; // local epid
  777. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  778. t = type; // user type
  779. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  780. t = BUS_TYPE_INFO; // type
  781. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  782. rc = send_pkt_raw(endpt, pkt);
  783. iobuffer_restore_read_state(pkt, read_state);
  784. iobuffer_restore_write_state(pkt, write_state);
  785. return rc;
  786. }
  787. TOOLKIT_API int bus_endpt_bcast_evt(bus_endpt_t* endpt, int type, iobuffer_t* pkt)
  788. {
  789. int t;
  790. int rc;
  791. int read_state;
  792. int write_state;
  793. TOOLKIT_ASSERT(endpt);
  794. read_state = iobuffer_get_read_state(pkt);
  795. write_state = iobuffer_get_write_state(pkt);
  796. t = endpt->epid;
  797. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  798. t = type;
  799. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  800. t = BUS_TYPE_EVENT;
  801. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  802. rc = send_pkt_raw(endpt, pkt);
  803. iobuffer_restore_read_state(pkt, read_state);
  804. iobuffer_restore_write_state(pkt, write_state);
  805. return rc;
  806. }
  807. static int bus_endpt_recv_pkt(bus_endpt_t* endpt, int* p_epid, int* p_type, iobuffer_t** p_pkt)
  808. {
  809. if (iobuffer_queue_count(endpt->rx_buf_queue) > 0) {
  810. iobuffer_t *pkt = iobuffer_queue_head(endpt->rx_buf_queue);
  811. int read_state = iobuffer_get_read_state(pkt);
  812. int pkt_type, usr_type, from_epid, to_epid, link_id;
  813. char bussinessId[LINKINFO_BUSSID_LEN] ,traceId[LINKINFO_TRACEID_LEN], spanId[LINKINFO_SPANID_LEN], parentSpanId[LINKINFO_PARENTSPANID_LEN];
  814. int readLen = 0;
  815. memset(bussinessId, 0, LINKINFO_BUSSID_LEN);
  816. memset(traceId, 0, LINKINFO_TRACEID_LEN);
  817. memset(spanId, 0, LINKINFO_SPANID_LEN);
  818. memset(parentSpanId, 0, LINKINFO_PARENTSPANID_LEN);
  819. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, 0);
  820. if (pkt_type == BUS_TYPE_PACKET || pkt_type == BUS_TYPE_INFO) {
  821. iobuffer_read(pkt, IOBUF_T_I4, &usr_type, 0);
  822. iobuffer_read(pkt, IOBUF_T_I4, &from_epid, 0);
  823. iobuffer_read(pkt, IOBUF_T_I4, &to_epid, 0);
  824. //iobuffer_read(pkt, IOBUF_T_I4, &link_id, 0);
  825. //MessageBox(NULL, NULL, NULL, 0);
  826. readLen = LINKINFO_BUSSID_LEN;
  827. iobuffer_read(pkt, IOBUF_T_BUF, bussinessId, &readLen);
  828. readLen = LINKINFO_TRACEID_LEN;
  829. iobuffer_read(pkt, IOBUF_T_BUF, traceId, &readLen);
  830. readLen = LINKINFO_SPANID_LEN;
  831. iobuffer_read(pkt, IOBUF_T_BUF, spanId, &readLen);
  832. readLen = LINKINFO_PARENTSPANID_LEN;
  833. iobuffer_read(pkt, IOBUF_T_BUF, parentSpanId, &readLen);
  834. iobuffer_set_linkInfo(pkt, bussinessId, traceId, spanId, parentSpanId);
  835. if (p_epid)
  836. *p_epid = from_epid;
  837. if (p_type)
  838. *p_type = usr_type;
  839. iobuffer_queue_deque(endpt->rx_buf_queue);
  840. if (p_pkt) {
  841. *p_pkt = pkt;
  842. }
  843. else {
  844. iobuffer_destroy(pkt);
  845. }
  846. return 0;
  847. }
  848. else {
  849. iobuffer_restore_read_state(pkt, read_state);
  850. }
  851. }
  852. return -1;
  853. }
  854. static int bus_endpt_recv_evt(bus_endpt_t* endpt, int* p_epid, int* p_type, iobuffer_t** p_pkt)
  855. {
  856. if (iobuffer_queue_count(endpt->rx_buf_queue) > 0) {
  857. iobuffer_t* pkt = iobuffer_queue_head(endpt->rx_buf_queue);
  858. int read_state = iobuffer_get_read_state(pkt);
  859. int pkt_type, usr_type, from_epid, link_id;
  860. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, 0);
  861. if (pkt_type == BUS_TYPE_EVENT) {
  862. //iobuffer_read(pkt, IOBUF_T_I4, &link_id, 0);
  863. iobuffer_read(pkt, IOBUF_T_I4, &usr_type, 0);
  864. iobuffer_read(pkt, IOBUF_T_I4, &from_epid, 0);
  865. if (p_epid)
  866. *p_epid = from_epid;
  867. if (p_type)
  868. *p_type = usr_type;
  869. iobuffer_queue_deque(endpt->rx_buf_queue);
  870. if (p_pkt) {
  871. *p_pkt = pkt;
  872. }
  873. else {
  874. iobuffer_destroy(pkt);
  875. }
  876. return 0;
  877. }
  878. else {
  879. iobuffer_restore_read_state(pkt, read_state);
  880. }
  881. }
  882. return -1;
  883. }
  884. static int bus_endpt_recv_sys(bus_endpt_t* endpt, int* p_epid, int* p_state)
  885. {
  886. if (iobuffer_queue_count(endpt->rx_buf_queue) > 0) {
  887. iobuffer_t* pkt = iobuffer_queue_head(endpt->rx_buf_queue);
  888. int read_state = iobuffer_get_read_state(pkt);
  889. int pkt_type, epid, state;
  890. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, 0);
  891. if (pkt_type == BUS_TYPE_SYSTEM) {
  892. iobuffer_read(pkt, IOBUF_T_I4, &epid, 0);
  893. iobuffer_read(pkt, IOBUF_T_I4, &state, 0);
  894. if (p_epid)
  895. *p_epid = epid;
  896. if (p_state)
  897. *p_state = state;
  898. iobuffer_queue_deque(endpt->rx_buf_queue);
  899. iobuffer_destroy(pkt);
  900. return 0;
  901. }
  902. else {
  903. iobuffer_restore_read_state(pkt, read_state);
  904. }
  905. }
  906. return -1;
  907. }
  908. static int bus_endpt_recv_msg(bus_endpt_t* endpt, msg_t** p_msg)
  909. {
  910. int rc = -1;
  911. TOOLKIT_ASSERT(endpt);
  912. TOOLKIT_ASSERT(p_msg);
  913. spinlock_enter(&endpt->msg_lock, -1);
  914. if (!list_empty(&endpt->msg_list)) {
  915. msg_t* e = list_first_entry(&endpt->msg_list, msg_t, entry);
  916. list_del(&e->entry);
  917. rc = 0;
  918. *p_msg = e;
  919. }
  920. spinlock_leave(&endpt->msg_lock);
  921. return rc;
  922. }
  923. TOOLKIT_API int bus_endpt_get_state(bus_endpt_t* endpt, int epid, int* p_state)
  924. {
  925. iobuffer_t* buf = NULL;
  926. int v;
  927. int rc = -1;
  928. TOOLKIT_ASSERT(endpt);
  929. buf = iobuffer_create(-1, -1);
  930. v = BUS_TYPE_ENDPT_GET_STATE;
  931. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  932. v = epid;
  933. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  934. rc = send_pkt_raw(endpt, buf);
  935. if (rc < 0)
  936. goto on_error;
  937. rc = recv_until_state(endpt, p_state);
  938. on_error:
  939. if (buf)
  940. iobuffer_destroy(buf);
  941. return rc;
  942. }
  943. TOOLKIT_API int bus_endpt_post_msg(bus_endpt_t* endpt, int msg, int nparam, int params[])
  944. {
  945. msg_t* e;
  946. TOOLKIT_ASSERT(endpt);
  947. WLog_DBG(TAG, "==> endpt(%d) post msg: %d", endpt->epid, msg);
  948. e = MALLOC_T(msg_t);
  949. if (e == NULL) {
  950. return -1;
  951. }
  952. e->type = msg;
  953. e->nparam = nparam;
  954. if (nparam) {
  955. e->params = (int*)malloc(sizeof(int) * nparam);
  956. if(e->params != NULL)
  957. memcpy(e->params, params, sizeof(int) * nparam);
  958. }
  959. else {
  960. e->params = NULL;
  961. }
  962. e->evt = NULL;
  963. spinlock_enter(&endpt->msg_lock, -1);
  964. list_add_tail(&e->entry, &endpt->msg_list);
  965. spinlock_leave(&endpt->msg_lock);
  966. ReleaseSemaphore(endpt->msg_sem, 1, NULL);
  967. WLog_DBG(TAG, "<== endpt(%d) post msg: %d", endpt->epid, msg);
  968. return 0;
  969. }
  970. TOOLKIT_API int bus_endpt_send_msg(bus_endpt_t* endpt, int msg, int nparam, int params[])
  971. {
  972. msg_t e;
  973. TOOLKIT_ASSERT(endpt);
  974. e.type = msg;
  975. e.nparam = nparam;
  976. if (nparam) {
  977. e.params = (int*)malloc(sizeof(int) * nparam);
  978. memcpy(e.params, params, sizeof(int) * nparam);
  979. }
  980. else {
  981. e.params = NULL;
  982. }
  983. e.evt_result = 0;
  984. e.evt = CreateEventA(NULL, TRUE, FALSE, NULL);
  985. spinlock_enter(&endpt->msg_lock, -1);
  986. list_add_tail(&e.entry, &endpt->msg_list);
  987. spinlock_leave(&endpt->msg_lock);
  988. ReleaseSemaphore(endpt->msg_sem, 1, NULL);
  989. WaitForSingleObject(e.evt, INFINITE);
  990. CloseHandle(e.evt);
  991. if (nparam) {
  992. free(e.params);
  993. }
  994. return e.evt_result;
  995. }
  996. TOOLKIT_API int bus_endpt_get_epid(bus_endpt_t* endpt)
  997. {
  998. return endpt->epid;
  999. }
  1000. TOOLKIT_API const char* bus_endpt_get_url(bus_endpt_t* endpt)
  1001. {
  1002. return endpt->url;
  1003. }
  1004. TOOLKIT_API int bus_endpt_poll(bus_endpt_t* endpt, int timeout)
  1005. {
  1006. int result;
  1007. int rc;
  1008. int epid, type, state;
  1009. iobuffer_t* pkt = NULL;
  1010. rc = bus_endpt_poll_internal(endpt, &result, timeout);
  1011. if (rc > 0) {
  1012. if (result == BUS_RESULT_DATA) {
  1013. bus_endpt_recv_pkt(endpt, &epid, &type, &pkt);
  1014. WLog_INFO(TAG, "bus_endpt_recv_pkt BUS_RESULT_DATA, epid:%d, type:0x%08X", epid, type);
  1015. if (endpt->callback.on_pkt)
  1016. endpt->callback.on_pkt(endpt, epid, type, &pkt, endpt->callback.user_data);
  1017. if (pkt)
  1018. iobuffer_dec_ref(pkt);
  1019. }
  1020. else if (result == BUS_RESULT_INFO) {
  1021. bus_endpt_recv_pkt(endpt, &epid, &type, &pkt);
  1022. WLog_INFO(TAG, "bus_endpt_recv_pkt BUS_RESULT_INFO, epid:%d, type:0x%08X", epid, type);
  1023. if (endpt->callback.on_inf)
  1024. endpt->callback.on_inf(endpt, epid, type, &pkt, endpt->callback.user_data);
  1025. if (pkt)
  1026. iobuffer_dec_ref(pkt);
  1027. }
  1028. else if (result == BUS_RESULT_EVENT) {
  1029. bus_endpt_recv_evt(endpt, &epid, &type, &pkt);
  1030. WLog_INFO(TAG, "bus_endpt_recv_pkt BUS_RESULT_EVENT, epid:%d, type:0x%08X", epid, type);
  1031. if (endpt->callback.on_evt)
  1032. endpt->callback.on_evt(endpt, epid, type, &pkt, endpt->callback.user_data);
  1033. if (pkt)
  1034. iobuffer_dec_ref(pkt);
  1035. }
  1036. else if (result == BUS_RESULT_SYSTEM) {
  1037. bus_endpt_recv_sys(endpt, &epid, &state);
  1038. WLog_INFO(TAG, "bus_endpt_recv_pkt BUS_RESULT_SYSTEM, epid:%d, state:%d", epid, state);
  1039. if (endpt->callback.on_sys)
  1040. endpt->callback.on_sys(endpt, epid, state, endpt->callback.user_data);
  1041. }
  1042. else if (result == BUS_RESULT_MSG) {
  1043. msg_t* msg = NULL;
  1044. bus_endpt_recv_msg(endpt, &msg);
  1045. WLog_INFO(TAG, "bus_endpt_recv_pkt BUS_RESULT_MSG, type:0x%08X", msg->type);
  1046. if (endpt->callback.on_msg) {
  1047. endpt->callback.on_msg(endpt, msg->type, msg->nparam, msg->params,
  1048. msg->evt ? &msg->evt_result : NULL,
  1049. endpt->callback.user_data);
  1050. if (msg->evt)
  1051. SetEvent(msg->evt);
  1052. else
  1053. free_msg(msg);
  1054. }
  1055. else {
  1056. if (msg->evt) {
  1057. msg->evt_result = -1;
  1058. SetEvent(msg->evt);
  1059. }
  1060. else {
  1061. free_msg(msg);
  1062. }
  1063. }
  1064. }
  1065. else {
  1066. TOOLKIT_ASSERT(0);
  1067. rc = -1;
  1068. }
  1069. }
  1070. else if (rc < 0) {
  1071. WLog_DBG(TAG, "bus_endpt_poll_internal failed, rc = %d", rc);
  1072. }
  1073. return rc;
  1074. }
  1075. TOOLKIT_API int bus_endpt_set_quit_flag(bus_endpt_t* endpt)
  1076. {
  1077. endpt->quit_flag = 1;
  1078. return 0;
  1079. }
  1080. TOOLKIT_API int bus_endpt_get_quit_flag(bus_endpt_t* endpt)
  1081. {
  1082. return endpt->quit_flag;
  1083. }