I wrote a simple DBus server in Linux using Qt's QDBus. It is a very small amount of code and the core of it is here:
InterfaceDescription::InterfaceDescription()
{
new ifadapter(this); // Cleans itself up
qDebug() << "Creating";
QDBusConnection dbus = QDBusConnection::sessionBus(); // Use session bus
dbus.registerObject("/mygatt",this); // Register object on the bus
dbus.registerService("com.my.gatt.interface"); // Expose interface to others
qDebug() << "Done creating";
}
QByteArray InterfaceDescription::read() {
qDebug() << "CALLING READ";
return QByteArray("HELLO");
}
I then wrote a small DBus client in Linux also using Qt's QDBus. It works great and I can successfully communicate from this client to my server. Client code:
#include <QCoreApplication>
#include "clientIf.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
clientIf* client = new clientIf("com.my.gatt.interface", "/mygatt", QDBusConnection::sessionBus(), 0);
qDebug() << "Sending Read() command over Dbus to server...";
client->read();
qDebug() << "Done sending read command...";
return a.exec();
}
No I am trying to use GDBus to implement the client. So far I have this:
#include <stdbool.h>
#include <stdio.h>
#include <glib/gprintf.h>
#include <gio/gio.h>
void test_Echo(GDBusProxy *proxy)
{
GVariant *result;
GError *error = NULL;
const gchar *str;
g_printf("Calling read...\n");
result = g_dbus_proxy_call_sync(proxy,
"read",
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
g_assert_no_error(error);
g_variant_get(result, "(&s)", &str);
g_printf("The server answered: '%s'\n", str);
g_variant_unref(result);
}
void test_Quit(GDBusProxy *proxy)
{
GVariant *result;
GError *error = NULL;
g_printf("Calling method Quit()...\n");
result = g_dbus_proxy_call_sync(proxy,
"Quit",
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
g_assert_no_error(error);
g_variant_unref(result);
}
int main(void)
{
GDBusProxy *proxy;
GDBusConnection *conn;
GError *error = NULL;
const char *version;
GVariant *variant;
conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
g_assert_no_error(error);
proxy = g_dbus_proxy_new_sync(conn,
G_DBUS_PROXY_FLAGS_NONE,
NULL, /* GDBusInterfaceInfo */
"com.my.gatt.interface", /* name */
"/mygatt", /* object path */
"com.my.gatt.interface", /* interface */
NULL, /* GCancellable */
&error);
g_assert_no_error(error);
/* Test all server methods */
test_Echo(proxy);
test_Quit(proxy);
g_object_unref(proxy);
g_object_unref(conn);
return 0;
}
When I run this code, it does not work like the QDBus does, it errors with the following error:
ERROR:../dbustester/main.cpp:29:void test_Echo(GDBusProxy*): assertion failed (error == NULL): GDBus.Error:org.freedesktop.DBus.Error.UnknownInterface: No such interface 'com.my.gatt.interface' at object path '/mygatt' (g-dbus-error-quark, 42)
Calling read...
Aborted
So QDBus works with the server, but GDBus does not. What am I doing wrong?
Figured it out, QDBus generates the interface with a strange name, so my Interface Name was specified wrong. I used the gdbus tool to figure this out.
Related
I am implimenting the StatusNotifierWatcher service using the low-level DBus C library. The StatusNotifierWatcher specification requires that the watcher can know when "A StatusNotifierItem instance has disappeared from the bus" So that it can send the StatusNotifierItemUnregistered signal.
An example implimentation:
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus.h>
DBusConnection *conn = NULL;
void item_unregistered_signal(const char *name) {
DBusMessage *signal = dbus_message_new_signal(
"/org/freedesktop/StatusNotifierWatcher",
"org.freedesktop.StatusNotifierWatcher",
"StatusNotifierItemUnregistered");
dbus_message_append_args(signal,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID);
dbus_connection_send(conn, signal, NULL);
dbus_message_unref(signal);
}
void watch_name(const char *name, void(*cb)(const char *)) {
// Not sure how to impliment
}
dbus_bool_t register_item(DBusConnection *connection, DBusMessage *message, void *_data) {
DBusError error;
char *name;
if (!dbus_message_get_args(message, &error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID)) {
fprintf(stderr, "Error parsing method args: %s\n", error.message);
return FALSE;
}
watch_name(name, item_unregistered_signal);
return TRUE;
}
static void check_and_abort(DBusError *error) {
if (dbus_error_is_set(error)) {
fprintf(stderr, "dbus_err: %s\n", error->message);
exit(EXIT_FAILURE);
}
}
int main() {
DBusError error;
dbus_error_init(&error);
conn = dbus_bus_get(DBUS_BUS_SESSION, &error);
check_and_abort(&error);
dbus_bus_request_name(conn, "org.freedesktop.StatusNotifierWatcher",
DBUS_NAME_FLAG_REPLACE_EXISTING,
&error);
check_and_abort(&error);
dbus_connection_add_filter(conn, register_item, NULL, free);
while(1) {
dbus_connection_read_write_dispatch(conn, 1000);
}
}
If I have the well-known name to a DBus service, how do I know when the name disappears from the bus?
Well, I figured this out and I'll post an answer for any future poor souls who need to work with libdbus.
org.freedesktop.DBus sends the NameOwnerChanged signal whenever any DBus name changes. One can use this signal to track if an item has disappeared because the NewOwner argument is a null string.
This function can do that:
static DBusHandlerResult signal_handler(DBusConnection *connection,
DBusMessage *message, void *_usr_data) {
if (dbus_message_is_signal(message, "org.freedesktop.DBus",
"NameOwnerChanged")) {
const char *name;
const char *old_owner;
const char *new_owner;
if (!dbus_message_get_args(message, NULL,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &old_owner,
DBUS_TYPE_STRING, &new_owner,
DBUS_TYPE_INVALID)) {
fprintf(stderr, "Error getting OwnerChanged args");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (strcmp(name, "") != 0) {
// Name not lost, just swapped owners
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
name_lost(name);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
One also needs to add a match so that your program will be called with this signal. I added this in main()
dbus_bus_add_match(conn,
"type='signal',\
sender='org.freedesktop.DBus',\
interface='org.freedesktop.DBus',\
member='NameOwnerChanged'",
&error);
check_and_abort(&error);
dbus_connection_add_filter(conn, signal_handler, NULL, free);
For those using GDBus (recommended) instead of libdbus (discouraged), the equivalent is g_bus_watch_name().
I am trying to learn gio library, especially the giostream and gthreadedsocketservice. I want to write a simple server that:
Each incoming connection will be handled by an individual new thread
At the client side, user types a string and it will be sent to the server; at the server side upon receiving the string, immediately displays it to stdout.
Unless server or client is terminated, the connection is not closed. Namely multiple messages can be sent from client to server without the need to connect multiple times.
The code I tried is:
Client-side:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
int main(int argc, char* argv[]){
GError* error = NULL;
GSocketConnection* connection = NULL;
GOutputStream* ostream = NULL;
GSocketClient* client = g_socket_client_new();
gchar message[1024];
connection = g_socket_client_connect_to_host(client, (gchar*)"localhost", 1500, NULL, &error);
if (error) {
g_error(error->message);
g_error_free(error);
return 1;
}
else g_print("Message: connected.\n");
while(TRUE){
scanf("%s", message);
ostream = g_io_stream_get_output_stream(G_IO_STREAM(connection));
g_output_stream_write(ostream, message, strlen(message), NULL, &error);
if (error) {
g_error(error->message);
g_error_free(error);
return 1;
}
}
g_print("Message: client terminated.\n");
return 0;
}
Server-side:
#include <glib.h>
#include <gio/gio.h>
gboolean run_callback(GThreadedSocketService*, GSocketConnection*, GObject*, gpointer);
int main(int argc, char **argv){
int port = 1500;
GError* error = NULL;
GMainLoop* loop = NULL;
GThreadedSocketService* service = NULL;
service = (GThreadedSocketService*)g_threaded_socket_service_new(-1);
g_socket_listener_add_inet_port((GSocketListener*)service, port, NULL, &error);
if (error != NULL) {g_error(error->message);}
g_signal_connect(service, "run", G_CALLBACK(run_callback), NULL);
g_socket_service_start((GSocketService*)service);
g_print("Message: server launched...\n");
loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
return 0;
}
gboolean run_callback(GThreadedSocketService* service, GSocketConnection* connection, GObject* source_object, gpointer user_data){
GInputStream* instream = NULL;
gchar message[1024];
GError* error = NULL;
instream = g_io_stream_get_input_stream(G_IO_STREAM(connection));
g_input_stream_read_all(instream, message, 1024, NULL, NULL, &error);
if (error != NULL) {
g_error(error->message);
g_error_free(error);
return FALSE;
}
g_print("Received: %s\n", message);
g_print("Message: connection terminated.\n");
if (error) g_error_free(error);
return FALSE;
}
The problem is when I tested it out, on client side I typed three lines:
aaa
bbb
ccc
But nothing is shown on the server side. Only when I exit the client, on the server screen it shows:
aaabbbccc
But what I wanted was when I type "aaa" and entered, it immediately shows up on the server screen.
Any idea on where it goes wrong?
The problem is that you use g_input_stream_read_all. Notice the suffix all in the name? It means that it will attempt to read the size you pass to it, only returning when it has received all those bytes or there is an error or disconnection.
Instead use e.g. g_input_stream_read in a loop.
I would be glad if I can be pointed in the right direction the community concerning my above topic.
I am interested in connecting to bluetooth devices using the gnome-bluetooth api in ubuntu 14.04 using the bluetooth_client_connect_service() function.
I have tried searching but could not find good results on how to use it so I decided to read the gnome-bluetooth's source code but due to insufficient commenting I am unable to understand.
Below is what I have done so far but I do not get any errors when I try running my application yet when I double-click on a device it does nothing.
#define AGENT_PATH "/org/bluez/agent/wizard"
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <signal.h>
#include <math.h>
#include <glib.h>
//#include <dbus/dbus.h>
#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <bluetooth-chooser.h>
#include <bluetooth-client.h>
#include <bluetooth-utils.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#define CONNECT_TIMEOUT 3.0
#define AGENT_PATH "/org/bluez/agent/wizard"
typedef struct {
char *path;
GTimer *timer;
} ConnectData;
BluetoothClient *client;
GtkWidget *selector;
GtkWidget *vboxMainLayout;
GValue value = { 0, };
int find_conn(int s, int dev_id, long arg)
{
struct hci_conn_list_req *cl;
struct hci_conn_info *ci;
int i;
if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
perror("Can't allocate memory");
exit(1);
}
cl->dev_id = dev_id;
cl->conn_num = 10;
ci = cl->conn_info;
if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
perror("Can't get connection list");
exit(1);
}
for (i = 0; i < cl->conn_num; i++, ci++)
if (!bacmp((bdaddr_t *) arg, &ci->bdaddr)) {
free(cl);
return 1;
}
free(cl);
return 0;
}
void cmd_rssi(const char *bt_address)
{
struct hci_conn_info_req *cr;
bdaddr_t bdaddr;
int8_t rssi;
int dd, dev_id;
str2ba(bt_address, &bdaddr);
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
if (dev_id < 0) {
g_print("\tNot connected.\n");
return;
}
dd = hci_open_dev(dev_id);
if (dd < 0) {
perror("HCI device open failed");
exit(1);
}
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
if (!cr) {
perror("Can't allocate memory");
exit(1);
}
bacpy(&cr->bdaddr, &bdaddr);
cr->type = ACL_LINK;
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
perror("Get connection info failed");
exit(1);
}
if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {
perror("Read RSSI failed");
exit(1);
}
g_print("\tRSSI return value: %d\n", rssi);
free(cr);
hci_close_dev(dd);
}
void connect_callback (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
ConnectData *data = (ConnectData *) user_data;
gboolean success;
success = bluetooth_client_connect_service_finish (client, res, NULL);
if (success == FALSE && g_timer_elapsed (data->timer, NULL) < CONNECT_TIMEOUT) {
bluetooth_client_connect_service (client, data->path, TRUE, NULL, connect_callback, data);
return;
}
if (success == FALSE)
g_print ("\tFailed to connect to device %s", data->path);
else
g_print("\n\tConnection successfully.. ha.. I'm tired\n");
g_timer_destroy (data->timer);
g_free (data->path);
g_free (data);
}
void create_callback (BluetoothClient *_client, const char *path, const GError *error, gpointer user_data)
{
ConnectData *data;
//compiler throws "implicit declaration" warning here
//bluetooth_client_set_trusted(client, path, TRUE);
data = g_new0 (ConnectData, 1);
data->path = g_strdup (path);
data->timer = g_timer_new ();
bluetooth_client_connect_service (client, path, TRUE, NULL, connect_callback, data);
}
void get_device_info(BluetoothChooser *self)
{
const gchar* result;
g_print ("Info dumped:\n");
if (bluetooth_chooser_get_selected_device_info (self, "name", &value)) {
g_print ("\tName: '%s'\n", g_value_get_string (&value));
g_value_unset (&value);
}
if (bluetooth_chooser_get_selected_device_info (self, "address", &value)) {
g_print ("\tAddress: '%s'\n", g_value_get_string (&value));
g_value_unset (&value);
}
if (bluetooth_chooser_get_selected_device_info (self, "paired", &value)) {
result = g_value_get_boolean (&value)? "Paired":"Unpaired";
g_print ("\tPaired: '%s'\n", result);
g_value_unset (&value);
}
guint type = bluetooth_chooser_get_selected_device_type (self);
const gchar *device_type = bluetooth_type_to_string(type);
if(type)
{
g_print("\tType: '%s'\n", device_type);
}
if (bluetooth_chooser_get_selected_device_info (self, "connected", &value)) {
result = g_value_get_boolean (&value)? "Connected":"Not Connected";
g_print ("\tConnected: '%s'\n", result);
g_value_unset (&value);
}
}
/* My problem lies here.. how to connect to the detected device
* no error message is displayed when a device is double-clicked
*/
void connect_button_clicked(GtkWidget *widget, gpointer user_data)
{
const char *path = AGENT_PATH;
ConnectData *data = (ConnectData *) user_data;
GValue value = { 0, };
bluetooth_chooser_get_selected_device_info (widget, "address", &value);
bluetooth_client_connect_service (client, path, TRUE, NULL, connect_callback, data);
//function to get the rssi value of the remote device
cmd_rssi(g_value_get_string (&value));
}
void create_interface(GtkApplication *app, gpointer user_data)
{
GtkWidget *frmTopWindow;
frmTopWindow = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(frmTopWindow), "Test");
gtk_window_set_position(GTK_WINDOW(frmTopWindow),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(frmTopWindow),200,400);
selector = bluetooth_chooser_new();
g_object_set(selector,
"show-searching", TRUE,
"show-device-type", FALSE,
"show-pairing" , TRUE,
"show-device-category", FALSE,
NULL);
client = bluetooth_client_new();
vboxMainLayout = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
g_object_set(vboxMainLayout,
"width-request", 190,
"height-request", 300, NULL);
gtk_container_add(GTK_CONTAINER(frmTopWindow),selector);
/*Events and Signals*/
/*------------------*/
// When user double-clicks on a detected device, try and connect to that device
// and display it's RSSI
g_signal_connect(G_OBJECT(selector),"notify::selected-device-activated",G_CALLBACK(connect_button_clicked),client);
//When user clicks on a detected device, display information about that device in the
// standard output
g_signal_connect(G_OBJECT(selector),"notify::device-selected",G_CALLBACK(get_device_info),vboxMainLayout);
gtk_widget_show_all(frmTopWindow);
}
int main(int argc, char** argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("rucst.project.test", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (create_interface), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
In my implementation code, I have a g_signal_connect function that calls it as shown below:
g_signal_connect(G_OBJECT(selector),"notify::selected-device-activated",G_CALLBACK(connect_button_clicked),client);
This code I expect to connect to the selected detected device when it receives a double-click signal but at the moment nothing happens when I double-click on it.
I would be very grateful to receive guidance from the experts.
Thank you in advance
g_signal_connect(G_OBJECT(selector),"notify::selected-device-activated",
G_CALLBACK(connect_button_clicked),client);
This signal signature would be used if there was a property "selected-device-activated" and you wanted to know when the property value changes. But in this case "selected-device-activated" is an actual signal so you should just do:
g_signal_connect(G_OBJECT(selector),"selected-device-activated",
G_CALLBACK(callback), client);
The single click version just happens to work because you've found a property that does what you want, so connecting to "notify::device-selected" works (I'd still connect to the "selected-device-changed" signal for consistency instead).
After that in connect_button_clicked() your connect call uses what seems like a an unrelated path as the device object path... Something like this might work instead (although I'm not 100% sure about the "proxy" field name, I've not used this API myself):
GValue value = { 0, };
if (bluetooth_chooser_get_selected_device_info (BLUETOOTH_CHOOSER (widget),
"proxy", &value)) {
GDBusProxy *proxy = g_value_get_object (&value);
g_print ("Connecting to %s\n", g_dbus_proxy_get_object_path(proxy));
bluetooth_client_connect_service (client, g_dbus_proxy_get_object_path(proxy),
TRUE, NULL, connect_callback, data);
}
Note also that the userdata pointers you give to the callbacks are wrong: e.g. connect_button_clicked() expects a ConnectData* but gets a BluetoothClient*.
I have currently a synchronous UDP application receiving messages.
The code :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <gio/gio.h>
int main(argc,argv)
int argc;
char ** argv;{
char buf[256], *ptr, sep[] = "| ";
GError * error = NULL;
GSocket * socket;
GSocketAddress *gsockAddr, *gfromAddr;
guint16 udp_port = 1500;
//Creates socket udp ipv4
socket = g_socket_new(G_SOCKET_FAMILY_IPV4,
G_SOCKET_TYPE_DATAGRAM,
G_SOCKET_PROTOCOL_UDP,
&error);
g_assert(error == NULL);
if (socket == NULL) {
g_print("ERROR");
exit(1);
}
//sockaddr struct like
gsockAddr = G_SOCKET_ADDRESS(g_inet_socket_address_new(g_inet_address_new_any(G_SOCKET_FAMILY_IPV4), udp_port));
if(gsockAddr == NULL){
g_error("Error socket\n");
exit(1);
}
//
if (g_socket_bind (socket, gsockAddr, TRUE, NULL) == FALSE){
g_print("Error bind\n");
exit(1);
}
int bytes = g_socket_receive_from (socket,
&gfromAddr,
buf,
255,
NULL,
&error);
if (bytes == -1) {
g_warning ("Failed to receive from socket: %s", error->message);
g_error_free (error);
return TRUE;
}
g_message("Server receive: %s", buf);
guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(gfromAddr));
g_print("...from %s(%d)\n",g_inet_address_to_string(g_inet_socket_address_get_address(G_INET_SO CKET_ADDRESS(gfromAddr))), (int) port);
exit(0);
}
So, I want to make the receive operation, non-blocking instead of blocking. I want to make it either ansynchronous, or/and threaded so that, meanwhile, I could do other operations related to the application I want to develop.
But I did not suceed to make it like I want. I tried to use GLib IO Channels, but I can not make it works. The processus is waiting, but only because of the Main Loop (I can not telnet the application).
The code :
#include <gio/gio.h>
#include <glib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BLOCK_SIZE 1024
static gboolean
gio_read_socket (GIOChannel *channel,
GIOCondition condition,
gpointer data)
{
char buf[1024];
gsize bytes_read;
GError *error = NULL;
if (condition & G_IO_HUP) return FALSE; /* this channel is done */
g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read,
&error);
g_assert (error == NULL);
buf[bytes_read] = '\0';
g_print ("%s", buf);
return TRUE;
}
int
main (int argc, char **argv)
{
GSocket * s_udp;
GError *err = NULL;
guint16 udp_port = 5556;
s_udp = g_socket_new(G_SOCKET_FAMILY_IPV4,
G_SOCKET_TYPE_DATAGRAM,
G_SOCKET_PROTOCOL_UDP,
&err);
g_assert(err == NULL);
if (s_udp == NULL) {
g_print("ERROR");
exit(1);
}
g_socket_bind(s_udp,
G_SOCKET_ADDRESS(g_inet_socket_address_new(g_inet_address_new_any(G_SOCKET_FAMILY_IPV4), udp_port)),
TRUE,
&err);
g_assert(err == NULL);
int fd = g_socket_get_fd(s_udp);
GIOChannel* channel = g_io_channel_unix_new(fd);
guint source = g_io_add_watch(channel, G_IO_IN,
(GIOFunc) gio_read_socket, NULL);
g_io_channel_unref(channel);
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
g_main_loop_unref(loop);
}
I am quite a beginner with GLib/Gio, and I think I am doing wrong with the IO Channels. I would like to add it to the main loop as an event, so that I could use my callback function. Maybe there is a simpler way to do that.
Besides, I have a TCP asynchronous and threaded server that is working, but I did not find how to do the same with UDP (using a GThreadedSocketService and creating a socket listener, then adding the service to the main loop. Easy as pie with TCP).
Do you have any idea how to proceed ? If you know how to do but only with the basic API socket, I still take it ! Thanks.
I figure it out.
I am indeed quite a beginner. Because, when I wanted to test my udp application (the second code block), I used telnet to connect to it and try to send messages. However, we can not telnet udp applications of course...
So I tried with a simple udp sender (I used Glib/Gio for it by the way) instead of telnet and it worked, perfectly non-blocking and reusable. I did make some changes but basically, it is the same. I put an idle function to show you how non-blocking it is, whether this can help someone one day.
My simple Glib/Gio UDP app, non blocking :
#include <gio/gio.h>
#include <glib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BLOCK_SIZE 1024
static gboolean
gio_read_socket (GIOChannel *channel,
GIOCondition condition,
gpointer data)
{
char buf[1024];
gsize bytes_read;
GError *error = NULL;
if (condition & G_IO_HUP) return FALSE; /* this channel is done */
g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read,
&error);
g_assert (error == NULL);
buf[bytes_read] = '\0';
g_print ("%s", buf);
int *a = data;
*a = *a + 1;
return TRUE;
}
gboolean
idleCpt (gpointer user_data){
int *a = user_data;
g_print("%d\n", *a);
sleep(1);
return TRUE;
}
int
main (int argc, char **argv)
{
GSocket * s_udp;
GError *err = NULL;
int idIdle = -1, dataI = 0;
guint16 udp_port = 1505;
GSocketAddress * gsockAddr = G_SOCKET_ADDRESS(g_inet_socket_address_new(g_inet_address_new_any(G_SOCKET_FAMILY_IPV4), udp_port));
s_udp = g_socket_new(G_SOCKET_FAMILY_IPV4,
G_SOCKET_TYPE_DATAGRAM,
G_SOCKET_PROTOCOL_UDP,
&err);
g_assert(err == NULL);
if (s_udp == NULL) {
g_print("ERREUR");
exit(1);
}
if (g_socket_bind (s_udp, gsockAddr, TRUE, NULL) == FALSE){
g_print("Erreur bind\n");
exit(1);
}
g_assert(err == NULL);
int fd = g_socket_get_fd(s_udp);
GIOChannel* channel = g_io_channel_unix_new(fd);
guint source = g_io_add_watch(channel, G_IO_IN,
(GIOFunc) gio_read_socket, &dataI);
g_io_channel_unref(channel);
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
idIdle = g_idle_add(idleCpt, &dataI);
g_main_loop_run(loop);
}
The code is not perfect, there is a lot of optimisations to make, but we can do nice things from that I think. If you want to see my udp sender, just ask.
I'm learning the basics of writing a simple, efficient socket server using GLib. I'm experimenting with GSocketService. So far I can only seem to accept connections but then they are immediately closed. From the docs I can't figure out what step I am missing. I'm hoping someone can shed some light on this for me.
When running the following:
# telnet localhost 4000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
# telnet localhost 4000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
# telnet localhost 4000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.
Output from the server:
# ./server
New Connection from 127.0.0.1:36962
New Connection from 127.0.0.1:36963
New Connection from 127.0.0.1:36965
Current code:
/*
* server.c
*
* Created on: Mar 10, 2010
* Author: mark
*/
#include <glib.h>
#include <gio/gio.h>
gchar *buffer;
gboolean
network_read(GIOChannel *source,
GIOCondition cond,
gpointer data)
{
GString *s = g_string_new(NULL);
GError *error;
GIOStatus ret = g_io_channel_read_line_string(source, s, NULL, &error);
if (ret == G_IO_STATUS_ERROR)
g_error ("Error reading: %s\n", error->message);
else
g_print("Got: %s\n", s->str);
}
gboolean
new_connection(GSocketService *service,
GSocketConnection *connection,
GObject *source_object,
gpointer user_data)
{
GSocketAddress *sockaddr = g_socket_connection_get_remote_address(connection, NULL);
GInetAddress *addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr));
g_print("New Connection from %s:%d\n", g_inet_address_to_string(addr), port);
GSocket *socket = g_socket_connection_get_socket(connection);
gint fd = g_socket_get_fd(socket);
GIOChannel *channel = g_io_channel_unix_new(fd);
g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, NULL);
return TRUE;
}
int main(int argc, char **argv) {
g_type_init();
GSocketService *service = g_socket_service_new();
GInetAddress *address = g_inet_address_new_from_string("127.0.0.1");
GSocketAddress *socket_address = g_inet_socket_address_new(address, 4000);
g_socket_listener_add_address(G_SOCKET_LISTENER(service), socket_address, G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_TCP, NULL, NULL, NULL);
g_object_unref(socket_address);
g_object_unref(address);
g_socket_service_start(service);
g_signal_connect(service, "incoming", G_CALLBACK(new_connection), NULL);
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
}
The GSocketConnection has to be ref'ed in the incoming callback, this will keep the connection alive. You can pass it to a data structure, a class, or as user_data to the watch callback.
gboolean
new_connection(...)
{
...
g_object_ref (connection);
GSocket *socket = g_socket_connection_get_socket(connection);
gint fd = g_socket_get_fd(socket);
GIOChannel *channel = g_io_channel_unix_new(fd);
// Pass connection as user_data to the watch callback
g_io_add_watch(channel, G_IO_IN, (GIOFunc) network_read, connection);
return TRUE;
}
You are not returning in the watch callback network_read(), you must end it with "return true". From the documentation: "the function should return FALSE if the event source should be removed".
The 100% CPU is caused by the fact that at the time the connection is closed the channel is still alive. Make sure to properly remove the event source when no longer needed.
gboolean
network_read(GIOChannel *source,
GIOCondition cond,
gpointer data)
{
GString *s = g_string_new(NULL);
GError *error = NULL;
GIOStatus ret = g_io_channel_read_line_string(source, s, NULL, &error);
if (ret == G_IO_STATUS_ERROR) {
//g_error ("Error reading: %s\n", error->message);
g_warning ("Error reading: %s\n", error->message);
// Drop last reference on connection
g_object_unref (data);
// Remove the event source
return FALSE;
}
else
g_print("Got: %s\n", s->str);
if (ret == G_IO_STATUS_EOF) {
return FALSE;
}
It's not documented in the GSocketService docs (I had to go through the GLib sources to find it), but the routine that calls the callback (new_connection in this case) *does a g_object_unref() on the connection object* after it returns. This effectively closes the connection immediately new_connection() returns to it.
I have no idea why it does this, but the solution is to add a g_object_ref() on entering the callback:
gboolean
new_connection(GSocketService *service,
GSocketConnection *connection,
GObject *source_object,
gpointer user_data)
{
g_object_ref(connection); /* Tell glib not to disconnect */
GSocketAddress *sockaddr = g_socket_connection_get_remote_address(connection, NULL);
GInetAddress *addr =
g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(sockaddr));
guint16 port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(sockaddr));
Without that addition, polling the file descriptor in the main loop just returned POLLNVAL because the connection had been closed. In the absence of a handler for that result, it did that continuously -- and that's what caused the 100% CPU load.
From the GIO docs :
The GIOStream object owns the input and the output streams, not the other way around, so keeping the substreams alive will not keep the GIOStream object alive. If the GIOStream object is freed it will be closed, thus closing the substream, so even if the substreams stay alive they will always just return a G_IO_ERROR_CLOSED for all operations.