forked from zhaarey/wrapper
- Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathmain.cpp
More file actions
Latest commit
51 lines (44 loc) · 1.59 KB
/
Copy pathmain.cpp
File metadata and controls
51 lines (44 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* main.cpp — main.c 中 socket 处理器的 C++ 异常屏障
* handle() 会调用可能抛出 SVError 的 C++ 函数 (如 getPersistentKey),
* 这里用 extern "C" 包装并捕获异常, 避免 C++ 异常穿过 C 边界导致崩溃。
* 每个新 socket 服务 (decrypt/m3u8/key/account) 都经 *_cpp 分发。
*/
#include<cstdio>
#include<cstring>
#include<exception>
#include<functional>
#include<unistd.h>
extern"C"voidhandle(int fd);
extern"C"uint8_thandle_cpp(int fd) {
try {
handle(fd);
return1;
} catch (const std::exception &e) {
fprintf(stderr, "[!] catched an exception: %s\n", e.what());
return0;
}
}
extern"C"voidhandle_key_request(int fd);
/* C++ exception barrier for the HTTP key service (getPersistentKey can throw SVError). */
extern"C"uint8_thandle_key_request_cpp(int fd) {
try {
handle_key_request(fd);
return1;
} catch (const std::exception &e) {
fprintf(stderr, "[!] key request exception: %s\n", e.what());
constchar *resp = "HTTP/1.1 500 Internal Server Error\r\nContent-Type: application/json\r\nContent-Length: 28\r\nConnection: close\r\n\r\n{\"error\":\"key retrieval failed\"}";
write(fd, resp, strlen(resp));
return0;
}
}
staticvoidendLeaseCb(intconst &c) {
fprintf(stderr, "[.] end lease code %d\n", c);
exit(1);
}
staticvoidpbErrCb(void *) {
fprintf(stderr, "[.] playback error\n");
exit(1);
}
extern"C" std::function<void (intconst&)> endLeaseCallback(endLeaseCb);
extern"C" std::function<void (void *)> pbErrCallback(pbErrCb);