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

Function xs_tcp_constructor

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

Source from the content-addressed store, hash-verified

86};
87
88void xs_tcp_constructor(xsMachine *the)
89{
90 TCP tcp;
91 uint8_t create = xsmcArgc > 0;
92 uint8_t triggerable = 0, triggered = 0, nodelay = 0, format = kIOFormatBuffer;
93 xsSlot *onReadable, *onWritable, *onError;
94 int port = 0;
95 struct sockaddr_in addr = {0};
96
97 xsmcVars(1);
98
99 tcp = c_calloc(1, sizeof(TCPRecord));
100 if (!tcp)
101 xsRangeError("no memory");
102 tcp->skt = -1;
103
104 modInstrumentationAdjust(NetworkSockets, +1);
105
106 xsmcSetHostData(xsThis, tcp);
107
108 if (create) {
109 format = builtinInitializeFormat(the, format);
110 if ((kIOFormatNumber != format) && (kIOFormatBuffer != format))
111 xsRangeError("unimplemented");
112
113 onReadable = builtinGetCallback(the, xsID_onReadable);
114 if (onReadable)
115 triggerable |= kTCPReadable;
116
117 onWritable = builtinGetCallback(the, xsID_onWritable);
118 if (onWritable)
119 triggerable |= kTCPWritable;
120
121 onError = builtinGetCallback(the, xsID_onError);
122 if (onError)
123 triggerable |= kTCPError;
124
125 if (xsmcGet(xsVar(0), xsArg(0), xsID_nodelay))
126 nodelay = xsmcToBoolean(xsVar(0)) ? 2 : 1;
127
128 if (xsmcGet(xsVar(0), xsArg(0), xsID_from)) {
129 TCP from;
130
131 from = xsmcGetHostDataValidate(xsVar(0), (void *)&xsTCPHooks);
132 if (!from->skt)
133 xsUnknownError("invalid from");
134
135 triggered = from->triggered;
136 modTimerRemove(from->task);
137 from->task = NULL;
138 tcp->task = modTimerAdd(kTaskInterval, kTaskInterval, tcpTask, &tcp, sizeof(tcp));
139 tcp->skt = from->skt;
140 tcp->port = from->port;
141 tcp->bytesWritable = from->bytesWritable;
142 tcp->bytesReadable = from->bytesReadable;
143 tcp->readPosition = from->readPosition;
144 memmove(tcp->readBuf, from->readBuf, sizeof(from->readBuf));
145 tcp->error = from->error;

Callers

nothing calls this directly

Calls 10

builtinInitializeFormatFunction · 0.85
builtinGetCallbackFunction · 0.85
builtinGetSignedIntegerFunction · 0.85
builtinInitializeTargetFunction · 0.85
doCloseFunction · 0.70
tcpTriggerFunction · 0.70
tcpHoldFunction · 0.70
modTimerRemoveFunction · 0.50
modTimerAddFunction · 0.50
connectFunction · 0.50

Tested by

no test coverage detected