IoObject *IoSocket_asyncUdpRead(IoSocket *self, IoObject *locals, IoMessage *m) { /*doc Socket asyncUdpRead(ipAddress, aSeq, readSize) Reads up to readSize number of bytes from ipAddress into aSeq if data is available. Returns self immediately if successful. Returns an error object on Error. Returns nil if the socket is disconnected. */ IoObject *address = IoMessage_locals_addressArgAt_(m, locals, 0); UArray *buffer = IoSeq_rawUArray(IoMessage_locals_mutableSeqArgAt_(m, locals, 1)); size_t readSize = IoMessage_locals_sizetArgAt_(m, locals, 2); if (Socket_udpRead(SOCKET(self), IoSocket_rawAddressFrom_(address), buffer, readSize)) { return self; } else { if (Socket_asyncFailed()) { return SOCKETERROR("Socket udp read failed"); } else { return IONIL(self); } } }
IoObject *IoSocket_asyncStreamRead(IoSocket *self, IoObject *locals, IoMessage *m) { /*doc Socket asyncStreamRead(aSeq, readSize) Reads up to readSize number of bytes into aSeq if data is available. Returns self immediately if successful. Returns an error object on Error. Returns nil if the socket is disconnected. */ IoSeq *bufferSeq = IoMessage_locals_mutableSeqArgAt_(m, locals, 0); UArray *buffer = IoSeq_rawUArray(bufferSeq); size_t readSize = IoMessage_locals_intArgAt_(m, locals, 1); if (Socket_streamRead(SOCKET(self), buffer, readSize)) { return self; } if (Socket_asyncFailed()) { IoSocket_close(self, locals, m); return SOCKETERROR("Socket stream read failed"); } //if (readSize == 0) //SocketErrorStatus() == 0) if (SocketErrorStatus() == 0) { // 0 bytes means the other end disconnected //printf("SocketErrorStatus() == 0, closing\n"); IoSocket_close(self, locals, m); } return IONIL(self); }
IO_METHOD(IoFile, readToBufferLength) { /*doc File readToBufferLength(aBuffer, aNumber) Reads at most aNumber number of items and appends them to aBuffer. Returns number of items read. */ IoSeq *buffer = IoMessage_locals_mutableSeqArgAt_(m, locals, 0); size_t length = IoMessage_locals_longArgAt_(m, locals, 1); UArray *ba = IoSeq_rawUArray(buffer); size_t itemsRead = UArray_readNumberOfItems_fromCStream_(ba, length, DATA(self)->stream); return IONUMBER(itemsRead); }
IoObject *IoAsyncRequest_copyBufferTo(IoAsyncRequest *self, IoObject *locals, IoMessage *m) { /*doc AsyncRequest copyBufferto(aSeq) Copies the request buffer's data to aSeq. Returns nil on error or self otherwise. */ if (IOCB_BUFFER(self)) { IoSeq *data = IoMessage_locals_mutableSeqArgAt_(m, locals, 0); UArray *ba = IoSeq_rawUArray(data); UArray_setData_type_size_copy_(ba, IOCB_BUFFER(self), CTYPE_uint8_t, IOCB(self)->aio_nbytes, 1); return self; } return IONIL(self); }