int f_read_ms_ #else int f_read_ms #endif (DATA_HDR *fhdr, /* pointer to FORTRAN DATA_HDR. */ void *data_buffer, /* pointer to output data buffer. */ int *maxpts, /* max # data points to return. */ FILE **pfp) /* FILE pointer for input file. */ { DATA_HDR *hdr; /* pointer to DATA_HDR. */ int nread; nread = 0; nread = read_ms (&hdr, data_buffer, *maxpts, *pfp); /* Copy hdr to fortran structure, and convert char strings. */ /* For FORTRAN use, I will not return the blockettes, since they */ /* can't reference them directly or free them. */ if (nread > 0 && hdr == NULL) return (MS_ERROR); if (hdr != NULL) { *fhdr = *hdr; cstr_to_fstr(fhdr->station_id, DH_STATION_LEN+1); cstr_to_fstr(fhdr->location_id, DH_LOCATION_LEN+1); cstr_to_fstr(fhdr->channel_id, DH_CHANNEL_LEN+1); cstr_to_fstr(fhdr->network_id, DH_NETWORK_LEN+1); free_data_hdr (hdr); fhdr->pblockettes = NULL; } return (nread); }
/* returns HTTP code or -1 if error occured, blocking function * returns: * > 0 - http response bytes in buffer * -1 - error writing request * -2 - timeout waiting for response * -3 - incomplete/bad headers */ int http_handshake(int sock, const void *hdr, unsigned hdr_len, pstr_t *data) { int bytes, code, i; char *p; char buf[4096]; if (write_full_ms(sock, hdr, hdr_len, 1000) != hdr_len) return -1; bytes = read_ms(sock, buf, sizeof(buf), 5000); if (bytes < 12 /* minimal headers length */ || memcmp(buf, "HTTP/1.", 7) != 0 || (buf[7] != '0' && buf[7] != '1') || buf[8] != 0x20 || !isdigit(buf[9])) return -2; /* read response code */ code = 0; for (i = 9; i < bytes && isdigit(buf[i]); i++) code = code * 10 + (buf[i] - '0'); /* find end-of-headers */ p = memmem(buf, sizeof(buf), "\r\n\r\n", 4); if (p == NULL) return -3; data->len = buf + bytes - (p + 4); memcpy(data->data, buf, data->len); return code; }
int main() { int status; pstr_t body; uint64_t nbytes = 0; char buf[4096]; int s = connect_to(HOST, 80); body.data = buf; body.len = sizeof(buf); status = http_handshake(s, rqh, sizeof(rqh), &body); printf("GOT %d + %u\n", status, body.len); for (;;) { int bytes = read_ms(s, buf, sizeof(buf), 5000); if (bytes <= 0) break; nbytes += bytes; } printf("%" PRIu64 " bytes transferred\n", nbytes); return 0; }
float Timer::read() { return (float)read_ms() / 1000; }