Skip to content

Commit

Permalink
修复UDP链接id逻辑错误
Browse files Browse the repository at this point in the history
  • Loading branch information
xia-chu committed Dec 5, 2024
1 parent e1e68cd commit 0421201
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Network/UdpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ SessionHelper::Ptr UdpServer::createSession(const PeerIdType &id, Buffer::Ptr &b
if (!strong_self) {
return;
}

auto new_id = makeSockId(addr, addr_len);
//快速判断是否为本会话的的数据, 通常应该成立 [AUTO-TRANSLATED:d5d147e4]
//Quickly determine if it's data for the current session, usually should be true
if (id == makeSockId(addr, addr_len)) {
if (id == new_id) {
if (auto strong_helper = weak_helper.lock()) {
emitSessionRecv(strong_helper, buf);
}
Expand All @@ -302,7 +302,7 @@ SessionHelper::Ptr UdpServer::createSession(const PeerIdType &id, Buffer::Ptr &b

//收到非本peer fd的数据,让server去派发此数据到合适的session对象 [AUTO-TRANSLATED:e5f44445]
//Received data from a non-current peer fd, let the server dispatch this data to the appropriate session object
strong_self->onRead_l(false, id, buf, addr, addr_len);
strong_self->onRead_l(false, new_id, buf, addr, addr_len);
});
socket->setOnErr([weak_self, weak_helper, id](const SockException &err) {
// 在本函数作用域结束时移除会话对象 [AUTO-TRANSLATED:b2ade305]
Expand Down

3 comments on commit 0421201

@alexliyu7352
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix UDP connection ID logic error

TRANS_BY_GITHUB_AI_ASSISTANT

@alexliyu7352
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Review for src/Network/UdpServer.cpp:

Code Review: Patch to src/Network/UdpServer.cpp

Summary

This patch addresses a logic error in the UDP connection ID handling within the UdpServer class. The core change involves consistently using a newly calculated new_id instead of the potentially outdated id when handling received data and managing sessions. This review is focused on the patch itself and its impact on the surrounding code.

Detailed Feedback

Code Overview

The patch modifies the createSession function within the UdpServer class. The primary change is the introduction of auto new_id = makeSockId(addr, addr_len); to recalculate the peer ID based on the received address information. This new_id then replaces the original id in the if (id == new_id) check and in the subsequent call to onRead_l. This ensures that the server correctly identifies the session associated with the received data, even if the initial id is outdated or incorrect.

Strengths

  • Addresses a potential bug: The patch directly addresses a likely source of errors in UDP session management. By recalculating the peer ID, it prevents potential mismatches between the received data and the associated session.
  • Improved accuracy: Using new_id ensures that the session identification is based on the most up-to-date information from the received packet.
  • Concise and targeted: The changes are localized and focused, making the patch easy to understand and review.

Areas for Improvement

1. Clarity and Commenting

  • Issue: While the comments in the original code are helpful, the added logic could benefit from more detailed comments explaining why the change was necessary. The existing comments are mostly translations and don't fully explain the underlying logic.
  • Suggestion: Add comments explaining the potential race condition or timing issue that the original code suffered from. Explain why recalculating makeSockId is crucial for correctness.
  • Example:
    // Recalculate the peer ID based on the received address. This is necessary because the initial 'id' 
    // might be outdated due to [explain the reason for potential outdated ID, e.g., asynchronous operations, delayed processing].
    // Using the newly calculated 'new_id' ensures that the correct session is identified.
    auto new_id = makeSockId(addr, addr_len);

2. Error Handling

  • Issue: The code assumes that makeSockId will always succeed. While unlikely, there's a potential for failure (e.g., invalid address family).
  • Suggestion: Add error handling within createSession to gracefully handle potential errors from makeSockId. This might involve logging an error message or returning an appropriate error code.
  • Example:
    auto new_id = makeSockId(addr, addr_len);
    if (new_id.empty()) {
        ErrorL << "Failed to create socket ID from address";
        return nullptr; // Or handle the error appropriately
    }

3. Potential for Optimization (Minor)

  • Issue: makeSockId is called twice within the createSession function.
  • Suggestion: Consider calculating new_id only once and reusing it. This is a minor optimization but improves code efficiency slightly.

Conclusion

The patch effectively addresses a logic error in UDP session ID handling. The changes are well-implemented and improve the accuracy and robustness of the UdpServer class. However, adding more explanatory comments and improving error handling would further enhance the code's clarity and reliability. The suggested minor optimization is a worthwhile consideration.

TRANS_BY_GITHUB_AI_ASSISTANT

@alexliyu7352
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix UDP connection ID logic error

TRANS_BY_GITHUB_AI_ASSISTANT

Please sign in to comment.