forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimpl_socket.c
More file actions
130 lines (113 loc) · 3.4 KB
/
impl_socket.c
File metadata and controls
130 lines (113 loc) · 3.4 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "devs_internal.h"
#ifndef JD_USER_SOCKET
#define JD_USER_SOCKET JD_NETWORK
#endif
#if JD_USER_SOCKET
static bool inside_sock;
static void tcpsock_on_event(unsigned event, const void *data, unsigned size) {
int ev = 0;
value_t arg = devs_undefined;
devs_ctx_t *ctx = devsmgr_get_ctx();
if (!ctx || ctx->error_code)
return;
value_t fn = devs_maplike_get_no_bind(
ctx, devs_get_builtin_object(ctx, DEVS_BUILTIN_OBJECT_DEVICESCRIPT),
devs_builtin_string(DEVS_BUILTIN_STRING__SOCKETONEVENT));
if (devs_is_undefined(fn))
return;
switch (event) {
case JD_CONN_EV_OPEN:
ev = DEVS_BUILTIN_STRING_OPEN;
break;
case JD_CONN_EV_MESSAGE:
ev = DEVS_BUILTIN_STRING_DATA;
arg = devs_value_from_gc_obj(ctx, devs_buffer_try_alloc_init(ctx, data, size));
break;
case JD_CONN_EV_CLOSE:
jd_tcpsock_on_event_override = NULL;
ev = DEVS_BUILTIN_STRING_CLOSE;
break;
case JD_CONN_EV_ERROR:
jd_tcpsock_on_event_override = NULL;
ev = DEVS_BUILTIN_STRING_ERROR_;
jd_tcpsock_close();
if (data)
arg = devs_value_from_gc_obj(ctx, devs_string_try_alloc_init(ctx, data, size));
break;
default:
JD_PANIC();
return;
}
if (!inside_sock)
DEVS_CHECK_CTX_FREE(ctx);
ctx->stack_top_for_gc = 3;
ctx->the_stack[0] = fn;
ctx->the_stack[1] = devs_builtin_string(ev);
ctx->the_stack[2] = arg;
devs_fiber_start(ctx, 2, DEVS_OPCALL_BG);
}
#endif
void fun2_DeviceScript__socketOpen(devs_ctx_t *ctx) {
#if JD_USER_SOCKET
const char *host = devs_string_get_utf8(ctx, devs_arg(ctx, 0), NULL);
int port = devs_arg_int(ctx, 1);
if (dcfg_get_bool("devNetwork")) {
devs_throw_range_error(ctx, "devNetwork enabled");
return;
}
if (!jd_tcpsock_is_available()) {
devs_throw_range_error(ctx, "network not available");
return;
}
if (!host || !port) {
devs_throw_type_error(ctx, "host or port invalid");
return;
}
jd_tcpsock_on_event_override = tcpsock_on_event;
devs_ret(ctx, devs_arg(ctx, 0)); // make sure host argument is not GC-ed
inside_sock = true;
int r = jd_tcpsock_new(host, port);
inside_sock = false;
if (r != 0) {
jd_tcpsock_on_event_override = NULL;
devs_ret_int(ctx, -1);
}
devs_ret_int(ctx, 0);
#else
devs_throw_not_supported_error(ctx, "Networking");
#endif
}
void fun0_DeviceScript__socketClose(devs_ctx_t *ctx) {
#if JD_USER_SOCKET
if (jd_tcpsock_on_event_override) {
jd_tcpsock_close();
jd_tcpsock_on_event_override = NULL;
devs_ret_int(ctx, 0);
} else {
devs_ret_int(ctx, -1);
}
#else
devs_throw_not_supported_error(ctx, "Networking");
#endif
}
void fun1_DeviceScript__socketWrite(devs_ctx_t *ctx) {
#if JD_USER_SOCKET
if (!jd_tcpsock_on_event_override) {
devs_ret_int(ctx, -100);
} else {
unsigned sz;
const void *buf = devs_bufferish_data(ctx, devs_arg(ctx, 0), &sz);
if (buf == NULL)
devs_ret_int(ctx, -101);
else {
devs_ret(ctx, devs_arg(ctx, 0)); // "pin" it
inside_sock = true;
int r = jd_tcpsock_write(buf, sz);
inside_sock = false;
devs_ret_int(ctx, r);
}
}
#else
devs_throw_not_supported_error(ctx, "Networking");
#endif
}