Commit e792aac3 authored by Vladimír Čunát's avatar Vladimír Čunát

daemon/session: combine _new() and _set_handle()

They don't seem to be useful individually, so why not clean up.
parent 96d7d117
......@@ -108,10 +108,9 @@ static int udp_bind_finalize(uv_handle_t *handle)
{
check_bufsize(handle);
/* Handle is already created, just create context. */
struct session *s = session_new();
struct session *s = session_new(handle);
assert(s);
session_flags(s)->outgoing = false;
session_set_handle(s, handle);
return io_start_read(handle);
}
......@@ -412,9 +411,8 @@ int io_create(uv_loop_t *loop, uv_handle_t *handle, int type, unsigned family)
if (ret != 0) {
return ret;
}
struct session *s = session_new();
struct session *s = session_new(handle);
assert(s);
session_set_handle(s, handle);
uv_timer_t *t = session_get_timer(s);
t->data = s;
return uv_timer_init(loop, t);
......
......@@ -73,11 +73,6 @@ void session_clear(struct session *session)
memset(session, 0, sizeof(*session));
}
struct session *session_new(void)
{
return calloc(1, sizeof(struct session));
}
void session_close(struct session *session)
{
assert(session->tasks.len == 0 && session->waiting.len == 0);
......@@ -254,18 +249,21 @@ uv_handle_t *session_get_handle(struct session *session)
return session->handle;
}
int session_set_handle(struct session *session, uv_handle_t *handle)
struct session *session_new(uv_handle_t *handle)
{
if (!handle) {
return kr_error(EINVAL);
return NULL;
}
struct session *session = calloc(1, sizeof(struct session));
if (!session) {
return NULL;
}
assert(session->handle == NULL);
if (handle->type == UV_TCP) {
uint8_t *wire_buf = malloc(KNOT_WIRE_MAX_PKTSIZE);
if (!wire_buf) {
return kr_error(ENOMEM);
free(session);
return NULL;
}
session->wire_buf = wire_buf;
session->wire_buf_size = KNOT_WIRE_MAX_PKTSIZE;
......@@ -286,7 +284,7 @@ int session_set_handle(struct session *session, uv_handle_t *handle)
session->handle = handle;
handle->data = session;
return kr_ok();
return session;
}
uv_timer_t *session_get_timer(struct session *session)
......
......@@ -33,8 +33,8 @@ struct session_flags {
bool wirebuf_error : 1; /**< True: last operation with wirebuf ended up with an error. */
};
/* Allocate new session. */
struct session *session_new(void);
/* Allocate new session for a libuv handle. */
struct session *session_new(uv_handle_t *handle);
/* Clear and free given session. */
void session_free(struct session *session);
/* Clear session. */
......@@ -103,8 +103,6 @@ struct tls_common_ctx *session_tls_get_common_ctx(const struct session *session)
/** Get pointer to underlying libuv handle for IO operations. */
uv_handle_t *session_get_handle(struct session *session);
/** Set pointer to libuv handle for IO operations. */
int session_set_handle(struct session *session, uv_handle_t *handle);
/** Get pointer to session timer handle. */
uv_timer_t *session_get_timer(struct session *session);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment