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

Function tcpTask

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

Source from the content-addressed store, hash-verified

412}
413
414void tcpTask(modTimer timer, void *refcon, int refconSize)
415{
416 TCP tcp = *(TCP *)refcon;
417 fd_set rfds, wfds;
418 struct timeval tv;
419
420 if ((-1 == tcp->skt) || tcp->done || tcp->error)
421 return; // closed socket
422
423 tcpHold(tcp);
424
425 FD_ZERO(&wfds);
426 FD_ZERO(&rfds);
427
428 if (!tcp->connected || (kBufferSize != tcp->bytesWritable))
429 FD_SET(tcp->skt, &wfds);
430 if (tcp->bytesReadable < kBufferSize)
431 FD_SET(tcp->skt, &rfds);
432
433 tv.tv_sec = tv.tv_usec = 0;
434 int result = select(tcp->skt + 1, &rfds, &wfds, NULL, &tv);
435 if (result > 0) {
436 if (FD_ISSET(tcp->skt, &wfds)) {
437 tcp->connected = true;
438 if (kBufferSize != tcp->bytesWritable) {
439 tcp->bytesWritable = kBufferSize;
440 tcpTrigger(tcp, kTCPWritable);
441 }
442 }
443 if (FD_ISSET(tcp->skt, &rfds) && (tcp->bytesReadable < kBufferSize)) {
444 if (tcp->readPosition) {
445 if (tcp->bytesReadable)
446 memmove(tcp->readBuf, tcp->readBuf + tcp->readPosition, tcp->bytesReadable);
447 tcp->readPosition = 0;
448 }
449 int bytesRead = read(tcp->skt, tcp->readBuf + tcp->bytesReadable, kBufferSize - tcp->bytesReadable);
450 if (bytesRead > 0) {
451 tcp->bytesReadable += bytesRead;
452 tcpTrigger(tcp, kTCPReadable);
453 modInstrumentationAdjust(NetworkBytesRead, bytesRead);
454 }
455 else {
456 tcp->error = 1;
457 if (0 == tcp->bytesReadable)
458 tcpTrigger(tcp, kTCPError);
459 }
460 }
461 }
462
463 if (tcp->triggered)
464 reportTrigger(tcp);
465
466 tcpRelease(tcp);
467}
468
469void reportTrigger(TCP tcp)
470{

Callers

nothing calls this directly

Calls 6

selectFunction · 0.85
tcpHoldFunction · 0.70
tcpTriggerFunction · 0.70
reportTriggerFunction · 0.70
tcpReleaseFunction · 0.70
readFunction · 0.50

Tested by

no test coverage detected