|
|
|
|
@@ -36,6 +36,7 @@ typedef struct kcp_socket_debug_state {
|
|
|
|
|
uint32_t next_tx_id;
|
|
|
|
|
kcp_packet_debug_pending_t *pending_head;
|
|
|
|
|
atomic_int closed;
|
|
|
|
|
atomic_int last_send_errno;
|
|
|
|
|
} kcp_socket_debug_state_t;
|
|
|
|
|
|
|
|
|
|
typedef struct kcp_session_entry kcp_session_entry_t;
|
|
|
|
|
@@ -766,6 +767,7 @@ static void *kcp_socket_debug_errqueue_thread(void *arg) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int kcp_socket_debug_init(kcp_socket_debug_state_t *state, int fd, kcp_packet_debug_logger_t *logger, const char *node_role, const char *node_id) {
|
|
|
|
|
int thread_rc;
|
|
|
|
|
memset(state, 0, sizeof(*state));
|
|
|
|
|
state->fd = fd;
|
|
|
|
|
state->logger = logger;
|
|
|
|
|
@@ -779,7 +781,9 @@ static int kcp_socket_debug_init(kcp_socket_debug_state_t *state, int fd, kcp_pa
|
|
|
|
|
pthread_mutex_destroy(&state->pending_mu);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (pthread_create(&state->errqueue_thread, NULL, kcp_socket_debug_errqueue_thread, state) != 0) {
|
|
|
|
|
thread_rc = pthread_create(&state->errqueue_thread, NULL, kcp_socket_debug_errqueue_thread, state);
|
|
|
|
|
if (thread_rc != 0) {
|
|
|
|
|
errno = thread_rc;
|
|
|
|
|
pthread_mutex_destroy(&state->write_mu);
|
|
|
|
|
pthread_mutex_destroy(&state->pending_mu);
|
|
|
|
|
return -1;
|
|
|
|
|
@@ -803,17 +807,23 @@ static int kcp_socket_send_packet(kcp_socket_debug_state_t *state, const struct
|
|
|
|
|
uint32_t tx_id = 0;
|
|
|
|
|
ssize_t rc;
|
|
|
|
|
if (state->logger != NULL && kcp_socket_debug_reserve_tx(state, remote_addr, remote_addr_len, packet, packet_len, &tx_id) != 0) {
|
|
|
|
|
atomic_store(&state->last_send_errno, errno != 0 ? errno : EIO);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
pthread_mutex_lock(&state->write_mu);
|
|
|
|
|
rc = sendto(state->fd, packet, packet_len, 0, (const struct sockaddr *) remote_addr, remote_addr_len);
|
|
|
|
|
pthread_mutex_unlock(&state->write_mu);
|
|
|
|
|
if (rc < 0 || (size_t) rc != packet_len) {
|
|
|
|
|
if (rc >= 0 && (size_t) rc != packet_len && errno == 0) {
|
|
|
|
|
errno = EIO;
|
|
|
|
|
}
|
|
|
|
|
atomic_store(&state->last_send_errno, errno != 0 ? errno : EIO);
|
|
|
|
|
if (state->logger != NULL) {
|
|
|
|
|
kcp_socket_debug_rollback_tx(state, tx_id);
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
atomic_store(&state->last_send_errno, 0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -1106,10 +1116,13 @@ static void *kcp_update_thread_main(void *arg) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int kcp_conn_start_stats_thread(kcp_conn_t *conn) {
|
|
|
|
|
int thread_rc;
|
|
|
|
|
if (conn == NULL || conn->stats_logger == NULL || conn->stats_thread_started) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (pthread_create(&conn->stats_thread, NULL, kcp_stats_thread_main, conn) != 0) {
|
|
|
|
|
thread_rc = pthread_create(&conn->stats_thread, NULL, kcp_stats_thread_main, conn);
|
|
|
|
|
if (thread_rc != 0) {
|
|
|
|
|
errno = thread_rc;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
conn->stats_thread_started = 1;
|
|
|
|
|
@@ -1119,8 +1132,10 @@ static int kcp_conn_start_stats_thread(kcp_conn_t *conn) {
|
|
|
|
|
static kcp_conn_t *kcp_conn_alloc_common(int fd, const struct sockaddr_storage *remote_addr, socklen_t remote_addr_len, kcp_socket_debug_state_t *sock_state, latency_logger_t *logger, const char *node_role, const char *node_id, kcp_session_stats_logger_t *stats_logger, int stats_interval_ms) {
|
|
|
|
|
kcp_conn_t *conn = (kcp_conn_t *) calloc(1, sizeof(*conn));
|
|
|
|
|
uint32_t conv;
|
|
|
|
|
int thread_rc;
|
|
|
|
|
|
|
|
|
|
if (conn == NULL) {
|
|
|
|
|
errno = ENOMEM;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
conn->fd = fd;
|
|
|
|
|
@@ -1146,6 +1161,7 @@ static kcp_conn_t *kcp_conn_alloc_common(int fd, const struct sockaddr_storage *
|
|
|
|
|
}
|
|
|
|
|
conn->kcp = ikcp_create(conv, conn);
|
|
|
|
|
if (conn->kcp == NULL) {
|
|
|
|
|
errno = ENOMEM;
|
|
|
|
|
protocol_frame_decoder_destroy(&conn->decoder);
|
|
|
|
|
pthread_cond_destroy(&conn->rx_cond);
|
|
|
|
|
pthread_mutex_destroy(&conn->kcp_mu);
|
|
|
|
|
@@ -1177,7 +1193,9 @@ static kcp_conn_t *kcp_conn_alloc_common(int fd, const struct sockaddr_storage *
|
|
|
|
|
free(conn);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
if (pthread_create(&conn->update_thread, NULL, kcp_update_thread_main, conn) != 0) {
|
|
|
|
|
thread_rc = pthread_create(&conn->update_thread, NULL, kcp_update_thread_main, conn);
|
|
|
|
|
if (thread_rc != 0) {
|
|
|
|
|
errno = thread_rc;
|
|
|
|
|
if (conn->stats_thread_started) {
|
|
|
|
|
atomic_store(&conn->closed, 1);
|
|
|
|
|
pthread_join(conn->stats_thread, NULL);
|
|
|
|
|
@@ -1202,12 +1220,14 @@ kcp_conn_t *kcp_conn_dial(const char *server_addr, const char *bind_ip, const ch
|
|
|
|
|
int fd = kcp_socket_open_dial(server_addr, bind_ip, bind_device, &remote_addr, &remote_len, &family);
|
|
|
|
|
kcp_conn_t *conn;
|
|
|
|
|
kcp_socket_debug_state_t *sock_state;
|
|
|
|
|
int thread_rc;
|
|
|
|
|
(void) family;
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
sock_state = (kcp_socket_debug_state_t *) calloc(1, sizeof(*sock_state));
|
|
|
|
|
if (sock_state == NULL) {
|
|
|
|
|
errno = ENOMEM;
|
|
|
|
|
close(fd);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
@@ -1225,7 +1245,9 @@ kcp_conn_t *kcp_conn_dial(const char *server_addr, const char *bind_ip, const ch
|
|
|
|
|
}
|
|
|
|
|
conn->is_client = 1;
|
|
|
|
|
conn->owns_socket = 1;
|
|
|
|
|
if (pthread_create(&conn->recv_thread, NULL, kcp_client_recv_thread_main, conn) != 0) {
|
|
|
|
|
thread_rc = pthread_create(&conn->recv_thread, NULL, kcp_client_recv_thread_main, conn);
|
|
|
|
|
if (thread_rc != 0) {
|
|
|
|
|
errno = thread_rc;
|
|
|
|
|
kcp_conn_free(conn);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
@@ -1518,6 +1540,8 @@ int kcp_conn_configure_runtime(kcp_conn_t *conn, latency_logger_t *logger, const
|
|
|
|
|
int kcp_conn_send(kcp_conn_t *conn, const message_t *msg) {
|
|
|
|
|
uint8_t *frame = NULL;
|
|
|
|
|
size_t frame_len = 0;
|
|
|
|
|
int send_errno = 0;
|
|
|
|
|
int kcp_send_rc = 0;
|
|
|
|
|
if (conn == NULL || msg == NULL) {
|
|
|
|
|
errno = EINVAL;
|
|
|
|
|
return -1;
|
|
|
|
|
@@ -1529,14 +1553,23 @@ int kcp_conn_send(kcp_conn_t *conn, const message_t *msg) {
|
|
|
|
|
kcp_log_session_snapshot(conn, "send_handoff_begin");
|
|
|
|
|
kcp_process_sampler_request_sample(conn->process_sampler, "send_handoff_begin");
|
|
|
|
|
pthread_mutex_lock(&conn->kcp_mu);
|
|
|
|
|
atomic_store(&conn->sock_state->last_send_errno, 0);
|
|
|
|
|
conn->kcp->current = omni_now_millis32();
|
|
|
|
|
if (ikcp_send(conn->kcp, (const char *) frame, (int) frame_len) != 0) {
|
|
|
|
|
kcp_send_rc = ikcp_send(conn->kcp, (const char *) frame, (int) frame_len);
|
|
|
|
|
if (kcp_send_rc < 0) {
|
|
|
|
|
pthread_mutex_unlock(&conn->kcp_mu);
|
|
|
|
|
errno = kcp_send_rc == -2 ? EMSGSIZE : EINVAL;
|
|
|
|
|
free(frame);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
ikcp_flush(conn->kcp);
|
|
|
|
|
send_errno = atomic_load(&conn->sock_state->last_send_errno);
|
|
|
|
|
pthread_mutex_unlock(&conn->kcp_mu);
|
|
|
|
|
if (send_errno != 0) {
|
|
|
|
|
errno = send_errno;
|
|
|
|
|
free(frame);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
kcp_log_session_snapshot(conn, "send_handoff_end");
|
|
|
|
|
kcp_process_sampler_request_sample(conn->process_sampler, "send_handoff_end");
|
|
|
|
|
latencylog_log_message_event(conn->logger, conn->node_role, conn->node_id, EVENT_SEND_HANDOFF_END, msg);
|
|
|
|
|
|