Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http,tcp: add http_listen_fd and tcp_sock_alloc_fd #618

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/re_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ struct http_conn;
typedef void (http_req_h)(struct http_conn *conn, const struct http_msg *msg,
void *arg);

int http_listen_fd(struct http_sock **sockp, re_sock_t fd, http_req_h *reqh,
void *arg);
int http_listen(struct http_sock **sockp, const struct sa *laddr,
http_req_h *reqh, void *arg);
int https_listen(struct http_sock **sockp, const struct sa *laddr,
Expand Down
2 changes: 2 additions & 0 deletions include/re_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ int tcp_conn_settos(struct tcp_conn *tc, uint32_t tos);


/* TCP Connection */
int tcp_sock_alloc_fd(struct tcp_sock **tsp, re_sock_t fd, tcp_conn_h *ch,
void *arg);
int tcp_conn_alloc(struct tcp_conn **tcp, const struct sa *peer,
tcp_estab_h *eh, tcp_recv_h *rh, tcp_close_h *ch,
void *arg);
Expand Down
40 changes: 40 additions & 0 deletions src/http/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,46 @@ static void connect_handler(const struct sa *peer, void *arg)
}


/**
* Create an HTTP socket from file descriptor
*
* @param sockp Pointer to returned HTTP Socket
* @param fd File descriptor
* @param reqh Request handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int http_listen_fd(struct http_sock **sockp, re_sock_t fd, http_req_h *reqh,
void *arg)
{
struct http_sock *sock;
int err;

if (!sockp || fd == RE_BAD_SOCK || !reqh)
return EINVAL;

sock = mem_zalloc(sizeof(*sock), sock_destructor);
if (!sock)
return ENOMEM;

err = tcp_sock_alloc_fd(&sock->ts, fd, connect_handler, sock);
if (err)
goto out;

sock->reqh = reqh;
sock->arg = arg;

out:
if (err)
mem_deref(sock);
else
*sockp = sock;

return err;
}


/**
* Create an HTTP socket
*
Expand Down
32 changes: 32 additions & 0 deletions src/tcp/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,38 @@ static void tcp_conn_handler(int flags, void *arg)
}


/**
* Create a TCP Socket with fd
*
* @param tsp Pointer to returned TCP Socket
* @param fd File descriptor
* @param ch Incoming connection handler
* @param arg Handler argument
*
* @return 0 if success, otherwise errorcode
*/
int tcp_sock_alloc_fd(struct tcp_sock **tsp, re_sock_t fd, tcp_conn_h *ch,
void *arg)
{
struct tcp_sock *ts = NULL;

if (!tsp || fd == RE_BAD_SOCK)
return EINVAL;

ts = mem_zalloc(sizeof(*ts), sock_destructor);
if (!ts)
return ENOMEM;

ts->fd = fd;
ts->connh = ch;
ts->arg = arg;

*tsp = ts;

return fd_listen(ts->fd, FD_READ, tcp_conn_handler, ts);
}


/**
* Create a TCP Socket
*
Expand Down