接下来看io_accept_event这个接口:
- static gboolean io_accept_event(GIOChannel *chan, GIOCondition cond, gpointer data)
- {
- GIOChannel *io;
- int nsk;
- if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
- g_io_channel_unref(chan);
- return FALSE;
- }
- if (data == &l2cap_sock) {
- struct sockaddr_l2 addr;
- socklen_t len = sizeof(addr);
- nsk = accept(l2cap_sock, (struct sockaddr *) &addr, &len);
- } else if (data == &unix_sock) {
- struct sockaddr_un addr;
- socklen_t len = sizeof(addr);
- nsk = accept(unix_sock, (struct sockaddr *) &addr, &len);
- } else
- return FALSE;
- if (nsk < 0) {
- error("Can't accept connection: %s", strerror(errno));
- return TRUE;
- }
- //here we accept the connect from client, and then generate a new socket, the new socket
- //is for really data transfer, and here we can see we use io_session_event func to deal with
- //the new socket for processing the coming data.
- io = g_io_channel_unix_new(nsk);
- g_io_channel_set_close_on_unref(io, TRUE);
- g_io_add_watch(io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
- io_session_event, data);
- g_io_channel_unref(io);
- return TRUE;
- }
为其中注册的io_session_event接口。下面看io_session_event数据处理接口:
- static gboolean io_session_event(GIOChannel *chan, GIOCondition cond, gpointer data)
- {
- sdp_pdu_hdr_t hdr;
- uint8_t *buf;
- int sk, len, size;
- if (cond & G_IO_NVAL)
- return FALSE;
- sk = g_io_channel_unix_get_fd(chan);
- if (cond & (G_IO_HUP | G_IO_ERR)) {
- sdp_svcdb_collect_all(sk);
- return FALSE;
- }
- //first receive the sdp pdu hdr, connecting msg i guess
- len = recv(sk, &hdr, sizeof(sdp_pdu_hdr_t), MSG_PEEK);
- if (len <= 0) {
- sdp_svcdb_collect_all(sk);
- return FALSE;
- }
- size = sizeof(sdp_pdu_hdr_t) + ntohs(hdr.plen);
- buf = malloc(size);
- if (!buf)
- return TRUE;
- //then receive the real data
- len = recv(sk, buf, size, 0);
- if (len <= 0) {
- sdp_svcdb_collect_all(sk);
- free(buf);
- return FALSE;
- }
- //here we will process the data
- handle_request(sk, buf, len);
- return TRUE;
- }
sdp_req_t结构体,然后辗转调用到process_request这个函数,并将sdp_req_t结构传入,sdp_req_t结构如下:
- typedef struct request {
- bdaddr_t device;
- bdaddr_t bdaddr;
- int local;
- int sock;
- int mtu;
- int flags;
- uint8_t *buf;
- int len;
- } sdp_req_t;
