MCPcopy Create free account
hub / github.com/acl-dev/acl / send_file

Function send_file

lib_acl_cpp/samples/websocket/upload_client/main.cpp:26–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

24}
25
26static 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 }

Callers 2

uploadFunction · 0.85
uploadMethod · 0.85

Calls 10

fsizeMethod · 0.80
send_frame_dataMethod · 0.80
last_serrorFunction · 0.50
sleepFunction · 0.50
open_readMethod · 0.45
sizeMethod · 0.45
formatMethod · 0.45
resetMethod · 0.45
eofMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…