porter/src/tech-sample/tcpServer/tcpServer.h

1 ファイル

1.1 porter/src/tech-sample/tcpServer/tcpServer.h

TCP サーバーサンプル共通定義。

1.1.1 作者

c-modernization-kit sample team

1.1.2 バージョン

1.0.0

1.1.3 日付

2026/03/17

1.1.4 著作権

Copyright (C) CompanyName, Ltd. 2026. All rights reserved.

2 関数

2.1 handle_client_session

void handle_client_session ( ClientFd fd )

TCP 通信メインループ (デフォルト実装)。

受信したデータをそのまま返します。クライアントが切断すると戻ります。 ソケットは本関数内で閉じます。fork モード・prefork モード共用。

2.1.1 引数

  • fd [in] クライアントソケット。

2.2 platform_init

void platform_init ( ClientSessionFn session_fn )

プラットフォーム初期化 (Windows: WSAStartup / Linux: no-op)。

2.2.1 引数

  • session_fn [in] セッション処理関数。g_session_fn に保存されます。

2.3 platform_cleanup

void platform_cleanup ( void )

プラットフォーム後処理 (Windows: WSACleanup / Linux: no-op)。

2.4 dispatch_internal_args

int dispatch_internal_args ( int argc, char *argv[] )

内部起動引数を処理します。

Windows では --child <handle> / --worker <pipe> を検出して処理します。 Linux では常に 0 を返します。

2.4.1 引数

  • argc [in] コマンドライン引数の数。
  • argv [in] コマンドライン引数の配列。

2.4.2 戻り値

内部起動引数を処理した場合は 1、通常起動の場合は 0 を返します。

2.5 run_fork_server

void run_fork_server ( int port )

fork モードのサーバーを起動します。

2.5.1 引数

  • port [in] 待ち受けポート番号。

2.6 run_prefork_server

void run_prefork_server ( int port, int num_workers, int conns_per_worker )

prefork モードのサーバーを起動します。

2.6.1 引数

  • port [in] 待ち受けポート番号。
  • num_workers [in] 事前生成するワーカープロセス数。
  • conns_per_worker [in] 1 ワーカーあたりの同時接続数。 1 の場合は従来の逐次処理。 2 以上の場合はイベント駆動型の多重接続処理。

3 変数

3.1 g_session_fn

ClientSessionFn g_session_fn;

登録済みセッション処理関数。

platform_init() で設定します。

登録済みセッション処理関数。

4 定数、マクロ

4.1 get_pid

#define get_pid ()
    ((PidType)getpid())

現在のプロセス ID を取得する。

4.2 client_recv

#define client_recv ( fd, buf, len )
    read((fd), (buf), (len))

クライアントからデータを受信する。

4.3 client_send

#define client_send ( fd, buf, len )
    write((fd), (buf), (len))

クライアントへデータを送信する。

4.4 client_close

#define client_close ( fd )
    close(fd)

クライアントソケットを閉じる。

4.5 DEFAULT_PORT

#define DEFAULT_PORT 8080

デフォルト待ち受けポート番号。

4.6 DEFAULT_WORKERS

#define DEFAULT_WORKERS 4

デフォルト prefork ワーカー数。

4.7 DEFAULT_CONNS_PER_WORKER

#define DEFAULT_CONNS_PER_WORKER 1

デフォルト 1 ワーカーあたりの同時接続数。

4.8 BUFFER_SIZE

#define BUFFER_SIZE 1024

送受信バッファサイズ (バイト)。

5

5.1 ServerMode

サーバー動作モード。

列挙子 説明
MODE_PREFORK 0 プリフォークモード (デフォルト)。
MODE_FORK 1 接続ごと fork モード。

5.2 ClientFd

typedef int ClientFd;

クライアントソケットの型。Linux では int、Windows では SOCKET。

5.3 PidType

typedef pid_t PidType;

プロセス ID の型。Linux では pid_t、Windows では DWORD。

5.4 ClientSessionFn

typedef void (*ClientSessionFn) (ClientFd fd);

セッション処理関数の型。

クライアントソケットを受け取り、通信処理を行い、ソケットを閉じます。