| 235 | } |
| 236 | |
| 237 | void xs_serial_write(xsMachine *the) |
| 238 | { |
| 239 | xsSerial s = xsmcGetHostData(xsThis); |
| 240 | xsUnsignedValue count = 0; |
| 241 | void *data; |
| 242 | char byte; |
| 243 | |
| 244 | if (1 == s->bufferFormat) { |
| 245 | xsmcGetBufferReadable(xsArg(0), &data, &count); |
| 246 | } |
| 247 | else if (2 == s->bufferFormat) { |
| 248 | count = 1; |
| 249 | byte = xsmcToInteger(xsArg(0)); |
| 250 | data = &byte; |
| 251 | } |
| 252 | if (!count) |
| 253 | return; |
| 254 | |
| 255 | if ((count > kWriteBufferSize) || s->flush) |
| 256 | xsUnknownError("would overflow"); |
| 257 | |
| 258 | while (count) { |
| 259 | int result = write(s->fd, data, count); |
| 260 | if (result < 0) { |
| 261 | if (EAGAIN != errno) |
| 262 | xsUnknownError("write failed"); |
| 263 | |
| 264 | c_memmove(s->writeBuffer, data, count); |
| 265 | s->toWrite = s->writeBuffer; |
| 266 | s->bytesToWrite = count; |
| 267 | |
| 268 | s->flush = modTimerAdd(0, 0, fxSerialFlush, &s, sizeof(s)); |
| 269 | return; |
| 270 | } |
| 271 | count -= result; |
| 272 | data += result; |
| 273 | } |
| 274 | |
| 275 | if (!s->writable && s->hasOnWritable) |
| 276 | s->writable = modTimerAdd(0, 0, fxSerialWritable, &s, sizeof(s)); |
| 277 | } |
| 278 | |
| 279 | void xs_serial_set(xsMachine *the) |
| 280 | { |
nothing calls this directly
no test coverage detected