Finished basic game networking stack (no error handling)

This commit is contained in:
Gergely Polonkai 2012-03-11 16:50:45 +01:00
parent 8334d5fdc8
commit 6f71c03a8c

View File

@ -9,23 +9,41 @@ struct {
int line; int line;
} debug_context_loc = {NULL, 0}; } debug_context_loc = {NULL, 0};
struct AcceptData {
GSocketListener *listener;
GMainContext *context;
};
gboolean gboolean
game_source_callback(gpointer user_data) client_callback(GSocket *client, GIOCondition condition, gpointer user_data)
{ {
gssize len;
gchar *buf = g_malloc0(sizeof(gchar) * 1024);
gsize max_len = 1024;
len = g_socket_receive(client, buf, max_len, NULL, NULL);
g_print("Client data arrived (%d bytes): \"%s\"\n", len, buf);
return TRUE; return TRUE;
} }
gboolean gboolean
accept_incoming(GSocketService *service, GSocketConnection *connection, GObject source_object, gpointer user_data) game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData *accept_data)
{ {
g_printf("New connection!\n"); GSocket *client_socket;
GSource *client_source;
g_print("New connection.\n");
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);
g_source_set_callback(client_source, (GSourceFunc)client_callback, client_socket, NULL);
g_source_attach(client_source, accept_data->context);
return TRUE; return TRUE;
} }
gboolean gboolean
rl_sec_elapsed(gpointer user_data) rl_sec_elapsed(gpointer user_data)
{ {
g_printf("RL sec elapsed.\n"); g_print("RL sec elapsed.\n");
return TRUE; return TRUE;
} }
@ -58,11 +76,11 @@ main(int argc, char **argv)
GSocket *game_socket6, GSocket *game_socket6,
*game_socket4; *game_socket4;
gboolean need_ipv4_socket = TRUE; gboolean need_ipv4_socket = TRUE;
GSocketService *game_service; GSocketListener *game_listener;
g_type_init(); g_type_init();
g_printf("Starting up...\n"); g_print("Starting up...\n");
game_context = g_main_context_new(); game_context = g_main_context_new();
game_loop = g_main_loop_new(game_context, FALSE); game_loop = g_main_loop_new(game_context, FALSE);
@ -72,7 +90,7 @@ main(int argc, char **argv)
timeout_id = g_source_attach(timeout_source, game_context); timeout_id = g_source_attach(timeout_source, game_context);
g_source_unref(timeout_source); g_source_unref(timeout_source);
game_service = g_threaded_socket_service_new(-1); game_listener = g_socket_listener_new();
/* The following snippet is borrowed from GLib 2.30's gsocketlistener.c /* The following snippet is borrowed from GLib 2.30's gsocketlistener.c
* code, to create the necessary sockets to listen on both IPv4 and * code, to create the necessary sockets to listen on both IPv4 and
@ -97,7 +115,7 @@ main(int argc, char **argv)
if (!result) if (!result)
{ {
g_object_unref(game_socket6); g_object_unref(game_socket6);
g_printf("Unable to create listener IPv6 socket!\n"); g_print("Unable to create listener IPv6 socket!\n");
return 1; return 1;
} }
@ -105,7 +123,7 @@ main(int argc, char **argv)
need_ipv4_socket = FALSE; need_ipv4_socket = FALSE;
game_net_source6 = g_socket_create_source(game_socket6, G_IO_IN, NULL); game_net_source6 = g_socket_create_source(game_socket6, G_IO_IN, NULL);
g_socket_listener_add_socket(G_SOCKET_LISTENER(game_service), game_socket6, NULL, &err); g_socket_listener_add_socket(game_listener, game_socket6, NULL, &err);
} }
if (need_ipv4_socket) if (need_ipv4_socket)
@ -133,12 +151,12 @@ main(int argc, char **argv)
if (!game_socket6) if (!game_socket6)
g_object_unref(game_socket6); g_object_unref(game_socket6);
g_printf("Unable to create listener IPv4 socket!\n"); g_print("Unable to create listener IPv4 socket!\n");
return 1; return 1;
} }
game_net_source4 = g_socket_create_source(game_socket4, G_IO_IN, NULL); game_net_source4 = g_socket_create_source(game_socket4, G_IO_IN, NULL);
g_socket_listener_add_socket(G_SOCKET_LISTENER(game_service), game_socket4, NULL, &err); g_socket_listener_add_socket(game_listener, game_socket4, NULL, &err);
} }
} }
else else
@ -151,19 +169,19 @@ main(int argc, char **argv)
if (game_net_source6) if (game_net_source6)
{ {
g_source_set_callback(game_net_source6, game_source_callback, NULL, NULL); struct AcceptData accept_data = {game_listener, game_context};
g_source_set_callback(game_net_source6, (GSourceFunc)game_source_callback, (gpointer)&accept_data, NULL);
g_source_attach(game_net_source6, game_context); g_source_attach(game_net_source6, game_context);
} }
if (game_net_source4) if (game_net_source4)
{ {
g_source_set_callback(game_net_source4, game_source_callback, NULL, NULL); struct AcceptData accept_data = {game_listener, game_context};
g_source_set_callback(game_net_source4, (GSourceFunc)game_source_callback, (gpointer)&accept_data, NULL);
g_source_attach(game_net_source4, game_context); g_source_attach(game_net_source4, game_context);
} }
g_socket_service_start(game_service);
g_signal_connect(G_OBJECT(game_service), "incoming", G_CALLBACK(accept_incoming), NULL);
g_printf("Startup finished\n"); g_print("Startup finished\n");
g_main_loop_run(game_loop); g_main_loop_run(game_loop);