Can receive messages
This commit is contained in:
parent
bb510e068e
commit
53651462d6
4
Makefile
4
Makefile
|
@ -6,10 +6,10 @@ DEBUG=-g
|
|||
CFLAGS=-D_XOPEN_SOURCE=700 -D_DEFAULT_SOURCE
|
||||
|
||||
open_card_table: main.o oct_networking.o oct_termbox_sprite.o oct_log.o
|
||||
$(CC) $(CFLAGS) $(INC) $(CLIB) -o $(BIN) $^
|
||||
$(CC) $(DEBUG) $(CFLAGS) $(INC) $(CLIB) -o $(BIN) $^
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) $(INC) $(CLIB) -c -o $@ $<
|
||||
$(CC) $(DEBUG) $(CFLAGS) $(INC) $(CLIB) -c -o $@ $<
|
||||
|
||||
test:
|
||||
$(CC) $(CFLAGS) $(INC) $(CLIB) test.c -o test $(DEBUG)
|
||||
|
|
4
main.c
4
main.c
|
@ -100,6 +100,7 @@ int main(int argc, char* argv[]) {
|
|||
if (ev.key == TB_KEY_ESC) {
|
||||
finish = 1;
|
||||
}
|
||||
oct_network_recv_msg();
|
||||
lua_getglobal(L, "oct_loop");
|
||||
lua_pushinteger(L, ev.key);
|
||||
lua_pushinteger(L, ev.ch);
|
||||
|
@ -112,6 +113,7 @@ int main(int argc, char* argv[]) {
|
|||
}
|
||||
else {
|
||||
while (!finish) {
|
||||
oct_network_recv_msg();
|
||||
lua_getglobal(L, "oct_loop");
|
||||
lua_pushinteger(L, 0);
|
||||
lua_pushinteger(L, 0);
|
||||
|
@ -240,7 +242,7 @@ int initialize_everything(char* lua_file) {
|
|||
|
||||
if (config.type == OCT_TYPE_SERVER) {
|
||||
OCT_LOG_INFO("Lua script is server type");
|
||||
if (!oct_network_node_init(config.port)) {
|
||||
if (!oct_network_node_init(config.port, L)) {
|
||||
OCT_LOG_ERROR("Could not establish a socket on port %s\n", config.port);
|
||||
return OCT_NETWORK_ERROR;
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
#include <netdb.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <lua5.3/lualib.h>
|
||||
#include <lua5.3/lauxlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
@ -11,14 +13,16 @@
|
|||
|
||||
struct {
|
||||
int needs_recv;
|
||||
int needs_send;
|
||||
char recv_buffer[BUFFER_SIZE];
|
||||
//char recv_from[NI_MAXHOST];
|
||||
|
||||
int needs_send;
|
||||
char send_buffer[BUFFER_SIZE];
|
||||
int sfd;
|
||||
} oct_network_node;
|
||||
|
||||
int oct_network_node_init(char* port) {
|
||||
oct_network_node.needs_recv = 0;
|
||||
int oct_network_node_init(char* port, lua_State* L) {
|
||||
oct_network_node.needs_recv = 1;
|
||||
oct_network_node.needs_send = 0;
|
||||
|
||||
struct addrinfo hints;
|
||||
|
@ -62,13 +66,45 @@ int oct_network_node_init(char* port) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
lua_pushcfunction(L, oct_network_recv_msg_lua);
|
||||
lua_setglobal(L, "oct_recv");
|
||||
lua_pushcfunction(L, oct_network_send_msg_lua);
|
||||
lua_setglobal(L, "oct_send");
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
// This runs every loop, filling the receive buffer if needed
|
||||
int oct_network_recv_msg() {
|
||||
if (oct_network_node.needs_recv) {
|
||||
struct sockaddr_storage peer_addr;
|
||||
socklen_t peer_addrlen = 0; // init is needed to make valgrind happy
|
||||
|
||||
// recvfrom returns -1 if nothing was received
|
||||
// Need MSG_DONTWAIT flag for nonblocking
|
||||
if (recvfrom(oct_network_node.sfd, oct_network_node.recv_buffer, BUFFER_SIZE, MSG_DONTWAIT, (struct sockaddr *) &peer_addr, &peer_addrlen) > 0) {
|
||||
oct_network_node.needs_recv = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int oct_network_send_msg() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int oct_network_recv_msg_lua(lua_State* L) {
|
||||
if (!oct_network_node.needs_recv) {
|
||||
lua_pushstring(L, oct_network_node.recv_buffer);
|
||||
oct_network_node.needs_recv = 1;
|
||||
}
|
||||
else {
|
||||
lua_pushstring(L, "");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int oct_network_send_msg_lua(lua_State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
#define OCT_NETWORKING_H
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <lua5.3/lualib.h>
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
|
@ -15,9 +16,12 @@ struct oct_network_client;
|
|||
struct oct_network_server;
|
||||
|
||||
|
||||
int oct_network_node_init(char* port);
|
||||
int oct_network_node_init(char* port, lua_State* L);
|
||||
int oct_network_node_deinit();
|
||||
int oct_network_recv_msg();
|
||||
int oct_network_send_msg();
|
||||
|
||||
int oct_network_recv_msg_lua(lua_State* L);
|
||||
int oct_network_send_msg_lua(lua_State* L);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -14,5 +14,8 @@ function oct_init()
|
|||
end
|
||||
|
||||
function oct_loop(key)
|
||||
|
||||
str = oct_recv();
|
||||
if (str ~= "" or str == nil) then
|
||||
OCT_LOG_INFO(str);
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue