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