bus.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. #include "precompile.h"
  2. #include "toolkit.h"
  3. #include "bus.h"
  4. #include "sockutil.h"
  5. #include "url.h"
  6. #include "spinlock.h"
  7. #include "list.h"
  8. #include "bus_internal.h"
  9. #include "evtpoll.h"
  10. #include "core.h"
  11. #include "dbgutil.h"
  12. #include <winpr/file.h>
  13. #include <winpr/pipe.h>
  14. #include <winpr/synch.h>
  15. #include <winpr/string.h>
  16. #include <sys/eventfd.h>
  17. #include <fcntl.h>
  18. #include "memutil.h"
  19. #define TAG TOOLKIT_TAG("bus")
  20. #define BUS_RESULT_DATA 1 // ==BUS_TYPE_PACKET, callback: callback.on_pkt, no use
  21. #define BUS_RESULT_INFO 2 // ==BUS_TYPE_INFO, callback: callback.on_inf
  22. #define BUS_RESULT_EVENT 3 // ==BUS_TYPE_EVENT, callback: callback.on_evt , no use
  23. #define BUS_RESULT_SYSTEM 4 // ==BUS_TYPE_SYSTEM, callback: callback.on_sys
  24. #define BUS_RESULT_MSG 5 // send package msg, callback: callback.on_msg
  25. #define BUS_RESULT_UNKNOWN 6
  26. typedef struct msg_t {
  27. struct list_head entry;
  28. int type;
  29. int nparam;
  30. param_size_t* params;
  31. HANDLE evt;
  32. int evt_result;
  33. }msg_t;
  34. struct bus_endpt_t {
  35. int type;
  36. int epid;
  37. union {
  38. HANDLE pipe_handle;
  39. SOCKET sock_handle;
  40. };
  41. char* url;
  42. /*define here or iom_t area, this is definitely a problem for now.*/
  43. evtpoll_t* ep;
  44. int msg_fd;
  45. event_epoll_data_t* msg_sem;
  46. bus_endpt_callback callback;
  47. struct list_head msg_list;
  48. spinlock_t msg_lock;
  49. HANDLE tx_evt; //manually
  50. HANDLE rx_evt; //manually
  51. OVERLAPPED rx_overlapped;
  52. int rx_pending;
  53. int rx_pending_pkt_len;
  54. int rx_pending_pkt_uc_len;
  55. iobuffer_queue_t* rx_buf_queue;
  56. volatile int quit_flag;
  57. };
  58. static void free_msg(msg_t* msg)
  59. {
  60. free(msg->params);
  61. free(msg);
  62. }
  63. static __inline int bus_endpoint__data_is_handle(const bus_endpt_t* endpt, void* data)
  64. {
  65. if ((endpt->type == TYPE_TCP && data == &endpt->sock_handle) ||
  66. (endpt->type == TYPE_PIPE && data == &endpt->pipe_handle)) {
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. static int to_result(int pkt_type)
  72. {
  73. switch (pkt_type) {
  74. case BUS_TYPE_EVENT:
  75. return BUS_RESULT_EVENT;
  76. case BUS_TYPE_SYSTEM:
  77. return BUS_RESULT_SYSTEM;
  78. case BUS_TYPE_PACKET:
  79. return BUS_RESULT_DATA;
  80. case BUS_TYPE_INFO:
  81. return BUS_RESULT_INFO;
  82. default:
  83. break;
  84. }
  85. return BUS_RESULT_UNKNOWN;
  86. }
  87. static HANDLE create_pipe_handle(const char* name)
  88. {
  89. char tmp[MAX_PATH];
  90. SOCKET domain_socket;
  91. int len;
  92. struct sockaddr_un saddr, sun;
  93. HANDLE pipe = INVALID_HANDLE_VALUE;
  94. sprintf(tmp, "/tmp/%s", name);
  95. if (strlen(tmp) >= sizeof(saddr.sun_path)) {
  96. WLog_ERR(TAG, "pipe path name is longer than sun_path's pacaticy");
  97. return -1;
  98. }
  99. /*create a UNIX domain stream socket*/
  100. domain_socket = _socket(AF_UNIX, SOCK_STREAM, 0);
  101. if (domain_socket < 0) {
  102. WLog_ERR(TAG, "create domain socket failed: %d", errno);
  103. return INVALID_HANDLE_VALUE;
  104. }
  105. nonblock_sock(domain_socket);
  106. if (make_fd_cloexec(domain_socket, 1) != 0) {
  107. WLog_ERR(TAG, "set bus domain socked fd cloexec failed: %d", errno);
  108. closesocket(domain_socket);
  109. return INVALID_HANDLE_VALUE;
  110. }
  111. memset(&saddr, 0, sizeof(struct sockaddr_un));
  112. saddr.sun_family = AF_UNIX;
  113. sprintf(saddr.sun_path, "/tmp/sphost.1.%08x", GetTickCount() * GetCurrentProcessId() % 0xffff);
  114. len = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
  115. unlink(saddr.sun_path);
  116. if (_bind(domain_socket, (struct sockaddr*) & saddr, len) < 0) {
  117. WLog_ERR(TAG, "bind domain socket failed: %d", errno);
  118. closesocket(domain_socket);
  119. return INVALID_HANDLE_VALUE;
  120. }
  121. if (chmod(saddr.sun_path, S_IRWXU) < 0) {
  122. WLog_ERR(TAG, "chmod domain sun_path failed: %d", errno);
  123. closesocket(domain_socket);
  124. unlink(saddr.sun_path);
  125. return INVALID_HANDLE_VALUE;
  126. }
  127. memset(&sun, 0, sizeof(sun));
  128. sun.sun_family = AF_UNIX;
  129. strcpy(sun.sun_path, tmp);
  130. len = offsetof(struct sockaddr_un, sun_path) + strlen(tmp);
  131. if (_connect(domain_socket, (struct sockaddr*) & sun, len) < 0) {
  132. WLog_ERR(TAG, "domain socket connet to server failed: %s(%d)", strerror(errno), errno);
  133. closesocket(domain_socket);
  134. unlink(saddr.sun_path);
  135. return INVALID_HANDLE_VALUE;
  136. }
  137. pipe = (HANDLE)domain_socket;
  138. return pipe;
  139. }
  140. static SOCKET create_socket_handle(const char* ip, int port)
  141. {
  142. SOCKET fd;
  143. struct sockaddr_in addr;
  144. /*Warning: only front three param is effective*/
  145. fd = WSASocketA(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
  146. if (fd == INVALID_SOCKET) {
  147. return fd;
  148. }
  149. if (make_fd_cloexec(fd, 1)) {
  150. WLog_ERR(TAG, "set cloexec for socket fd failed: %d", errno);
  151. closesocket(fd);
  152. return INVALID_SOCKET;
  153. }
  154. {
  155. BOOL f = TRUE;
  156. u_long l = TRUE;
  157. setsockopt(fd, SOL_SOCKET, SO_DONTLINGER, (char*)&f, sizeof(f));
  158. /*non block sock*/
  159. _ioctlsocket(fd, FIONBIO, &l);
  160. }
  161. memset(&addr, 0, sizeof(addr));
  162. addr.sin_family = AF_INET;
  163. addr.sin_port = htons(port);
  164. addr.sin_addr.s_addr = inet_addr(ip);
  165. WLog_INFO(TAG, "fd(%d) start to connect...", fd);
  166. if (_connect(fd, (struct sockaddr*) & addr, sizeof(addr)) != 0) {
  167. if (errno == EINPROGRESS) {
  168. WLog_WARN(TAG, "in connect progress...");
  169. fd_set wr_set, ex_set;
  170. FD_ZERO(&wr_set);
  171. FD_ZERO(&ex_set);
  172. FD_SET(fd, &wr_set);
  173. FD_SET(fd, &ex_set);
  174. if (_select(fd + 1, NULL, &wr_set, &ex_set, NULL) > 0 && FD_ISSET(fd, &wr_set)) {
  175. return fd;
  176. }
  177. } else {
  178. WLog_ERR(TAG, "_connect failed : %d", errno);
  179. }
  180. closesocket(fd);
  181. return INVALID_SOCKET;
  182. }
  183. WLog_WARN(TAG, "this should be appear hardly!!!");
  184. return fd;
  185. }
  186. static int tcp_send_buf(bus_endpt_t* endpt, const char* buf, int n)
  187. {
  188. DWORD left = n;
  189. DWORD offset = 0;
  190. WLog_DBG(TAG, "==> fd(%d): tcp send buf len: %d", endpt->sock_handle, n);
  191. while (left > 0) {
  192. BOOL ret;
  193. WSABUF wsabuf;
  194. DWORD dwBytesTransfer;
  195. OVERLAPPED overlapped;
  196. memset(&overlapped, 0, sizeof(overlapped));
  197. overlapped.hEvent = endpt->tx_evt;
  198. ResetEvent(endpt->tx_evt);
  199. wsabuf.buf = (char*)buf + offset;
  200. wsabuf.len = left;
  201. ret = FALSE;
  202. n = _send(endpt->sock_handle, wsabuf.buf, wsabuf.len, 0);
  203. if (n == -1) {
  204. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  205. fd_set wfds;
  206. struct timeval tv;
  207. int retval;
  208. FD_ZERO(&wfds);
  209. FD_SET(endpt->sock_handle, &wfds);
  210. tv.tv_sec = 5;
  211. tv.tv_usec = 0;
  212. retval = _select(endpt->sock_handle + 1, NULL, &wfds, NULL, &tv);
  213. if (retval == -1) {
  214. WLog_ERR(TAG, "select error, errno: %d", errno);
  215. }
  216. else if (retval && FD_ISSET(endpt->sock_handle, &wfds) > 0) {
  217. WLog_INFO(TAG, "can write");
  218. n = _send(endpt->sock_handle, wsabuf.buf, wsabuf.len, 0);
  219. if (n >= 0) {
  220. ret = TRUE;
  221. dwBytesTransfer = n;
  222. }
  223. } else {
  224. WLog_WARN(TAG, "write timeout");
  225. ret = TRUE;
  226. dwBytesTransfer = 0;
  227. }
  228. } else {
  229. WLog_ERR(TAG, "_send failed: %d", errno);
  230. }
  231. } else {
  232. ret = TRUE;
  233. dwBytesTransfer = n;
  234. }
  235. if (ret && dwBytesTransfer) {
  236. offset += dwBytesTransfer;
  237. left -= dwBytesTransfer;
  238. } else {
  239. WLog_DBG(TAG, "<== error fd(%d): tcp send buf left: %d", endpt->sock_handle, left);
  240. return -1;
  241. }
  242. }
  243. WLog_DBG(TAG, "<== fd(%d): tcp send buf len: %d", endpt->sock_handle, n);
  244. return 0;
  245. }
  246. static int pipe_send_buf(bus_endpt_t* endpt, const char* buf, int n)
  247. {
  248. DWORD left = n;
  249. DWORD offset = 0;
  250. while (left > 0) {
  251. BOOL ret;
  252. DWORD dwBytesTransfer;
  253. OVERLAPPED overlapped;
  254. memset(&overlapped, 0, sizeof(overlapped));
  255. overlapped.hEvent = endpt->tx_evt;
  256. ResetEvent(endpt->tx_evt);
  257. ret = WriteFile(endpt->pipe_handle, buf + offset, left, &dwBytesTransfer, &overlapped);
  258. if (!ret && GetLastError() == ERROR_IO_PENDING) {
  259. ret = GetOverlappedResult(endpt->pipe_handle, &overlapped, &dwBytesTransfer, TRUE);
  260. }
  261. if (ret && dwBytesTransfer) {
  262. offset += dwBytesTransfer;
  263. left -= dwBytesTransfer;
  264. }
  265. else {
  266. return -1;
  267. }
  268. }
  269. return 0;
  270. }
  271. static int send_buf(bus_endpt_t* endpt, const char* buf, int n)
  272. {
  273. if (endpt->type == TYPE_PIPE) {
  274. return tcp_send_buf(endpt, buf, n);
  275. }
  276. else if (endpt->type == TYPE_TCP) {
  277. return tcp_send_buf(endpt, buf, n);
  278. }
  279. else {
  280. return -1;
  281. }
  282. }
  283. static int send_pkt_raw(bus_endpt_t* endpt, iobuffer_t* pkt)
  284. {
  285. int pkt_len = iobuffer_get_length(pkt);
  286. int rc;
  287. iobuffer_write_head(pkt, IOBUF_T_I4, &pkt_len, 0);
  288. rc = send_buf(endpt, iobuffer_data(pkt, 0), iobuffer_get_length(pkt));
  289. return rc;
  290. }
  291. static int pipe_recv_buf(bus_endpt_t* endpt, char* buf, DWORD n)
  292. {
  293. DWORD left = n;
  294. DWORD offset = 0;
  295. while (left > 0) {
  296. BOOL ret;
  297. DWORD dwBytesTransfer;
  298. OVERLAPPED overlapped;
  299. memset(&overlapped, 0, sizeof(overlapped));
  300. overlapped.hEvent = endpt->rx_evt;
  301. ResetEvent(overlapped.hEvent);
  302. ret = ReadFile(endpt->pipe_handle, buf + offset, left, &dwBytesTransfer, &overlapped);
  303. if (!ret && GetLastError() == ERROR_IO_PENDING) {
  304. ret = GetOverlappedResult(endpt->pipe_handle, &overlapped, &dwBytesTransfer, TRUE);
  305. }
  306. if (ret && dwBytesTransfer) {
  307. offset += dwBytesTransfer;
  308. left -= dwBytesTransfer;
  309. }
  310. else {
  311. return -1;
  312. }
  313. }
  314. return 0;
  315. }
  316. static int tcp_recv_buf(bus_endpt_t* endpt, char* buf, DWORD n)
  317. {
  318. DWORD left = n;
  319. DWORD offset = 0;
  320. WLog_DBG(TAG, "==> fd(%d): tcp recv buf len: %d", endpt->sock_handle, n);
  321. while (left > 0) {
  322. BOOL ret;
  323. int readn;
  324. DWORD dwBytesTransfer = 0;
  325. ret = FALSE;
  326. readn = _recv(endpt->sock_handle, buf + offset, left, 0);
  327. if (readn == -1) {
  328. if (errno == EAGAIN || errno == EWOULDBLOCK) {
  329. fd_set rfds;
  330. struct timeval tv;
  331. int retval;
  332. FD_ZERO(&rfds);
  333. FD_SET(endpt->sock_handle, &rfds);
  334. tv.tv_sec = 5;
  335. tv.tv_usec = 0;
  336. retval = _select(endpt->sock_handle + 1, &rfds, NULL, NULL, &tv);
  337. if (retval == -1) {
  338. WLog_ERR(TAG, "select failed: %d", errno);
  339. }
  340. else if (retval && FD_ISSET(endpt->sock_handle, &rfds) > 0) {
  341. readn = _recv(endpt->sock_handle, buf + offset, left, 0);
  342. WLog_INFO(TAG, "sock fd have something to read now: %d", readn);
  343. if (readn >= 0) {
  344. ret = TRUE;
  345. dwBytesTransfer = readn;
  346. }
  347. }
  348. else {
  349. WLog_WARN(TAG, "read timeout: %d, left: %d", retval, left);
  350. ret = TRUE;
  351. }
  352. }
  353. }
  354. else {
  355. ret = TRUE;
  356. dwBytesTransfer = readn;
  357. }
  358. if (ret && dwBytesTransfer) {
  359. offset += dwBytesTransfer;
  360. left -= dwBytesTransfer;
  361. }
  362. else if (!ret) {
  363. WLog_ERR(TAG, "<== error fd(%d): tcp recv buf len: %d", endpt->sock_handle, left);
  364. return -1;
  365. }
  366. }
  367. WLog_DBG(TAG, "<== fd(%d): tcp recv buf left: %d", endpt->sock_handle, left);
  368. return 0;
  369. }
  370. /*read data block, n is the dream length of data to read which will store in buf*/
  371. static int recv_buf(bus_endpt_t* endpt, char* buf, DWORD n)
  372. {
  373. if (endpt->type == TYPE_PIPE) {
  374. return tcp_recv_buf(endpt, buf, n);
  375. }
  376. else if (endpt->type == TYPE_TCP) {
  377. return tcp_recv_buf(endpt, buf, n);
  378. }
  379. else {
  380. return -1;
  381. }
  382. }
  383. static int recv_pkt_raw(bus_endpt_t* endpt, iobuffer_t** pkt)
  384. {
  385. int pkt_len;
  386. int rc = -1;
  387. rc = recv_buf(endpt, (char*)&pkt_len, 4);
  388. if (rc != 0)
  389. return rc;
  390. *pkt = iobuffer_create(-1, pkt_len);
  391. iobuffer_push_count(*pkt, pkt_len);
  392. if (pkt_len > 0) {
  393. rc = recv_buf(endpt, iobuffer_data(*pkt, 0), pkt_len);
  394. }
  395. if (rc < 0) {
  396. iobuffer_destroy(*pkt);
  397. *pkt = NULL;
  398. }
  399. return rc;
  400. }
  401. static int start_read_pkt(bus_endpt_t* endpt, iobuffer_t** p_pkt)
  402. {
  403. DWORD dwBytesTransferred;
  404. int ret;
  405. int rc = 0;
  406. iobuffer_t* pkt = NULL;
  407. int last_errno = 0;
  408. *p_pkt = NULL;
  409. WLog_DBG(TAG, "==>endpt(%d): start_read_pkt", endpt->epid);
  410. ResetEvent(endpt->rx_evt);
  411. WLog_DBG(TAG, "ResetEvent(endpt->rx_evt).");
  412. endpt->rx_pending_pkt_uc_len = 0;
  413. endpt->rx_pending_pkt_len = 0;
  414. if (endpt->type == TYPE_PIPE) {
  415. WLog_DBG(TAG, "to _recv pipe.");
  416. ret = _recv(endpt->pipe_handle, (char*)&endpt->rx_pending_pkt_len, 4, 0);
  417. last_errno = errno;
  418. WLog_DBG(TAG, "_recv pipe return: %d,,,,,,,, (%d)", ret, last_errno);
  419. }
  420. else if (endpt->type == TYPE_TCP) {
  421. WLog_DBG(TAG, "to _recv.");
  422. ret = _recv(endpt->sock_handle, (char*)&endpt->rx_pending_pkt_len, 4, 0);
  423. last_errno = errno;
  424. WLog_DBG(TAG, "_recv return: %d, (%d)", ret, last_errno);
  425. }
  426. else {
  427. WLog_ERR(TAG, "<== endpt(%d): start_read_pkt unkonwn type", endpt->epid);
  428. return -1;
  429. }
  430. if (ret >= 0) {
  431. dwBytesTransferred = ret;
  432. endpt->rx_pending_pkt_uc_len = ret;
  433. if (dwBytesTransferred == 0) {
  434. WLog_ERR(TAG, "<== endpt(%d): start_read_pkt peer socket close.", endpt->epid);
  435. return -1;
  436. }
  437. if (dwBytesTransferred < 4) {
  438. WLog_DBG(TAG, "endpt(%d): receive buffer less than dream len", endpt->epid);
  439. rc = recv_buf(endpt, (char*)&endpt->rx_pending_pkt_len + dwBytesTransferred, 4 - dwBytesTransferred);
  440. if (rc < 0) {
  441. WLog_ERR(TAG, "<== endpt(%d): start_read_pkt recv_buf error.", endpt->epid);
  442. return rc;
  443. }
  444. }
  445. WLog_DBG(TAG, "iobuffer_create ???");
  446. pkt = iobuffer_create(0, endpt->rx_pending_pkt_len);
  447. endpt->rx_pending_pkt_uc_len = 0;
  448. if (endpt->rx_pending_pkt_len > 0) {
  449. WLog_DBG(TAG, "endpt->rx_pending_pkt_len > 0 ???");
  450. rc = recv_buf(endpt, iobuffer_data(pkt, 0), endpt->rx_pending_pkt_len);
  451. if (rc < 0) {
  452. WLog_DBG(TAG, "iobuffer_destroy ???");
  453. iobuffer_destroy(pkt);
  454. WLog_ERR(TAG, "<== endpt(%d): start_read_pkt recv_buf error.", endpt->epid);
  455. return rc;
  456. }
  457. WLog_DBG(TAG, "iobuffer_push_count ???");
  458. iobuffer_push_count(pkt, endpt->rx_pending_pkt_len);
  459. }
  460. WLog_DBG(TAG, "*p_pkt = pkt ???");
  461. *p_pkt = pkt;
  462. WLog_ERR(TAG, "<== endpt(%d): start_read_pkt recv_buf succ.", endpt->epid);
  463. return 0;
  464. }
  465. else if(last_errno == EAGAIN || last_errno == EWOULDBLOCK) {
  466. WLog_DBG(TAG, "endpt(%d): set rx pending flag.", endpt->epid);
  467. endpt->rx_pending = 1;
  468. return 0;
  469. }
  470. WLog_ERR(TAG, "<== endpt(%d): start_read_pkt recv_buf failed.", endpt->epid);
  471. return -1;
  472. }
  473. static int read_left_pkt(bus_endpt_t* endpt, iobuffer_t** p_pkt)
  474. {
  475. int ret = -1;
  476. int rc;
  477. DWORD dwBytesTransferred;
  478. iobuffer_t* pkt = NULL;
  479. WLog_DBG(TAG, "==> fd(%d): read left pkt", endpt->sock_handle);
  480. if (endpt->type == TYPE_PIPE) {
  481. ret = _recv(endpt->pipe_handle,
  482. (char*)&endpt->rx_pending_pkt_len + endpt->rx_pending_pkt_uc_len,
  483. 4 - endpt->rx_pending_pkt_uc_len, 0);
  484. }
  485. else if (endpt->type == TYPE_TCP) {
  486. ret = _recv(endpt->sock_handle,
  487. (char*)&endpt->rx_pending_pkt_len + endpt->rx_pending_pkt_uc_len,
  488. 4 - endpt->rx_pending_pkt_uc_len, 0);
  489. }
  490. else {
  491. TOOLKIT_ASSERT(0);
  492. }
  493. if (ret < 0) {
  494. WLog_ERR(TAG, "<== fd(%d): read left pkt failed: ret %d, err: %d", endpt->sock_handle, ret, (errno));
  495. return -1;
  496. }
  497. else if(ret == 0) {
  498. WLog_WARN(TAG, "<== fd(%d): peer socket close.", endpt->sock_handle);
  499. return -1;
  500. }
  501. dwBytesTransferred = endpt->rx_pending_pkt_uc_len + ret;
  502. endpt->rx_pending_pkt_uc_len = dwBytesTransferred;
  503. if (dwBytesTransferred < 4) {
  504. rc = recv_buf(endpt, (char*)&endpt->rx_pending_pkt_len + dwBytesTransferred, 4 - dwBytesTransferred);
  505. if (rc < 0)
  506. return rc;
  507. }
  508. /*the first 4 bytes indicates the length of content and then read the buffer content.*/
  509. pkt = iobuffer_create(-1, endpt->rx_pending_pkt_len);
  510. WLog_DBG(TAG, "after read pkt len, start to read pkt's content: %d", endpt->rx_pending_pkt_len);
  511. rc = recv_buf(endpt, iobuffer_data(pkt, 0), endpt->rx_pending_pkt_len);
  512. if (rc < 0) {
  513. iobuffer_destroy(pkt);
  514. return rc;
  515. }
  516. iobuffer_push_count(pkt, endpt->rx_pending_pkt_len);
  517. *p_pkt = pkt;
  518. WLog_DBG(TAG, "endpt(%d): reset rx_pending and return!", endpt->epid);
  519. endpt->rx_pending = 0;
  520. return 0;
  521. }
  522. static int append_rx_pkt(bus_endpt_t* endpt, iobuffer_t* pkt)
  523. {
  524. int type;
  525. int read_state;
  526. read_state = iobuffer_get_read_state(pkt);
  527. iobuffer_read(pkt, IOBUF_T_I4, &type, 0);
  528. iobuffer_restore_read_state(pkt, read_state);
  529. if (type == BUS_TYPE_PACKET || type == BUS_TYPE_INFO || type == BUS_TYPE_EVENT || type == BUS_TYPE_SYSTEM) {
  530. iobuffer_queue_enqueue(endpt->rx_buf_queue, pkt);
  531. WLog_DBG(TAG, "<== append_rx_pkt finished");
  532. return 1;
  533. }
  534. else {
  535. WLog_DBG(TAG, "<== append_rx_pkt failed!");
  536. return -1;
  537. }
  538. }
  539. TOOLKIT_API int bus_endpt_create(const char* url, int epid, const bus_endpt_callback* callback, bus_endpt_t** p_endpt)
  540. {
  541. bus_endpt_t* endpt = NULL;
  542. char* tmp_url;
  543. url_fields uf;
  544. int rc;
  545. int v;
  546. iobuffer_t* buf = NULL;
  547. iobuffer_t* ans_buf = NULL;
  548. if (!url)
  549. return -1;
  550. tmp_url = _strdup(url);
  551. if (url_parse(tmp_url, &uf) < 0) {
  552. free(tmp_url);
  553. WLog_ERR(TAG, "url parse failed!");
  554. return -1;
  555. }
  556. endpt = ZALLOC_T(bus_endpt_t);
  557. endpt->sock_handle = -1;
  558. endpt->msg_fd = -1;
  559. endpt->ep = NULL;
  560. endpt->url = tmp_url;
  561. if (_stricmp(uf.scheme, "tcp") == 0) {
  562. endpt->type = TYPE_TCP;
  563. endpt->sock_handle = create_socket_handle(uf.host, uf.port);
  564. if (endpt->sock_handle == INVALID_SOCKET)
  565. goto on_error;
  566. WLog_INFO(TAG, "bus endpt socket fd: %d", endpt->sock_handle);
  567. }
  568. else if (_stricmp(uf.scheme, "pipe") == 0) {
  569. endpt->type = TYPE_PIPE;
  570. endpt->pipe_handle = create_pipe_handle(uf.host);
  571. if (endpt->pipe_handle == INVALID_HANDLE_VALUE)
  572. goto on_error;
  573. WLog_INFO(TAG, "bus endpt pipe fd: %d", endpt->pipe_handle);
  574. } else {
  575. goto on_error;
  576. }
  577. endpt->ep = evtpoll_create();
  578. if (!endpt->ep) {
  579. WLog_ERR(TAG, "evtpoll create failed!");
  580. goto on_error;
  581. }
  582. endpt->epid = epid;
  583. endpt->tx_evt = CreateEventA(NULL, TRUE, FALSE, NULL);
  584. endpt->rx_evt = CreateEventA(NULL, TRUE, FALSE, NULL);
  585. endpt->rx_buf_queue = iobuffer_queue_create();
  586. endpt->msg_fd = eventfd(0, EFD_CLOEXEC | EFD_SEMAPHORE);
  587. if (endpt->msg_fd == -1) {
  588. WLog_ERR(TAG, "create event fd failed: %d", errno);
  589. endpt->msg_fd = 0;
  590. goto on_error;
  591. }
  592. INIT_LIST_HEAD(&endpt->msg_list);
  593. spinlock_init(&endpt->msg_lock);
  594. memcpy(&endpt->callback, callback, sizeof(bus_endpt_callback));
  595. buf = iobuffer_create(-1, -1);
  596. v = BUS_TYPE_ENDPT_REGISTER;
  597. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  598. v = endpt->epid;
  599. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  600. WLog_DBG(TAG, "%s::start to send_pkt_raw...", __FUNCTION__);
  601. rc = send_pkt_raw(endpt, buf);
  602. WLog_INFO(TAG, "%s::send_pkt_raw return %d", __FUNCTION__, rc);
  603. if (rc != 0)
  604. goto on_error;
  605. rc = recv_pkt_raw(endpt, &ans_buf);
  606. WLog_INFO(TAG, "%s::recv_pkt_raw return %d", __FUNCTION__, rc);
  607. if (rc != 0) {
  608. goto on_error;
  609. }
  610. iobuffer_read(ans_buf, IOBUF_T_I4, &v, 0);
  611. iobuffer_read(ans_buf, IOBUF_T_I4, &rc, 0);
  612. if (rc != 0)
  613. goto on_error;
  614. url_free_fields(&uf);
  615. if (buf)
  616. iobuffer_destroy(buf);
  617. if (ans_buf)
  618. iobuffer_destroy(ans_buf);
  619. if (-1 == evtpoll_attach(endpt->ep, endpt->msg_fd)) {
  620. goto on_error;
  621. }
  622. if (evtpoll_subscribe(endpt->ep, EV_READ, endpt->msg_fd, &endpt->msg_fd, NULL)) {
  623. WLog_ERR(TAG, "epoll subscribe bus endpt eventfd failed.");
  624. goto on_error_msg;
  625. }
  626. /* subscribe read event [3/27/2020 Gifur] */
  627. if (evtpoll_attach(endpt->ep, endpt->sock_handle)) {
  628. WLog_ERR(TAG, "epoll attch bus endpt failed.");
  629. goto on_error_msg;
  630. }
  631. if (evtpoll_subscribe(endpt->ep, EV_READ, endpt->sock_handle, &endpt->sock_handle, NULL)) {
  632. WLog_ERR(TAG, "epoll subscribe bus endpt failed.");
  633. goto on_error_handle;
  634. }
  635. *p_endpt = endpt;
  636. return 0;
  637. on_error_handle:
  638. evtpoll_detach(endpt->ep, endpt->sock_handle);
  639. on_error_msg:
  640. evtpoll_detach(endpt->ep, endpt->msg_fd);
  641. on_error:
  642. if (endpt->type == TYPE_TCP) {
  643. closesocket(endpt->sock_handle);
  644. }
  645. else if (endpt->type == TYPE_PIPE) {
  646. close(endpt->pipe_handle);
  647. }
  648. if (endpt->msg_fd > 0)
  649. close(endpt->msg_fd);
  650. if (endpt->tx_evt)
  651. CloseHandle(endpt->tx_evt);
  652. if (endpt->rx_evt)
  653. CloseHandle(endpt->rx_evt);
  654. if (endpt->rx_buf_queue)
  655. iobuffer_queue_destroy(endpt->rx_buf_queue);
  656. if (endpt->url)
  657. free(endpt->url);
  658. free(endpt);
  659. url_free_fields(&uf);
  660. if (buf)
  661. iobuffer_destroy(buf);
  662. if (ans_buf)
  663. iobuffer_destroy(ans_buf);
  664. if (endpt->ep != NULL) {
  665. evtpoll_destroy(endpt->ep);
  666. }
  667. return -1;
  668. }
  669. TOOLKIT_API void bus_endpt_destroy(bus_endpt_t* endpt)
  670. {
  671. int rc = -1;
  672. iobuffer_t* buf = NULL;
  673. iobuffer_t* ans_buf = NULL;
  674. int v;
  675. TOOLKIT_ASSERT(endpt);
  676. buf = iobuffer_create(-1, -1);
  677. v = BUS_TYPE_ENDPT_UNREGISTER;
  678. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  679. v = endpt->epid;
  680. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  681. rc = send_pkt_raw(endpt, buf);
  682. if (rc != 0)
  683. goto on_error;
  684. rc = recv_pkt_raw(endpt, &ans_buf);
  685. if (rc != 0)
  686. goto on_error;
  687. iobuffer_read(ans_buf, IOBUF_T_I4, &v, 0);
  688. iobuffer_read(ans_buf, IOBUF_T_I4, &rc, 0);
  689. on_error:
  690. spinlock_enter(&endpt->msg_lock, -1);
  691. if (!list_empty(&endpt->msg_list)) {
  692. msg_t* msg, * t;
  693. list_for_each_entry_safe(msg, t, &endpt->msg_list, msg_t, entry) {
  694. list_del(&msg->entry);
  695. if (msg->evt)
  696. CloseHandle(msg->evt);
  697. if (msg->type == 2/*IOM_T_SEND_INFO*/) {
  698. iobuffer_t* pkt = (iobuffer_t*)msg->params[5];
  699. if (pkt)
  700. iobuffer_dec_ref(pkt);
  701. }
  702. free_msg(msg);
  703. }
  704. }
  705. spinlock_leave(&endpt->msg_lock);
  706. if (buf)
  707. iobuffer_destroy(buf);
  708. if (ans_buf)
  709. iobuffer_destroy(ans_buf);
  710. if (endpt->type == TYPE_TCP) {
  711. evtpoll_detach(endpt->ep, endpt->sock_handle);
  712. closesocket(endpt->sock_handle);
  713. }
  714. else if (endpt->type == TYPE_PIPE) {
  715. evtpoll_detach(endpt->ep, endpt->pipe_handle);
  716. closesocket(endpt->pipe_handle);
  717. }
  718. if (endpt->msg_fd) {
  719. evtpoll_detach(endpt->ep, endpt->msg_fd);
  720. close(endpt->msg_fd);
  721. }
  722. if (endpt->tx_evt)
  723. CloseHandle(endpt->tx_evt);
  724. if (endpt->rx_evt)
  725. CloseHandle(endpt->rx_evt);
  726. if (endpt->rx_buf_queue)
  727. iobuffer_queue_destroy(endpt->rx_buf_queue);
  728. TOOLKIT_ASSERT(endpt->ep);
  729. evtpoll_destroy(endpt->ep);
  730. if (endpt->url)
  731. free(endpt->url);
  732. free(endpt);
  733. }
  734. // 1 : recv ok
  735. // 0 : time out
  736. // <0 : error
  737. static int bus_endpt_poll_internal(bus_endpt_t* endpt, int* result, int timeout)
  738. {
  739. iobuffer_t* pkt = NULL;
  740. int rc;
  741. BOOL ret;
  742. TOOLKIT_ASSERT(endpt);
  743. // peek first packge type
  744. if (iobuffer_queue_count(endpt->rx_buf_queue) > 0) {
  745. pkt = iobuffer_queue_head(endpt->rx_buf_queue);
  746. }
  747. else {
  748. // no received package, try to receive one
  749. if (!endpt->rx_pending) {
  750. rc = start_read_pkt(endpt, &pkt);
  751. if (rc < 0) {
  752. WLog_ERR(TAG, "start read pkt failed.");
  753. return rc;
  754. }
  755. if (pkt) {
  756. WLog_INFO(TAG, "pkt has read");
  757. rc = append_rx_pkt(endpt, pkt); // append pkt to rx_buf_queue
  758. if (rc < 0) {
  759. iobuffer_destroy(pkt);
  760. return -2;
  761. }
  762. }
  763. }
  764. else {
  765. //WLog_ERR(TAG, "is pending now.");
  766. }
  767. // if receive is pending, wait for send or receive complete event
  768. if (!pkt) {
  769. //WLog_DBG(TAG, "wait msg sem or received event. tiemout: %d", timeout);
  770. {
  771. int nfds;
  772. int ret;
  773. int i;
  774. struct epoll_event events[MAX_EPOLL_EVENT];
  775. struct epoll_event* pe;
  776. nfds = evtpoll_wait(endpt->ep, events, MAX_EPOLL_EVENT, timeout);
  777. if (nfds == TOOLKIT_ETIMEDOUT) {
  778. //WLog_DBG(TAG, "epoll wait timeout.");
  779. return 0; //timeout
  780. }
  781. if (nfds == -1) {
  782. return -3;
  783. }
  784. WLog_DBG(TAG, "epoll wait return nfd: %d", nfds);
  785. for (i = 0; i < nfds; ++i) {
  786. void* pdata = NULL;
  787. pe = events + i;
  788. WLog_INFO(TAG, "loop events[%d]::fd(0x%08X) OUT:%d, IN:%d", i, pe->data.fd,
  789. pe->events & EPOLLOUT ? 1 : 0, pe->events & EPOLLIN ? 1 : 0);
  790. TOOLKIT_ASSERT(pe->events & EPOLLIN);
  791. ret = evtpoll_deal(endpt->ep, pe, &pdata, 0);
  792. if (!ret) {
  793. TOOLKIT_ASSERT(pdata);
  794. if(bus_endpoint__data_is_handle(endpt, pdata))
  795. {
  796. rc = read_left_pkt(endpt, &pkt);
  797. if (rc < 0)
  798. return rc;
  799. if (pkt) {
  800. rc = append_rx_pkt(endpt, pkt);
  801. if (rc < 0) {
  802. iobuffer_destroy(pkt);
  803. return -4;
  804. }
  805. }
  806. }
  807. else if (pdata == &endpt->msg_fd) {
  808. uint64_t rdata;
  809. WLog_DBG(TAG, "message arrive.");
  810. do
  811. {
  812. ret = read(endpt->msg_fd, &rdata, sizeof rdata);
  813. } while (ret < 0 && errno == EINTR);
  814. if (ret < 0) {
  815. WLog_ERR(TAG, "read msg fd failed: %d", (errno));
  816. abort();
  817. }
  818. *result = BUS_RESULT_MSG;
  819. return 1;
  820. }
  821. }
  822. }
  823. }
  824. }
  825. else {
  826. WLog_ERR(TAG, "pkt has readed");
  827. }
  828. }
  829. if (pkt) {
  830. int type;
  831. int read_state = iobuffer_get_read_state(pkt);
  832. iobuffer_read(pkt, IOBUF_T_I4, &type, 0);
  833. iobuffer_restore_read_state(pkt, read_state);
  834. *result = to_result(type);
  835. if (*result == BUS_RESULT_UNKNOWN) {
  836. WLog_ERR(TAG, "bug: unknown pkt type!");
  837. return -5;
  838. }
  839. return 1;
  840. }
  841. return -6;
  842. }
  843. static int recv_until(bus_endpt_t* endpt, int type, iobuffer_t** p_ansbuf)
  844. {
  845. int rc;
  846. iobuffer_t* ans_pkt = NULL;
  847. int ans_type;
  848. WLog_DBG(TAG, "==>endpt(%d): recv until type: 0x%08X", endpt->epid, type);
  849. for (;;) {
  850. if (!endpt->rx_pending) {
  851. rc = start_read_pkt(endpt, &ans_pkt);
  852. if (rc < 0) {
  853. break;
  854. }
  855. } else {
  856. WLog_DBG(TAG, "endpt(%d) is pending", endpt->epid);
  857. }
  858. if (!ans_pkt) {
  859. int nfds;
  860. int ret;
  861. int i, flag = 0;
  862. struct epoll_event events[MAX_EPOLL_EVENT];
  863. struct epoll_event* pe;
  864. nfds = evtpoll_wait(endpt->ep, events, MAX_EPOLL_EVENT, -1);
  865. if (nfds == TOOLKIT_ETIMEDOUT) {
  866. continue;
  867. }
  868. if (nfds == -1) {
  869. return -1;
  870. }
  871. WLog_DBG(TAG, "epoll wait return nfd: %d", nfds);
  872. for (i = 0; i < nfds; ++i) {
  873. void* pdata = NULL;
  874. pe = events + i;
  875. ret = evtpoll_deal(endpt->ep, pe, &pdata, 0);
  876. if (!ret && bus_endpoint__data_is_handle(endpt, pdata)) {
  877. flag = 1;
  878. break;
  879. }
  880. }
  881. if (flag) {
  882. rc = read_left_pkt(endpt, &ans_pkt);
  883. if (rc < 0) {
  884. break;
  885. }
  886. }
  887. }
  888. if (ans_pkt) {
  889. int read_state = iobuffer_get_read_state(ans_pkt);
  890. iobuffer_read(ans_pkt, IOBUF_T_I4, &ans_type, 0);
  891. iobuffer_restore_read_state(ans_pkt, read_state);
  892. if (ans_type == type) {
  893. *p_ansbuf = ans_pkt;
  894. break;
  895. }
  896. else {
  897. rc = append_rx_pkt(endpt, ans_pkt);
  898. if (rc < 0) {
  899. iobuffer_destroy(ans_pkt);
  900. break;
  901. }
  902. else {
  903. ans_pkt = NULL;
  904. }
  905. }
  906. }
  907. }
  908. return rc;
  909. }
  910. static int recv_until_result(bus_endpt_t* endpt, int* p_result)
  911. {
  912. int rc;
  913. iobuffer_t* ans_pkt = NULL;
  914. int type, error;
  915. rc = recv_until(endpt, BUS_TYPE_ERROR, &ans_pkt);
  916. if (rc < 0)
  917. return rc;
  918. iobuffer_read(ans_pkt, IOBUF_T_I4, &type, 0);
  919. iobuffer_read(ans_pkt, IOBUF_T_I4, &error, 0);
  920. iobuffer_destroy(ans_pkt);
  921. *p_result = error;
  922. return rc;
  923. }
  924. static int recv_until_state(bus_endpt_t* endpt, int* p_state)
  925. {
  926. int rc;
  927. iobuffer_t* ans_pkt = NULL;
  928. int type, epid, state;
  929. rc = recv_until(endpt, BUS_TYPE_ENDPT_GET_STATE, &ans_pkt);
  930. if (rc < 0)
  931. return rc;
  932. iobuffer_read(ans_pkt, IOBUF_T_I4, &type, 0);
  933. iobuffer_read(ans_pkt, IOBUF_T_I4, &epid, 0);
  934. iobuffer_read(ans_pkt, IOBUF_T_I4, &state, 0);
  935. iobuffer_destroy(ans_pkt);
  936. WLog_DBG(TAG, "state address: 0x%08X", p_state);
  937. *p_state = state;
  938. return rc;
  939. }
  940. TOOLKIT_API int bus_endpt_send_pkt(bus_endpt_t* endpt, int epid, int type, iobuffer_t* pkt)
  941. {
  942. int t;
  943. int rc;
  944. int read_state;
  945. int write_state;
  946. int error;
  947. char bussinessId[LINKINFO_BUSSID_LEN];
  948. char traceId[LINKINFO_TRACEID_LEN];
  949. char spanId[LINKINFO_SPANID_LEN];
  950. char parentSpanId[LINKINFO_PARENTSPANID_LEN];
  951. TOOLKIT_ASSERT(endpt);
  952. read_state = iobuffer_get_read_state(pkt);
  953. write_state = iobuffer_get_write_state(pkt);
  954. /** 这里塞的是完整的固定长度*/
  955. iobuffer_get_linkInfo(pkt, bussinessId, traceId, spanId, parentSpanId);
  956. iobuffer_write_head(pkt, IOBUF_T_BUF, parentSpanId, sizeof(parentSpanId));
  957. iobuffer_write_head(pkt, IOBUF_T_BUF, spanId, sizeof(spanId));
  958. iobuffer_write_head(pkt, IOBUF_T_BUF, traceId, sizeof(traceId));
  959. iobuffer_write_head(pkt, IOBUF_T_BUF, bussinessId, sizeof(bussinessId));
  960. t = epid; // remote epid
  961. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  962. t = endpt->epid; // local epid
  963. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  964. t = type; // user type
  965. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  966. t = BUS_TYPE_PACKET; // type
  967. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  968. rc = send_pkt_raw(endpt, pkt);
  969. iobuffer_restore_read_state(pkt, read_state);
  970. iobuffer_restore_write_state(pkt, write_state);
  971. if (rc < 0)
  972. return rc;
  973. rc = recv_until_result(endpt, &error);
  974. if (rc == 0 && error != 0)
  975. rc = error;
  976. return rc;
  977. }
  978. TOOLKIT_API int bus_endpt_send_info(bus_endpt_t* endpt, int epid, int type, iobuffer_t* pkt)
  979. {
  980. int t;
  981. int rc;
  982. int read_state;
  983. int write_state;
  984. char bussinessId[LINKINFO_BUSSID_LEN];
  985. char traceId[LINKINFO_TRACEID_LEN];
  986. char spanId[LINKINFO_SPANID_LEN];
  987. char parentSpanId[LINKINFO_PARENTSPANID_LEN];
  988. TOOLKIT_ASSERT(endpt);
  989. WLog_DBG(TAG, "==> endpt(%d) send info: %d, 0x%08X.", endpt->epid, epid, type);
  990. read_state = iobuffer_get_read_state(pkt);
  991. write_state = iobuffer_get_write_state(pkt);
  992. iobuffer_get_linkInfo(pkt, bussinessId, traceId, spanId, parentSpanId);
  993. iobuffer_write_head(pkt, IOBUF_T_BUF, parentSpanId, sizeof(parentSpanId));
  994. iobuffer_write_head(pkt, IOBUF_T_BUF, spanId, sizeof(spanId));
  995. iobuffer_write_head(pkt, IOBUF_T_BUF, traceId, sizeof(traceId));
  996. iobuffer_write_head(pkt, IOBUF_T_BUF, bussinessId, sizeof(bussinessId));
  997. t = epid; // remote epid
  998. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  999. t = endpt->epid; // local epid
  1000. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  1001. t = type; // user type
  1002. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  1003. t = BUS_TYPE_INFO; // type
  1004. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  1005. rc = send_pkt_raw(endpt, pkt);
  1006. iobuffer_restore_read_state(pkt, read_state);
  1007. iobuffer_restore_write_state(pkt, write_state);
  1008. return rc;
  1009. }
  1010. TOOLKIT_API int bus_endpt_bcast_evt(bus_endpt_t* endpt, int type, iobuffer_t* pkt)
  1011. {
  1012. int t;
  1013. int rc;
  1014. int read_state;
  1015. int write_state;
  1016. TOOLKIT_ASSERT(endpt);
  1017. read_state = iobuffer_get_read_state(pkt);
  1018. write_state = iobuffer_get_write_state(pkt);
  1019. t = endpt->epid;
  1020. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  1021. t = type;
  1022. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  1023. t = BUS_TYPE_EVENT;
  1024. iobuffer_write_head(pkt, IOBUF_T_I4, &t, 0);
  1025. rc = send_pkt_raw(endpt, pkt);
  1026. iobuffer_restore_read_state(pkt, read_state);
  1027. iobuffer_restore_write_state(pkt, write_state);
  1028. return rc;
  1029. }
  1030. static int bus_endpt_recv_pkt(bus_endpt_t* endpt, int* p_epid, int* p_type, iobuffer_t** p_pkt)
  1031. {
  1032. if (iobuffer_queue_count(endpt->rx_buf_queue) > 0) {
  1033. iobuffer_t* pkt = iobuffer_queue_head(endpt->rx_buf_queue);
  1034. int read_state = iobuffer_get_read_state(pkt);
  1035. int pkt_type, usr_type, from_epid, to_epid, link_id;
  1036. char bussinessId[LINKINFO_BUSSID_LEN+1];
  1037. char traceId[LINKINFO_TRACEID_LEN+1];
  1038. char spanId[LINKINFO_SPANID_LEN+1];
  1039. char parentSpanId[LINKINFO_PARENTSPANID_LEN+1];
  1040. int readLen = 0;
  1041. memset(bussinessId, 0, LINKINFO_BUSSID_LEN+1);
  1042. memset(traceId, 0, LINKINFO_TRACEID_LEN+1);
  1043. memset(spanId, 0, LINKINFO_SPANID_LEN+1);
  1044. memset(parentSpanId, 0, LINKINFO_PARENTSPANID_LEN+1);
  1045. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, 0);
  1046. if (pkt_type == BUS_TYPE_PACKET || pkt_type == BUS_TYPE_INFO) {
  1047. iobuffer_read(pkt, IOBUF_T_I4, &usr_type, 0);
  1048. iobuffer_read(pkt, IOBUF_T_I4, &from_epid, 0);
  1049. iobuffer_read(pkt, IOBUF_T_I4, &to_epid, 0);
  1050. readLen = LINKINFO_BUSSID_LEN;
  1051. iobuffer_read(pkt, IOBUF_T_BUF, bussinessId, &readLen);
  1052. readLen = LINKINFO_TRACEID_LEN;
  1053. iobuffer_read(pkt, IOBUF_T_BUF, traceId, &readLen);
  1054. readLen = LINKINFO_SPANID_LEN;
  1055. iobuffer_read(pkt, IOBUF_T_BUF, spanId, &readLen);
  1056. readLen = LINKINFO_PARENTSPANID_LEN;
  1057. iobuffer_read(pkt, IOBUF_T_BUF, parentSpanId, &readLen);
  1058. iobuffer_set_linkInfo(pkt, bussinessId, traceId, spanId, parentSpanId);
  1059. //WLog_DBG(TAG, "bussinessId:%s,traceId:%s,spanId:%s,parentSpanId:%s", bussinessId, traceId, spanId, parentSpanId);
  1060. if (p_epid)
  1061. *p_epid = from_epid;
  1062. if (p_type)
  1063. *p_type = usr_type;
  1064. iobuffer_queue_deque(endpt->rx_buf_queue);
  1065. if (p_pkt) {
  1066. *p_pkt = pkt;
  1067. }
  1068. else {
  1069. iobuffer_destroy(pkt);
  1070. }
  1071. return 0;
  1072. }
  1073. else {
  1074. iobuffer_restore_read_state(pkt, read_state);
  1075. }
  1076. }
  1077. return -1;
  1078. }
  1079. static int bus_endpt_recv_evt(bus_endpt_t* endpt, int* p_epid, int* p_type, iobuffer_t** p_pkt)
  1080. {
  1081. if (iobuffer_queue_count(endpt->rx_buf_queue) > 0) {
  1082. iobuffer_t* pkt = iobuffer_queue_head(endpt->rx_buf_queue);
  1083. int read_state = iobuffer_get_read_state(pkt);
  1084. int pkt_type, usr_type, from_epid;
  1085. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, 0);
  1086. if (pkt_type == BUS_TYPE_EVENT) {
  1087. iobuffer_read(pkt, IOBUF_T_I4, &usr_type, 0);
  1088. iobuffer_read(pkt, IOBUF_T_I4, &from_epid, 0);
  1089. if (p_epid)
  1090. *p_epid = from_epid;
  1091. if (p_type)
  1092. *p_type = usr_type;
  1093. iobuffer_queue_deque(endpt->rx_buf_queue);
  1094. if (p_pkt) {
  1095. *p_pkt = pkt;
  1096. }
  1097. else {
  1098. iobuffer_destroy(pkt);
  1099. }
  1100. return 0;
  1101. }
  1102. else {
  1103. iobuffer_restore_read_state(pkt, read_state);
  1104. }
  1105. }
  1106. return -1;
  1107. }
  1108. static int bus_endpt_recv_sys(bus_endpt_t* endpt, int* p_epid, int* p_state)
  1109. {
  1110. if (iobuffer_queue_count(endpt->rx_buf_queue) > 0) {
  1111. iobuffer_t* pkt = iobuffer_queue_head(endpt->rx_buf_queue);
  1112. int read_state = iobuffer_get_read_state(pkt);
  1113. int pkt_type, epid, state;
  1114. iobuffer_read(pkt, IOBUF_T_I4, &pkt_type, 0);
  1115. if (pkt_type == BUS_TYPE_SYSTEM) {
  1116. iobuffer_read(pkt, IOBUF_T_I4, &epid, 0);
  1117. iobuffer_read(pkt, IOBUF_T_I4, &state, 0);
  1118. if (p_epid)
  1119. *p_epid = epid;
  1120. if (p_state)
  1121. *p_state = state;
  1122. iobuffer_queue_deque(endpt->rx_buf_queue);
  1123. iobuffer_destroy(pkt);
  1124. return 0;
  1125. }
  1126. else {
  1127. iobuffer_restore_read_state(pkt, read_state);
  1128. }
  1129. }
  1130. return -1;
  1131. }
  1132. static int bus_endpt_recv_msg(bus_endpt_t* endpt, msg_t** p_msg)
  1133. {
  1134. int rc = -1;
  1135. TOOLKIT_ASSERT(endpt);
  1136. TOOLKIT_ASSERT(p_msg);
  1137. spinlock_enter(&endpt->msg_lock, -1);
  1138. if (!list_empty(&endpt->msg_list)) {
  1139. msg_t* e = list_first_entry(&endpt->msg_list, msg_t, entry);
  1140. list_del(&e->entry);
  1141. rc = 0;
  1142. *p_msg = e;
  1143. }
  1144. spinlock_leave(&endpt->msg_lock);
  1145. return rc;
  1146. }
  1147. TOOLKIT_API int bus_endpt_get_state(bus_endpt_t* endpt, int epid, int* p_state)
  1148. {
  1149. iobuffer_t* buf = NULL;
  1150. int v;
  1151. int rc = -1;
  1152. TOOLKIT_ASSERT(endpt);
  1153. buf = iobuffer_create(-1, -1);
  1154. v = BUS_TYPE_ENDPT_GET_STATE;
  1155. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  1156. v = epid;
  1157. iobuffer_write(buf, IOBUF_T_I4, &v, 0);
  1158. rc = send_pkt_raw(endpt, buf);
  1159. if (rc < 0) {
  1160. WLog_ERR(TAG, "send pkt raw failed.");
  1161. goto on_error;
  1162. }
  1163. rc = recv_until_state(endpt, p_state);
  1164. on_error:
  1165. if (buf)
  1166. iobuffer_destroy(buf);
  1167. return rc;
  1168. }
  1169. TOOLKIT_API int bus_endpt_post_msg(bus_endpt_t* endpt, int msg, int nparam, param_size_t params[])
  1170. {
  1171. msg_t* e;
  1172. int rc;
  1173. uint64_t wdata = 0;
  1174. TOOLKIT_ASSERT(endpt);
  1175. WLog_DBG(TAG, "==> endpt(%d) post msg: %d", endpt->epid, msg);
  1176. e = MALLOC_T(msg_t);
  1177. e->type = msg;
  1178. e->nparam = nparam;
  1179. if (nparam) {
  1180. e->params = (param_size_t*)malloc(sizeof(param_size_t) * nparam);
  1181. memcpy(e->params, params, sizeof(param_size_t) * nparam);
  1182. }
  1183. else {
  1184. e->params = NULL;
  1185. }
  1186. e->evt = NULL;
  1187. spinlock_enter(&endpt->msg_lock, -1);
  1188. list_add_tail(&e->entry, &endpt->msg_list);
  1189. spinlock_leave(&endpt->msg_lock);
  1190. wdata = 1;
  1191. do {
  1192. rc = write(endpt->msg_fd, &wdata, sizeof wdata);
  1193. } while (rc < 0 && rc == EINTR);
  1194. if (rc == -1) {
  1195. WLog_ERR(TAG, "<== endpt(%d) post msg: %d failed: %d", endpt->epid, msg, errno);
  1196. return -1;
  1197. }
  1198. WLog_DBG(TAG, "<== endpt(%d) post msg: %d", endpt->epid, msg);
  1199. return 0;
  1200. }
  1201. TOOLKIT_API int bus_endpt_send_msg(bus_endpt_t* endpt, int msg, int nparam, param_size_t params[])
  1202. {
  1203. msg_t e;
  1204. int rc;
  1205. uint64_t wdata = 0;
  1206. TOOLKIT_ASSERT(endpt);
  1207. WLog_DBG(TAG, "==> endpt(%d) send msg: epid %d, param counts %d", endpt->epid, msg, nparam);
  1208. e.type = msg;
  1209. e.nparam = nparam;
  1210. if (nparam) {
  1211. e.params = (param_size_t*)malloc(sizeof(param_size_t) * nparam);
  1212. memcpy(e.params, params, sizeof(param_size_t) * nparam);
  1213. }
  1214. else {
  1215. e.params = NULL;
  1216. }
  1217. e.evt_result = 0;
  1218. e.evt = CreateEventA(NULL, TRUE, FALSE, NULL);
  1219. TOOLKIT_ASSERT(e.evt != NULL);
  1220. spinlock_enter(&endpt->msg_lock, -1);
  1221. list_add_tail(&e.entry, &endpt->msg_list);
  1222. spinlock_leave(&endpt->msg_lock);
  1223. wdata = 1;
  1224. do {
  1225. rc = write(endpt->msg_fd, &wdata, sizeof wdata);
  1226. } while (rc < 0 && rc == EINTR);
  1227. if (rc == -1) {
  1228. WLog_ERR(TAG, "write to eventfd failed: %d", errno);
  1229. CloseHandle(e.evt);
  1230. if (nparam) {
  1231. free(e.params);
  1232. }
  1233. WLog_DBG(TAG, "<== error endpt(%d) send msg: %d", endpt->epid, msg);
  1234. return -1;
  1235. }
  1236. WaitForSingleObject(e.evt, INFINITE);
  1237. CloseHandle(e.evt);
  1238. if (nparam) {
  1239. free(e.params);
  1240. }
  1241. WLog_DBG(TAG, "<== endpt(%d) send msg: epid %d, evt_res: %d", endpt->epid, msg, e.evt_result);
  1242. return e.evt_result;
  1243. }
  1244. TOOLKIT_API int bus_endpt_get_epid(bus_endpt_t* endpt)
  1245. {
  1246. return endpt->epid;
  1247. }
  1248. TOOLKIT_API const char* bus_endpt_get_url(bus_endpt_t* endpt)
  1249. {
  1250. return endpt->url;
  1251. }
  1252. TOOLKIT_API int bus_endpt_poll(bus_endpt_t* endpt, int timeout)
  1253. {
  1254. int result;
  1255. int rc;
  1256. int epid, type, state;
  1257. iobuffer_t* pkt = NULL;
  1258. rc = bus_endpt_poll_internal(endpt, &result, timeout);
  1259. if (rc > 0) {
  1260. if (result == BUS_RESULT_DATA) {
  1261. bus_endpt_recv_pkt(endpt, &epid, &type, &pkt);
  1262. if (endpt->callback.on_pkt)
  1263. endpt->callback.on_pkt(endpt, epid, type, &pkt, endpt->callback.user_data);
  1264. if (pkt)
  1265. iobuffer_dec_ref(pkt);
  1266. }
  1267. else if (result == BUS_RESULT_INFO) {
  1268. bus_endpt_recv_pkt(endpt, &epid, &type, &pkt);
  1269. if (endpt->callback.on_inf)
  1270. endpt->callback.on_inf(endpt, epid, type, &pkt, endpt->callback.user_data);
  1271. if (pkt)
  1272. iobuffer_dec_ref(pkt);
  1273. }
  1274. else if (result == BUS_RESULT_EVENT) {
  1275. bus_endpt_recv_evt(endpt, &epid, &type, &pkt);
  1276. if (endpt->callback.on_evt)
  1277. endpt->callback.on_evt(endpt, epid, type, &pkt, endpt->callback.user_data);
  1278. if (pkt)
  1279. iobuffer_dec_ref(pkt);
  1280. }
  1281. else if (result == BUS_RESULT_SYSTEM) {
  1282. bus_endpt_recv_sys(endpt, &epid, &state);
  1283. if (endpt->callback.on_sys)
  1284. endpt->callback.on_sys(endpt, epid, state, endpt->callback.user_data);
  1285. }
  1286. else if (result == BUS_RESULT_MSG) {
  1287. msg_t* msg = NULL;
  1288. bus_endpt_recv_msg(endpt, &msg);
  1289. if (endpt->callback.on_msg) {
  1290. endpt->callback.on_msg(endpt,
  1291. msg->type,
  1292. msg->nparam,
  1293. msg->params,
  1294. msg->evt ? &msg->evt_result : NULL,
  1295. endpt->callback.user_data);
  1296. if (msg->evt) {
  1297. WLog_DBG(TAG, "after recv msg, send finished evt.");
  1298. SetEvent(msg->evt);
  1299. }
  1300. else {
  1301. WLog_DBG(TAG, "free msg");
  1302. free_msg(msg);
  1303. }
  1304. }
  1305. else {
  1306. if (msg->evt) {
  1307. msg->evt_result = -1;
  1308. WLog_DBG(TAG, "after on msg failed, send finished evt.");
  1309. SetEvent(msg->evt);
  1310. }
  1311. else {
  1312. WLog_DBG(TAG, "free msg");
  1313. free_msg(msg);
  1314. }
  1315. }
  1316. }
  1317. else {
  1318. TOOLKIT_ASSERT(0);
  1319. rc = -1;
  1320. }
  1321. }
  1322. return rc;
  1323. }
  1324. TOOLKIT_API int bus_endpt_set_quit_flag(bus_endpt_t* endpt)
  1325. {
  1326. endpt->quit_flag = 1;
  1327. return 0;
  1328. }
  1329. TOOLKIT_API int bus_endpt_get_quit_flag(bus_endpt_t* endpt)
  1330. {
  1331. return endpt->quit_flag;
  1332. }