| 423 | } |
| 424 | |
| 425 | void xs_socket_write(xsMachine *the) |
| 426 | { |
| 427 | xsSocket xss = xsmcGetHostData(xsThis); |
| 428 | int argc = xsmcArgc; |
| 429 | uint8_t *dst; |
| 430 | uint16_t available, needed = 0; |
| 431 | unsigned char pass, arg; |
| 432 | |
| 433 | if ((NULL == xss) || xss->done) |
| 434 | xsUnknownError("write on closed socket"); |
| 435 | |
| 436 | if (kUDP == xss->kind) { |
| 437 | char temp[64]; |
| 438 | |
| 439 | xsmcToStringBuffer(xsArg(0), temp, sizeof(temp)); |
| 440 | |
| 441 | int len = xsmcGetArrayBufferLength(xsArg(2)); |
| 442 | uint8_t *buf = xsmcToArrayBuffer(xsArg(2)); |
| 443 | |
| 444 | struct sockaddr_in dest_addr = { 0 }; |
| 445 | |
| 446 | int port = xsmcToInteger(xsArg(1)); |
| 447 | |
| 448 | dest_addr.sin_family = AF_INET; |
| 449 | dest_addr.sin_port = htons(port); |
| 450 | dest_addr.sin_addr.s_addr = inet_addr(temp); |
| 451 | |
| 452 | int result = sendto(xss->skt, buf, len, 0, (const struct sockaddr *)&dest_addr, sizeof(dest_addr)); |
| 453 | if (result < 0) |
| 454 | xsUnknownError("sendto failed"); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | available = sizeof(xss->writeBuf) - xss->writeBytes; |
| 459 | if (0 == argc) { |
| 460 | xsResult = xsInteger(available); |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | dst = xss->writeBuf + xss->writeBytes; |
| 465 | for (pass = 0; pass < 2; pass++) { |
| 466 | for (arg = 0; arg < argc; arg++) { |
| 467 | xsType t = xsmcTypeOf(xsArg(arg)); |
| 468 | |
| 469 | if (xsStringType == t) { |
| 470 | char *msg = xsmcToString(xsArg(arg)); |
| 471 | int msgLen = c_strlen(msg); |
| 472 | if (0 == pass) |
| 473 | needed += msgLen; |
| 474 | else { |
| 475 | c_memcpy(dst, msg, msgLen); |
| 476 | dst += msgLen; |
| 477 | } |
| 478 | } |
| 479 | else if ((xsNumberType == t) || (xsIntegerType == t)) { |
| 480 | if (0 == pass) |
| 481 | needed += 1; |
| 482 | else |
nothing calls this directly
no test coverage detected