Document of c-modernization-kit (porter) 1.0.0
Loading...
Searching...
No Matches
potrConnectThread.c File Reference

TCP 接続管理スレッドモジュール。 More...

#include <stdlib.h>
#include <inttypes.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <porter_const.h>
#include "../potrContext.h"
#include "../protocol/packet.h"
#include "../infra/potrSendQueue.h"
#include "../protocol/window.h"
#include "../infra/potrLog.h"
#include "potrConnectThread.h"
#include "potrRecvThread.h"
#include "potrSendThread.h"
#include "potrHealthThread.h"
Include dependency graph for potrConnectThread.c:

Go to the source code of this file.

Data Structures

struct  ConnectArg

Macros

#define TCP_SESSION_NEW   ( 1) /* 新セッション (または初回接続) */
#define TCP_SESSION_SAME   ( 0) /* 同一セッション */
#define TCP_SESSION_OLD   (-1) /* 旧セッション (破棄すべき) */

Functions

static uint64_t connect_get_ms (void)
static void close_tcp_conn (struct PotrContext_ *ctx, int path_idx)
static void reconnect_wait (struct PotrContext_ *ctx, int path_idx, uint32_t wait_ms)
static int accept_tcp_read_all (PotrSocket fd, uint8_t *buf, size_t n)
static int accept_tcp_wait_readable (PotrSocket fd, uint32_t wait_ms)
static int tcp_read_first_packet (PotrSocket fd, uint8_t *buf, size_t max_buf, size_t *out_len, uint32_t timeout_ms)
static int tcp_session_compare (const struct PotrContext_ *ctx, const PotrPacket *pkt)
static void join_recv_thread (struct PotrContext_ *ctx, int path_idx)
static void reset_connection_state (struct PotrContext_ *ctx)
static void reset_all_paths_disconnected (struct PotrContext_ *ctx)
static void reset_send_queue (struct PotrContext_ *ctx)
static int start_connected_threads (struct PotrContext_ *ctx, int path_idx)
static void stop_connected_threads (struct PotrContext_ *ctx, int path_idx)
static PotrSocket tcp_connect_with_timeout (struct PotrContext_ *ctx, int path_idx)
static void sender_connect_loop (struct PotrContext_ *ctx, int path_idx)
static void receiver_accept_loop (struct PotrContext_ *ctx, int path_idx)
static void * connect_thread_func (void *arg)
int potr_connect_thread_start (struct PotrContext_ *ctx)
 TCP 接続管理スレッドを起動します (path 数分)。
void potr_connect_thread_stop (struct PotrContext_ *ctx)
 TCP 接続管理スレッドを停止します。

Variables

static ConnectArg s_connect_args [POTR_MAX_PATH]

Detailed Description

TCP 接続管理スレッドモジュール。

Author
c-modernization-kit sample team
Date
2026/03/23
Version
1.0.0

SENDER: TCP connect/reconnect ループを管理するスレッドです。
RECEIVER: TCP accept ループを管理するスレッドです。
接続確立後、送受信・ヘルスチェックスレッドを起動し、
recv スレッドが切断を検知して終了するまで待機してから再接続サイクルへ移行します。

Definition in file potrConnectThread.c.

Macro Definition Documentation

◆ TCP_SESSION_NEW

#define TCP_SESSION_NEW   ( 1) /* 新セッション (または初回接続) */

Definition at line 217 of file potrConnectThread.c.

Referenced by receiver_accept_loop(), and tcp_session_compare().

◆ TCP_SESSION_SAME

#define TCP_SESSION_SAME   ( 0) /* 同一セッション */

Definition at line 218 of file potrConnectThread.c.

Referenced by tcp_session_compare().

◆ TCP_SESSION_OLD

#define TCP_SESSION_OLD   (-1) /* 旧セッション (破棄すべき) */

Definition at line 219 of file potrConnectThread.c.

Referenced by receiver_accept_loop(), and tcp_session_compare().

Function Documentation

◆ connect_get_ms()

uint64_t connect_get_ms ( void )
static

Definition at line 62 of file potrConnectThread.c.

Referenced by receiver_accept_loop(), and sender_connect_loop().

Here is the caller graph for this function:

◆ close_tcp_conn()

void close_tcp_conn ( struct PotrContext_ * ctx,
int path_idx )
static

Definition at line 74 of file potrConnectThread.c.

References POTR_INVALID_SOCKET, and PotrContext_::tcp_conn_fd.

Referenced by receiver_accept_loop(), start_connected_threads(), and stop_connected_threads().

Here is the caller graph for this function:

◆ reconnect_wait()

void reconnect_wait ( struct PotrContext_ * ctx,
int path_idx,
uint32_t wait_ms )
static

Definition at line 90 of file potrConnectThread.c.

References PotrContext_::connect_thread_running, PotrContext_::tcp_state_cv, and PotrContext_::tcp_state_mutex.

Referenced by sender_connect_loop().

Here is the caller graph for this function:

◆ accept_tcp_read_all()

int accept_tcp_read_all ( PotrSocket fd,
uint8_t * buf,
size_t n )
static

