核心内容是:
static GSourceFuncs funcs = {
cs_prepare,
cs_check,
cs_dispatch,
NULL,
};
connection_source = (ConnectionSource*)g_source_new(&funcs, sizeof(ConnectionSource));
connection_source->handle = handle;
connection_source->pollfd.fd = lgsm_fd(handle);
connection_source->pollfd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
connection_source->pollfd.revents = 0;
g_source_add_poll((GSource*)connection_source, &connection_source->pollfd);
g_source_attach((GSource*)connection_source, NULL);
gtk_main()在时刻监听着,当有数据进入到该句柄(可以是socket,文件,设备)时,会执行cs_dispatch函数
static gboolean
cs_dispatch(GSource *source,
GSourceFunc callback,
gpointer data)
{
int size;
char buf[MAX_BUF_SIZE];
ConnectionSource *self;
self = (ConnectionSource*)source;
size = read(self->pollfd.fd, buf, MAX_BUF_SIZE);
if (size > 0) {
handle_packet_greedy(self->handle, buf, size, NULL);
return TRUE;
} else if (size == 0) { /* EOF */
return FALSE;
} else {
g_warning("cs_dispatch:%s %s",
"read error from libgsmd:",
g_strerror(errno));
return FALSE;
}
}
其他的详情
static gboolean
cs_prepare(GSource *self,
gint *timeout)
{
*timeout = -1;
return FALSE;
}
static gboolean
cs_check(GSource *source)
{
ConnectionSource *self;
self = (ConnectionSource*)source;
return self->pollfd.revents & G_IO_IN;
}
注:
lgsm_fd(handle);即获得socket的句柄
handle = lgsm_init(LGSMD_DEVICE_GSMD);
*sid = GPOINTER_TO_INT(handle);
阅读(1532) | 评论(0) | 转发(0) |