|
| 1 | +/* |
| 2 | + * Copyright (c) 2019, Christopher Zimmermann |
| 3 | + * |
| 4 | + * Permission to use, copy, modify, and/or distribute this software for any |
| 5 | + * purpose with or without fee is hereby granted, provided that the above |
| 6 | + * copyright notice and this permission notice appear in all copies. |
| 7 | + * |
| 8 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 11 | + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 13 | + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 14 | + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + */ |
| 16 | + |
| 17 | + |
| 18 | +#include <caml/mlvalues.h> |
| 19 | +#include <caml/bigarray.h> |
| 20 | +#include <caml/unixsupport.h> |
| 21 | +#include <caml/threads.h> |
| 22 | + |
| 23 | +#ifndef Handle_val |
| 24 | +#include <unistd.h> |
| 25 | +#endif |
| 26 | + |
| 27 | +CAMLprim value |
| 28 | +ocaml_bigstring_unix_read(value vfd, value vba, value voff, value vlen) |
| 29 | +{ |
| 30 | + void *iobuf = ((char *)Caml_ba_data_val(vba)) + Unsigned_long_val(voff); |
| 31 | +#ifdef Handle_val |
| 32 | + unsigned len; |
| 33 | + int err; |
| 34 | + |
| 35 | + if (Descr_kind_val(vfd) == KIND_SOCKET) { |
| 36 | + SOCKET s = Socket_val(vfd); |
| 37 | + caml_release_runtime_system(); |
| 38 | + if ((err = recv(s, iobuf, Unsigned_int_val(vlen), 0)) == SOCKET_ERROR) |
| 39 | + err = WSAGetLastError(); |
| 40 | + else { |
| 41 | + len = err; |
| 42 | + err = 0; |
| 43 | + } |
| 44 | + caml_acquire_runtime_system(); |
| 45 | + } else { |
| 46 | + HANDLE h = Handle_val(vfd); |
| 47 | + caml_release_runtime_system(); |
| 48 | + if (ReadFile(h, iobuf, Unsigned_int_val(vlen), &len, NULL)) |
| 49 | + err = 0; |
| 50 | + else { |
| 51 | +check_error: |
| 52 | + switch (err = GetLastError()) { |
| 53 | + case ERROR_BROKEN_PIPE: |
| 54 | + /* This is no error, but just a closed pipe. */ |
| 55 | + err = len = 0; |
| 56 | + break; |
| 57 | + case ERROR_MORE_DATA: |
| 58 | +#if 1 |
| 59 | + do { |
| 60 | + char buf[1024]; |
| 61 | + unsigned dummy_len; |
| 62 | + if (ReadFile(h, buf, sizeof(buf), &dummy_len, NULL)) |
| 63 | + break; |
| 64 | + else |
| 65 | + goto check_error; |
| 66 | + } while (0); |
| 67 | +#else |
| 68 | + err = 0; |
| 69 | +#endif |
| 70 | + default: |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + caml_acquire_runtime_system(); |
| 75 | + } |
| 76 | + /* GetLastError() and WSAGetLastError() error numbers _are_ compatible, |
| 77 | + * although not documented this behaviour will hopefully never change. */ |
| 78 | + if (err) { |
| 79 | + win32_maperr(err); |
| 80 | + uerror("Bigstringaf.read", Nothing); |
| 81 | + } |
| 82 | + else |
| 83 | + return Val_int(len); |
| 84 | + |
| 85 | +#else |
| 86 | + ssize_t ret; |
| 87 | + |
| 88 | + caml_release_runtime_system(); |
| 89 | + ret = read(Int_val(vfd), iobuf, Unsigned_long_val(vlen)); |
| 90 | + caml_acquire_runtime_system(); |
| 91 | + if (ret < 0) uerror("Bigstringaf.read", Nothing); |
| 92 | + return Val_long(ret); |
| 93 | +#endif |
| 94 | +} |
| 95 | + |
| 96 | +CAMLprim value |
| 97 | +ocaml_bigstring_unix_write(value vfd, value vba, value voff, value vlen) |
| 98 | +{ |
| 99 | + char *iobuf = ((char *)Caml_ba_data_val(vba)) + Unsigned_long_val(voff); |
| 100 | +#ifdef Handle_val |
| 101 | + unsigned len; |
| 102 | + int err; |
| 103 | + |
| 104 | + if (Descr_kind_val(vfd) == KIND_SOCKET) { |
| 105 | + SOCKET s = Socket_val(vfd); |
| 106 | + caml_release_runtime_system(); |
| 107 | + if ((err = send(s, iobuf, Unsigned_int_val(vlen), 0)) == SOCKET_ERROR) |
| 108 | + err = WSAGetLastError(); |
| 109 | + else { |
| 110 | + len = err; |
| 111 | + err = 0; |
| 112 | + } |
| 113 | + caml_acquire_runtime_system(); |
| 114 | + } else { |
| 115 | + HANDLE h = Handle_val(vfd); |
| 116 | + caml_release_runtime_system(); |
| 117 | + if (WriteFile(h, iobuf, Unsigned_int_val(vlen), &len, NULL)) |
| 118 | + err = 0; |
| 119 | + else |
| 120 | + err = GetLastError(); |
| 121 | + caml_acquire_runtime_system(); |
| 122 | + } |
| 123 | + /* GetLastError() and WSAGetLastError() error numbers _are_ compatible, |
| 124 | + * although not documented this behaviour will hopefully never change. */ |
| 125 | + if (err) { |
| 126 | + win32_maperr(err); |
| 127 | + uerror("Bigstringaf.write", Nothing); |
| 128 | + } |
| 129 | + return Val_long(len); |
| 130 | + |
| 131 | +#else |
| 132 | + ssize_t ret; |
| 133 | + |
| 134 | + caml_release_runtime_system(); |
| 135 | + ret = write(Int_val(vfd), iobuf, Unsigned_long_val(vlen)); |
| 136 | + caml_acquire_runtime_system(); |
| 137 | + if (ret < 0) uerror("Bigstringaf.write", Nothing); |
| 138 | + return Val_long(ret); |
| 139 | +#endif |
| 140 | +} |
0 commit comments