Document of c-modernization-kit (porter) 1.0.0
Loading...
Searching...
No Matches
tcpServer.h
Go to the documentation of this file.
1
13
14#ifndef TCPSERVER_H
15#define TCPSERVER_H
16
17/* ============================================================
18 * プラットフォーム抽象化
19 * handle_client_session() が ClientFd / get_pid() / client_recv() 等を
20 * プラットフォームを意識せずに使えるようにします。
21 * ============================================================ */
22
23#ifndef _WIN32
24
25 #include <sys/types.h>
26 #include <unistd.h>
27
29 typedef int ClientFd;
31 typedef pid_t PidType;
32
34 #define get_pid() ((PidType)getpid())
36 #define client_recv(fd, buf, len) read((fd), (buf), (len))
38 #define client_send(fd, buf, len) write((fd), (buf), (len))
40 #define client_close(fd) close(fd)
41
42#else /* _WIN32 */
43
44 #define WIN32_LEAN_AND_MEAN
45 #include <windows.h>
46 #include <winsock2.h>
47 #include <ws2tcpip.h>
48
50 typedef SOCKET ClientFd;
52 typedef DWORD PidType;
53
55 #define get_pid() ((PidType)GetCurrentProcessId())
57 #define client_recv(fd, buf, len) recv((fd), (buf), (int)(len), 0)
59 #define client_send(fd, buf, len) send((fd), (buf), (int)(len), 0)
61 #define client_close(fd) closesocket(fd)
62
63#endif /* _WIN32 */
64
65/* ============================================================
66 * 共通定数
67 * ============================================================ */
68
70#define DEFAULT_PORT 8080
72#define DEFAULT_WORKERS 4
74#define DEFAULT_CONNS_PER_WORKER 1
76#define BUFFER_SIZE 1024
77
78/* ============================================================
79 * 共通型定義
80 * ============================================================ */
81
87typedef enum {
91
99typedef void (*ClientSessionFn)(ClientFd fd);
100
109
110/* ============================================================
111 * 関数プロトタイプ
112 * ============================================================ */
113
114/* --- 共通実装 (tcpServer.c) --- */
115
126
127/* --- プラットフォームフック (各プラットフォームファイルで実装) --- */
128
135void platform_init(ClientSessionFn session_fn);
136
142void platform_cleanup(void);
143
155int dispatch_internal_args(int argc, char *argv[]);
156
163void run_fork_server(int port);
164
175void run_prefork_server(int port, int num_workers, int conns_per_worker);
176
177#endif /* TCPSERVER_H */
void(* ClientSessionFn)(ClientFd fd)
セッション処理関数の型。
Definition tcpServer.h:99
void platform_init(ClientSessionFn session_fn)
プラットフォーム初期化 (Windows: WSAStartup / Linux: no-op)。
ServerMode
サーバー動作モード。
Definition tcpServer.h:87
@ MODE_FORK
接続ごと fork モード。
Definition tcpServer.h:89
@ MODE_PREFORK
プリフォークモード (デフォルト)。
Definition tcpServer.h:88
int dispatch_internal_args(int argc, char *argv[])
内部起動引数を処理します。
void platform_cleanup(void)
プラットフォーム後処理 (Windows: WSACleanup / Linux: no-op)。
void run_prefork_server(int port, int num_workers, int conns_per_worker)
prefork モードのサーバーを起動します。
ClientSessionFn g_session_fn
登録済みセッション処理関数。
void run_fork_server(int port)
fork モードのサーバーを起動します。
int ClientFd
クライアントソケットの型。Linux では int、Windows では SOCKET。
Definition tcpServer.h:29
void handle_client_session(ClientFd fd)
TCP 通信メインループ (デフォルト実装)。
Definition tcpServer.c:34
pid_t PidType
プロセス ID の型。Linux では pid_t、Windows では DWORD。
Definition tcpServer.h:31