1
0
Fork 0
mirror of https://github.com/ctruLua/ctruLua.git synced 2025-10-27 16:39:29 +00:00

Fixed UDP sockets, added documentation, cleaning

udp:receivefrom arguments and return value changed so it actually works.
This commit is contained in:
Reuh 2016-06-27 19:31:07 +02:00
parent 3303e9783d
commit 5888ee3810

View file

@ -455,7 +455,7 @@ static int socket_send(lua_State *L) {
size_t size = 0; size_t size = 0;
char *data = (char*)luaL_checklstring(L, 2, &size); char *data = (char*)luaL_checklstring(L, 2, &size);
size_t sent; ssize_t sent;
if (!userdata->isSSL) { if (!userdata->isSSL) {
sent = send(userdata->socket, data, size, 0); sent = send(userdata->socket, data, size, 0);
} else { } else {
@ -483,77 +483,90 @@ UDP sockets
*/ */
/*** /***
Receive some data from a server. Receive a datagram from the UDP object.
@function :receivefrom @function :receivefrom
@tparam number count amount of data to receive @tparam[opt=8191] number count maximum amount of bytes to receive from the datagram. Must be lower than 8192.
@tparam string host host name @treturn[1] string data
@tparam number port port @treturn[1] string IP address of the sender
@treturn string data @treturn[1] integer port number of the sender
@treturn[2] nil in case of error or no datagram to receive
@treturn[2] string error message
*/ */
static int socket_receivefrom(lua_State *L) { static int socket_receivefrom(lua_State *L) {
socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket");
int count = luaL_checkinteger(L, 2); int count = luaL_optinteger(L, 2, 8191);
size_t namesize = 0;
char *hostname = (char*)luaL_optlstring(L, 3, NULL, &namesize);
int port = luaL_optinteger(L, 4, 0);
struct sockaddr_in from = {0}; struct sockaddr_in from;
if (hostname != NULL) { // For a server socklen_t addr_len;
struct hostent *hostinfo = gethostbyname(hostname);
if (hostinfo == NULL) { char* buffer = calloc(1, count+1);
ssize_t n = recvfrom(userdata->socket, buffer, count, 0, (struct sockaddr *)&from, &addr_len);
if (n == 0) {
free(buffer);
lua_pushnil(L); lua_pushnil(L);
return 1; lua_pushstring(L, "nothing to receive");
} return 2;
from.sin_addr = *(struct in_addr*)hostinfo->h_addr;
from.sin_port = htons(port);
from.sin_family = AF_INET;
}
char* buffer = malloc(count+1); } else if (n < 0) {
int n = recvfrom(userdata->socket, buffer, count, 0, (struct sockaddr*)&from, NULL); free(buffer);
*(buffer+n) = 0x0; lua_pushnil(L);
lua_pushstring(L, strerror(n));
return 2;
}
lua_pushstring(L, buffer); lua_pushstring(L, buffer);
if (hostname != NULL) {
return 1;
} else {
lua_pushstring(L, inet_ntoa(from.sin_addr)); lua_pushstring(L, inet_ntoa(from.sin_addr));
lua_pushinteger(L, ntohs(from.sin_port)); lua_pushinteger(L, ntohs(from.sin_port));
free(buffer);
return 3; return 3;
} }
}
/*** /***
Send some data to a server. Send a datagram to the specified IP and port.
@function :sendto @function :sendto
@tparam string data data to send @tparam string data data to send
@tparam string host host name @tparam string host IP/hostname of the recipient
@tparam number port port @tparam number port port number of the recipient
@treturn[1] boolean true in case of success
@treturn[2] boolean false in case of error
@treturn[2] string error message
*/ */
static int socket_sendto(lua_State *L) { static int socket_sendto(lua_State *L) {
socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket"); socket_userdata *userdata = luaL_checkudata(L, 1, "LSocket");
size_t datasize = 0; size_t datasize;
char *data = (char*)luaL_checklstring(L, 2, &datasize); const char *data = luaL_checklstring(L, 2, &datasize);
size_t namesize = 0; const char *hostname = luaL_checkstring(L, 3);
char *hostname = (char*)luaL_checklstring(L, 3, &namesize);
int port = luaL_checkinteger(L, 4); int port = luaL_checkinteger(L, 4);
struct hostent *hostinfo = gethostbyname(hostname); struct hostent *hostinfo = gethostbyname(hostname);
if (hostinfo == NULL) { if (hostinfo == NULL) {
lua_pushnil(L); lua_pushboolean(L, false);
return 1; lua_pushstring(L, "unknown host");
return 2;
} }
struct sockaddr_in to = {0};
struct sockaddr_in to;
to.sin_addr = *(struct in_addr *)hostinfo->h_addr; to.sin_addr = *(struct in_addr *)hostinfo->h_addr;
to.sin_port = htons(port); to.sin_port = htons(port);
to.sin_family = AF_INET; to.sin_family = AF_INET;
sendto(userdata->socket, data, datasize, 0, (struct sockaddr*)&to, sizeof(to)); ssize_t n = sendto(userdata->socket, data, datasize, 0, (struct sockaddr *)&to, sizeof(to));
return 0; if (n < 0) {
lua_pushboolean(L, false);
lua_pushstring(L, strerror(n));
return 2;
} }
// module functions lua_pushboolean(L, true);
return 1;
}
// Module functions
static const struct luaL_Reg socket_functions[] = { static const struct luaL_Reg socket_functions[] = {
{ "init", socket_init }, { "init", socket_init },
{ "shutdown", socket_shutdown }, { "shutdown", socket_shutdown },
@ -563,7 +576,7 @@ static const struct luaL_Reg socket_functions[] = {
{NULL, NULL} {NULL, NULL}
}; };
// object // Object methods
static const struct luaL_Reg socket_methods[] = { static const struct luaL_Reg socket_methods[] = {
{ "accept", socket_accept }, { "accept", socket_accept },
{ "bind", socket_bind }, { "bind", socket_bind },