Finished basic network stack

Game network stack is now working fine. It still needs error checking to be done.
This commit is contained in:
Polonkai Gergely 2012-03-11 22:44:29 +00:00
parent 6f71c03a8c
commit 9e6780cded

View File

@ -4,6 +4,8 @@
#include "config.h" #include "config.h"
#endif #endif
#define MAX_RECV_LEN 1024
struct { struct {
char *file; char *file;
int line; int line;
@ -15,14 +17,30 @@ struct AcceptData {
}; };
gboolean gboolean
client_callback(GSocket *client, GIOCondition condition, gpointer user_data) client_callback(GSocket *client, GIOCondition condition, struct AcceptData *accept_data)
{ {
if (condition & G_IO_HUP)
{
g_print("Connection closed.\n");
g_socket_close(client, NULL);
return FALSE;
}
else if ((condition & G_IO_IN) || (condition & G_IO_PRI))
{
gssize len; gssize len;
gchar *buf = g_malloc0(sizeof(gchar) * 1024); gchar *buf = g_malloc0(sizeof(gchar) * (MAX_RECV_LEN + 1));
gsize max_len = 1024;
len = g_socket_receive(client, buf, max_len, NULL, NULL); if ((len = g_socket_receive(client, buf, MAX_RECV_LEN, NULL, NULL)) == 0)
{
g_print("Connection closed.\n");
g_socket_close(client, NULL);
g_free(buf);
return FALSE;
}
g_print("Client data arrived (%d bytes): \"%s\"\n", len, buf); g_print("Client data arrived (%d bytes): \"%s\"\n", len, buf);
g_free(buf);
}
return TRUE; return TRUE;
} }
@ -32,11 +50,12 @@ game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData
GSocket *client_socket; GSocket *client_socket;
GSource *client_source; GSource *client_source;
g_print("New connection.\n");
client_socket = g_socket_listener_accept_socket(accept_data->listener, NULL, NULL, NULL); client_socket = g_socket_listener_accept_socket(accept_data->listener, NULL, NULL, NULL);
client_source = g_socket_create_source(client_socket, G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL, NULL); client_source = g_socket_create_source(client_socket, G_IO_IN | G_IO_OUT | G_IO_PRI | G_IO_ERR | G_IO_HUP | G_IO_NVAL, NULL);
g_source_set_callback(client_source, (GSourceFunc)client_callback, client_socket, NULL); g_source_set_callback(client_source, (GSourceFunc)client_callback, client_socket, NULL);
g_source_attach(client_source, accept_data->context); g_source_attach(client_source, accept_data->context);
g_print("New connection.\n");
return TRUE; return TRUE;
} }