| 24 | } |
| 25 | |
| 26 | static bool send_file(acl::websocket& ws, const char* filepath) |
| 27 | { |
| 28 | acl::ifstream in; |
| 29 | if (!in.open_read(filepath)) { |
| 30 | printf("open %s error %s\r\n", filepath, acl::last_serror()); |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | long long size = in.fsize(); |
| 35 | if (size <= 0) { |
| 36 | printf("filename: %s, invalid size: %lld\r\n", filepath, size); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | acl::string buf; |
| 41 | buf.basename(filepath); |
| 42 | |
| 43 | unsigned mask = ~0; |
| 44 | ws.set_frame_fin(true) |
| 45 | .set_frame_opcode(acl::FRAME_TEXT) |
| 46 | .set_frame_payload_len(buf.size()) |
| 47 | .set_frame_masking_key(mask); |
| 48 | |
| 49 | if (!ws.send_frame_data(buf, buf.size())) { |
| 50 | printf("send filenam error %s\r\n", acl::last_serror()); |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | buf.format("%lld", size); |
| 55 | ws.reset().set_frame_fin(true) |
| 56 | .set_frame_opcode(acl::FRAME_TEXT) |
| 57 | .set_frame_payload_len(buf.size()); |
| 58 | if (!ws.send_frame_data(buf, buf.size())) { |
| 59 | printf("send file size error %s\r\n", acl::last_serror()); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | long long total = 0; |
| 64 | char cbuf[128000]; |
| 65 | while (!in.eof()) { |
| 66 | int ret = in.read(cbuf, sizeof(cbuf), false); |
| 67 | if (ret == -1) { |
| 68 | break; |
| 69 | } |
| 70 | |
| 71 | printf(">>send %d\r\n", ret); |
| 72 | ws.reset().set_frame_fin(true) |
| 73 | .set_frame_opcode(acl::FRAME_BINARY) |
| 74 | .set_frame_payload_len(ret); |
| 75 | if (!ws.send_frame_data(cbuf, ret)) { |
| 76 | printf("send data error %s\r\n", acl::last_serror()); |
| 77 | return false; |
| 78 | } |
| 79 | total += ret; |
| 80 | if (total % 10240000 == 0) { |
| 81 | sleep(1); |
| 82 | } |
| 83 | } |
no test coverage detected
searching dependent graphs…