aboutsummaryrefslogtreecommitdiffstats
path: root/src/buffer_event.c
blob: 2f5dbcddc908a61088e1f07d3a36ad2809496c68 (plain)
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
/* LuaEvent - Copyright (C) 2007 Thomas Harning <harningt@gmail.com>
 * Licensed as LGPL - See doc/COPYING for details */
#include "buffer_event.h"
#include "luaevent.h"
#include "utility.h"
#include <lauxlib.h>
#include <malloc.h>
#include "event_buffer.h"

#define BUFFER_EVENT_MT "BUFFER_EVENT_MT"

/* Locations of READ/WRITE buffers in the fenv */
#define READ_BUFFER_LOCATION 4
#define WRITE_BUFFER_LOCATION 5

/* Obtains an le_bufferevent structure from a given index */
static le_bufferevent* buffer_event_get(lua_State* L, int idx) {
	return (le_bufferevent*)luaL_checkudata(L, idx, BUFFER_EVENT_MT);
}

/* Obtains an le_bufferevent structure from a given index
	AND checks that it hadn't been prematurely freed
*/
le_bufferevent* buffer_event_check(lua_State* L, int idx) {
	le_bufferevent* buf = (le_bufferevent*)luaL_checkudata(L, idx, BUFFER_EVENT_MT);
	if(!buf->ev)
		luaL_argerror(L, idx, "Attempt to use closed buffer_event object");
	return buf;
}

/* Checks if the given index contains an le_buffer object */
int is_buffer_event(lua_State* L, int idx) {
	int ret;
	lua_getmetatable(L, idx);
	luaL_getmetatable(L, BUFFER_EVENT_MT);
	ret = lua_rawequal(L, -2, -1);
	lua_pop(L, 2);
	return ret;
}

static void handle_callback(le_bufferevent* le_ev, short what, int callbackIndex) {
	lua_State* L = le_ev->base->loop_L;
	le_weak_get(L, le_ev);
	lua_getfenv(L, -1);
	lua_rawgeti(L, -1, callbackIndex);
	lua_remove(L, -2);
	lua_pushvalue(L, -2);
	lua_remove(L, -3);
	/* func, bufferevent */
	lua_pushinteger(L, what);
	/* What to do w/ errors...? */
	lua_pcall(L, 3, 0, 0);
}

static void buffer_event_readcb(struct bufferevent *ev, void *ptr) {
	handle_callback((le_bufferevent*)ptr, EVBUFFER_READ, 1);
}

static void buffer_event_writecb(struct bufferevent *ev, void *ptr) {
	handle_callback((le_bufferevent*)ptr, EVBUFFER_WRITE, 2);
}

static void buffer_event_errorcb(struct bufferevent *ev, short what, void *ptr) {
	handle_callback((le_bufferevent*)ptr, what, 3);
}

/* LUA: new(fd, read, write, error)
	Pushes a new bufferevent instance on the stack
	Accepts: base, fd, read, write, error cb
	Requires base, fd and error cb
*/
static int buffer_event_push(lua_State* L) {
	le_bufferevent *ev;
	le_base* base = event_base_get(L, 1);
	/* NOTE: Should probably reference the socket as well... */
	int fd = getSocketFd(L, 2);
	luaL_checktype(L, 5, LUA_TFUNCTION);
	if(!lua_isnil(L, 3)) luaL_checktype(L, 3, LUA_TFUNCTION);
	if(!lua_isnil(L, 4)) luaL_checktype(L, 4, LUA_TFUNCTION);
	ev= (le_bufferevent*)lua_newuserdata(L, sizeof(le_bufferevent));
	luaL_getmetatable(L, BUFFER_EVENT_MT);
	lua_setmetatable(L, -2);
	ev->ev = bufferevent_new(fd, buffer_event_readcb, buffer_event_writecb, buffer_event_errorcb, ev);
	lua_createtable(L, 5, 0);
	lua_pushvalue(L, 3);
	lua_rawseti(L, -2, 1); // Read
	lua_pushvalue(L, 4);
	lua_rawseti(L, -2, 2); // Write
	lua_pushvalue(L, 5);
	lua_rawseti(L, -2, 3); // Err

	event_buffer_push(L, ev->ev->input);
	lua_rawseti(L, -2, READ_BUFFER_LOCATION);
	event_buffer_push(L, ev->ev->output);
	lua_rawseti(L, -2, WRITE_BUFFER_LOCATION);
	lua_setfenv(L, -2);
	ev->base = base;
	return 1;
}

/* LUA: __gc and buffer:close()
	Releases the buffer resources
*/
static int buffer_event_gc(lua_State* L) {
	le_bufferevent* ev = buffer_event_get(L, 1);
	if(ev->ev) {
		le_buffer *read, *write;
		bufferevent_free(ev->ev);
		ev->ev = NULL;
		/* Also clear out the associated input/output event_buffers
		 * since they would have already been freed.. */
		lua_getfenv(L, 1);
		lua_rawgeti(L, -1, READ_BUFFER_LOCATION);
		lua_rawgeti(L, -2, WRITE_BUFFER_LOCATION);
		read = event_buffer_check(L, -2);
		write = event_buffer_check(L, -1);
		/* Erase Lua's link to the buffers */
		lua_pushnil(L);
		/* LS: ..., fenv, readBuf, writeBuf, nil */
		lua_rawseti(L, -4, READ_BUFFER_LOCATION);
		lua_pushnil(L);
		lua_rawseti(L, -4, WRITE_BUFFER_LOCATION);
		/* Erase their knowledge of the buffer so that the GC won't try to double-free */
		read->buffer = NULL;
		write->buffer = NULL;
	}
	return 0;
}

static int buffer_event_get_read(lua_State* L) {
	(void)buffer_event_get(L, 1);
	lua_getfenv(L, 1);
	lua_rawgeti(L, -1, READ_BUFFER_LOCATION);
	return 1;
}

static int buffer_event_get_write(lua_State* L) {
	(void)buffer_event_get(L, 1);
	lua_getfenv(L, 1);
	lua_rawgeti(L, -1, WRITE_BUFFER_LOCATION);
	return 1;
}

static luaL_Reg buffer_event_funcs[] = {
	{"get_read", buffer_event_get_read},
	{"get_write", buffer_event_get_write},
	{NULL, NULL}
};

static luaL_Reg funcs[] = {
	{"new", buffer_event_push},
	{NULL, NULL}
};

int buffer_event_register(lua_State* L) {
	luaL_newmetatable(L, BUFFER_EVENT_MT);
	lua_pushcfunction(L, buffer_event_gc);
	lua_setfield(L, -2, "__gc");
	lua_newtable(L);
	luaL_register(L, NULL, buffer_event_funcs);
	lua_setfield(L, -2, "__index");
	lua_pop(L, 1);

	luaL_register(L, "luaevent.core.bufferevent", funcs);
	return 1;
}