Chinaunix首页 | 论坛 | 博客
  • 博客访问: 521663
  • 博文数量: 51
  • 博客积分: 345
  • 博客等级: 民兵
  • 技术积分: 534
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-21 12:02
个人简介

文章分类

全部博文(51)

文章存档

2023年(2)

2022年(1)

2021年(7)

2020年(10)

2019年(2)

2016年(20)

2015年(5)

2014年(1)

2011年(3)

我的朋友

分类: C/C++

2016-08-23 12:01:22

/var/run/ubus.sockUBUS_UNIX_SOCKET

static struct uloop_fd server_fd = {

         .cb = server_cb,

};

 

-->uloop_init();

-->usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);

         -->usock_unix(type, host, socktype, server);

                   -->usock_connect    绑定socketfd存放在server_fd.fd

-->uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);

         设置fd属性为非阻塞,通过register_poll函数与poll_fd关联;回调函数为server_cb

-->uloop_run();

 

 

 

server_cb

-->get_next_connection

         -->提取client fd

         -->ubusd_proto_new_client

                   -->ubus_alloc_id分配id,并插入到clients  avl树中;

                  -->ubusd_send_hello

                            -->blob_buf_init

                                     -->blob_add(buf, buf->buf, id, 0)

         -->uloop_fd_add       设置fd属性为非阻塞;

                   -->register_poll        fd添加到epoll中进行监控;回调函数为client_cb

 

接下来就是epoll_wait监控阶段,这里对两种事件关心;

1、  存在关联请求消息,其回调为server_cb,将client fd添加到epoll中;

2、  client发送消息时,其回调为client_cb

 

/* blob_raw_len: returns the complete length of an attribute (including the header)*/

static inline unsigned int

blob_raw_len(const struct blob_attr *attr)

{

              return blob_len(attr) + sizeof(struct blob_attr);

}

/* * blob_len: returns the length of the attribute's payload*/

static inline unsigned int  blob_len(const struct blob_attr *attr)

{

              return (be32_to_cpu(attr->id_len) & BLOB_ATTR_LEN_MASK) - sizeof(struct blob_attr);

}

#define BLOB_ATTR_LEN_MASK 0x00ffffff

id_len字段的低24位(23~0Bit)为struct blob_attr结构长度+data长度;

ID标记为30~24Bit

Extended标记为31Bit

client_cb

-->首先检查tx_queue[]是否有缓存中的信息;若存在则调用ubus_msg_writev发送消息;

-->通过fd读取socket上数据;调用ubusd_proto_receive_message对数据进行解析;

不同消息类型对应不同处理函数

static const ubus_cmd_cb handlers[__UBUS_MSG_LAST] = {

              [UBUS_MSG_PING] = ubusd_send_pong,

              [UBUS_MSG_ADD_OBJECT] = ubusd_handle_add_object,

              [UBUS_MSG_REMOVE_OBJECT] = ubusd_handle_remove_object,

              [UBUS_MSG_LOOKUP] = ubusd_handle_lookup,

              [UBUS_MSG_INVOKE] = ubusd_handle_invoke,

              [UBUS_MSG_STATUS] = ubusd_handle_response,

              [UBUS_MSG_DATA] = ubusd_handle_response,

              [UBUS_MSG_SUBSCRIBE] = ubusd_handle_add_watch,

              [UBUS_MSG_UNSUBSCRIBE] = ubusd_handle_remove_watch,

              [UBUS_MSG_NOTIFY] = ubusd_handle_notify,

};

         消息解析为固定的struct blob_attr格式;

         接着由特定类型处理函数处理;

 

若消息无需响应,关闭fd

         ubus_msg_close_fd(ub);

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