Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2023798
  • 博文数量: 433
  • 博客积分: 918
  • 博客等级: 准尉
  • 技术积分: 3218
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 18:21
个人简介

你是不是暗恋我,那就给我发个消息呀,让我知道o(∩∩)o

文章分类

全部博文(433)

分类:

2013-01-03 13:17:36

核心内容是:
 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);




阅读(1492) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~