|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2016-2018 Moddable Tech, Inc. |
| 2 | + * Copyright (c) 2016-2022 Moddable Tech, Inc. |
3 | 3 | *
|
4 | 4 | * This file is part of the Moddable SDK Runtime.
|
5 | 5 | *
|
@@ -52,6 +52,7 @@ struct xsSocketRecord {
|
52 | 52 | int32_t unreportedSent; // bytes sent to the socket but not yet reported to object as sent
|
53 | 53 |
|
54 | 54 | uint8_t writeBuf[1024];
|
| 55 | + uint8_t readBuf[1024]; |
55 | 56 | };
|
56 | 57 |
|
57 | 58 | typedef struct xsListenerRecord xsListenerRecord;
|
@@ -218,22 +219,22 @@ DWORD WINAPI hostResolveProc(LPVOID lpParameter)
|
218 | 219 |
|
219 | 220 | int doReadSocket(xsMachine *the, xsSocket xss)
|
220 | 221 | {
|
221 |
| - unsigned char buffer[1024]; |
222 |
| - int count; |
223 |
| - |
224 |
| - count = recv(xss->skt, buffer, sizeof(buffer), 0); |
225 |
| - if (count > 0) { |
| 222 | + int count = 0; |
| 223 | + int full = sizeof(xss->readBuf) == xss->readBytes; |
| 224 | + |
| 225 | + if (!full) { |
| 226 | + if (xss->readBytes) |
| 227 | + c_memcpy(xss->readBuf, xss->readBuffer, xss->readBytes); |
| 228 | + xss->readBuffer = xss->readBuf; |
| 229 | + count = recv(xss->skt, xss->readBuf + xss->readBytes, sizeof(xss->readBuf) - xss->readBytes, 0); |
| 230 | + } |
| 231 | + if ((count > 0) || full) { |
226 | 232 | modInstrumentationAdjust(NetworkBytesRead, count);
|
227 |
| - |
228 |
| - xss->readBuffer = buffer; |
229 |
| - xss->readBytes = count; |
| 233 | + xss->readBytes += count; |
230 | 234 |
|
231 | 235 | xsBeginHost(the);
|
232 |
| - xsCall2(xss->obj, xsID_callback, xsInteger(kSocketMsgDataReceived), xsInteger(count)); |
| 236 | + xsCall2(xss->obj, xsID_callback, xsInteger(kSocketMsgDataReceived), xsInteger(xss->readBytes)); |
233 | 237 | xsEndHost(the);
|
234 |
| - |
235 |
| - xss->readBuffer = NULL; |
236 |
| - xss->readBytes = 0; |
237 | 238 | }
|
238 | 239 |
|
239 | 240 | return count;
|
@@ -262,9 +263,11 @@ LRESULT CALLBACK modSocketWindowProc(HWND window, UINT message, WPARAM wParam, L
|
262 | 263 | count = doReadSocket(the, xss);
|
263 | 264 | } while (count > 0);
|
264 | 265 |
|
265 |
| - xsBeginHost(the); |
266 |
| - xsCall1(xss->obj, xsID_callback, xsInteger(kSocketMsgDisconnect)); |
267 |
| - xsEndHost(the); |
| 266 | + if (!xss->done) { |
| 267 | + xsBeginHost(the); |
| 268 | + xsCall1(xss->obj, xsID_callback, xsInteger(kSocketMsgDisconnect)); |
| 269 | + xsEndHost(the); |
| 270 | + } |
268 | 271 | } break;
|
269 | 272 | case FD_READ: {
|
270 | 273 | doReadSocket(the, xss);
|
|
0 commit comments