Definition at line 127 of file potrConnectThread.c.

Referenced by tcp_read_first_packet().

Here is the caller graph for this function:

◆ accept_tcp_wait_readable()

int accept_tcp_wait_readable ( PotrSocket fd,
uint32_t wait_ms )
static

Definition at line 150 of file potrConnectThread.c.

Referenced by tcp_read_first_packet().

Here is the caller graph for this function:

◆ tcp_read_first_packet()

int tcp_read_first_packet ( PotrSocket fd,
uint8_t * buf,
size_t max_buf,
size_t * out_len,
uint32_t timeout_ms )
static

Definition at line 179 of file potrConnectThread.c.

References accept_tcp_read_all(), accept_tcp_wait_readable(), and PACKET_HEADER_SIZE.

Referenced by receiver_accept_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_session_compare()

int tcp_session_compare ( const struct PotrContext_ * ctx,
const PotrPacket * pkt )
static

Definition at line 224 of file potrConnectThread.c.

References PotrContext_::peer_session_id, PotrContext_::peer_session_known, PotrContext_::peer_session_tv_nsec, PotrContext_::peer_session_tv_sec, PotrPacket::session_id, PotrPacket::session_tv_nsec, PotrPacket::session_tv_sec, TCP_SESSION_NEW, TCP_SESSION_OLD, and TCP_SESSION_SAME.

Referenced by receiver_accept_loop().

Here is the caller graph for this function:

◆ join_recv_thread()

void join_recv_thread ( struct PotrContext_ * ctx,
int path_idx )
static

Definition at line 240 of file potrConnectThread.c.

References PotrContext_::recv_thread.

Referenced by receiver_accept_loop(), sender_connect_loop(), and start_connected_threads().

Here is the caller graph for this function:

◆ reset_connection_state()

void reset_connection_state ( struct PotrContext_ * ctx)
static

Definition at line 259 of file potrConnectThread.c.

References PotrContext_::frag_buf_len, and PotrContext_::peer_session_known.

Referenced by receiver_accept_loop(), and sender_connect_loop().

Here is the caller graph for this function:

◆ reset_all_paths_disconnected()

void reset_all_paths_disconnected ( struct PotrContext_ * ctx)
static

◆ reset_send_queue()

void reset_send_queue ( struct PotrContext_ * ctx)
static

Definition at line 295 of file potrConnectThread.c.

References PotrSendQueue::depth, PotrContext_::global, PotrGlobalConfig::max_payload, potr_send_queue_destroy(), potr_send_queue_init(), and PotrContext_::send_queue.

Referenced by receiver_accept_loop(), and sender_connect_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ start_connected_threads()

int start_connected_threads ( struct PotrContext_ * ctx,
int path_idx )
static

Definition at line 308 of file potrConnectThread.c.

References close_tcp_conn(), join_recv_thread(), POTR_ERROR, POTR_LOG, POTR_ROLE_SENDER, potr_send_thread_start(), potr_send_thread_stop(), POTR_SUCCESS, potr_tcp_health_thread_start(), POTR_TRACE_ERROR, POTR_TYPE_TCP_BIDIR, PotrContext_::role, PotrContext_::running, PotrContext_::send_thread_running, PotrContext_::service, PotrServiceDef::service_id, tcp_recv_thread_start(), and PotrServiceDef::type.

Referenced by receiver_accept_loop(), and sender_connect_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop_connected_threads()

void stop_connected_threads ( struct PotrContext_ * ctx,
int path_idx )
static

Definition at line 363 of file potrConnectThread.c.

References close_tcp_conn(), POTR_ROLE_SENDER, potr_tcp_health_thread_stop(), POTR_TYPE_TCP_BIDIR, PotrContext_::role, PotrContext_::service, and PotrServiceDef::type.

Referenced by receiver_accept_loop(), and sender_connect_loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tcp_connect_with_timeout()

PotrSocket tcp_connect_with_timeout ( struct PotrContext_ * ctx,
int path_idx )
static

Definition at line 381 of file potrConnectThread.c.

References PotrServiceDef::connect_timeout_ms, PotrContext_::dst_addr_resolved, PotrServiceDef::dst_port, POTR_INVALID_SOCKET, POTR_LOG, POTR_TRACE_ERROR, POTR_TRACE_VERBOSE, PotrContext_::service, PotrServiceDef::service_id, PotrServiceDef::src_addr, PotrContext_::src_addr_resolved, and PotrServiceDef::src_port.

Referenced by sender_connect_loop().

Here is the caller graph for this function:

◆ sender_connect_loop()

void sender_connect_loop ( struct PotrContext_ * ctx,
int path_idx )
static

◆ receiver_accept_loop()

void receiver_accept_loop ( struct PotrContext_ * ctx,
int path_idx )
static

Definition at line 783 of file potrConnectThread.c.

