forked from sanikoyes/skynet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua-clientsocket.c
211 lines (176 loc) · 3.77 KB
/
lua-clientsocket.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
// simple lua socket library for client
// It's only for demo, limited feature. Don't use it in your project.
// Rewrite socket library by yourself .
#define LUA_LIB
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
#include <stdint.h>
#include <pthread.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#define CACHE_SIZE 0x1000
static int
lconnect(lua_State *L) {
const char * addr = luaL_checkstring(L, 1);
int port = luaL_checkinteger(L, 2);
int fd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in my_addr;
my_addr.sin_addr.s_addr=inet_addr(addr);
my_addr.sin_family=AF_INET;
my_addr.sin_port=htons(port);
int r = connect(fd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr_in));
if (r == -1) {
return luaL_error(L, "Connect %s %d failed", addr, port);
}
int flag = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flag | O_NONBLOCK);
lua_pushinteger(L, fd);
return 1;
}
static int
lclose(lua_State *L) {
int fd = luaL_checkinteger(L, 1);
close(fd);
return 0;
}
static void
block_send(lua_State *L, int fd, const char * buffer, int sz) {
while(sz > 0) {
int r = send(fd, buffer, sz, 0);
if (r < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
luaL_error(L, "socket error: %s", strerror(errno));
}
buffer += r;
sz -= r;
}
}
/*
integer fd
string message
*/
static int
lsend(lua_State *L) {
size_t sz = 0;
int fd = luaL_checkinteger(L,1);
const char * msg = luaL_checklstring(L, 2, &sz);
block_send(L, fd, msg, (int)sz);
return 0;
}
/*
intger fd
string last
table result
return
boolean (true: data, false: block, nil: close)
string last
*/
struct socket_buffer {
void * buffer;
int sz;
};
static int
lrecv(lua_State *L) {
int fd = luaL_checkinteger(L,1);
char buffer[CACHE_SIZE];
int r = recv(fd, buffer, CACHE_SIZE, 0);
if (r == 0) {
lua_pushliteral(L, "");
// close
return 1;
}
if (r < 0) {
if (errno == EAGAIN || errno == EINTR) {
return 0;
}
luaL_error(L, "socket error: %s", strerror(errno));
}
lua_pushlstring(L, buffer, r);
return 1;
}
static int
lusleep(lua_State *L) {
int n = luaL_checknumber(L, 1);
usleep(n);
return 0;
}
// quick and dirty none block stdin readline
#define QUEUE_SIZE 1024
struct queue {
pthread_mutex_t lock;
int head;
int tail;
char * queue[QUEUE_SIZE];
};
static void *
readline_stdin(void * arg) {
struct queue * q = arg;
char tmp[1024];
while (!feof(stdin)) {
if (fgets(tmp,sizeof(tmp),stdin) == NULL) {
// read stdin failed
exit(1);
}
int n = strlen(tmp) -1;
char * str = malloc(n+1);
memcpy(str, tmp, n);
str[n] = 0;
pthread_mutex_lock(&q->lock);
q->queue[q->tail] = str;
if (++q->tail >= QUEUE_SIZE) {
q->tail = 0;
}
if (q->head == q->tail) {
// queue overflow
exit(1);
}
pthread_mutex_unlock(&q->lock);
}
return NULL;
}
static int
lreadstdin(lua_State *L) {
struct queue *q = lua_touserdata(L, lua_upvalueindex(1));
pthread_mutex_lock(&q->lock);
if (q->head == q->tail) {
pthread_mutex_unlock(&q->lock);
return 0;
}
char * str = q->queue[q->head];
if (++q->head >= QUEUE_SIZE) {
q->head = 0;
}
pthread_mutex_unlock(&q->lock);
lua_pushstring(L, str);
free(str);
return 1;
}
LUAMOD_API int
luaopen_clientsocket(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{ "connect", lconnect },
{ "recv", lrecv },
{ "send", lsend },
{ "close", lclose },
{ "usleep", lusleep },
{ NULL, NULL },
};
luaL_newlib(L, l);
struct queue * q = lua_newuserdata(L, sizeof(*q));
memset(q, 0, sizeof(*q));
pthread_mutex_init(&q->lock, NULL);
lua_pushcclosure(L, lreadstdin, 1);
lua_setfield(L, -2, "readstdin");
pthread_t pid ;
pthread_create(&pid, NULL, readline_stdin, q);
return 1;
}