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

Function xs_tcp_write

modules/io/socket/zephyr/tcp.c:376–431  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

374}
375
376void xs_tcp_write(xsMachine *the)
377{
378 TCP tcp = xsmcGetHostDataValidate(xsThis, (void *)&xsTCPHooks);
379 xsUnsignedValue needed;
380 void *buffer;
381 uint8_t value;
382
383 if (!tcp->ready || tcp->pending)
384 xsUnknownError("not ready");
385
386 if (tcp->error || !tcp->context)
387 return; //@@
388
389 if (kIOFormatBuffer == tcp->format)
390 xsmcGetBufferReadable(xsArg(0), &buffer, &needed);
391 else {
392 needed = 1;
393 value = (uint8_t)xsmcToInteger(xsArg(0));
394 buffer = &value;
395 }
396 int available = tcp->sendBufferSize - tcp->sendBytesInFlight;
397 if ((needed > available) || tcp->pending)
398 xsUnknownError("would block");
399
400 TCPWrite tw = c_malloc(sizeof(TCPWriteRecord));
401 if (!tw)
402 xsUnknownError("no memory");
403 tw->tcp = tcp;
404 tw->byteLength = needed;
405
406 int result = net_context_send(tcp->context, buffer, needed, tcpSent, K_NO_WAIT, tw);
407 if (-EAGAIN == result)
408 result = 0;
409 else if (result < 0) {
410 xsTrace("tcp write failed\n");
411 tcpTrigger(tcp, kTCPError); //@@ correct!?!
412 return;
413 }
414
415 if (result != needed) {
416 tcp->pending = c_malloc(needed - result);
417 if (!tcp->pending)
418 xsUnknownError("no memory"); //@@ unrecoverable
419 c_memcpy(tcp->pending, result + (uint8_t *)buffer, needed - result);
420 tcp->pendingBuffer = tcp->pending;
421 tcp->pendingBytes = needed - result;
422
423 tcp->pendingTimer = modTimerAdd(100, 50, tcpTrySend, &tcp, sizeof(tcp));
424 }
425
426 tcp->sendBytesInFlight += needed;
427 available -= needed;
428
429 modInstrumentationAdjust(NetworkBytesWritten, needed);
430 xsmcSetInteger(xsResult, tcp->pendingBytes ? 0 : available);
431}
432
433void xs_tcp_get_remoteAddress(xsMachine *the)

Callers

nothing calls this directly

Calls 2

tcpTriggerFunction · 0.70
modTimerAddFunction · 0.50

Tested by

no test coverage detected