Skip to content

Commit f0bd3a6

Browse files
committed
docs: Sumomo HTTP サーバー実装の潜在的な改善点をコメントで文書化
- HttpSession: Keep-Alive 対応時の注意事項を記載 - Send メソッド: エラーハンドリングとソケットクローズの検討事項を追記 - GetStatsFromSumomo: 設計改善の提案を追加 - HttpListener: コンストラクタでのリソース管理の重要性を明記 - その他、解像度処理やポート番号検証の意図を明確化 これらのコメントは将来的な改善のための指針として機能する
1 parent 532564a commit f0bd3a6

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

examples/sumomo/src/sumomo.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ struct SumomoConfig {
136136
// 数字で指定した場合の処理 (例 640x480 )
137137
auto pos = resolution.find('x');
138138
if (pos == std::string::npos) {
139+
// TODO: 無効な形式の場合、16x16 を返すよりエラーを投げるべきか検討
140+
// 現在は最小解像度として 16x16 を返している
139141
return {16, 16};
140142
}
141143
auto width = std::atoi(resolution.substr(0, pos).c_str());
@@ -151,6 +153,10 @@ using tcp = net::ip::tcp;
151153

152154
class Sumomo;
153155

156+
// HTTP リクエストを処理するセッション
157+
// 注意: 現在は1リクエスト=1コネクションの設計
158+
// Keep-Alive に対応する場合は、DoRead() の再帰呼び出しと
159+
// リクエスト数/タイムアウトの制限を実装する必要がある
154160
class HttpSession : public std::enable_shared_from_this<HttpSession> {
155161
public:
156162
HttpSession(tcp::socket socket, std::weak_ptr<Sumomo> sumomo)
@@ -203,10 +209,17 @@ class HttpSession : public std::enable_shared_from_this<HttpSession> {
203209
http::async_write(
204210
socket_, *sp,
205211
[self = shared_from_this(), sp](beast::error_code ec, std::size_t) {
212+
// TODO: 現在はエラーハンドリングをしていないが、
213+
// エラー時のログ出力とソケットの明示的なクローズを検討する
214+
// 現状は Keep-Alive 非対応で1リクエスト=1コネクションなので
215+
// 大きな問題にはなっていない
206216
self->socket_.shutdown(tcp::socket::shutdown_send, ec);
207217
});
208218
}
209219

220+
// TODO: この関数は HttpSession のメンバ関数だが、Sumomo の定義後に
221+
// 実装する必要があるため外部で定義している。
222+
// 将来的にはより良い設計(例: stats 取得用のインターフェース)を検討
210223
void GetStatsFromSumomo(std::shared_ptr<Sumomo> sumomo,
211224
std::shared_ptr<HttpSession> self);
212225

@@ -225,6 +238,9 @@ class HttpListener : public std::enable_shared_from_this<HttpListener> {
225238
beast::error_code ec;
226239

227240
// 例外発生時のリソースクリーンアップ用ラムダ
241+
// 注意: コンストラクタ内で例外が発生した場合、デストラクタは呼ばれないため
242+
// 明示的にリソースをクリーンアップする必要がある
243+
// RAII は効かないので、手動でのクリーンアップが重要
228244
auto cleanup = [this]() {
229245
beast::error_code ignore_ec;
230246
if (acceptor_.is_open()) {
@@ -291,6 +307,7 @@ class HttpListener : public std::enable_shared_from_this<HttpListener> {
291307
}
292308
return;
293309
}
310+
// 正常にコネクションを受け入れた場合は次の accept を開始
294311
self->DoAccept();
295312
});
296313
}
@@ -419,6 +436,8 @@ class Sumomo : public std::enable_shared_from_this<Sumomo>,
419436
// HTTP サーバーの起動(接続前に起動する)
420437
if (config_.http_port.has_value()) {
421438
try {
439+
// TODO: make_address は無効なアドレスで例外を投げるが、
440+
// より具体的なエラーメッセージのためにエラーコード版の使用を検討
422441
tcp::endpoint endpoint{boost::asio::ip::make_address(config_.http_host),
423442
static_cast<unsigned short>(*config_.http_port)};
424443
http_listener_ =
@@ -745,6 +764,8 @@ int main(int argc, char* argv[]) {
745764
}
746765
try {
747766
int port = std::stoi(value);
767+
// ウェルノウンポート (0-1023) は特権ポートなので除外
768+
// 1024-65535 の範囲のみ許可
748769
if (port >= 1024 && port <= 65535) {
749770
return std::string();
750771
}

0 commit comments

Comments
 (0)