mirror of
https://github.com/lunarmodules/luasocket.git
synced 2024-12-26 20:38:22 +01:00
Adjusted wsocket to match usocket. Adjusted windows projects.
This commit is contained in:
parent
195069cf5f
commit
42e0e74487
@ -139,6 +139,9 @@
|
|||||||
<File
|
<File
|
||||||
RelativePath=".\mime.c">
|
RelativePath=".\mime.c">
|
||||||
</File>
|
</File>
|
||||||
|
<File
|
||||||
|
RelativePath=".\options.c">
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\select.c">
|
RelativePath=".\select.c">
|
||||||
</File>
|
</File>
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include "auxiliar.h"
|
#include "auxiliar.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
|
#include "inet.h"
|
||||||
|
|
||||||
static int opt_setmembership(lua_State *L, p_sock ps, int level, int name);
|
static int opt_setmembership(lua_State *L, p_sock ps, int level, int name);
|
||||||
static int opt_setboolean(lua_State *L, p_sock ps, int level, int name);
|
static int opt_setboolean(lua_State *L, p_sock ps, int level, int name);
|
||||||
|
@ -140,7 +140,7 @@ int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len, p_tm tm)
|
|||||||
{
|
{
|
||||||
t_sock sock = *ps;
|
t_sock sock = *ps;
|
||||||
SA dummy_addr;
|
SA dummy_addr;
|
||||||
socklen_t dummy_len;
|
socklen_t dummy_len = sizeof(dummy_addr);
|
||||||
if (sock == SOCK_INVALID) return IO_CLOSED;
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
||||||
if (!addr) addr = &dummy_addr;
|
if (!addr) addr = &dummy_addr;
|
||||||
if (!addr_len) addr_len = &dummy_len;
|
if (!addr_len) addr_len = &dummy_len;
|
||||||
|
@ -15,6 +15,10 @@
|
|||||||
|
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
|
|
||||||
|
static const char *sock_createstrerror(void);
|
||||||
|
static const char *sock_bindstrerror(void);
|
||||||
|
static const char *sock_connectstrerror(void);
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Initializes module
|
* Initializes module
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
@ -53,63 +57,64 @@ void sock_shutdown(p_sock ps, int how)
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Creates and sets up a socket
|
* Creates and sets up a socket
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
int sock_create(p_sock ps, int domain, int type, int protocol)
|
const char *sock_create(p_sock ps, int domain, int type, int protocol)
|
||||||
{
|
{
|
||||||
int val = 1;
|
int val = 1;
|
||||||
t_sock sock = socket(domain, type, protocol);
|
t_sock sock = socket(domain, type, protocol);
|
||||||
if (sock == SOCK_INVALID) return IO_ERROR;
|
if (sock == SOCK_INVALID) return sock_createstrerror();
|
||||||
*ps = sock;
|
*ps = sock;
|
||||||
sock_setnonblocking(ps);
|
sock_setnonblocking(ps);
|
||||||
setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
|
setsockopt(*ps, SOL_SOCKET, SO_REUSEADDR, (char *) &val, sizeof(val));
|
||||||
return IO_DONE;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Connects or returns error message
|
* Connects or returns error message
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
int sock_connect(p_sock ps, SA *addr, socklen_t addr_len, int timeout)
|
const char *sock_connect(p_sock ps, SA *addr, socklen_t addr_len, p_tm tm)
|
||||||
{
|
{
|
||||||
t_sock sock = *ps;
|
t_sock sock = *ps;
|
||||||
if (sock == SOCK_INVALID) return IO_CLOSED;
|
int err, timeout = tm_getretry(tm);
|
||||||
/* if connect fails, we have to find out why */
|
|
||||||
if (connect(sock, addr, addr_len) < 0) {
|
|
||||||
int err;
|
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
fd_set efds, wfds;
|
fd_set efds, wfds;
|
||||||
|
/* don't call on closed socket */
|
||||||
|
if (sock == SOCK_INVALID) return io_strerror(IO_CLOSED);
|
||||||
|
/* ask system to connect */
|
||||||
|
err = connect(sock, addr, addr_len);
|
||||||
|
/* if no error, we're done */
|
||||||
|
if (err == 0) return NULL;
|
||||||
/* make sure the system is trying to connect */
|
/* make sure the system is trying to connect */
|
||||||
if (WSAGetLastError() != WSAEWOULDBLOCK) return IO_ERROR;
|
if (WSAGetLastError() != WSAEWOULDBLOCK) return sock_connectstrerror();
|
||||||
|
/* wait for a timeout or for the system's answer */
|
||||||
tv.tv_sec = timeout / 1000;
|
tv.tv_sec = timeout / 1000;
|
||||||
tv.tv_usec = (timeout % 1000) * 1000;
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
FD_ZERO(&wfds); FD_SET(sock, &wfds);
|
FD_ZERO(&wfds); FD_SET(sock, &wfds);
|
||||||
FD_ZERO(&efds); FD_SET(sock, &efds);
|
FD_ZERO(&efds); FD_SET(sock, &efds);
|
||||||
/* we run select to avoid busy waiting */
|
/* we run select to wait */
|
||||||
err = select(0, NULL, &wfds, &efds, timeout >= 0? &tv: NULL);
|
err = select(0, NULL, &wfds, &efds, timeout >= 0? &tv: NULL);
|
||||||
/* if select returned due to an event */
|
/* if select returned due to an event */
|
||||||
if (err > 0 ) {
|
if (err > 0 ) {
|
||||||
/* the sets tell whether it was a sucess or failure */
|
/* if was in efds, we failed */
|
||||||
if (FD_ISSET(sock,&efds) || !FD_ISSET(sock,&wfds)) {
|
if (FD_ISSET(sock,&efds) || !FD_ISSET(sock,&wfds)) {
|
||||||
int why;
|
int why;
|
||||||
int len = sizeof(why);
|
int len = sizeof(why);
|
||||||
/* find out why it failed */
|
/* find out why we failed */
|
||||||
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&why, &len);
|
getsockopt(sock, SOL_SOCKET, SO_ERROR, (char *)&why, &len);
|
||||||
WSASetLastError(why);
|
WSASetLastError(why);
|
||||||
return IO_ERROR;
|
return sock_connectstrerror();
|
||||||
} else return IO_DONE;
|
/* if was in wfds, we succeeded */
|
||||||
|
} else return NULL;
|
||||||
/* if nothing happened, we timed out */
|
/* if nothing happened, we timed out */
|
||||||
} else if (err == 0) return IO_TIMEOUT;
|
} else return io_strerror(IO_TIMEOUT);
|
||||||
/* otherwise, I don't know what happened */
|
|
||||||
else return IO_ERROR;
|
|
||||||
/* otherwise, it worked */
|
|
||||||
} else return IO_DONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Binds or returns error message
|
* Binds or returns error message
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
int sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
|
const char *sock_bind(p_sock ps, SA *addr, socklen_t addr_len)
|
||||||
{
|
{
|
||||||
if (bind(*ps, addr, addr_len) < 0) return IO_ERROR;
|
if (bind(*ps, addr, addr_len) < 0) return sock_bindstrerror();
|
||||||
else return IO_DONE;
|
else return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
@ -123,27 +128,29 @@ void sock_listen(p_sock ps, int backlog)
|
|||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
* Accept with timeout
|
* Accept with timeout
|
||||||
\*-------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------*/
|
||||||
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len,
|
int sock_accept(p_sock ps, p_sock pa, SA *addr, socklen_t *addr_len, p_tm tm)
|
||||||
int timeout)
|
|
||||||
{
|
{
|
||||||
t_sock sock = *ps;
|
t_sock sock = *ps;
|
||||||
struct timeval tv;
|
|
||||||
SA dummy_addr;
|
SA dummy_addr;
|
||||||
socklen_t dummy_len;
|
socklen_t dummy_len = sizeof(dummy_addr);
|
||||||
fd_set fds;
|
|
||||||
if (sock == SOCK_INVALID) return IO_CLOSED;
|
if (sock == SOCK_INVALID) return IO_CLOSED;
|
||||||
|
if (!addr) addr = &dummy_addr;
|
||||||
|
if (!addr_len) addr_len = &dummy_len;
|
||||||
|
for (;;) {
|
||||||
|
int timeout = tm_getretry(tm);
|
||||||
|
struct timeval tv;
|
||||||
|
fd_set fds;
|
||||||
|
*pa = accept(sock, addr, addr_len);
|
||||||
|
if (*pa != SOCK_INVALID) return IO_DONE;
|
||||||
|
if (timeout == 0) return IO_TIMEOUT;
|
||||||
tv.tv_sec = timeout / 1000;
|
tv.tv_sec = timeout / 1000;
|
||||||
tv.tv_usec = (timeout % 1000) * 1000;
|
tv.tv_usec = (timeout % 1000) * 1000;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(sock, &fds);
|
FD_SET(sock, &fds);
|
||||||
*pa = SOCK_INVALID;
|
/* call select just to avoid busy-wait. */
|
||||||
if (select(0, &fds, NULL, NULL, timeout >= 0 ? &tv : NULL) <= 0)
|
select(0, &fds, NULL, NULL, timeout >= 0? &tv: NULL);
|
||||||
return IO_TIMEOUT;
|
}
|
||||||
if (!addr) addr = &dummy_addr;
|
return IO_TIMEOUT; /* can't get here */
|
||||||
if (!addr_len) addr_len = &dummy_len;
|
|
||||||
*pa = accept(sock, addr, addr_len);
|
|
||||||
if (*pa == SOCK_INVALID) return IO_ERROR;
|
|
||||||
else return IO_DONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------*\
|
||||||
@ -313,7 +320,7 @@ const char *sock_hoststrerror(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *sock_createstrerror(void)
|
static const char *sock_createstrerror(void)
|
||||||
{
|
{
|
||||||
switch (WSAGetLastError()) {
|
switch (WSAGetLastError()) {
|
||||||
case WSANOTINITIALISED: return "not initialized";
|
case WSANOTINITIALISED: return "not initialized";
|
||||||
@ -324,7 +331,7 @@ const char *sock_createstrerror(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *sock_bindstrerror(void)
|
static const char *sock_bindstrerror(void)
|
||||||
{
|
{
|
||||||
switch (WSAGetLastError()) {
|
switch (WSAGetLastError()) {
|
||||||
case WSANOTINITIALISED: return "not initialized";
|
case WSANOTINITIALISED: return "not initialized";
|
||||||
@ -338,7 +345,7 @@ const char *sock_bindstrerror(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *sock_connectstrerror(void)
|
static const char *sock_connectstrerror(void)
|
||||||
{
|
{
|
||||||
switch (WSAGetLastError()) {
|
switch (WSAGetLastError()) {
|
||||||
case WSANOTINITIALISED: return "not initialized";
|
case WSANOTINITIALISED: return "not initialized";
|
||||||
|
@ -132,6 +132,7 @@ end
|
|||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
function test_asciiline(len)
|
function test_asciiline(len)
|
||||||
|
reconnect()
|
||||||
local str, str10, back, err
|
local str, str10, back, err
|
||||||
str = string.rep("x", math.mod(len, 10))
|
str = string.rep("x", math.mod(len, 10))
|
||||||
str10 = string.rep("aZb.c#dAe?", math.floor(len/10))
|
str10 = string.rep("aZb.c#dAe?", math.floor(len/10))
|
||||||
@ -149,6 +150,7 @@ end
|
|||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
function test_rawline(len)
|
function test_rawline(len)
|
||||||
|
reconnect()
|
||||||
local str, str10, back, err
|
local str, str10, back, err
|
||||||
str = string.rep(string.char(47), math.mod(len, 10))
|
str = string.rep(string.char(47), math.mod(len, 10))
|
||||||
str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100),
|
str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100),
|
||||||
@ -167,6 +169,7 @@ end
|
|||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
function test_raw(len)
|
function test_raw(len)
|
||||||
|
reconnect()
|
||||||
local half = math.floor(len/2)
|
local half = math.floor(len/2)
|
||||||
local s1, s2, back, err
|
local s1, s2, back, err
|
||||||
s1 = string.rep("x", half)
|
s1 = string.rep("x", half)
|
||||||
@ -186,8 +189,8 @@ end
|
|||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
function test_totaltimeoutreceive(len, tm, sl)
|
function test_totaltimeoutreceive(len, tm, sl)
|
||||||
local str, err, total
|
|
||||||
reconnect()
|
reconnect()
|
||||||
|
local str, err, total
|
||||||
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
|
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
|
||||||
remote (string.format ([[
|
remote (string.format ([[
|
||||||
data:settimeout(%d)
|
data:settimeout(%d)
|
||||||
@ -206,8 +209,8 @@ end
|
|||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
function test_totaltimeoutsend(len, tm, sl)
|
function test_totaltimeoutsend(len, tm, sl)
|
||||||
local str, err, total
|
|
||||||
reconnect()
|
reconnect()
|
||||||
|
local str, err, total
|
||||||
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
|
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
|
||||||
remote (string.format ([[
|
remote (string.format ([[
|
||||||
data:settimeout(%d)
|
data:settimeout(%d)
|
||||||
@ -226,8 +229,8 @@ end
|
|||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
function test_blockingtimeoutreceive(len, tm, sl)
|
function test_blockingtimeoutreceive(len, tm, sl)
|
||||||
local str, err, total
|
|
||||||
reconnect()
|
reconnect()
|
||||||
|
local str, err, total
|
||||||
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
|
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
|
||||||
remote (string.format ([[
|
remote (string.format ([[
|
||||||
data:settimeout(%d)
|
data:settimeout(%d)
|
||||||
@ -246,8 +249,8 @@ end
|
|||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
function test_blockingtimeoutsend(len, tm, sl)
|
function test_blockingtimeoutsend(len, tm, sl)
|
||||||
local str, err, total
|
|
||||||
reconnect()
|
reconnect()
|
||||||
|
local str, err, total
|
||||||
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
|
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
|
||||||
remote (string.format ([[
|
remote (string.format ([[
|
||||||
data:settimeout(%d)
|
data:settimeout(%d)
|
||||||
@ -266,11 +269,14 @@ end
|
|||||||
|
|
||||||
------------------------------------------------------------------------
|
------------------------------------------------------------------------
|
||||||
function empty_connect()
|
function empty_connect()
|
||||||
|
reconnect()
|
||||||
if data then data:close() data = nil end
|
if data then data:close() data = nil end
|
||||||
|
print("before remote")
|
||||||
remote [[
|
remote [[
|
||||||
if data then data:close() data = nil end
|
if data then data:close() data = nil end
|
||||||
data = server:accept()
|
data = server:accept()
|
||||||
]]
|
]]
|
||||||
|
print("after remote")
|
||||||
data, err = socket.connect("", port)
|
data, err = socket.connect("", port)
|
||||||
if not data then
|
if not data then
|
||||||
pass("ok")
|
pass("ok")
|
||||||
@ -445,7 +451,6 @@ test("connect with timeout (if it hangs, it failed:)")
|
|||||||
connect_timeout()
|
connect_timeout()
|
||||||
|
|
||||||
test("mixed patterns")
|
test("mixed patterns")
|
||||||
reconnect()
|
|
||||||
test_mixed(1)
|
test_mixed(1)
|
||||||
test_mixed(17)
|
test_mixed(17)
|
||||||
test_mixed(200)
|
test_mixed(200)
|
||||||
@ -457,7 +462,6 @@ test_mixed(17)
|
|||||||
test_mixed(1)
|
test_mixed(1)
|
||||||
|
|
||||||
test("character line")
|
test("character line")
|
||||||
reconnect()
|
|
||||||
test_asciiline(1)
|
test_asciiline(1)
|
||||||
test_asciiline(17)
|
test_asciiline(17)
|
||||||
test_asciiline(200)
|
test_asciiline(200)
|
||||||
|
@ -6,7 +6,8 @@ if not server then print("server: " .. tostring(error)) os.exit() end
|
|||||||
ack = "\n"
|
ack = "\n"
|
||||||
while 1 do
|
while 1 do
|
||||||
print("server: waiting for client connection...");
|
print("server: waiting for client connection...");
|
||||||
control = server:accept()
|
control, error = server:accept()
|
||||||
|
assert(control, error)
|
||||||
-- control:setoption("nodelay", true)
|
-- control:setoption("nodelay", true)
|
||||||
while 1 do
|
while 1 do
|
||||||
command, error = control:receive()
|
command, error = control:receive()
|
||||||
|
Loading…
Reference in New Issue
Block a user