-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathlsocket.c
214 lines (180 loc) · 3.49 KB
/
lsocket.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <lua.h>
#include <lauxlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
# include <windows.h>
# include <winsock2.h>
static void
startup() {
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
printf("WSAStartup failed with error: %d\n", err);
exit(1);
}
}
static void
yield() {
Sleep(0);
}
#define EINTR WSAEINTR
#define EWOULDBLOCK WSAEWOULDBLOCK
#else
#include <sys/select.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#define closesocket close
static void
startup() {
}
static void
yield() {
sleep(0);
}
#endif
#define BUFFER_SIZE 1024
struct socket {
int listen_fd;
int fd;
int closed;
};
static int
lstart(lua_State *L) {
const char * addr = luaL_checkstring(L,1);
int port = luaL_checkinteger(L,2);
struct socket * s = lua_newuserdata(L, sizeof(*s));
s->listen_fd = -1;
s->fd = -1;
s->closed = 0;
int lfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
int reuse = 1;
setsockopt(s->listen_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&reuse, sizeof(int));
struct sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr(addr);
service.sin_port = htons(port);
if (bind(lfd, (const struct sockaddr *)&service, sizeof(service)) < 0) {
closesocket(lfd);
printf("bind() failed");
exit(1);
}
if (listen(lfd, 1) < 0) {
printf("listen(): Error");
exit(1);
}
s->listen_fd = lfd;
return 1;
}
static int
fdcanread(int fd) {
fd_set rfds;
struct timeval tv = {0,0};
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
return select(fd+1, &rfds, NULL, NULL, &tv) == 1;
}
static int
test(struct socket *s, const char * welcome, size_t sz) {
if (s->closed) {
closesocket(s->fd);
s->fd = -1;
s->closed = 0;
}
if (s->fd < 0) {
if (fdcanread(s->listen_fd)) {
s->fd = accept(s->listen_fd, NULL, NULL);
if (s->fd < 0) {
return -1;
}
send(s->fd , welcome , sz , 0 );
}
}
if (fdcanread(s->fd)) {
return s->fd;
}
return -1;
}
static int
lread(lua_State *L) {
struct socket * s = lua_touserdata(L,1);
if (s == NULL || s->listen_fd < 0) {
return luaL_error(L, "start socket first");
}
size_t sz = 0;
const char * welcome = luaL_checklstring(L,2,&sz);
int fd = test(s, welcome,sz);
if (fd >= 0) {
char buffer[BUFFER_SIZE];
int rd = recv(fd, buffer, BUFFER_SIZE, 0);
if (rd <= 0) {
s->closed = 1;
lua_pushboolean(L, 0);
return 1;
}
lua_pushlstring(L, buffer, rd);
return 1;
}
return 0;
}
static int
lwrite(lua_State *L) {
struct socket * s = lua_touserdata(L,1);
if (s == NULL || s->listen_fd < 0 || s->fd < 0) {
return luaL_error(L, "start socket first");
}
size_t sz = 0;
const char * buffer = luaL_checklstring(L, 2, &sz);
int p = 0;
for (;;) {
int wt = send(s->fd, buffer+p, sz-p, 0);
if (wt < 0) {
switch(errno) {
case EWOULDBLOCK:
case EINTR:
continue;
default:
closesocket(s->fd);
s->fd = -1;
lua_pushboolean(L,0);
return 1;
}
}
if (wt == sz - p)
break;
p+=wt;
}
if (s->closed) {
closesocket(s->fd);
s->fd = -1;
s->closed = 0;
}
return 0;
}
static int
lyield(lua_State *L) {
yield();
return 0;
}
int
luaopen_ldebug_socket(lua_State *L) {
luaL_checkversion(L);
startup();
luaL_Reg l[] = {
{ "start", lstart },
{ "read", lread },
{ "write", lwrite },
{ "yield", lyield },
{ NULL, NULL },
};
luaL_newlib(L,l);
return 1;
}