Repository navigation
Expand file tree
/
Copy pathluv_stream.c
More file actions
179 lines (141 loc) · 4.19 KB
/
Copy pathluv_stream.c
File metadata and controls
179 lines (141 loc) · 4.19 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#ifndef LIB_LUV_STREAM
#define LIB_LUV_STREAM
#include "common.h"
static uv_buf_t luv_on_alloc(uv_handle_t* handle, size_t suggested_size) {
uv_buf_t buf;
buf.base = malloc(suggested_size);
buf.len = suggested_size;
return buf;
}
static void luv_on_read(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) {
lua_State* L = luv_prepare_event(handle->data);
if (nread >= 0) {
if (luv_get_callback(L, -1, "ondata")) {
lua_pushlstring (L, buf.base, nread);
lua_call(L, 2, 0);
}
} else {
uv_err_t err = uv_last_error(uv_default_loop());
if (err.code == UV_EOF) {
if (luv_get_callback(L, -1, "onend")) {
lua_call(L, 1, 0);
}
} else {
fprintf(stderr, "TODO: Implement async error handling\n");
assert(0);
}
}
free(buf.base);
}
static void luv_on_connection(uv_stream_t* handle, int status) {
lua_State* L = luv_prepare_event(handle->data);
if (luv_get_callback(L, -1, "onconnection")) {
lua_call(L, 1, 0);
}
}
static void luv_after_write(uv_write_t* req, int status) {
lua_State* L = luv_prepare_callback(req->data);
if (lua_isfunction(L, -1)) {
lua_call(L, 0, 0);
} else {
lua_pop(L, 1);
}
luv_handle_unref(L, req->handle->data);
free(req->data);
free(req);
}
static void luv_after_shutdown(uv_shutdown_t* req, int status) {
lua_State* L = luv_prepare_callback(req->data);
if (lua_isfunction(L, -1)) {
lua_call(L, 0, 0);
} else {
lua_pop(L, 1);
}
luv_handle_unref(L, req->handle->data);
free(req->data);
free(req);
}
static int luv_read_start(lua_State* L) {
uv_stream_t* handle = luv_get_stream(L, 1);
uv_read_start(handle, luv_on_alloc, luv_on_read);
luv_handle_ref(L, handle->data, 1);
return 0;
}
static int luv_read_stop(lua_State* L) {
uv_stream_t* handle = luv_get_stream(L, 1);
luv_handle_unref(L, handle->data);
uv_read_stop(handle);
return 0;
}
static int luv_listen(lua_State* L) {
uv_stream_t* handle = luv_get_stream(L, 1);
int backlog_size = luaL_optint(L, 2, 128);
if (uv_listen(handle, backlog_size, luv_on_connection)) {
luaL_error(L, "Problem listening");
}
luv_handle_ref(L, handle->data, 1);
return 0;
}
static int luv_accept(lua_State* L) {
uv_stream_t* server = luv_get_stream(L, 1);
uv_stream_t* client = luv_get_stream(L, 2);
if (uv_accept(server, client)) {
luaL_error(L, "Problem accepting client");
}
return 0;
}
static int luv_write(lua_State* L) {
uv_stream_t* handle = luv_get_stream(L, 1);
size_t len;
const char* chunk = luaL_checklstring(L, 2, &len);
uv_buf_t buf = uv_buf_init((char*)chunk, len);
uv_write_t* req = (uv_write_t*)malloc(sizeof(uv_write_t));
luv_req_t* lreq = (luv_req_t*)malloc(sizeof(luv_req_t));
req->data = (void*)lreq;
lreq->lhandle = handle->data;
// Reference the string in the registry
lua_pushvalue(L, 2);
lreq->data_ref = luaL_ref(L, LUA_REGISTRYINDEX);
// Reference the callback in the registry
lua_pushvalue(L, 3);
lreq->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
luv_handle_ref(L, handle->data, 1);
uv_write(req, handle, &buf, 1, luv_after_write);
return 0;
}
static int luv_shutdown(lua_State* L) {
uv_stream_t* handle = luv_get_stream(L, 1);
uv_shutdown_t* req = (uv_shutdown_t*)malloc(sizeof(uv_shutdown_t));
luv_req_t* lreq = (luv_req_t*)malloc(sizeof(luv_req_t));
req->data = (void*)lreq;
lreq->lhandle = handle->data;
lreq->data_ref = LUA_NOREF;
lua_pushvalue(L, 2);
lreq->callback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
luv_handle_ref(L, handle->data, 1);
uv_shutdown(req, handle, luv_after_shutdown);
return 0;
}
static int luv_is_readable(lua_State* L) {
uv_stream_t* handle = luv_get_stream(L, 1);
lua_pushboolean(L, uv_is_readable(handle));
return 1;
}
static int luv_is_writable(lua_State* L) {
uv_stream_t* handle = luv_get_stream(L, 1);
lua_pushboolean(L, uv_is_writable(handle));
return 1;
}
static const luaL_reg luv_stream_m[] = {
{"write", luv_write},
{"shutdown", luv_shutdown},
{"readStart", luv_read_start},
{"readStop", luv_read_stop},
{"listen", luv_listen},
{"accept", luv_accept},
{"write", luv_write},
{"isReadable", luv_is_readable},
{"isWritable", luv_is_writable},
{NULL, NULL}
};
#endif