rws_socketpriv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * Copyright (c) 2014 - 2017 Kulykov Oleh <info@resident.name>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #include "../librws.h"
  23. #include "rws_socket.h"
  24. #include "rws_memory.h"
  25. #include "rws_string.h"
  26. #define RWS_CONNECT_RETRY_DELAY 200
  27. #define RWS_CONNECT_ATTEMPS 5
  28. #ifndef RWS_OS_WINDOWS
  29. #define WSAEWOULDBLOCK EAGAIN
  30. #define WSAEINPROGRESS EINPROGRESS
  31. #endif
  32. unsigned int rws_socket_get_next_message_id(_rws_socket * s) {
  33. const unsigned int mess_id = ++s->next_message_id;
  34. if (mess_id > 9999999) {
  35. s->next_message_id = 0;
  36. }
  37. return mess_id;
  38. }
  39. void rws_socket_send_ping(_rws_socket * s) {
  40. char buff[16];
  41. size_t len = 0;
  42. _rws_frame * frame = rws_frame_create();
  43. len = rws_sprintf(buff, 16, "%u", rws_socket_get_next_message_id(s));
  44. frame->is_masked = rws_true;
  45. frame->opcode = rws_opcode_ping;
  46. rws_frame_fill_with_send_data(frame, buff, len);
  47. rws_socket_append_send_frames(s, frame);
  48. }
  49. void rws_socket_inform_recvd_frames(_rws_socket * s) {
  50. rws_bool is_all_finished = rws_true;
  51. _rws_frame * frame = NULL;
  52. _rws_node * cur = s->recvd_frames;
  53. while (cur) {
  54. frame = (_rws_frame *)cur->value.object;
  55. if (frame) {
  56. if (frame->is_finished) {
  57. switch (frame->opcode) {
  58. case rws_opcode_text_frame:
  59. if (s->on_recvd_text) {
  60. s->on_recvd_text(s, (const char *)frame->data, (unsigned int)frame->data_size);
  61. }
  62. break;
  63. case rws_opcode_binary_frame:
  64. if (s->on_recvd_bin) {
  65. s->on_recvd_bin(s, frame->data, (unsigned int)frame->data_size);
  66. }
  67. break;
  68. default: break;
  69. }
  70. rws_frame_delete(frame);
  71. cur->value.object = NULL;
  72. } else {
  73. is_all_finished = rws_false;
  74. }
  75. }
  76. cur = cur->next;
  77. }
  78. if (is_all_finished) {
  79. rws_list_delete_clean(&s->recvd_frames);
  80. }
  81. }
  82. void rws_socket_read_handshake_responce_value(const char * str, char ** value) {
  83. const char * s = NULL;
  84. size_t len = 0;
  85. while (*str == ':' || *str == ' ') {
  86. str++;
  87. }
  88. s = str;
  89. while (*s != '\r' && *s != '\n') {
  90. s++;
  91. len++;
  92. }
  93. if (len > 0) {
  94. *value = rws_string_copy_len(str, len);
  95. }
  96. }
  97. rws_bool rws_socket_process_handshake_responce(_rws_socket * s) {
  98. const char * str = (const char *)s->received;
  99. const char * sub = NULL;
  100. float http_ver = -1;
  101. int http_code = -1;
  102. rws_error_delete_clean(&s->error);
  103. sub = strstr(str, "HTTP/");
  104. if (!sub) {
  105. return rws_false;
  106. }
  107. sub += 5;
  108. if (rws_sscanf(sub, "%f %i", &http_ver, &http_code) != 2) {
  109. http_ver = -1;
  110. http_code = -1;
  111. }
  112. sub = strstr(str, k_rws_socket_sec_websocket_accept); // "Sec-WebSocket-Accept"
  113. if (sub) {
  114. sub += strlen(k_rws_socket_sec_websocket_accept);
  115. rws_socket_read_handshake_responce_value(sub, &s->sec_ws_accept);
  116. }
  117. if (http_code != 101 || !s->sec_ws_accept) {
  118. s->error = rws_error_new_code_descr(rws_error_code_parse_handshake,
  119. (http_code != 101) ? "HTPP code not found or non 101" : "Accept key not found");
  120. return rws_false;
  121. }
  122. return rws_true;
  123. }
  124. // need close socket on error
  125. rws_bool rws_socket_send(_rws_socket * s, const void * data, const size_t data_size) {
  126. int sended = -1, error_number = -1;
  127. rws_error_delete_clean(&s->error);
  128. //errno = -1;
  129. #if defined(RWS_OS_WINDOWS)
  130. sended = send(s->socket, (const char *)data, data_size, 0);
  131. error_number = WSAGetLastError();
  132. #else
  133. sended = (int)send(s->socket, data, (int)data_size, 0);
  134. error_number = errno;
  135. #endif
  136. if (sended > 0) {
  137. return rws_true;
  138. }
  139. rws_socket_check_write_error(s, error_number);
  140. if (s->error) {
  141. rws_socket_close(s);
  142. return rws_false;
  143. }
  144. return rws_true;
  145. }
  146. rws_bool rws_socket_recv(_rws_socket * s) {
  147. int is_reading = 1, error_number = -1, len = -1;
  148. char * received = NULL;
  149. size_t total_len = 0;
  150. char buff[8192];
  151. rws_error_delete_clean(&s->error);
  152. while (is_reading) {
  153. len = (int)recv(s->socket, buff, 8192, 0);
  154. #if defined(RWS_OS_WINDOWS)
  155. error_number = WSAGetLastError();
  156. #else
  157. error_number = errno;
  158. #endif
  159. if (len > 0) {
  160. total_len += len;
  161. if (s->received_size - s->received_len < len) {
  162. rws_socket_resize_received(s, s->received_size + len);
  163. }
  164. received = (char *)s->received;
  165. if (s->received_len) {
  166. received += s->received_len;
  167. }
  168. memcpy(received, buff, len);
  169. s->received_len += len;
  170. } else {
  171. is_reading = 0;
  172. }
  173. }
  174. //if (error_number < 0) return rws_true;
  175. if (error_number != WSAEWOULDBLOCK && error_number != WSAEINPROGRESS) {
  176. s->error = rws_error_new_code_descr(rws_error_code_read_write_socket, "Failed read/write socket");
  177. rws_socket_close(s);
  178. return rws_false;
  179. }
  180. return rws_true;
  181. }
  182. _rws_frame * rws_socket_last_unfin_recvd_frame_by_opcode(_rws_socket * s, const rws_opcode opcode) {
  183. _rws_frame * last = NULL;
  184. _rws_frame * frame = NULL;
  185. _rws_node * cur = s->recvd_frames;
  186. while (cur) {
  187. frame = (_rws_frame *)cur->value.object;
  188. if (frame) {
  189. // [FIN=0,opcode !=0 ],[FIN=0,opcode ==0 ],....[FIN=1,opcode ==0 ]
  190. if (!frame->is_finished /*&& frame->opcode == opcode*/) {
  191. last = frame;
  192. }
  193. }
  194. cur = cur->next;
  195. }
  196. return last;
  197. }
  198. void rws_socket_process_bin_or_text_frame(_rws_socket * s, _rws_frame * frame) {
  199. _rws_frame * last_unfin = rws_socket_last_unfin_recvd_frame_by_opcode(s, frame->opcode);
  200. if (last_unfin) {
  201. rws_frame_combine_datas(last_unfin, frame);
  202. last_unfin->is_finished = frame->is_finished;
  203. rws_frame_delete(frame);
  204. } else if (frame->data && frame->data_size) {
  205. rws_socket_append_recvd_frames(s, frame);
  206. } else {
  207. rws_frame_delete(frame);
  208. }
  209. }
  210. void rws_socket_process_ping_frame(_rws_socket * s, _rws_frame * frame) {
  211. _rws_frame * pong_frame = rws_frame_create();
  212. pong_frame->opcode = rws_opcode_pong;
  213. pong_frame->is_masked = rws_true;
  214. rws_frame_fill_with_send_data(pong_frame, frame->data, frame->data_size);
  215. rws_frame_delete(frame);
  216. rws_socket_append_send_frames(s, pong_frame);
  217. }
  218. void rws_socket_process_conn_close_frame(_rws_socket * s, _rws_frame * frame) {
  219. s->command = COMMAND_INFORM_DISCONNECTED;
  220. s->error = rws_error_new_code_descr(rws_error_code_connection_closed, "Connection was closed by endpoint");
  221. //rws_socket_close(s);
  222. rws_frame_delete(frame);
  223. }
  224. void rws_socket_process_received_frame(_rws_socket * s, _rws_frame * frame) {
  225. switch (frame->opcode) {
  226. case rws_opcode_ping: rws_socket_process_ping_frame(s, frame); break;
  227. case rws_opcode_text_frame:
  228. case rws_opcode_binary_frame:
  229. case rws_opcode_continuation:
  230. rws_socket_process_bin_or_text_frame(s, frame);
  231. break;
  232. case rws_opcode_connection_close: rws_socket_process_conn_close_frame(s, frame); break;
  233. default:
  234. // unprocessed => delete
  235. rws_frame_delete(frame);
  236. break;
  237. }
  238. }
  239. void rws_socket_idle_recv(_rws_socket * s) {
  240. _rws_frame * frame = NULL;
  241. if (!rws_socket_recv(s)) {
  242. // sock already closed
  243. if (s->error) {
  244. s->command = COMMAND_INFORM_DISCONNECTED;
  245. }
  246. return;
  247. }
  248. const size_t nframe_size = rws_check_recv_frame_size(s->received, s->received_len);
  249. if (nframe_size) {
  250. frame = rws_frame_create_with_recv_data(s->received, nframe_size);
  251. if (frame) {
  252. rws_socket_process_received_frame(s, frame);
  253. }
  254. if (nframe_size == s->received_len) {
  255. s->received_len = 0;
  256. } else if (s->received_len > nframe_size) {
  257. const size_t nLeftLen = s->received_len - nframe_size;
  258. memmove((char*)s->received, (char*)s->received + nframe_size, nLeftLen);
  259. s->received_len = nLeftLen;
  260. }
  261. }
  262. }
  263. void rws_socket_idle_send(_rws_socket * s) {
  264. _rws_node * cur = NULL;
  265. rws_bool sending = rws_true;
  266. _rws_frame * frame = NULL;
  267. rws_mutex_lock(s->send_mutex);
  268. cur = s->send_frames;
  269. if (cur) {
  270. while (cur && s->is_connected && sending) {
  271. frame = (_rws_frame *)cur->value.object;
  272. cur->value.object = NULL;
  273. if (frame) {
  274. sending = rws_socket_send(s, frame->data, frame->data_size);
  275. }
  276. rws_frame_delete(frame);
  277. cur = cur->next;
  278. }
  279. rws_list_delete_clean(&s->send_frames);
  280. if (s->error) {
  281. s->command = COMMAND_INFORM_DISCONNECTED;
  282. }
  283. }
  284. rws_mutex_unlock(s->send_mutex);
  285. }
  286. void rws_socket_wait_handshake_responce(_rws_socket * s) {
  287. if (!rws_socket_recv(s)) {
  288. // sock already closed
  289. if (s->error) {
  290. s->command = COMMAND_INFORM_DISCONNECTED;
  291. }
  292. return;
  293. }
  294. if (s->received_len == 0) {
  295. return;
  296. }
  297. if (rws_socket_process_handshake_responce(s)) {
  298. s->received_len = 0;
  299. s->is_connected = rws_true;
  300. s->command = COMMAND_INFORM_CONNECTED;
  301. } else {
  302. rws_socket_close(s);
  303. s->command = COMMAND_INFORM_DISCONNECTED;
  304. }
  305. }
  306. void rws_socket_send_disconnect(_rws_socket * s) {
  307. char buff[16];
  308. size_t len = 0;
  309. _rws_frame * frame = rws_frame_create();
  310. len = rws_sprintf(buff, 16, "%u", rws_socket_get_next_message_id(s));
  311. frame->is_masked = rws_true;
  312. frame->opcode = rws_opcode_connection_close;
  313. rws_frame_fill_with_send_data(frame, buff, len);
  314. rws_socket_send(s, frame->data, frame->data_size);
  315. rws_frame_delete(frame);
  316. s->command = COMMAND_END;
  317. rws_thread_sleep(RWS_CONNECT_RETRY_DELAY); // little bit wait after send message
  318. }
  319. void rws_socket_send_handshake(_rws_socket * s) {
  320. char buff[512];
  321. char * ptr = buff;
  322. size_t writed = 0;
  323. writed = rws_sprintf(ptr, 512, "GET %s HTTP/%s\r\n", s->path, k_rws_socket_min_http_ver);
  324. if (s->port == 80) {
  325. writed += rws_sprintf(ptr + writed, 512 - writed, "Host: %s\r\n", s->host);
  326. } else {
  327. writed += rws_sprintf(ptr + writed, 512 - writed, "Host: %s:%i\r\n", s->host, s->port);
  328. }
  329. writed += rws_sprintf(ptr + writed, 512 - writed,
  330. "Upgrade: websocket\r\n"
  331. "Connection: Upgrade\r\n"
  332. "Origin: %s://%s\r\n",
  333. s->scheme, s->host);
  334. writed += rws_sprintf(ptr + writed, 512 - writed,
  335. "Sec-WebSocket-Key: %s\r\n"
  336. "Sec-WebSocket-Protocol: chat, superchat\r\n"
  337. "Sec-WebSocket-Version: 13\r\n"
  338. "\r\n",
  339. "dGhlIHNhbXBsZSBub25jZQ==");
  340. if (rws_socket_send(s, buff, writed)) {
  341. s->command = COMMAND_WAIT_HANDSHAKE_RESPONCE;
  342. } else {
  343. if (s->error) {
  344. s->error->code = rws_error_code_send_handshake;
  345. } else {
  346. s->error = rws_error_new_code_descr(rws_error_code_send_handshake, "Send handshake");
  347. }
  348. rws_socket_close(s);
  349. s->command = COMMAND_INFORM_DISCONNECTED;
  350. }
  351. }
  352. struct addrinfo * rws_socket_connect_getaddr_info(_rws_socket * s) {
  353. struct addrinfo hints;
  354. char portstr[16];
  355. struct addrinfo * result = NULL;
  356. int ret = 0, retry_number = 0, last_ret = 0;
  357. #if defined(RWS_OS_WINDOWS)
  358. WSADATA wsa;
  359. #endif
  360. rws_error_delete_clean(&s->error);
  361. #if defined(RWS_OS_WINDOWS)
  362. memset(&wsa, 0, sizeof(WSADATA));
  363. if (WSAStartup(MAKEWORD(2,2), &wsa) != 0) {
  364. s->error = rws_error_new_code_descr(rws_error_code_connect_to_host, "Failed initialise winsock");
  365. s->command = COMMAND_INFORM_DISCONNECTED;
  366. return NULL;
  367. }
  368. #endif
  369. rws_sprintf(portstr, 16, "%i", s->port);
  370. while (++retry_number < RWS_CONNECT_ATTEMPS) {
  371. result = NULL;
  372. memset(&hints, 0, sizeof(hints));
  373. hints.ai_family = AF_UNSPEC;
  374. hints.ai_socktype = SOCK_STREAM;
  375. ret = getaddrinfo(s->host, portstr, &hints, &result);
  376. if (ret == 0 && result) {
  377. return result;
  378. }
  379. if (ret != 0) {
  380. last_ret = ret;
  381. }
  382. if (result) {
  383. freeaddrinfo(result);
  384. }
  385. rws_thread_sleep(RWS_CONNECT_RETRY_DELAY);
  386. }
  387. #if defined(RWS_OS_WINDOWS)
  388. WSACleanup();
  389. #endif
  390. s->error = rws_error_new_code_descr(rws_error_code_connect_to_host,
  391. (last_ret > 0) ? gai_strerror(last_ret) : "Failed connect to host");
  392. s->command = COMMAND_INFORM_DISCONNECTED;
  393. return NULL;
  394. }
  395. void rws_socket_connect_to_host(_rws_socket * s) {
  396. struct addrinfo * result = NULL;
  397. struct addrinfo * p = NULL;
  398. rws_socket_t sock = RWS_INVALID_SOCKET;
  399. int retry_number = 0;
  400. #if defined(RWS_OS_WINDOWS)
  401. unsigned long iMode = 0;
  402. #endif
  403. result = rws_socket_connect_getaddr_info(s);
  404. if (!result) {
  405. return;
  406. }
  407. while ((++retry_number < RWS_CONNECT_ATTEMPS) && (sock == RWS_INVALID_SOCKET)) {
  408. for (p = result; p != NULL; p = p->ai_next) {
  409. sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
  410. if (sock != RWS_INVALID_SOCKET) {
  411. rws_socket_set_option(sock, SO_ERROR, 1); // When an error occurs on a socket, set error variable so_error and notify process
  412. rws_socket_set_option(sock, SO_KEEPALIVE, 1); // Periodically test if connection is alive
  413. if (connect(sock, p->ai_addr, p->ai_addrlen) == 0) {
  414. s->received_len = 0;
  415. s->socket = sock;
  416. #if defined(RWS_OS_WINDOWS)
  417. // If iMode != 0, non-blocking mode is enabled.
  418. iMode = 1;
  419. ioctlsocket(s->socket, FIONBIO, &iMode);
  420. #else
  421. fcntl(s->socket, F_SETFL, O_NONBLOCK);
  422. #endif
  423. break;
  424. }
  425. RWS_SOCK_CLOSE(sock);
  426. }
  427. }
  428. if (sock == RWS_INVALID_SOCKET) {
  429. rws_thread_sleep(RWS_CONNECT_RETRY_DELAY);
  430. }
  431. }
  432. freeaddrinfo(result);
  433. if (s->socket == RWS_INVALID_SOCKET) {
  434. #if defined(RWS_OS_WINDOWS)
  435. WSACleanup();
  436. #endif
  437. s->error = rws_error_new_code_descr(rws_error_code_connect_to_host, "Failed connect to host");
  438. s->command = COMMAND_INFORM_DISCONNECTED;
  439. } else {
  440. s->command = COMMAND_SEND_HANDSHAKE;
  441. }
  442. }
  443. static void rws_socket_work_th_func(void * user_object) {
  444. _rws_socket * s = (_rws_socket *)user_object;
  445. size_t loop_number = 0;
  446. while (s->command < COMMAND_END) {
  447. loop_number++;
  448. rws_mutex_lock(s->work_mutex);
  449. switch (s->command) {
  450. case COMMAND_CONNECT_TO_HOST: rws_socket_connect_to_host(s); break;
  451. case COMMAND_SEND_HANDSHAKE: rws_socket_send_handshake(s); break;
  452. case COMMAND_WAIT_HANDSHAKE_RESPONCE: rws_socket_wait_handshake_responce(s); break;
  453. case COMMAND_DISCONNECT: rws_socket_send_disconnect(s); break;
  454. case COMMAND_IDLE:
  455. if (loop_number >= 400) {
  456. loop_number = 0;
  457. if (s->is_connected) {
  458. rws_socket_send_ping(s);
  459. }
  460. }
  461. if (s->is_connected) {
  462. rws_socket_idle_send(s);
  463. }
  464. if (s->is_connected) {
  465. rws_socket_idle_recv(s);
  466. }
  467. break;
  468. default: break;
  469. }
  470. rws_mutex_unlock(s->work_mutex);
  471. switch (s->command) {
  472. case COMMAND_INFORM_CONNECTED:
  473. s->command = COMMAND_IDLE;
  474. if (s->on_connected) {
  475. s->on_connected(s);
  476. }
  477. break;
  478. case COMMAND_INFORM_DISCONNECTED: {
  479. s->command = COMMAND_END;
  480. rws_socket_send_disconnect(s);
  481. if (s->on_disconnected) {
  482. s->on_disconnected(s);
  483. }
  484. }
  485. break;
  486. case COMMAND_IDLE:
  487. if (s->recvd_frames) {
  488. rws_socket_inform_recvd_frames(s);
  489. }
  490. break;
  491. default: break;
  492. }
  493. rws_thread_sleep(5);
  494. }
  495. rws_socket_close(s);
  496. s->work_thread = NULL;
  497. rws_socket_delete(s);
  498. }
  499. rws_bool rws_socket_create_start_work_thread(_rws_socket * s) {
  500. rws_error_delete_clean(&s->error);
  501. s->command = COMMAND_NONE;
  502. s->work_thread = rws_thread_create(&rws_socket_work_th_func, s);
  503. if (s->work_thread) {
  504. s->command = COMMAND_CONNECT_TO_HOST;
  505. return rws_true;
  506. }
  507. return rws_false;
  508. }
  509. void rws_socket_resize_received(_rws_socket * s, const size_t size) {
  510. void * res = NULL;
  511. size_t min = 0;
  512. if (size == s->received_size) {
  513. return;
  514. }
  515. res = rws_malloc(size);
  516. assert(res && (size > 0));
  517. min = (s->received_size < size) ? s->received_size : size;
  518. if (min > 0 && s->received) {
  519. memcpy(res, s->received, min);
  520. }
  521. rws_free_clean(&s->received);
  522. s->received = res;
  523. s->received_size = size;
  524. }
  525. void rws_socket_close(_rws_socket * s) {
  526. s->received_len = 0;
  527. if (s->socket != RWS_INVALID_SOCKET) {
  528. RWS_SOCK_CLOSE(s->socket);
  529. s->socket = RWS_INVALID_SOCKET;
  530. #if defined(RWS_OS_WINDOWS)
  531. WSACleanup();
  532. #endif
  533. }
  534. s->is_connected = rws_false;
  535. }
  536. void rws_socket_append_recvd_frames(_rws_socket * s, _rws_frame * frame) {
  537. _rws_node_value frame_list_var;
  538. frame_list_var.object = frame;
  539. if (s->recvd_frames) {
  540. rws_list_append(s->recvd_frames, frame_list_var);
  541. } else {
  542. s->recvd_frames = rws_list_create();
  543. s->recvd_frames->value = frame_list_var;
  544. }
  545. }
  546. void rws_socket_append_send_frames(_rws_socket * s, _rws_frame * frame) {
  547. _rws_node_value frame_list_var;
  548. frame_list_var.object = frame;
  549. if (s->send_frames) {
  550. rws_list_append(s->send_frames, frame_list_var);
  551. } else {
  552. s->send_frames = rws_list_create();
  553. s->send_frames->value = frame_list_var;
  554. }
  555. }
  556. rws_bool rws_socket_send_text_priv(_rws_socket * s, const char * text) {
  557. size_t len = text ? strlen(text) : 0;
  558. _rws_frame * frame = NULL;
  559. if (len <= 0) {
  560. return rws_false;
  561. }
  562. frame = rws_frame_create();
  563. frame->is_masked = rws_true;
  564. frame->opcode = rws_opcode_text_frame;
  565. rws_frame_fill_with_send_data(frame, text, len);
  566. rws_socket_append_send_frames(s, frame);
  567. return rws_true;
  568. }
  569. void rws_socket_delete_all_frames_in_list(_rws_list * list_with_frames) {
  570. _rws_frame * frame = NULL;
  571. _rws_node * cur = list_with_frames;
  572. while (cur) {
  573. frame = (_rws_frame *)cur->value.object;
  574. if (frame) {
  575. rws_frame_delete(frame);
  576. }
  577. cur->value.object = NULL;
  578. }
  579. }
  580. void rws_socket_set_option(rws_socket_t s, int option, int value) {
  581. setsockopt(s, SOL_SOCKET, option, (char *)&value, sizeof(int));
  582. }
  583. void rws_socket_check_write_error(_rws_socket * s, int error_num) {
  584. #if defined(RWS_OS_WINDOWS)
  585. int socket_code = 0, code = 0;
  586. unsigned int socket_code_size = sizeof(int);
  587. #else
  588. int socket_code = 0, code = 0;
  589. socklen_t socket_code_size = sizeof(socket_code);
  590. #endif
  591. if (s->socket != RWS_INVALID_SOCKET) {
  592. #if defined(RWS_OS_WINDOWS)
  593. if (getsockopt(s->socket, SOL_SOCKET, SO_ERROR, (char *)&socket_code, (int*)&socket_code_size) != 0) {
  594. socket_code = 0;
  595. }
  596. #else
  597. if (getsockopt(s->socket, SOL_SOCKET, SO_ERROR, &socket_code, &socket_code_size) != 0) {
  598. socket_code = 0;
  599. }
  600. #endif
  601. }
  602. code = (socket_code > 0) ? socket_code : error_num;
  603. if (code <= 0) {
  604. return;
  605. }
  606. switch (code) {
  607. // send errors
  608. case EACCES: //
  609. // case EAGAIN: // The socket is marked nonblocking and the requested operation would block
  610. // case EWOULDBLOCK: // The socket is marked nonblocking and the receive operation would block
  611. case EBADF: // An invalid descriptor was specified
  612. case ECONNRESET: // Connection reset by peer
  613. case EDESTADDRREQ: // The socket is not connection-mode, and no peer address is set
  614. case EFAULT: // An invalid user space address was specified for an argument
  615. // The receive buffer pointer(s) point outside the process's address space.
  616. case EINTR: // A signal occurred before any data was transmitted
  617. // The receive was interrupted by delivery of a signal before any data were available
  618. case EINVAL: // Invalid argument passed
  619. case EISCONN: // The connection-mode socket was connected already but a recipient was specified
  620. case EMSGSIZE: // The socket type requires that message be sent atomically, and the size of the message to be sent made this impossible
  621. case ENOBUFS: // The output queue for a network interface was full
  622. case ENOMEM: // No memory available
  623. case ENOTCONN: // The socket is not connected, and no target has been given
  624. // The socket is associated with a connection-oriented protocol and has not been connected
  625. case ENOTSOCK: // The argument sockfd is not a socket
  626. // The argument sockfd does not refer to a socket
  627. case EOPNOTSUPP: // Some bit in the flags argument is inappropriate for the socket type.
  628. case EPIPE: // The local end has been shut down on a connection oriented socket
  629. // recv errors
  630. case ECONNREFUSED: // A remote host refused to allow the network connection (typically because it is not running the requested service).
  631. s->error = rws_error_new_code_descr(rws_error_code_read_write_socket, rws_strerror(code));
  632. break;
  633. default:
  634. break;
  635. }
  636. }