Duktape debug transport callback: (possibly partial) read. */
| 143 | |
| 144 | /* Duktape debug transport callback: (possibly partial) read. */ |
| 145 | duk_size_t duk_trans_socket_read_cb(void *udata, char *buffer, duk_size_t length) { |
| 146 | ssize_t ret; |
| 147 | |
| 148 | (void) udata; /* not needed by the example */ |
| 149 | |
| 150 | #if defined(DEBUG_PRINTS) |
| 151 | fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n", |
| 152 | __func__, (void *) udata, (void *) buffer, (long) length); |
| 153 | fflush(stderr); |
| 154 | #endif |
| 155 | |
| 156 | if (client_sock < 0) { |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | if (length == 0) { |
| 161 | /* This shouldn't happen. */ |
| 162 | fprintf(stderr, "%s: read request length == 0, closing connection\n", |
| 163 | __FILE__); |
| 164 | fflush(stderr); |
| 165 | goto fail; |
| 166 | } |
| 167 | |
| 168 | if (buffer == NULL) { |
| 169 | /* This shouldn't happen. */ |
| 170 | fprintf(stderr, "%s: read request buffer == NULL, closing connection\n", |
| 171 | __FILE__); |
| 172 | fflush(stderr); |
| 173 | goto fail; |
| 174 | } |
| 175 | |
| 176 | /* In a production quality implementation there would be a sanity |
| 177 | * timeout here to recover from "black hole" disconnects. |
| 178 | */ |
| 179 | |
| 180 | ret = read(client_sock, (void *) buffer, (size_t) length); |
| 181 | if (ret < 0) { |
| 182 | fprintf(stderr, "%s: debug read failed, closing connection: %s\n", |
| 183 | __FILE__, strerror(errno)); |
| 184 | fflush(stderr); |
| 185 | goto fail; |
| 186 | } else if (ret == 0) { |
| 187 | fprintf(stderr, "%s: debug read failed, ret == 0 (EOF), closing connection\n", |
| 188 | __FILE__); |
| 189 | fflush(stderr); |
| 190 | goto fail; |
| 191 | } else if (ret > (ssize_t) length) { |
| 192 | fprintf(stderr, "%s: debug read failed, ret too large (%ld > %ld), closing connection\n", |
| 193 | __FILE__, (long) ret, (long) length); |
| 194 | fflush(stderr); |
| 195 | goto fail; |
| 196 | } |
| 197 | |
| 198 | return (duk_size_t) ret; |
| 199 | |
| 200 | fail: |
| 201 | if (client_sock >= 0) { |
| 202 | (void) close(client_sock); |
nothing calls this directly
no outgoing calls
no test coverage detected