MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / xs_tcp_read

Function xs_tcp_read

modules/io/socket/lin/tcp.c:267–312  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

265}
266
267void xs_tcp_read(xsMachine *the)
268{
269 TCP tcp = xsmcGetHostDataValidate(xsThis, (void *)&xsTCPHooks);
270 uint32_t requested;
271 uint8_t *out, value;
272
273 if (!tcp->bytesReadable)
274 return;
275
276 if (kIOFormatBuffer == tcp->format) {
277 uint8_t allocate = 1;
278 xsUnsignedValue byteLength;
279
280 if (0 == xsmcArgc)
281 requested = tcp->bytesReadable;
282 else if (xsReferenceType == xsmcTypeOf(xsArg(0))) {
283 xsResult = xsArg(0);
284 xsmcGetBufferWritable(xsResult, (void **)&out, &byteLength);
285 requested = (int)byteLength;
286 xsmcSetInteger(xsResult, requested);
287 allocate = 0;
288 }
289 else
290 requested = xsmcToInteger(xsArg(0));
291
292 if ((requested <= 0) || (requested > tcp->bytesReadable))
293 xsUnknownError("invalid");
294
295 if (allocate)
296 out = xsmcSetArrayBuffer(xsResult, NULL, requested);
297 }
298 else {
299 requested = 1;
300 out = &value;
301 }
302
303 c_memmove(out, tcp->readBuf + tcp->readPosition, requested);
304 tcp->readPosition += requested;
305 tcp->bytesReadable -= requested;
306
307 if (tcp->error && !tcp->bytesReadable)
308 tcpTrigger(tcp, kTCPError);
309
310 if (kIOFormatNumber == tcp->format)
311 xsmcSetInteger(xsResult, value);
312}
313
314void xs_tcp_write(xsMachine *the)
315{

Callers

nothing calls this directly

Calls 1

tcpTriggerFunction · 0.70

Tested by

no test coverage detected