Skip to content

Commit 5df3b55

Browse files
committed
Windows socket fixes (see #755) (code from @phoddie)
1 parent 8bd7a2f commit 5df3b55

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

modules/network/socket/win/modSocket.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2018 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2022 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
55
*
@@ -52,6 +52,7 @@ struct xsSocketRecord {
5252
int32_t unreportedSent; // bytes sent to the socket but not yet reported to object as sent
5353

5454
uint8_t writeBuf[1024];
55+
uint8_t readBuf[1024];
5556
};
5657

5758
typedef struct xsListenerRecord xsListenerRecord;
@@ -218,22 +219,22 @@ DWORD WINAPI hostResolveProc(LPVOID lpParameter)
218219

219220
int doReadSocket(xsMachine *the, xsSocket xss)
220221
{
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) {
226232
modInstrumentationAdjust(NetworkBytesRead, count);
227-
228-
xss->readBuffer = buffer;
229-
xss->readBytes = count;
233+
xss->readBytes += count;
230234

231235
xsBeginHost(the);
232-
xsCall2(xss->obj, xsID_callback, xsInteger(kSocketMsgDataReceived), xsInteger(count));
236+
xsCall2(xss->obj, xsID_callback, xsInteger(kSocketMsgDataReceived), xsInteger(xss->readBytes));
233237
xsEndHost(the);
234-
235-
xss->readBuffer = NULL;
236-
xss->readBytes = 0;
237238
}
238239

239240
return count;
@@ -262,9 +263,11 @@ LRESULT CALLBACK modSocketWindowProc(HWND window, UINT message, WPARAM wParam, L
262263
count = doReadSocket(the, xss);
263264
} while (count > 0);
264265

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+
}
268271
} break;
269272
case FD_READ: {
270273
doReadSocket(the, xss);

0 commit comments

Comments
 (0)