X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=hcconn.c;h=c15256fbd696440724b301a2c7b9e835dafd6f46;hb=cf309ca41249b88d93bf6ae0b927e6c9ac6527bb;hp=02f15721d5d3b45db226ebd98bd4c69181f000b1;hpb=1516afb0ad6c38be922c304d2d002161cfeb8b7a;p=cascardo%2Frnetproxy.git diff --git a/hcconn.c b/hcconn.c index 02f1572..c15256f 100644 --- a/hcconn.c +++ b/hcconn.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Thadeu Lima de Souza Cascardo + * Copyright (C) 2009 Thadeu Lima de Souza Cascardo * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -111,28 +111,40 @@ hc_conn_watch (GIOChannel *channel, GIOCondition cond, gpointer data) int fd = g_io_channel_unix_get_fd (channel); char buffer; int r; - switch (cond) + if (cond & G_IO_IN) { - case G_IO_IN: event = HC_EVENT_READ; r = recv (fd, &buffer, 1, MSG_PEEK); if (r == 0) - event = HC_EVENT_CLOSE; - break; - case G_IO_HUP: + { + event = HC_EVENT_CLOSE; + } + else if (r == -1) + { + /* FIXME: create HC_EVENT_ERROR */ + event = HC_EVENT_CLOSE; + } + } + else if (cond & G_IO_HUP) + { + event = HC_EVENT_CLOSE; + } + else if (cond & G_IO_ERR) + { + /* FIXME: create HC_EVENT_ERROR */ event = HC_EVENT_CLOSE; - break; - default: + } + else + { /* TODO: handle other conditions and create error event */ g_warning ("Received an unexpected IO condition."); - break; } if (conn->func) conn->func (conn, event, conn->data); return TRUE; } -void +int hc_conn_set_driver_channel (HCConn *conn, int fd) { struct channel_layer *layer = g_slice_new (struct channel_layer); @@ -142,12 +154,14 @@ hc_conn_set_driver_channel (HCConn *conn, int fd) conn->write = hc_conn_channel_write; conn->close = hc_conn_channel_close; /* TODO: We must watch other events */ - layer->watch = g_io_add_watch (layer->channel, G_IO_IN | G_IO_HUP, + layer->watch = g_io_add_watch (layer->channel, + G_IO_IN | G_IO_HUP | G_IO_ERR, hc_conn_watch, conn); /* TODO: connection should be asynchronous so this could make sense */ if (conn->func) conn->func (conn, HC_EVENT_CONNECT, conn->data); fcntl (fd, F_SETFL, fcntl (fd, F_GETFL, 0) | O_NONBLOCK); + return 0; }