-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyfdart.c
114 lines (91 loc) · 2.26 KB
/
yfdart.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* Copyright (c) 2022, The YNDXFuck project */
#include "yfdart.h"
#include "yflog.h"
#include <stdio.h>
/*
* Dart DL API wrappers
*/
intptr_t yfdart_init_dart_api(void *data) {
LOG_clean();
return Dart_InitializeApiDL(data);
}
static int send_packet_to_dart(struct yfdart_packet *p, Dart_Port_DL port)
{
if (!port) {
LOG("port is NULL\n");
return -1;
}
if (!p->type || (p->type == YFDART_PACKET_END && p->data == NULL)) {
LOG("invalid packet\n");
return -2;
}
Dart_CObject obj;
obj.type = Dart_CObject_kInt64;
obj.value.as_int64 = (uint64_t)p;
Dart_PostCObject_DL(port, &obj);
return 0;
}
int yfdart_attach_port(struct yfdart_connection *c, Dart_Port_DL port) {
if (!c) {
LOG("c is NULL\n");
return -1;
}
if (c->port_id == port) {
LOG("c has got same port already\n");
return -2;
}
c->port_id = port;
c->ready = true;
return 0;
}
int yfdart_start_connection(struct yfdart_connection *c) {
if (!c) {
LOG("c is NULL\n");
return -1;
}
if (!c->ready || !c->port_id) {
LOG("c not ready\n");
return -1;
}
struct yfdart_packet *p = malloc(sizeof(struct yfdart_packet));
p->type = YFDART_PACKET_HELLO;
p->data = "placeholder";
return send_packet_to_dart(p, c->port_id);
}
int yfdart_connection_post(struct yfdart_connection *c, void *data) {
if (!c) {
LOG("c is NULL\n");
return -1;
}
if (!c->ready || !c->port_id) {
LOG("c not ready\n");
return -1;
}
struct yfdart_packet p = {
YFDART_PACKET_DATA,
data,
};
return send_packet_to_dart(&p, c->port_id);
}
int yfdart_end_connection(struct yfdart_connection **cp, int32_t end_code) {
struct yfdart_connection *c = *cp;
if (!c) {
LOG("c is NULL\n");
return -1;
}
if (!c->ready || !c->port_id) {
LOG("c not ready\n");
return -1;
}
int32_t *end_code_p = malloc(sizeof(int32_t));
if (!end_code_p)
return -3;
*end_code_p = end_code;
struct yfdart_packet p = {
YFDART_PACKET_END,
end_code_p,
};
int ret = send_packet_to_dart(&p, c->port_id);
free(*cp);
return ret;
}