1
1
/*
2
- * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
2
+ * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*/
@@ -545,6 +545,42 @@ static esp_err_t esp_websocket_client_create_transport(esp_websocket_client_hand
545
545
return ESP_OK ;
546
546
}
547
547
548
+ static bool esp_websocket_client_send_with_exact_opcode (esp_websocket_client_handle_t client , ws_transport_opcodes_t opcode , const uint8_t * data , int len , TickType_t timeout )
549
+ {
550
+ int ret = -1 ;
551
+ int need_write = len ;
552
+ int wlen = 0 , widx = 0 ;
553
+
554
+ while (widx < len || opcode ) { // allow for sending "current_opcode" only message with len==0
555
+ if (need_write > client -> buffer_size ) {
556
+ need_write = client -> buffer_size ;
557
+ }
558
+ memcpy (client -> tx_buffer , data + widx , need_write );
559
+ // send with ws specific way and specific opcode
560
+ wlen = esp_transport_ws_send_raw (client -> transport , opcode , (char * )client -> tx_buffer , need_write ,
561
+ (timeout == portMAX_DELAY ) ? -1 : timeout * portTICK_PERIOD_MS );
562
+ if (wlen < 0 || (wlen == 0 && need_write != 0 )) {
563
+ ret = wlen ;
564
+ esp_websocket_free_buf (client , true);
565
+ esp_tls_error_handle_t error_handle = esp_transport_get_error_handle (client -> transport );
566
+ if (error_handle ) {
567
+ esp_websocket_client_error (client , "esp_transport_write() returned %d, transport_error=%s, tls_error_code=%i, tls_flags=%i, errno=%d" ,
568
+ ret , esp_err_to_name (error_handle -> last_error ), error_handle -> esp_tls_error_code ,
569
+ error_handle -> esp_tls_flags , errno );
570
+ } else {
571
+ esp_websocket_client_error (client , "esp_transport_write() returned %d, errno=%d" , ret , errno );
572
+ }
573
+ esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
574
+ return false;
575
+ }
576
+ opcode = 0 ;
577
+ widx += wlen ;
578
+ need_write = len - widx ;
579
+ }
580
+ esp_websocket_free_buf (client , true);
581
+ return true;
582
+ }
583
+
548
584
esp_websocket_client_handle_t esp_websocket_client_init (const esp_websocket_client_config_t * config )
549
585
{
550
586
esp_websocket_client_handle_t client = calloc (1 , sizeof (struct esp_websocket_client ));
@@ -1092,17 +1128,33 @@ int esp_websocket_client_send_text(esp_websocket_client_handle_t client, const c
1092
1128
return esp_websocket_client_send_with_opcode (client , WS_TRANSPORT_OPCODES_TEXT , (const uint8_t * )data , len , timeout );
1093
1129
}
1094
1130
1131
+ int esp_websocket_client_send_text_partial (esp_websocket_client_handle_t client , const char * data , int len , TickType_t timeout )
1132
+ {
1133
+ return esp_websocket_client_send_with_exact_opcode (client , WS_TRANSPORT_OPCODES_TEXT , (const uint8_t * )data , len , timeout );
1134
+ }
1135
+
1136
+ int esp_websocket_client_send_cont_msg (esp_websocket_client_handle_t client , const char * data , int len , TickType_t timeout )
1137
+ {
1138
+ return esp_websocket_client_send_with_exact_opcode (client , WS_TRANSPORT_OPCODES_CONT , (const uint8_t * )data , len , timeout );
1139
+ }
1140
+
1095
1141
int esp_websocket_client_send_bin (esp_websocket_client_handle_t client , const char * data , int len , TickType_t timeout )
1096
1142
{
1097
1143
return esp_websocket_client_send_with_opcode (client , WS_TRANSPORT_OPCODES_BINARY , (const uint8_t * )data , len , timeout );
1098
1144
}
1099
1145
1100
- int esp_websocket_client_send_with_opcode (esp_websocket_client_handle_t client , ws_transport_opcodes_t opcode , const uint8_t * data , int len , TickType_t timeout )
1146
+ int esp_websocket_client_send_bin_partial (esp_websocket_client_handle_t client , const char * data , int len , TickType_t timeout )
1101
1147
{
1102
- int need_write = len ;
1103
- int wlen = 0 , widx = 0 ;
1104
- int ret = ESP_FAIL ;
1148
+ return esp_websocket_client_send_with_exact_opcode (client , WS_TRANSPORT_OPCODES_BINARY , (const uint8_t * )data , len , timeout );
1149
+ }
1105
1150
1151
+ int esp_websocket_client_send_fin (esp_websocket_client_handle_t client , TickType_t timeout )
1152
+ {
1153
+ return esp_websocket_client_send_with_exact_opcode (client , WS_TRANSPORT_OPCODES_FIN , NULL , 0 , timeout );
1154
+ }
1155
+
1156
+ int esp_websocket_client_send_with_opcode (esp_websocket_client_handle_t client , ws_transport_opcodes_t opcode , const uint8_t * data , int len , TickType_t timeout )
1157
+ {
1106
1158
if (client == NULL || len < 0 || (data == NULL && len > 0 )) {
1107
1159
ESP_LOGE (TAG , "Invalid arguments" );
1108
1160
return ESP_FAIL ;
@@ -1126,41 +1178,13 @@ int esp_websocket_client_send_with_opcode(esp_websocket_client_handle_t client,
1126
1178
ESP_LOGE (TAG , "Failed to setup tx buffer" );
1127
1179
goto unlock_and_return ;
1128
1180
}
1129
- uint32_t current_opcode = opcode ;
1130
- while (widx < len || current_opcode ) { // allow for sending "current_opcode" only message with len==0
1131
- if (need_write > client -> buffer_size ) {
1132
- need_write = client -> buffer_size ;
1133
- } else {
1134
- current_opcode |= WS_TRANSPORT_OPCODES_FIN ;
1135
- }
1136
- memcpy (client -> tx_buffer , data + widx , need_write );
1137
- // send with ws specific way and specific opcode
1138
- wlen = esp_transport_ws_send_raw (client -> transport , current_opcode , (char * )client -> tx_buffer , need_write ,
1139
- (timeout == portMAX_DELAY ) ? -1 : timeout * portTICK_PERIOD_MS );
1140
- if (wlen < 0 || (wlen == 0 && need_write != 0 )) {
1141
- ret = wlen ;
1142
- esp_websocket_free_buf (client , true);
1143
- esp_tls_error_handle_t error_handle = esp_transport_get_error_handle (client -> transport );
1144
- if (error_handle ) {
1145
- esp_websocket_client_error (client , "esp_transport_write() returned %d, transport_error=%s, tls_error_code=%i, tls_flags=%i, errno=%d" ,
1146
- ret , esp_err_to_name (error_handle -> last_error ), error_handle -> esp_tls_error_code ,
1147
- error_handle -> esp_tls_flags , errno );
1148
- } else {
1149
- esp_websocket_client_error (client , "esp_transport_write() returned %d, errno=%d" , ret , errno );
1150
- }
1151
- esp_websocket_client_abort_connection (client , WEBSOCKET_ERROR_TYPE_TCP_TRANSPORT );
1152
- goto unlock_and_return ;
1153
- }
1154
- current_opcode = 0 ;
1155
- widx += wlen ;
1156
- need_write = len - widx ;
1157
-
1181
+ if (esp_websocket_client_send_with_exact_opcode (client , opcode | WS_TRANSPORT_OPCODES_FIN , data , len , timeout ) != true) {
1182
+ ESP_LOGE (TAG , "Failed to send the buffer" );
1183
+ goto unlock_and_return ;
1158
1184
}
1159
- ret = widx ;
1160
- esp_websocket_free_buf (client , true);
1161
1185
unlock_and_return :
1162
1186
xSemaphoreGiveRecursive (client -> lock );
1163
- return ret ;
1187
+ return ESP_FAIL ;
1164
1188
}
1165
1189
1166
1190
bool esp_websocket_client_is_connected (esp_websocket_client_handle_t client )
0 commit comments