5 #include <stdlib.h> |
5 #include <stdlib.h> |
6 |
6 |
7 #include "mandelbrot.h" |
7 #include "mandelbrot.h" |
8 #include "common.h" |
8 #include "common.h" |
9 |
9 |
10 int read_int (FILE *fh, u_int32_t *i) { |
10 int my_fread(FILE *fh, void *ptr, size_t size) { |
11 printf("read_int: fread(%p, %zu, 1, %p)... ", i, sizeof(*i), fh); |
11 int ret = fread(ptr, size, 1, fh); |
12 fflush(stdout); |
|
13 int ret = fread(i, sizeof(*i), 1, fh); |
|
14 printf("ok: %d\n", ret); |
|
15 |
12 |
16 if (ret == 0) { |
13 if (ret == 0) { |
17 error("EOF"); |
14 error("EOF"); |
18 return 0; |
15 return 0; |
|
16 |
19 } else if (ret != 1) { |
17 } else if (ret != 1) { |
20 perror("read_int"); |
18 perror("fread"); |
21 return 0; |
19 return 0; |
22 } |
20 } |
23 |
21 |
|
22 return 1; |
|
23 } |
|
24 |
|
25 int read_byte (FILE *fh, u_int8_t *byte) { |
|
26 return my_fread(fh, byte, sizeof(*byte)); |
|
27 } |
|
28 |
|
29 int read_int (FILE *fh, u_int32_t *i) { |
|
30 if (!my_fread(fh, i, sizeof(*i))) |
|
31 return 0; |
|
32 |
24 *i = ntohl(*i); |
33 *i = ntohl(*i); |
25 |
|
26 printf("read_int: %i\n", *i); |
|
27 |
34 |
28 return 1; |
35 return 1; |
29 } |
36 } |
30 |
37 |
31 int read_double (FILE *fh, double *dbl) { |
38 int read_double (FILE *fh, double *dbl) { |
32 int ret = fread(dbl, sizeof(*dbl), 1, fh); |
39 if (!my_fread(fh, dbl, sizeof(*dbl))) |
33 |
|
34 if (ret != 1) { |
|
35 perror("read_double"); |
|
36 return 0; |
40 return 0; |
37 } |
|
38 |
|
39 printf("read_double: %f\n", *dbl); |
|
40 |
41 |
41 return 1; |
42 return 1; |
42 } |
43 } |
43 |
44 |
44 void handle_client (int sock) { |
45 void handle_client (int sock) { |
45 // open it as a FILE* |
46 // open it as a FILE* |
46 FILE *fh = fdopen(sock, "r+"); |
47 FILE *fh = fdopen(sock, "r+"); |
47 |
48 |
48 // read the parameters |
49 // read the parameters |
|
50 u_int8_t mode; |
49 u_int32_t img_w, img_h; |
51 u_int32_t img_w, img_h; |
50 double x1, y1, x2, y2; |
52 double x1, y1, x2, y2; |
51 |
53 |
52 if ( |
54 if ( |
|
55 !read_byte(fh, &mode) || |
53 !read_int(fh, &img_w) || |
56 !read_int(fh, &img_w) || |
54 !read_int(fh, &img_h) || |
57 !read_int(fh, &img_h) || |
55 !read_double(fh, &x1) || |
58 !read_double(fh, &x1) || |
56 !read_double(fh, &y1) || |
59 !read_double(fh, &y1) || |
57 !read_double(fh, &x2) || |
60 !read_double(fh, &x2) || |