Duktape debug transport callback: (possibly partial) read. */
| 218 | |
| 219 | /* Duktape debug transport callback: (possibly partial) read. */ |
| 220 | duk_size_t duk_trans_socket_read_cb(void *udata, char *buffer, duk_size_t length) { |
| 221 | int ret; |
| 222 | |
| 223 | (void) udata; /* not needed by the example */ |
| 224 | |
| 225 | #if defined(DEBUG_PRINTS) |
| 226 | fprintf(stderr, "%s: udata=%p, buffer=%p, length=%ld\n", |
| 227 | __FUNCTION__, (void *) udata, (void *) buffer, (long) length); |
| 228 | fflush(stderr); |
| 229 | #endif |
| 230 | |
| 231 | if (client_sock == INVALID_SOCKET) { |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | if (length == 0) { |
| 236 | /* This shouldn't happen. */ |
| 237 | fprintf(stderr, "%s: read request length == 0, closing connection\n", |
| 238 | __FILE__); |
| 239 | fflush(stderr); |
| 240 | goto fail; |
| 241 | } |
| 242 | |
| 243 | if (buffer == NULL) { |
| 244 | /* This shouldn't happen. */ |
| 245 | fprintf(stderr, "%s: read request buffer == NULL, closing connection\n", |
| 246 | __FILE__); |
| 247 | fflush(stderr); |
| 248 | goto fail; |
| 249 | } |
| 250 | |
| 251 | /* In a production quality implementation there would be a sanity |
| 252 | * timeout here to recover from "black hole" disconnects. |
| 253 | */ |
| 254 | |
| 255 | ret = recv(client_sock, buffer, (int) length, 0); |
| 256 | if (ret < 0) { |
| 257 | fprintf(stderr, "%s: debug read failed, error %d, closing connection\n", |
| 258 | __FILE__, ret); |
| 259 | fflush(stderr); |
| 260 | goto fail; |
| 261 | } else if (ret == 0) { |
| 262 | fprintf(stderr, "%s: debug read failed, ret == 0 (EOF), closing connection\n", |
| 263 | __FILE__); |
| 264 | fflush(stderr); |
| 265 | goto fail; |
| 266 | } else if (ret > (int) length) { |
| 267 | fprintf(stderr, "%s: debug read failed, ret too large (%ld > %ld), closing connection\n", |
| 268 | __FILE__, (long) ret, (long) length); |
| 269 | fflush(stderr); |
| 270 | goto fail; |
| 271 | } |
| 272 | |
| 273 | return (duk_size_t) ret; |
| 274 | |
| 275 | fail: |
| 276 | if (client_sock != INVALID_SOCKET) { |
| 277 | (void) closesocket(client_sock); |
nothing calls this directly
no outgoing calls
no test coverage detected