References close_tcp_conn(), connect_get_ms(), PotrContext_::connect_thread_running, PotrContext_::global, join_recv_thread(), PotrGlobalConfig::max_payload, PotrContext_::n_path, PACKET_HEADER_SIZE, packet_parse(), PotrContext_::peer_session_id, POTR_INVALID_SOCKET, POTR_LOG, POTR_SUCCESS, POTR_TRACE_INFO, POTR_TRACE_VERBOSE, POTR_TRACE_WARNING, POTR_TYPE_TCP_BIDIR, reset_all_paths_disconnected(), reset_connection_state(), reset_send_queue(), PotrContext_::running, PotrContext_::service, PotrServiceDef::service_id, PotrContext_::session_establish_mutex, PotrPacket::session_id, PotrServiceDef::src_addr, PotrContext_::src_addr_resolved, PotrServiceDef::src_port, start_connected_threads(), stop_connected_threads(), PotrContext_::tcp_active_paths, PotrContext_::tcp_conn_fd, PotrContext_::tcp_first_pkt_buf, PotrContext_::tcp_first_pkt_len, PotrGlobalConfig::tcp_health_timeout_ms, PotrContext_::tcp_last_ping_recv_ms, PotrContext_::tcp_last_ping_req_recv_ms, PotrContext_::tcp_listen_sock, tcp_read_first_packet(), tcp_session_compare(), TCP_SESSION_NEW, TCP_SESSION_OLD, PotrContext_::tcp_state_mutex, and PotrServiceDef::type.

Referenced by connect_thread_func().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ connect_thread_func()

void * connect_thread_func ( void * arg)
static

Definition at line 1034 of file potrConnectThread.c.

References PotrContext_::connect_thread_running, ConnectArg::ctx, ConnectArg::path_idx, POTR_LOG, POTR_ROLE_SENDER, POTR_TRACE_VERBOSE, POTR_TYPE_TCP_BIDIR, receiver_accept_loop(), PotrContext_::role, sender_connect_loop(), PotrContext_::service, PotrServiceDef::service_id, and PotrServiceDef::type.

Referenced by potr_connect_thread_start().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ potr_connect_thread_start()

int potr_connect_thread_start ( struct PotrContext_ * ctx)

TCP 接続管理スレッドを起動します (path 数分)。

TCP 接続管理スレッドを起動します。

Parameters
[in,out]ctxセッションコンテキストへのポインタ。
Returns
成功時は POTR_SUCCESS、失敗時は POTR_ERROR を返します。

呼び出し前提:

  • SENDER: dst_addr_resolved[i] および dst_port が設定済みであること (n_path 分)。
  • RECEIVER: tcp_listen_sock[i] が listen 状態であること (n_path 分)。
  • tcp_state_mutex / tcp_state_cv / tcp_send_mutex[] が初期化済みであること。

Definition at line 1084 of file potrConnectThread.c.

References PotrContext_::connect_thread, connect_thread_func(), PotrContext_::connect_thread_running, PotrContext_::global, PotrGlobalConfig::max_payload, PotrContext_::n_path, PACKET_HEADER_SIZE, POTR_ERROR, POTR_LOG, POTR_ROLE_RECEIVER, POTR_SUCCESS, POTR_TRACE_ERROR, POTR_TRACE_VERBOSE, PotrContext_::role, s_connect_args, PotrContext_::service, PotrServiceDef::service_id, PotrContext_::session_establish_mutex, PotrContext_::tcp_first_pkt_buf, and PotrContext_::tcp_first_pkt_len.

Referenced by potrOpenService().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ potr_connect_thread_stop()

void potr_connect_thread_stop ( struct PotrContext_ * ctx)

TCP 接続管理スレッドを停止します。

Parameters
[in,out]ctxセッションコンテキストへのポインタ。
  1. connect_thread_running[i] フラグを全 path クリアして停止を通知する。
  2. tcp_state_cv をブロードキャストして reconnect sleep を中断する。
  3. tcp_listen_sock[i] (RECEIVER) を全 path クローズして accept ブロックを解除する。
  4. tcp_conn_fd[i] を全 path クローズして recv ループをブロック解除する。
  5. connect_thread[i] の終了を全 path 待機する。
  6. 送信スレッドを停止する (全 path join 後)。 依存スレッド (recv/health) のクリーンアップは各 connect_thread 内で行われる。

Definition at line 1183 of file potrConnectThread.c.

References PotrContext_::connect_thread, PotrContext_::connect_thread_running, PotrContext_::n_path, POTR_INVALID_SOCKET, POTR_LOG, POTR_ROLE_RECEIVER, potr_send_thread_stop(), POTR_TRACE_VERBOSE, PotrContext_::role, PotrContext_::service, PotrServiceDef::service_id, PotrContext_::session_establish_mutex, PotrContext_::tcp_conn_fd, PotrContext_::tcp_first_pkt_buf, PotrContext_::tcp_first_pkt_len, PotrContext_::tcp_listen_sock, PotrContext_::tcp_state_cv, and PotrContext_::tcp_state_mutex.

Referenced by potrCloseService().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ s_connect_args

ConnectArg s_connect_args[POTR_MAX_PATH]
static

Definition at line 59 of file potrConnectThread.c.

Referenced by potr_connect_thread_start().