TCP サーバーサンプル共通定義。
c-modernization-kit sample team
1.0.0
2026/03/17
Copyright (C) CompanyName, Ltd. 2026. All rights reserved.
void handle_client_session ( ClientFd fd )TCP 通信メインループ (デフォルト実装)。
受信したデータをそのまま返します。クライアントが切断すると戻ります。 ソケットは本関数内で閉じます。fork モード・prefork モード共用。
void platform_init ( ClientSessionFn session_fn )プラットフォーム初期化 (Windows: WSAStartup / Linux: no-op)。
void platform_cleanup ( void )プラットフォーム後処理 (Windows: WSACleanup / Linux: no-op)。
int dispatch_internal_args ( int argc, char *argv[] )内部起動引数を処理します。
Windows では --child <handle> / --worker <pipe> を検出して処理します。 Linux では常に 0 を返します。
内部起動引数を処理した場合は 1、通常起動の場合は 0 を返します。
void run_fork_server ( int port )fork モードのサーバーを起動します。
void run_prefork_server ( int port, int num_workers, int conns_per_worker )prefork モードのサーバーを起動します。
ClientSessionFn g_session_fn;登録済みセッション処理関数。
platform_init() で設定します。
登録済みセッション処理関数。
#define get_pid ()
((PidType)getpid())現在のプロセス ID を取得する。
#define client_recv ( fd, buf, len )
read((fd), (buf), (len))クライアントからデータを受信する。
#define client_send ( fd, buf, len )
write((fd), (buf), (len))クライアントへデータを送信する。
#define client_close ( fd )
close(fd)クライアントソケットを閉じる。
#define DEFAULT_PORT 8080デフォルト待ち受けポート番号。
#define DEFAULT_WORKERS 4デフォルト prefork ワーカー数。
#define DEFAULT_CONNS_PER_WORKER 1デフォルト 1 ワーカーあたりの同時接続数。
#define BUFFER_SIZE 1024送受信バッファサイズ (バイト)。
サーバー動作モード。
| 列挙子 | 値 | 説明 |
|---|---|---|
| MODE_PREFORK | 0 | プリフォークモード (デフォルト)。 |
| MODE_FORK | 1 | 接続ごと fork モード。 |
typedef int ClientFd;クライアントソケットの型。Linux では int、Windows では SOCKET。
typedef pid_t PidType;プロセス ID の型。Linux では pid_t、Windows では DWORD。
typedef void (*ClientSessionFn) (ClientFd fd);セッション処理関数の型。
クライアントソケットを受け取り、通信処理を行い、ソケットを閉じます。