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

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

#include <sys/types.h>
#include <unistd.h>
Include dependency graph for tcpServer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define get_pid()
 現在のプロセス ID を取得する。
#define client_recv(fd, buf, len)
 クライアントからデータを受信する。
#define client_send(fd, buf, len)
 クライアントへデータを送信する。
#define client_close(fd)
 クライアントソケットを閉じる。
#define DEFAULT_PORT   8080
 デフォルト待ち受けポート番号。
#define DEFAULT_WORKERS   4
 デフォルト prefork ワーカー数。
#define DEFAULT_CONNS_PER_WORKER   1
 デフォルト 1 ワーカーあたりの同時接続数。
#define BUFFER_SIZE   1024
 送受信バッファサイズ (バイト)。

Typedefs

typedef int ClientFd
 クライアントソケットの型。Linux では int、Windows では SOCKET。
typedef pid_t PidType
 プロセス ID の型。Linux では pid_t、Windows では DWORD。
typedef void(* ClientSessionFn) (ClientFd fd)
 セッション処理関数の型。

Enumerations

enum  ServerMode { MODE_PREFORK = 0 , MODE_FORK = 1 }
 サーバー動作モード。 More...

Functions

void handle_client_session (ClientFd fd)
 TCP 通信メインループ (デフォルト実装)。
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[])
 内部起動引数を処理します。
void run_fork_server (int port)
 fork モードのサーバーを起動します。
void run_prefork_server (int port, int num_workers, int conns_per_worker)
 prefork モードのサーバーを起動します。

Variables

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

Detailed Description

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

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

Definition in file tcpServer.h.

Macro Definition Documentation

◆ get_pid

#define get_pid ( )
Value:
((PidType)getpid())
pid_t PidType
プロセス ID の型。Linux では pid_t、Windows では DWORD。
Definition tcpServer.h:31

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

Definition at line 34 of file tcpServer.h.

Referenced by handle_client_session(), run_fork_server(), run_prefork_server(), and worker_loop().

◆ client_recv

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

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

Definition at line 36 of file tcpServer.h.

Referenced by handle_client_session(), and worker_loop().

◆ client_send

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

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

Definition at line 38 of file tcpServer.h.

Referenced by handle_client_session(), and worker_loop().

◆ client_close

#define client_close ( fd)
Value:
close(fd)

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

Definition at line 40 of file tcpServer.h.

Referenced by handle_client_session(), and worker_loop().

◆ DEFAULT_PORT

#define DEFAULT_PORT   8080

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

Definition at line 70 of file tcpServer.h.

Referenced by main().

◆ DEFAULT_WORKERS

#define DEFAULT_WORKERS   4

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

Definition at line 72 of file tcpServer.h.

Referenced by main().

◆ DEFAULT_CONNS_PER_WORKER

#define DEFAULT_CONNS_PER_WORKER   1

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

Definition at line 74 of file tcpServer.h.

Referenced by main().

◆ BUFFER_SIZE

#define BUFFER_SIZE   1024

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

Definition at line 76 of file tcpServer.h.

Referenced by handle_client_session(), and worker_loop().

Typedef Documentation

◆ ClientFd

typedef int ClientFd

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

Definition at line 29 of file tcpServer.h.

◆ PidType

typedef pid_t PidType

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

Definition at line 31 of file tcpServer.h.

◆ ClientSessionFn

typedef void(* ClientSessionFn) (ClientFd fd)

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

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

Definition at line 99 of file tcpServer.h.

Enumeration Type Documentation

◆ ServerMode

enum ServerMode

サーバー動作モード。

Enumerator
MODE_PREFORK 

プリフォークモード (デフォルト)。

MODE_FORK 

接続ごと fork モード。

Definition at line 87 of file tcpServer.h.

Function Documentation

◆ handle_client_session()

void handle_client_session ( ClientFd fd)

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

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

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

Definition at line 34 of file tcpServer.c.

References BUFFER_SIZE, client_close, client_recv, client_send, and get_pid.

Referenced by main().

Here is the caller graph for this function:

◆ platform_init()

void platform_init ( ClientSessionFn session_fn)

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

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

Definition at line 254 of file tcpServer_linux.c.

References g_session_fn.

Referenced by main().

Here is the caller graph for this function:

◆ platform_cleanup()

void platform_cleanup ( void )

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

Definition at line 259 of file tcpServer_linux.c.

Referenced by main().

Here is the caller graph for this function:

◆ dispatch_internal_args()

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

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

Parameters
[in]argcコマンドライン引数の数。
[in]argvコマンドライン引数の配列。
Returns
内部起動引数を処理した場合は 1、通常起動の場合は 0 を返します。

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

Definition at line 264 of file tcpServer_linux.c.

Referenced by main().

Here is the caller graph for this function:

◆ run_fork_server()

void run_fork_server ( int port)

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

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

Definition at line 275 of file tcpServer_linux.c.

References create_listen_socket(), g_session_fn, get_pid, and sigchld_handler().

Referenced by main().

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

◆ run_prefork_server()

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

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

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

Definition at line 316 of file tcpServer_linux.c.

References create_listen_socket(), get_pid, running, shutdown_handler(), sigchld_handler(), and worker_loop().

Referenced by main().

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

Variable Documentation

◆ g_session_fn

ClientSessionFn g_session_fn
extern

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

platform_init() で設定します。

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

Definition at line 19 of file tcpServer_common.c.

Referenced by platform_init(), run_fork_server(), and worker_loop().