Add new constructor for fully blocking operation

This commit is contained in:
Marcel Holtmann 2010-01-28 22:05:44 +01:00
parent fb918d39ba
commit c9a37e4a9a
4 changed files with 31 additions and 7 deletions

View File

@ -70,6 +70,7 @@ struct _GAtChat {
guint next_notify_id; /* Next notify id */ guint next_notify_id; /* Next notify id */
guint read_watch; /* GSource read id, 0 if none */ guint read_watch; /* GSource read id, 0 if none */
guint write_watch; /* GSource write id, 0 if none */ guint write_watch; /* GSource write id, 0 if none */
gboolean use_write_watch; /* watch usage for non blocking */
GIOChannel *channel; /* channel */ GIOChannel *channel; /* channel */
GQueue *command_queue; /* Command queue */ GQueue *command_queue; /* Command queue */
guint cmd_bytes_written; /* bytes written from cmd */ guint cmd_bytes_written; /* bytes written from cmd */
@ -880,14 +881,20 @@ static void g_at_chat_wakeup_writer(GAtChat *chat)
if (chat->write_watch != 0) if (chat->write_watch != 0)
return; return;
chat->write_watch = g_io_add_watch_full(chat->channel, if (chat->use_write_watch == TRUE) {
chat->write_watch = g_io_add_watch_full(chat->channel,
G_PRIORITY_DEFAULT, G_PRIORITY_DEFAULT,
G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL, G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
can_write_data, chat, can_write_data, chat,
(GDestroyNotify)write_watcher_destroy_notify); (GDestroyNotify)write_watcher_destroy_notify);
} else {
while (can_write_data(chat->channel, G_IO_OUT, chat) == TRUE);
write_watcher_destroy_notify(chat);
}
} }
GAtChat *g_at_chat_new(GIOChannel *channel, GAtSyntax *syntax) static GAtChat *create_chat(GIOChannel *channel, GIOFlags flags,
GAtSyntax *syntax)
{ {
GAtChat *chat; GAtChat *chat;
@ -907,7 +914,13 @@ GAtChat *g_at_chat_new(GIOChannel *channel, GAtSyntax *syntax)
chat->next_notify_id = 1; chat->next_notify_id = 1;
chat->debugf = NULL; chat->debugf = NULL;
chat->max_read_attempts = 1; if (flags & G_IO_FLAG_NONBLOCK) {
chat->use_write_watch = TRUE;
chat->max_read_attempts = 1;
} else {
chat->use_write_watch = FALSE;
chat->max_read_attempts = 1;
}
chat->buf = ring_buffer_new(4096); chat->buf = ring_buffer_new(4096);
@ -922,7 +935,7 @@ GAtChat *g_at_chat_new(GIOChannel *channel, GAtSyntax *syntax)
chat->notify_list = g_hash_table_new_full(g_str_hash, g_str_equal, chat->notify_list = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, (GDestroyNotify)at_notify_destroy); g_free, (GDestroyNotify)at_notify_destroy);
if (!g_at_util_setup_io(channel)) if (!g_at_util_setup_io(channel, flags))
goto error; goto error;
chat->channel = channel; chat->channel = channel;
@ -949,6 +962,16 @@ error:
return NULL; return NULL;
} }
GAtChat *g_at_chat_new(GIOChannel *channel, GAtSyntax *syntax)
{
return create_chat(channel, G_IO_FLAG_NONBLOCK, syntax);
}
GAtChat *g_at_chat_new_blocking(GIOChannel *channel, GAtSyntax *syntax)
{
return create_chat(channel, 0, syntax);
}
GIOChannel *g_at_chat_get_channel(GAtChat *chat) GIOChannel *g_at_chat_get_channel(GAtChat *chat)
{ {
if (chat == NULL) if (chat == NULL)

View File

@ -39,6 +39,7 @@ typedef void (*GAtResultFunc)(gboolean success, GAtResult *result,
typedef void (*GAtNotifyFunc)(GAtResult *result, gpointer user_data); typedef void (*GAtNotifyFunc)(GAtResult *result, gpointer user_data);
GAtChat *g_at_chat_new(GIOChannel *channel, GAtSyntax *syntax); GAtChat *g_at_chat_new(GIOChannel *channel, GAtSyntax *syntax);
GAtChat *g_at_chat_new_blocking(GIOChannel *channel, GAtSyntax *syntax);
GIOChannel *g_at_chat_get_channel(GAtChat *chat); GIOChannel *g_at_chat_get_channel(GAtChat *chat);

View File

@ -106,7 +106,7 @@ void g_at_util_debug_chat(gboolean in, const char *str, gsize len,
g_free(escaped_str); g_free(escaped_str);
} }
gboolean g_at_util_setup_io(GIOChannel *io) gboolean g_at_util_setup_io(GIOChannel *io, GIOFlags flags)
{ {
GIOFlags io_flags; GIOFlags io_flags;
@ -116,7 +116,7 @@ gboolean g_at_util_setup_io(GIOChannel *io)
io_flags = g_io_channel_get_flags(io); io_flags = g_io_channel_get_flags(io);
io_flags |= G_IO_FLAG_NONBLOCK; io_flags |= (flags & G_IO_FLAG_SET_MASK);
if (g_io_channel_set_flags(io, io_flags, NULL) != if (g_io_channel_set_flags(io, io_flags, NULL) !=
G_IO_STATUS_NORMAL) G_IO_STATUS_NORMAL)

View File

@ -31,7 +31,7 @@ extern "C" {
void g_at_util_debug_chat(gboolean in, const char *str, gsize len, void g_at_util_debug_chat(gboolean in, const char *str, gsize len,
GAtDebugFunc debugf, gpointer user_data); GAtDebugFunc debugf, gpointer user_data);
gboolean g_at_util_setup_io(GIOChannel *io); gboolean g_at_util_setup_io(GIOChannel *io, GIOFlags flags);
#ifdef __cplusplus #ifdef __cplusplus
} }