Skip to content

Commit c64a082

Browse files
committed
Merge branch 'write_big_data'
2 parents 522a67b + c911776 commit c64a082

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

src/WebSockets.cpp

+56-3
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,18 @@ bool WebSockets::sendFrame(WSclient_t * client, WSopcode_t opcode, uint8_t * pay
233233
// header has be added to payload
234234
// payload is forced to reserved 14 Byte but we may not need all based on the length and mask settings
235235
// offset in payload is calculatetd 14 - headerSize
236-
if(client->tcp->write(&payloadPtr[(WEBSOCKETS_MAX_HEADER_SIZE - headerSize)], (length + headerSize)) != (length + headerSize)) {
236+
if(write(client, &payloadPtr[(WEBSOCKETS_MAX_HEADER_SIZE - headerSize)], (length + headerSize)) != (length + headerSize)) {
237237
ret = false;
238238
}
239239
} else {
240240
// send header
241-
if(client->tcp->write(&buffer[0], headerSize) != headerSize) {
241+
if(write(client, &buffer[0], headerSize) != headerSize) {
242242
ret = false;
243243
}
244244

245245
if(payloadPtr && length > 0) {
246246
// send payload
247-
if(client->tcp->write(&payloadPtr[0], length) != length) {
247+
if(write(client, &payloadPtr[0], length) != length) {
248248
ret = false;
249249
}
250250
}
@@ -593,3 +593,56 @@ bool WebSockets::readCb(WSclient_t * client, uint8_t * out, size_t n, WSreadWait
593593
#endif
594594
return true;
595595
}
596+
597+
/**
598+
* write x byte to tcp or get timeout
599+
* @param client WSclient_t *
600+
* @param out uint8_t * data buffer
601+
* @param n size_t byte count
602+
* @return bytes send
603+
*/
604+
size_t WebSockets::write(WSclient_t * client, uint8_t *out, size_t n) {
605+
if(out == NULL) return 0;
606+
if(client == NULL) return 0;
607+
unsigned long t = millis();
608+
size_t len = 0;
609+
size_t total = 0;
610+
DEBUG_WEBSOCKETS("[write] n: %d t: %d\n", n, t);
611+
while(n > 0) {
612+
if(client->tcp == NULL) {
613+
DEBUG_WEBSOCKETS("[write] tcp is null!\n");
614+
break;
615+
}
616+
617+
if(!client->tcp->connected()) {
618+
DEBUG_WEBSOCKETS("[write] not connected!\n");
619+
break;
620+
}
621+
622+
if((millis() - t) > WEBSOCKETS_TCP_TIMEOUT) {
623+
DEBUG_WEBSOCKETS("[write] write TIMEOUT! %d\n", (millis() - t));
624+
break;
625+
}
626+
627+
len = client->tcp->write(out, n);
628+
if(len) {
629+
t = millis();
630+
out += len;
631+
n -= len;
632+
total += len;
633+
//DEBUG_WEBSOCKETS("write %d left %d!\n", len, n);
634+
} else {
635+
//DEBUG_WEBSOCKETS("write %d failed left %d!\n", len, n);
636+
}
637+
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
638+
delay(0);
639+
#endif
640+
}
641+
return total;
642+
}
643+
644+
size_t WebSockets::write(WSclient_t * client, const char *out) {
645+
if(client == NULL) return 0;
646+
if(out == NULL) return 0;
647+
return write(client, (uint8_t*)out, strlen(out));
648+
}

src/WebSockets.h

+2
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,8 @@ class WebSockets {
270270
String base64_encode(uint8_t * data, size_t length);
271271

272272
bool readCb(WSclient_t * client, uint8_t *out, size_t n, WSreadWaitCb cb);
273+
virtual size_t write(WSclient_t * client, uint8_t *out, size_t n);
274+
size_t write(WSclient_t * client, const char *out);
273275

274276

275277
};

src/WebSocketsClient.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ void WebSocketsClient::sendHeader(WSclient_t * client) {
528528
handshake += NEW_LINE;
529529

530530
DEBUG_WEBSOCKETS("[WS-Client][sendHeader] handshake %s", (uint8_t*)handshake.c_str());
531-
client->tcp->write((uint8_t*)handshake.c_str(), handshake.length());
531+
write(client, (uint8_t*)handshake.c_str(), handshake.length());
532532

533533
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266_ASYNC)
534534
client->tcp->readStringUntil('\n', &(client->cHttpLine), std::bind(&WebSocketsClient::handleHeader, this, client, &(client->cHttpLine)));
@@ -647,7 +647,6 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
647647
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] Websocket connection init done.\n");
648648
headerDone(client);
649649

650-
651650
runCbEvent(WStype_CONNECTED, (uint8_t *) client->cUrl.c_str(), client->cUrl.length());
652651

653652
} else if(clientIsConnected(client) && client->isSocketIO && client->cSessionId.length() > 0) {
@@ -656,7 +655,7 @@ void WebSocketsClient::handleHeader(WSclient_t * client, String * headerLine) {
656655
DEBUG_WEBSOCKETS("[WS-Client][handleHeader] no Websocket connection close.\n");
657656
_lastConnectionFail = millis();
658657
if(clientIsConnected(client)) {
659-
client->tcp->write("This is a webSocket client!");
658+
write(client, "This is a webSocket client!");
660659
}
661660
clientDisconnect(client);
662661
}

src/WebSocketsServer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ void WebSocketsServer::handleHeader(WSclient_t * client, String * headerLine) {
809809

810810
DEBUG_WEBSOCKETS("[WS-Server][%d][handleHeader] handshake %s", client->num, (uint8_t*)handshake.c_str());
811811

812-
client->tcp->write((uint8_t*)handshake.c_str(), handshake.length());
812+
write(client, (uint8_t*)handshake.c_str(), handshake.length());
813813

814814
headerDone(client);
815815

0 commit comments

Comments
 (0)