Lines Matching refs:s
41 unsigned int rws_socket_get_next_message_id(rws_socket s) { in rws_socket_get_next_message_id() argument
42 const unsigned int mess_id = ++s->next_message_id; in rws_socket_get_next_message_id()
44 s->next_message_id = 0; in rws_socket_get_next_message_id()
49 rws_bool rws_socket_send_ping_priv(rws_socket s) { in rws_socket_send_ping_priv() argument
57 len = rws_sprintf(buff, 16, "%u", rws_socket_get_next_message_id(s)); in rws_socket_send_ping_priv()
62 rws_socket_append_send_frames(s, frame); in rws_socket_send_ping_priv()
67 void rws_socket_inform_recvd_frames(rws_socket s) { in rws_socket_inform_recvd_frames() argument
70 _rws_node * cur = s->recvd_frames; in rws_socket_inform_recvd_frames()
78 if (s->on_recvd_bin) { in rws_socket_inform_recvd_frames()
79 s->on_recvd_bin(s, frame->data, (unsigned int)frame->data_size, frame->is_finished); in rws_socket_inform_recvd_frames()
83 if (s->on_recvd_text) { in rws_socket_inform_recvd_frames()
84 …s->on_recvd_text(s, (const char *)frame->data, (unsigned int)frame->data_size, frame->is_finished… in rws_socket_inform_recvd_frames()
88 if (s->on_recvd_bin) { in rws_socket_inform_recvd_frames()
89 s->on_recvd_bin(s, frame->data, (unsigned int)frame->data_size, frame->is_finished); in rws_socket_inform_recvd_frames()
93 if (s->on_recvd_pong) { in rws_socket_inform_recvd_frames()
94 s->on_recvd_pong(s); in rws_socket_inform_recvd_frames()
108 rws_list_delete_clean(&s->recvd_frames); in rws_socket_inform_recvd_frames()
113 const char * s = NULL; in rws_socket_read_handshake_responce_value() local
119 s = str; in rws_socket_read_handshake_responce_value()
120 while (*s != '\r' && *s != '\n') { in rws_socket_read_handshake_responce_value()
121 s++; in rws_socket_read_handshake_responce_value()
129 rws_bool rws_socket_process_handshake_responce(rws_socket s) { in rws_socket_process_handshake_responce() argument
130 const char * str = (const char *)s->received; in rws_socket_process_handshake_responce()
137 rws_error_delete_clean(&s->error); in rws_socket_process_handshake_responce()
164 rws_socket_read_handshake_responce_value(sub, &s->sec_ws_accept); in rws_socket_process_handshake_responce()
167 if (http_code != 101 || !s->sec_ws_accept) { in rws_socket_process_handshake_responce()
168 s->error = rws_error_new_code_descr(rws_error_code_parse_handshake, in rws_socket_process_handshake_responce()
176 rws_bool rws_socket_send(rws_socket s, const void * data, const size_t data_size) { in rws_socket_send() argument
179 rws_error_delete_clean(&s->error); in rws_socket_send()
183 if (s->scheme && strcmp(s->scheme, "wss") == 0) { in rws_socket_send()
184 if (s->ssl) in rws_socket_send()
185 …sended = mbedtls_ssl_write(&(s->ssl->ssl_ctx), (const unsigned char *)(data + writtenLen), (int)(d… in rws_socket_send()
194 sended = (int)send(s->socket, (data + writtenLen), (int)(data_size - writtenLen), 0); in rws_socket_send()
212 rws_socket_check_write_error(s, error_number); in rws_socket_send()
213 if (s->error) { in rws_socket_send()
215 rws_socket_close(s); in rws_socket_send()
221 rws_bool rws_socket_recv(rws_socket s) { in rws_socket_recv() argument
229 buffer = s->recv_buffer; in rws_socket_recv()
230 buffer_size = s->recv_buffer_size; in rws_socket_recv()
235 rws_error_delete_clean(&s->error); in rws_socket_recv()
239 if(s->scheme && strcmp(s->scheme, "wss") == 0) { in rws_socket_recv()
240 if (s->ssl) { in rws_socket_recv()
241 len = mbedtls_ssl_read(&(s->ssl->ssl_ctx), (unsigned char *)buffer, buffer_size); in rws_socket_recv()
249 len = (int)recv(s->socket, buffer, buffer_size, 0); in rws_socket_recv()
254 if (s->received_size - s->received_len < len) { in rws_socket_recv()
256 if (s->received_size + len > MAX_THRESH_VAL) { in rws_socket_recv()
258 s->received_len = 0; in rws_socket_recv()
263 rws_socket_resize_received(s, s->received_size + len); in rws_socket_recv()
265 received = (char *)s->received; in rws_socket_recv()
266 if (s->received_len) { in rws_socket_recv()
267 received += s->received_len; in rws_socket_recv()
270 s->received_len += len; in rws_socket_recv()
276 if(s->scheme && strcmp(s->scheme, "wss") == 0) { in rws_socket_recv()
302 s->error = rws_error_new_code_descr(rws_error_code_read_write_socket, "Failed read/write socket"); in rws_socket_recv()
303 rws_socket_close(s); in rws_socket_recv()
310 _rws_frame * rws_socket_last_unfin_recvd_frame_by_opcode(rws_socket s, const rws_opcode opcode) { in rws_socket_last_unfin_recvd_frame_by_opcode() argument
313 _rws_node * cur = s->recvd_frames; in rws_socket_last_unfin_recvd_frame_by_opcode()
327 void rws_socket_process_bin_or_text_frame(rws_socket s, _rws_frame * frame) { in rws_socket_process_bin_or_text_frame() argument
328 _rws_frame * last_unfin = rws_socket_last_unfin_recvd_frame_by_opcode(s, frame->opcode); in rws_socket_process_bin_or_text_frame()
334 rws_socket_append_recvd_frames(s, frame); in rws_socket_process_bin_or_text_frame()
336 rws_socket_append_recvd_frames(s, frame); in rws_socket_process_bin_or_text_frame()
342 void rws_socket_process_pong_frame(rws_socket s, _rws_frame * frame) { in rws_socket_process_pong_frame() argument
343 rws_socket_append_recvd_frames(s, frame); in rws_socket_process_pong_frame()
346 void rws_socket_process_ping_frame(rws_socket s, _rws_frame * frame) { in rws_socket_process_ping_frame() argument
352 rws_socket_append_send_frames(s, pong_frame); in rws_socket_process_ping_frame()
355 void rws_socket_process_conn_close_frame(rws_socket s, _rws_frame * frame) { in rws_socket_process_conn_close_frame() argument
356 s->command = COMMAND_INFORM_DISCONNECTED; in rws_socket_process_conn_close_frame()
357 …s->error = rws_error_new_code_descr(rws_error_code_connection_closed, "Connection was closed by en… in rws_socket_process_conn_close_frame()
362 void rws_socket_process_received_frame(rws_socket s, _rws_frame * frame) { in rws_socket_process_received_frame() argument
365 case rws_opcode_ping: rws_socket_process_ping_frame(s, frame); break; in rws_socket_process_received_frame()
368 if (s->on_recvd_pong) s->on_recvd_pong(s); in rws_socket_process_received_frame()
374 rws_socket_process_bin_or_text_frame(s, frame); in rws_socket_process_received_frame()
376 case rws_opcode_connection_close: rws_socket_process_conn_close_frame(s, frame); break; in rws_socket_process_received_frame()
384 void rws_socket_idle_recv(rws_socket s) { in rws_socket_idle_recv() argument
387 if (!rws_socket_recv(s)) { in rws_socket_idle_recv()
389 if (s->error) { in rws_socket_idle_recv()
390 s->command = COMMAND_INFORM_DISCONNECTED; in rws_socket_idle_recv()
395 const size_t nframe_size = rws_check_recv_frame_size(s->received, s->received_len); in rws_socket_idle_recv()
397 frame = rws_frame_create_with_recv_data(s->received, nframe_size); in rws_socket_idle_recv()
399 rws_socket_process_received_frame(s, frame); in rws_socket_idle_recv()
402 if (nframe_size == s->received_len) { in rws_socket_idle_recv()
403 s->received_len = 0; in rws_socket_idle_recv()
404 } else if (s->received_len > nframe_size) { in rws_socket_idle_recv()
405 const size_t nLeftLen = s->received_len - nframe_size; in rws_socket_idle_recv()
406 memmove((char*)s->received, (char*)s->received + nframe_size, nLeftLen); in rws_socket_idle_recv()
407 s->received_len = nLeftLen; in rws_socket_idle_recv()
412 void rws_socket_idle_send(rws_socket s) { in rws_socket_idle_send() argument
417 rws_mutex_lock(s->send_mutex); in rws_socket_idle_send()
418 cur = s->send_frames; in rws_socket_idle_send()
420 while (cur && s->is_connected && sending) { in rws_socket_idle_send()
424 sending = rws_socket_send(s, frame->data, frame->data_size); in rws_socket_idle_send()
428 s->on_send_ping) { in rws_socket_idle_send()
429 s->on_send_ping(s); in rws_socket_idle_send()
436 rws_socket_delete_all_frames_in_list(s->send_frames); in rws_socket_idle_send()
437 rws_list_delete_clean(&s->send_frames); in rws_socket_idle_send()
438 if (s->error) { in rws_socket_idle_send()
439 s->command = COMMAND_INFORM_DISCONNECTED; in rws_socket_idle_send()
442 rws_mutex_unlock(s->send_mutex); in rws_socket_idle_send()
445 void rws_socket_wait_handshake_responce(rws_socket s) { in rws_socket_wait_handshake_responce() argument
446 if (!rws_socket_recv(s)) { in rws_socket_wait_handshake_responce()
448 if (s->error) { in rws_socket_wait_handshake_responce()
449 s->command = COMMAND_INFORM_DISCONNECTED; in rws_socket_wait_handshake_responce()
454 if (s->received_len == 0) { in rws_socket_wait_handshake_responce()
458 if (rws_socket_process_handshake_responce(s)) { in rws_socket_wait_handshake_responce()
459 s->received_len = 0; in rws_socket_wait_handshake_responce()
460 s->is_connected = rws_true; in rws_socket_wait_handshake_responce()
461 s->command = COMMAND_INFORM_CONNECTED; in rws_socket_wait_handshake_responce()
463 rws_socket_close(s); in rws_socket_wait_handshake_responce()
464 s->command = COMMAND_INFORM_DISCONNECTED; in rws_socket_wait_handshake_responce()
468 void rws_socket_send_disconnect(rws_socket s) { in rws_socket_send_disconnect() argument
473 if (s->socket == RWS_INVALID_SOCKET) in rws_socket_send_disconnect()
478 len = rws_sprintf(buff, 16, "%u", rws_socket_get_next_message_id(s)); in rws_socket_send_disconnect()
483 rws_socket_send(s, frame->data, frame->data_size); in rws_socket_send_disconnect()
485 s->command = COMMAND_END; in rws_socket_send_disconnect()
489 void rws_socket_send_handshake(rws_socket s) { in rws_socket_send_handshake() argument
493 char * protocol = (s->sec_ws_protocol ? s->sec_ws_protocol : "chat, superchat"); in rws_socket_send_handshake()
494 writed = rws_sprintf(ptr, 512, "GET %s HTTP/%s\r\n", s->path, k_rws_socket_min_http_ver); in rws_socket_send_handshake()
496 if (s->port == 80) { in rws_socket_send_handshake()
497 writed += rws_sprintf(ptr + writed, 512 - writed, "Host: %s\r\n", s->host); in rws_socket_send_handshake()
499 writed += rws_sprintf(ptr + writed, 512 - writed, "Host: %s:%i\r\n", s->host, s->port); in rws_socket_send_handshake()
506 s->scheme, s->host); in rws_socket_send_handshake()
515 if (rws_socket_send(s, buff, writed)) { in rws_socket_send_handshake()
516 s->command = COMMAND_WAIT_HANDSHAKE_RESPONCE; in rws_socket_send_handshake()
518 if (s->error) { in rws_socket_send_handshake()
519 s->error->code = rws_error_code_send_handshake; in rws_socket_send_handshake()
521 s->error = rws_error_new_code_descr(rws_error_code_send_handshake, "Send handshake"); in rws_socket_send_handshake()
523 rws_socket_close(s); in rws_socket_send_handshake()
524 s->command = COMMAND_INFORM_DISCONNECTED; in rws_socket_send_handshake()
528 struct addrinfo * rws_socket_connect_getaddr_info(rws_socket s) { in rws_socket_connect_getaddr_info() argument
534 rws_error_delete_clean(&s->error); in rws_socket_connect_getaddr_info()
536 rws_sprintf(portstr, 16, "%i", s->port); in rws_socket_connect_getaddr_info()
543 ret = getaddrinfo(s->host, portstr, &hints, &result); in rws_socket_connect_getaddr_info()
563 s->error = rws_error_new_code_descr(rws_error_code_connect_to_host, in rws_socket_connect_getaddr_info()
565 s->command = COMMAND_INFORM_DISCONNECTED; in rws_socket_connect_getaddr_info()
571 void rws_socket_connect_to_host(rws_socket s) { in rws_socket_connect_to_host() argument
577 result = rws_socket_connect_getaddr_info(s); in rws_socket_connect_to_host()
590 s->received_len = 0; in rws_socket_connect_to_host()
591 s->socket = sock; in rws_socket_connect_to_host()
592 fcntl(s->socket, F_SETFL, O_NONBLOCK); in rws_socket_connect_to_host()
608 if (s->socket == RWS_INVALID_SOCKET) { in rws_socket_connect_to_host()
610 s->error = rws_error_new_code_descr(rws_error_code_connect_to_host, "Failed connect to host"); in rws_socket_connect_to_host()
611 s->command = COMMAND_INFORM_DISCONNECTED; in rws_socket_connect_to_host()
613 s->command = COMMAND_SEND_HANDSHAKE; in rws_socket_connect_to_host()
618 rws_socket s = (rws_socket)user_object; in rws_socket_work_th_func() local
620 while (s->command < COMMAND_END) { in rws_socket_work_th_func()
621 rws_mutex_lock(s->work_mutex); in rws_socket_work_th_func()
622 switch (s->command) { in rws_socket_work_th_func()
626 if(s->scheme && strcmp(s->scheme, "wss") == 0) { in rws_socket_work_th_func()
627 rws_ssl_conn(s); in rws_socket_work_th_func()
628 DBG("after rws_ssl_conn %d\n", s->command); in rws_socket_work_th_func()
634 rws_socket_connect_to_host(s); break; in rws_socket_work_th_func()
637 case COMMAND_SEND_HANDSHAKE: rws_socket_send_handshake(s); break; in rws_socket_work_th_func()
638 case COMMAND_WAIT_HANDSHAKE_RESPONCE: rws_socket_wait_handshake_responce(s); break; in rws_socket_work_th_func()
639 case COMMAND_DISCONNECT: rws_socket_send_disconnect(s); break; in rws_socket_work_th_func()
641 if (s->is_connected) { in rws_socket_work_th_func()
642 rws_socket_idle_send(s); in rws_socket_work_th_func()
645 if (s->is_connected) { in rws_socket_work_th_func()
646 rws_socket_idle_recv(s); in rws_socket_work_th_func()
652 rws_mutex_unlock(s->work_mutex); in rws_socket_work_th_func()
653 switch (s->command) { in rws_socket_work_th_func()
655 s->command = COMMAND_IDLE; in rws_socket_work_th_func()
656 if (s->on_connected) { in rws_socket_work_th_func()
657 s->on_connected(s); in rws_socket_work_th_func()
661 s->command = COMMAND_END; in rws_socket_work_th_func()
662 rws_error last_error = rws_socket_get_error(s); in rws_socket_work_th_func()
663 s->error = NULL; in rws_socket_work_th_func()
664 rws_socket_send_disconnect(s); in rws_socket_work_th_func()
666 if (s->error) { in rws_socket_work_th_func()
667 rws_error_delete_clean(&s->error); in rws_socket_work_th_func()
669 s->error = last_error; in rws_socket_work_th_func()
671 if (s->on_disconnected) { in rws_socket_work_th_func()
672 s->on_disconnected(s); in rws_socket_work_th_func()
678 if (s->recvd_frames) { in rws_socket_work_th_func()
679 rws_socket_inform_recvd_frames(s); in rws_socket_work_th_func()
687 rws_socket_close(s); in rws_socket_work_th_func()
688 s->work_thread = NULL; in rws_socket_work_th_func()
689 rws_sem_signal(s->exit_sem); in rws_socket_work_th_func()
693 rws_bool rws_socket_create_start_work_thread(rws_socket s) { in rws_socket_create_start_work_thread() argument
694 rws_error_delete_clean(&s->error); in rws_socket_create_start_work_thread()
695 s->command = COMMAND_NONE; in rws_socket_create_start_work_thread()
696 s->work_thread = rws_thread_create(&rws_socket_work_th_func, s); in rws_socket_create_start_work_thread()
697 if (s->work_thread) { in rws_socket_create_start_work_thread()
698 s->command = COMMAND_CONNECT_TO_HOST; in rws_socket_create_start_work_thread()
704 void rws_socket_resize_received(rws_socket s, const size_t need_size) { in rws_socket_resize_received() argument
709 if (need_size == s->received_size) { in rws_socket_resize_received()
720 min = (s->received_size < size) ? s->received_size : size; in rws_socket_resize_received()
721 if (min > 0 && s->received) { in rws_socket_resize_received()
722 memcpy(res, s->received, min); in rws_socket_resize_received()
724 rws_free_clean(&s->received); in rws_socket_resize_received()
725 s->received = res; in rws_socket_resize_received()
726 s->received_size = size; in rws_socket_resize_received()
729 void rws_socket_close(rws_socket s) { in rws_socket_close() argument
730 s->received_len = 0; in rws_socket_close()
731 if (s->socket != RWS_INVALID_SOCKET) { in rws_socket_close()
733 if(s->scheme && strcmp(s->scheme, "wss") == 0) in rws_socket_close()
734 rws_ssl_close(s); in rws_socket_close()
737 RWS_SOCK_CLOSE(s->socket); in rws_socket_close()
738 s->socket = RWS_INVALID_SOCKET; in rws_socket_close()
740 s->is_connected = rws_false; in rws_socket_close()
743 void rws_socket_append_recvd_frames(rws_socket s, _rws_frame * frame) { in rws_socket_append_recvd_frames() argument
747 if (s->recvd_frames) { in rws_socket_append_recvd_frames()
748 rws_list_append(s->recvd_frames, frame_list_var); in rws_socket_append_recvd_frames()
750 s->recvd_frames = rws_list_create(); in rws_socket_append_recvd_frames()
751 s->recvd_frames->value = frame_list_var; in rws_socket_append_recvd_frames()
755 void rws_socket_append_send_frames(rws_socket s, _rws_frame * frame) { in rws_socket_append_send_frames() argument
758 if (s->send_frames) { in rws_socket_append_send_frames()
759 rws_list_append(s->send_frames, frame_list_var); in rws_socket_append_send_frames()
761 s->send_frames = rws_list_create(); in rws_socket_append_send_frames()
762 s->send_frames->value = frame_list_var; in rws_socket_append_send_frames()
766 rws_bool rws_socket_send_text_priv(rws_socket s, const char * text) { in rws_socket_send_text_priv() argument
778 rws_socket_append_send_frames(s, frame); in rws_socket_send_text_priv()
783 rws_bool rws_socket_send_bin_priv(rws_socket s, const char * bin, size_t size, rws_binary bin_type) in rws_socket_send_bin_priv() argument
807 rws_socket_append_send_frames(s, frame); in rws_socket_send_bin_priv()
818 rws_bool rws_socket_send_bin_start_priv(rws_socket s, const char *bin, size_t len) { in rws_socket_send_bin_start_priv() argument
830 rws_socket_append_send_frames(s, frame); in rws_socket_send_bin_start_priv()
835 rws_bool rws_socket_send_bin_continue_priv(rws_socket s, const char *bin, size_t len) { in rws_socket_send_bin_continue_priv() argument
847 rws_socket_append_send_frames(s, frame); in rws_socket_send_bin_continue_priv()
852 rws_bool rws_socket_send_bin_finish_priv(rws_socket s, const char *bin, size_t len) { in rws_socket_send_bin_finish_priv() argument
864 rws_socket_append_send_frames(s, frame); in rws_socket_send_bin_finish_priv()
882 void rws_socket_set_option(rws_socket_t s, int option, int value) { in rws_socket_set_option() argument
883 setsockopt(s, SOL_SOCKET, option, (char *)&value, sizeof(int)); in rws_socket_set_option()
886 void rws_socket_check_write_error(rws_socket s, int error_num) { in rws_socket_check_write_error() argument
890 if (s->socket != RWS_INVALID_SOCKET) { in rws_socket_check_write_error()
891 if (getsockopt(s->socket, SOL_SOCKET, SO_ERROR, &socket_code, &socket_code_size) != 0) { in rws_socket_check_write_error()
929 s->error = rws_error_new_code_descr(rws_error_code_read_write_socket, rws_strerror(code)); in rws_socket_check_write_error()
1098 int rws_ssl_conn(rws_socket s) in rws_ssl_conn() argument
1107 s->ssl = rws_malloc_zero(sizeof(_rws_ssl)); in rws_ssl_conn()
1108 if (!s->ssl) { in rws_ssl_conn()
1114 ssl = s->ssl; in rws_ssl_conn()
1116 if (s->server_cert) in rws_ssl_conn()
1148 if (s->client_cert && s->client_pk) { in rws_ssl_conn()
1149 …ret = mbedtls_x509_crt_parse(&ssl->clicert, (const unsigned char *)s->client_cert, s->client_cert_… in rws_ssl_conn()
1156 …ret = mbedtls_pk_parse_key(&ssl->pkey, (const unsigned char *)s->client_pk, s->client_pk_len, NULL… in rws_ssl_conn()
1168 if (s->server_cert && ((value = mbedtls_x509_crt_parse(&ssl->cacert, in rws_ssl_conn()
1169 (const unsigned char *)s->server_cert, in rws_ssl_conn()
1170 s->server_cert_len)) < 0)) { in rws_ssl_conn()
1186 rws_socket_connect_to_host(s); in rws_ssl_conn()
1187 if (s->socket == RWS_INVALID_SOCKET){ in rws_ssl_conn()
1193 ssl->net_ctx.fd = s->socket; in rws_ssl_conn()
1217 …if (s->client_cert && (ret = mbedtls_ssl_conf_own_cert(&ssl->ssl_conf, &ssl->clicert, &ssl->pkey))… in rws_ssl_conn()
1272 s->error = rws_error_new_code_descr(rws_error_code_connect_to_host, "Failed connect to host"); in rws_ssl_conn()
1273 ERR("%s: code:%d, error:%s", __FUNCTION__, s->error->code, s->error->description); in rws_ssl_conn()
1274 s->command = COMMAND_INFORM_DISCONNECTED; in rws_ssl_conn()
1275 rws_ssl_close(s); in rws_ssl_conn()
1280 int rws_ssl_close(rws_socket s) in rws_ssl_close() argument
1282 _rws_ssl *ssl = s->ssl; in rws_ssl_close()
1287 s->ssl = NULL; in rws_ssl_close()
1288 s->client_cert = NULL; in rws_ssl_close()
1289 s->server_cert = NULL; in rws_ssl_close()
1290 s->client_pk = NULL; in rws_ssl_close()