Skip to content

Commit

Permalink
feat connection: added separation fd for read and write
Browse files Browse the repository at this point in the history
Signed-off-by: John Sanpe <[email protected]>
  • Loading branch information
sanpeqf committed Jun 13, 2024
1 parent 19c98d8 commit 16c53af
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 67 deletions.
8 changes: 4 additions & 4 deletions src/adb/xdbd_adb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
75 changes: 54 additions & 21 deletions src/connection/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ FIXME: remove this
#include <fcntl.h>
#include <unistd.h>

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;

Expand All @@ -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--;

Expand All @@ -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));
Expand All @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
6 changes: 4 additions & 2 deletions src/connection/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
39 changes: 28 additions & 11 deletions src/connection/usb/xdbd_connection_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 1 addition & 2 deletions src/connection/usb/xdbd_connection_usb.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
40 changes: 27 additions & 13 deletions src/event/select/xdbd_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
}
Expand Down
Loading

0 comments on commit 16c53af

Please sign in to comment.