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

Function tcpTask

modules/io/socket/win/tcp.c:407–460  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 5

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

Tested by

no test coverage detected