diff --git a/src/adb/xdbd_adb.c b/src/adb/xdbd_adb.c index 096a0a8..2b530e6 100644 --- a/src/adb/xdbd_adb.c +++ b/src/adb/xdbd_adb.c @@ -43,7 +43,7 @@ size_t xdbd_adb_read_adb_header(xdbd_adb_request_t *r) { } if (n == 0) { - bfdev_log_debug("close connection by client\n"); + bfdev_log_debug("%s close connection by client\n", __func__); } if (n == 0 || n == XDBD_ERR) { @@ -102,7 +102,7 @@ size_t xdbd_adb_read_adb_payload(xdbd_adb_request_t *r) { } if (n == 0) { - bfdev_log_debug("close connection by client\n"); + bfdev_log_debug("%s close connection by client\n", __func__); } if (n == 0 || n == XDBD_ERR) { @@ -247,7 +247,7 @@ void xdbd_adb_process_adb_header(xdbd_event_t *rev) { void xdbd_adb_wait_apacket_handler(xdbd_event_t *ev) { bfdev_log_debug("xdbd_adb_wait_apacket_handler\n"); xdbd_connection_t *c; - size_t n; + ssize_t n; xdbd_buf_t *b; size_t size; c = ev->data; @@ -287,7 +287,7 @@ void xdbd_adb_wait_apacket_handler(xdbd_event_t *ev) { } if (n == 0) { - bfdev_log_debug("close connection by client\n"); + bfdev_log_debug("%s close connection by client, %d\n", __func__, c->read_fd); xdbd_adb_close_connection(c); return; } diff --git a/src/connection/connection.c b/src/connection/connection.c index 5579b05..70b39d5 100644 --- a/src/connection/connection.c +++ b/src/connection/connection.c @@ -12,7 +12,7 @@ FIXME: remove this #include #include -xdbd_connection_t *xdbd_get_connection(xdbd_t *xdbd, xdbd_socket_t s) { +xdbd_connection_t *xdbd_get_connection(xdbd_t *xdbd, xdbd_socket_t read, xdbd_socket_t write) { xdbd_connection_t *c; xdbd_event_t *rev, *wev; @@ -23,7 +23,9 @@ xdbd_connection_t *xdbd_get_connection(xdbd_t *xdbd, xdbd_socket_t s) { return NULL; } - bfdev_log_err("xdbd_get_connection get c->fd %d, c %p\n", s, c); + bfdev_log_err("xdbd_get_connection get c->read_fd %d, c %p\n", read, c); + bfdev_log_err("xdbd_get_connection get c->write_fd %d, c %p\n", write, c); + xdbd->free_connections = c->data; xdbd->free_connection_n--; @@ -34,7 +36,8 @@ xdbd_connection_t *xdbd_get_connection(xdbd_t *xdbd, xdbd_socket_t s) { c->read = rev; c->write = wev; - c->fd = s; + c->read_fd = read; + c->write_fd = write; xdbd_memzero(rev, sizeof(xdbd_event_t)); xdbd_memzero(wev, sizeof(xdbd_event_t)); @@ -55,10 +58,50 @@ void xdbd_free_connection(xdbd_t *xdbd, xdbd_connection_t *c) { xdbd->free_connection_n++; } + +void xdbd_connection_fd_free(xdbd_connection_t *c) +{ + xdbd_socket_t read_fd, write_fd; + xdbd_err_t err; + + read_fd = c->read_fd; + c->read_fd = (xdbd_socket_t) -1; + + write_fd = c->write_fd; + c->write_fd = (xdbd_socket_t) -1; + + bfdev_log_err("xdbd_close_connection close c->read_fd %d, c %p\n", c->read_fd, c); + bfdev_log_err("xdbd_close_connection close c->write_fd %d, c %p\n", c->write_fd, c); + + if (read_fd == write_fd) { + if (xdbd_close(read_fd) == -1) { + err = xdbd_socket_errno; + bfdev_log_debug("xdbd_close read_fd error"); + } + + return; + } + + //read_fd != write_fd + if (xdbd_close(read_fd) == -1) { + err = xdbd_socket_errno; + bfdev_log_debug("xdbd_close read_fd error"); + } + + if (xdbd_close(write_fd) == -1) { + err = xdbd_socket_errno; + bfdev_log_debug("xdbd_close write_fd error"); + } + +} + void xdbd_close_connection(xdbd_t *xdbd, xdbd_connection_t *c) { - xdbd_socket_t fd; + xdbd_socket_t read_fd, write_fd; xdbd_err_t err; - if (c->fd == (xdbd_socket_t) -1) { + if (c->read_fd == (xdbd_socket_t) -1) { + return; + } + if (c->write_fd == (xdbd_socket_t) -1) { return; } @@ -73,18 +116,8 @@ void xdbd_close_connection(xdbd_t *xdbd, xdbd_connection_t *c) { c->read->closed = 1; c->write->closed = 1; - bfdev_log_err("xdbd_close_connection close c->fd %d, c %p\n", c->fd, c); - - xdbd_free_connection(xdbd, c); - - fd = c->fd; - c->fd = (xdbd_socket_t) -1; - - if (xdbd_close(fd) == -1) { - err = xdbd_socket_errno; - - bfdev_log_debug("xdbd_close error"); - } + xdbd_free_connection(c->listening->xdbd, c); + xdbd_connection_fd_free(c); } ssize_t xdbd_unix_recv(xdbd_connection_t *c, u_char *buf, size_t size) { @@ -94,7 +127,7 @@ ssize_t xdbd_unix_recv(xdbd_connection_t *c, u_char *buf, size_t size) { rev = c->read; do { - n = recv(c->fd, buf, size, 0); + n = recv(c->read_fd, buf, size, 0); // bfdev_log_debug("recv: fd:%d %z of %uz", c->fd, n, size); @@ -146,7 +179,7 @@ ssize_t xdbd_unix_read(xdbd_connection_t *c, u_char *buf, size_t size) { rev = c->read; do { - n = read(c->fd, buf, size); + n = read(c->read_fd, buf, size); // bfdev_log_debug("recv: fd:%d %z of %uz", c->fd, n, size); @@ -199,7 +232,7 @@ ssize_t xdbd_unix_send(xdbd_connection_t *c, u_char *buf, size_t size) { wev = c->write; for ( ;; ) { - n = send(c->fd, buf, size, 0); + n = send(c->write_fd, buf, size, 0); // bfdev_log_debug("send: fd:%d %z of %uz", c->fd, n, size); @@ -246,7 +279,7 @@ ssize_t xdbd_unix_write(xdbd_connection_t *c, u_char *buf, size_t size) { wev = c->write; for ( ;; ) { - n = write(c->fd, buf, size); + n = write(c->write_fd, buf, size); // bfdev_log_debug("send: fd:%d %z of %uz", c->fd, n, size); diff --git a/src/connection/connection.h b/src/connection/connection.h index 88272ca..de31458 100644 --- a/src/connection/connection.h +++ b/src/connection/connection.h @@ -8,7 +8,8 @@ struct xdbd_connection_s { xdbd_pool_t *pool; xdbd_pool_t *temp_pool; - xdbd_socket_t fd; + xdbd_socket_t read_fd; + xdbd_socket_t write_fd; xdbd_event_t *read; xdbd_event_t *write; @@ -41,8 +42,9 @@ struct xdbd_peer_connection_s { #define ADB_CONNECTION_DEFAULT_PORT 5555 -xdbd_connection_t *xdbd_get_connection(xdbd_t *xdbd, xdbd_socket_t s); +xdbd_connection_t *xdbd_get_connection(xdbd_t *xdbd, xdbd_socket_t read, xdbd_socket_t write); void xdbd_free_connection(xdbd_t *xdbd, xdbd_connection_t *c); +void xdbd_connection_fd_free(xdbd_connection_t *c); void xdbd_close_connection(xdbd_t *xdbd, xdbd_connection_t *c); ssize_t xdbd_unix_recv(xdbd_connection_t *c, u_char *buf, size_t size); diff --git a/src/connection/usb/xdbd_connection_usb.c b/src/connection/usb/xdbd_connection_usb.c index 4cf0481..feb3e52 100644 --- a/src/connection/usb/xdbd_connection_usb.c +++ b/src/connection/usb/xdbd_connection_usb.c @@ -217,36 +217,53 @@ FIXME: make this request async void xdbd_event_usb_accept(xdbd_event_t *ev) { xdbd_usb_listening_t *uls; - xdbd_connection_t *c; + xdbd_connection_t *lc, *c; + xdbd_event_t *rev, *wev; #if defined(XDBD_CONFIG_OSX) bfdev_log_err("OSX not support yet\r\n"); return; #endif - c = ev->data; - uls = c->ulistening; + lc = ev->data; + uls = lc->ulistening; if (xdbd_event_open_usb_connection(uls) != XDBD_OK) { return; } - uls->conn_ctl = xdbd_get_connection(c->ulistening->xdbd, uls->fd_ctr); + uls->conn_ctl = xdbd_get_connection(lc->ulistening->xdbd, uls->fd_ctr, uls->fd_ctr); if (uls->conn_ctl == NULL) { return; } - uls->conn_in = xdbd_get_connection(c->ulistening->xdbd, uls->fd_in); - if (uls->conn_ctl == NULL) { + c = xdbd_get_connection(lc->ulistening->xdbd, uls->fd_out, uls->fd_in); + if (c == NULL) { return; } - uls->conn_out = xdbd_get_connection(c->ulistening->xdbd, uls->fd_out); - if (uls->conn_ctl == NULL) { - return; - } + c = uls->conn_data; + + + c->pool = xdbd_create_pool(); + if (c->pool == NULL) { + return; + } + + c->temp_pool = xdbd_create_pool(); + if (c->temp_pool == NULL) { + return; + } - //FIXME: how to process ctl, in and out??? + c->send = xdbd_unix_send; + c->recv = xdbd_unix_recv; + wev = c->write; + rev = c->read; + + wev->ready = 1; + + uls->handler(c); ev->accepted = 1; + return; } diff --git a/src/connection/usb/xdbd_connection_usb.h b/src/connection/usb/xdbd_connection_usb.h index e1eba8d..054e2dd 100644 --- a/src/connection/usb/xdbd_connection_usb.h +++ b/src/connection/usb/xdbd_connection_usb.h @@ -20,8 +20,7 @@ struct xdbd_usb_listening_s { //process connection xdbd_connection_t *conn_ctl; - xdbd_connection_t *conn_in; - xdbd_connection_t *conn_out; + xdbd_connection_t *conn_data; xdbd_socket_t fd_ctr; xdbd_socket_t fd_in; diff --git a/src/event/select/xdbd_select.c b/src/event/select/xdbd_select.c index e3e9548..1c7283b 100644 --- a/src/event/select/xdbd_select.c +++ b/src/event/select/xdbd_select.c @@ -74,17 +74,23 @@ static int xdbd_select_add_event(xdbd_event_t *ev, int event, unsigned flags) { return XDBD_OK; } - bfdev_log_debug("select add event c->fd %d\n", c->fd); + if (event == XDBD_READ_EVENT) { - FD_SET(c->fd, &master_read_fd_set); + bfdev_log_debug("select add read event c->read_fd %d\n", c->read_fd); + FD_SET(c->read_fd, &master_read_fd_set); } if (event == XDBD_WRITE_EVENT) { - FD_SET(c->fd, &master_write_fd_set); + bfdev_log_debug("select add write event c->write_fd %d\n", c->write_fd); + FD_SET(c->write_fd, &master_write_fd_set); + } + + if (max_fd != -1 && max_fd < c->read_fd) { + max_fd = c->read_fd; } - if (max_fd != -1 && max_fd < c->fd) { - max_fd = c->fd; + if (max_fd != -1 && max_fd < c->write_fd) { + max_fd = c->write_fd; } ev->active = 1; @@ -105,16 +111,21 @@ static int xdbd_select_del_event(xdbd_event_t *ev, int event, unsigned flags) { return XDBD_ERR; } - bfdev_log_debug("select delete event c->fd %d\n", c->fd); if (event == XDBD_READ_EVENT) { - FD_CLR(c->fd, &master_read_fd_set); + bfdev_log_debug("select delete event c->read_fd %d\n", c->read_fd); + FD_CLR(c->read_fd, &master_read_fd_set); } if (event == XDBD_WRITE_EVENT) { - FD_CLR(c->fd, &master_write_fd_set); + bfdev_log_debug("select delete event c->write_fd %d\n", c->write_fd); + FD_CLR(c->write_fd, &master_write_fd_set); } - if (c->fd == max_fd) { + if (c->read_fd == max_fd) { + max_fd = -1; + } + + if (c->write_fd == max_fd) { max_fd = -1; } @@ -184,8 +195,11 @@ static int xdbd_select_process_events(xdbd_t *xdbd, xdbd_msec_t timer, unsigned if (max_fd == -1) { for (i = 0; i < nevents; i++) { c = event_index[i]->data; - if (max_fd < c->fd) { - max_fd = c->fd; + if (max_fd < c->read_fd) { + max_fd = c->read_fd; + } + if (max_fd < c->write_fd) { + max_fd = c->write_fd; } } @@ -248,14 +262,14 @@ static int xdbd_select_process_events(xdbd_t *xdbd, xdbd_msec_t timer, unsigned found = 0; if (ev->write) { - if (FD_ISSET(c->fd, &work_write_fd_set)) { + if (FD_ISSET(c->write_fd, &work_write_fd_set)) { found = 1; } } /*read event*/ if (!ev->write) { - if (FD_ISSET(c->fd, &work_read_fd_set)) { + if (FD_ISSET(c->read_fd, &work_read_fd_set)) { found = 1; } } diff --git a/src/event/xdbd_event.c b/src/event/xdbd_event.c index dcc718e..2563946 100644 --- a/src/event/xdbd_event.c +++ b/src/event/xdbd_event.c @@ -87,16 +87,10 @@ int xdbd_handle_write_event(xdbd_event_t *wev, unsigned flags) { static void xdbd_close_accepted_connection(xdbd_connection_t *c) { - xdbd_socket_t s; + xdbd_socket_t read_fd, write_fd; xdbd_free_connection(c->listening->xdbd, c); - - s = c->fd; - c->fd = (xdbd_socket_t)-1; - - if (xdbd_close(s) == (xdbd_socket_t) -1) { - bfdev_log_err("xdbd_close close error"); - } + xdbd_connection_fd_free(c); if (c->pool) { xdbd_destroy_pool(c->pool); @@ -136,13 +130,13 @@ static void xdbd_event_accept(xdbd_event_t *ev) { socklen = sizeof(xdbd_sockaddr_t); - s = accept(lc->fd, &sa.sockaddr, &socklen); + s = accept(lc->read_fd, &sa.sockaddr, &socklen); if (s == (xdbd_socket_t) -1) { //FIXME: add some check return; } - c = xdbd_get_connection(ls->xdbd, s); + c = xdbd_get_connection(ls->xdbd, s, s); if (c == NULL) { return; } @@ -360,7 +354,8 @@ static int xdbd_event_init_connections(xdbd_t *xdbd) { c[i].data = next; c[i].read = &xdbd->read_events[i]; c[i].write = &xdbd->write_events[i]; - c[i].fd = (xdbd_socket_t) -1; + c[i].read_fd = (xdbd_socket_t) -1; + c[i].write_fd = (xdbd_socket_t) -1; next = &c[i]; } @@ -371,7 +366,7 @@ static int xdbd_event_init_connections(xdbd_t *xdbd) { assert(ls != NULL); for (i = 0; i < bfdev_array_index(&xdbd->tcp_listening); i++) { - c = xdbd_get_connection(xdbd, ls[i].fd); + c = xdbd_get_connection(xdbd, ls[i].fd, ls[i].fd); if (c == NULL) { return XDBD_ERR; } @@ -394,7 +389,8 @@ static int xdbd_event_init_connections(xdbd_t *xdbd) { assert(uls != NULL); for (i = 0; i < bfdev_array_index(&xdbd->usb_listening); i++) { - c = xdbd_get_connection(xdbd, XDBD_CONNECTION_USB_TIMER_FD); + c = xdbd_get_connection(xdbd, XDBD_CONNECTION_USB_TIMER_FD, XDBD_CONNECTION_USB_TIMER_FD); + if (c == NULL) { return XDBD_ERR; } diff --git a/src/service/shell/xdbd_shell_service.c b/src/service/shell/xdbd_shell_service.c index f8c866d..c348581 100644 --- a/src/service/shell/xdbd_shell_service.c +++ b/src/service/shell/xdbd_shell_service.c @@ -121,7 +121,7 @@ static void xdbd_event_shell_handler(xdbd_event_t *ev) { xdbd_connection_t *create_service_connection(xdbd_t *xdbd, xdbd_adb_request_t *r, xdbd_shell_service_t *ss) { xdbd_connection_t *c; xdbd_event_t *rev; - c = xdbd_get_connection(xdbd, ss->fd); + c = xdbd_get_connection(xdbd, ss->fd, ss->fd); if (c == NULL) { return NULL;