Skip to content

NITWORK: Fix packet id doesn't need to change if he's resent#100

Merged
Saverio976 merged 17 commits intodevfrom
fix/Nitwork-dont-change-resent-packet-id
Oct 15, 2023
Merged

NITWORK: Fix packet id doesn't need to change if he's resent#100
Saverio976 merged 17 commits intodevfrom
fix/Nitwork-dont-change-resent-packet-id

Conversation

@romainpanno
Copy link
Copy Markdown
Collaborator

@romainpanno romainpanno commented Oct 15, 2023

PATCH

  • Please check if the PR fulfills these requirements
  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Norm of the code has been respected
  • In what state is your pull request?
  • Ready to merge / Waiting a reviwer to see my work.
  • Work In Progress (WIP) / My work is not finish but i want daily reviews. (Draft)
  • CI Review / I want to see what the CI thinks of my work.
  • Need feedback / Just to have feedback on what i produced. (May not be merged)
  • What kind of change does this PR introduce? (You can choose multiple)
  • Bug fix
  • Feature request
  • New / Updated documentation
  • Testing CI ( Make the pull request in draft mode)
  • What is the current behavior? (link an issue based on the kind of change this pr introduce)

  • What is the new behavior (if this is a feature change)?

  • Other information:

Summary by CodeRabbit

  • New Feature: Introduced a mechanism to handle packet resending more efficiently, improving network performance and reliability.
  • New Feature: Updated the server-client action flow to ensure the game starts only when all clients are ready, enhancing the multiplayer experience.
  • Refactor: Modified the start function in both NitworkClient and NitworkServer classes for better clarity and functionality. The NitworkClient's start function now includes an optional IP parameter, and the NitworkServer's start function includes a parameter for the maximum number of players.
  • Bug Fix: Added checks to prevent the server from exceeding the maximum number of players and to prevent sending star wave messages when there are not enough clients connected.
  • Documentation: Updated the RFC document to reflect the new action flow between the client and server.
  • Refactor: Improved argument validation in the main server and client functions for better error handling.

@romainpanno romainpanno added the bug Something isn't working label Oct 15, 2023
@romainpanno romainpanno self-assigned this Oct 15, 2023
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Oct 15, 2023

Rate Limit Exceeded

@romainpanno has exceeded the limit for the number of files or commits that can be reviewed per hour. Please wait 6 minutes and 25 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, convert this PR to a draft and then mark it as ready for review again to re-trigger the review. Alternatively, you can push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per repository.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 91d58ff and 3062a04.

Walkthrough

The changes primarily focus on improving the handling of packet resending in the Nitwork networking library. This includes adding a isResend flag to packets, modifying packet processing logic, and adjusting the client-server interaction flow. Additionally, the maximum number of clients is now configurable, and the client and server start functions have been refactored for better clarity and flexibility.

Changes

File(s) Summary
src/Nitwork/ANitwork.cpp, src/Nitwork/ANitwork.hpp Introduced isResend flag for packets and adjusted packet processing logic accordingly.
src/Nitwork/INitwork.hpp Added isResend member variable to Packet class with corresponding getter and setter methods.
src/Nitwork/Nitwork.h, src/Nitwork/NitworkServer.cpp Adjusted MAX_CLIENTS constant and added checks for maximum client count before sending messages.
src/Nitwork/NitworkClient.cpp, src/Nitwork/NitworkClient.hpp Refactored start function to startClient with adjusted parameters for better flexibility.
src/Nitwork/NitworkServer.cpp, src/Nitwork/NitworkServer.hpp Renamed start function to startServer and added nbPlayer parameter to set maximum number of players.
src/main_client.cpp, src/main_server.cpp Adjusted function calls and parameters to match changes in NitworkClient and NitworkServer classes.
docs/network/rfc/RFC.md Updated action flow to start game only when all clients are ready.

🐇💻

"In the land of code, where the shadows lie,

A rabbit hopped, with a twinkle in its eye.

Packets fly, with a resend flag,

And the server hums, without a lag.

Clients connect, but not too many,

For the server checks, and won't take any.

Start functions change, with parameters new,

A rabbit's work, for me and you." 🎉🥕


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • Mention @coderabbitai in any review comment for bot assistance.
  • Note: Review comments are made on code diffs or files, not on the PR overview.

Pause Incremental Reviews

  • Insert @coderabbitai: ignore in the PR description to halt the bot's ongoing reviews. Remove the line to resume.
  • Additionally, enter @coderabbitai pause to halt the bot's ongoing reviews as any issue or review comment. To
    resume reviews, enter @coderabbitai resume as any issue or review comment.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Details

Commits Files that changed from the base of the PR and between 466736b and 084422c.
Files selected for processing (3)
  • src/Nitwork/ANitwork.cpp (1 hunks)
  • src/Nitwork/ANitwork.hpp (1 hunks)
  • src/Nitwork/INitwork.hpp (1 hunks)
Additional comments (Suppressed): 3
src/Nitwork/ANitwork.hpp (1)
  • 43-61: The new hunk introduces a check for the isResend flag in the Packet class. If the packet is not a resend, the packet ID and header are updated. This is a good performance optimization as it avoids unnecessary operations for resending packets. However, ensure that the isResend flag is being correctly set elsewhere in the code where packets are being resent. Also, verify that skipping the update of packet ID and header for resending packets does not introduce any issues in the packet processing logic.
src/Nitwork/ANitwork.cpp (1)
  • 188-193: The new hunk introduces a flag isResend to the Packet class and sets it to true when a packet is being resent. This flag is used to optimize the packet resending process by avoiding redundant updates to the packet ID and header for resend packets. However, it's important to ensure that the isResend flag is reset to false when the packet is successfully sent, otherwise, it might cause issues in subsequent resends if the same packet object is reused.
                packet->isResend = true;
+                if (sendPacket(*packet)) {
+                    packet->isResend = false;
+                }
                addPacketToSend(*packet);
src/Nitwork/INitwork.hpp (1)
  • 56-63: The Packet class has been extended with a new boolean member isResend. Ensure that this new member is properly initialized in all places where a Packet object is created, and that it is correctly set to true when a packet is being resent. Also, verify that this change doesn't break any existing functionality that relies on the Packet class.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Details

Commits Files that changed from the base of the PR and between 084422c and c29f846.
Files selected for processing (1)
  • src/Nitwork/ANitwork.hpp (1 hunks)
Additional comments (Suppressed): 2
src/Nitwork/ANitwork.hpp (2)
  • 43-58: The new hunk introduces a check for packet.isResend before updating the packet ID and header. This is a logical change to prevent unnecessary updates for resend packets. However, it's important to ensure that the isResend flag is correctly set throughout the codebase, especially in the packet creation and resend logic. If not, this could lead to incorrect packet IDs and headers for resend packets.

  • 59-61: The async_send_to function is called with the same arguments as before. The changes in the hunk do not affect this part of the code.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Details

Commits Files that changed from the base of the PR and between c29f846 and 32246e5.
Files selected for processing (5)
  • src/Nitwork/ANitwork.cpp (1 hunks)
  • src/Nitwork/ANitwork.hpp (1 hunks)
  • src/Nitwork/INitwork.hpp (1 hunks)
  • src/Nitwork/Nitwork.h (1 hunks)
  • src/Nitwork/NitworkServer.cpp (1 hunks)
Files skipped from review due to trivial changes (2)
  • src/Nitwork/ANitwork.cpp
  • src/Nitwork/Nitwork.h
Additional comments (Suppressed): 5
src/Nitwork/NitworkServer.cpp (1)
  • 130-136: The new code introduces a check for the number of connected clients before sending the star wave message. This is a good practice to prevent unnecessary network traffic when there are not enough clients. However, ensure that this change does not affect other parts of the system that rely on the star wave message being sent regardless of the number of clients.
+        if (_endpoints.size() < MAX_CLIENTS) {
+            Logger::info("Not enough clients");
+            return;
+        }
src/Nitwork/ANitwork.hpp (2)
  • 43-58: The new code introduces a check for the isResend flag before updating the packet ID and header. This is a good optimization as it prevents unnecessary updates for resend packets. However, ensure that the isResend flag is correctly set in all places where a packet is being resent. Also, verify that the absence of ID and header updates for resend packets does not affect the functionality of the system.

  • 46-46: The use of std::any_cast can throw a bad_any_cast exception if the contained value in packet.body is not of type T. Ensure that the type of packet.body is always T when this line is executed, or add error handling for the bad_any_cast exception.

src/Nitwork/INitwork.hpp (2)
  • 56-60: The getIsResend and setIsResend methods have been added to the Packet class to manage the new _isResend member variable. This variable is used to indicate whether a packet is being resent. This is a good approach to avoid unnecessary updates for resend packets.

  • 68-68: The _isResend member variable is initialized to false. This is a good practice as it ensures that the variable is not left uninitialized, which could lead to undefined behavior.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Details

Commits Files that changed from the base of the PR and between 32246e5 and 591a295.
Files selected for processing (1)
  • src/Nitwork/NitworkServer.cpp (1 hunks)
Additional comments (Suppressed): 1
src/Nitwork/NitworkServer.cpp (1)
  • 133-136: The addition of a check for the number of connected clients before sending a star wave message is a good practice. It prevents unnecessary network traffic when there are not enough clients connected. However, ensure that this change does not affect the game logic or user experience negatively. For example, if a star wave message is critical for game progression, this change could potentially halt the game if there are not enough clients.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Details

Commits Files that changed from the base of the PR and between 591a295 and 8d0741d.
Files selected for processing (2)
  • src/Nitwork/ANitwork.hpp (1 hunks)
  • src/Nitwork/INitwork.hpp (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/Nitwork/INitwork.hpp
Additional comments (Suppressed): 1
src/Nitwork/ANitwork.hpp (1)
  • 43-58: The new code introduces a check for packet.getIsResend() before updating the packet ID and header. This is a good optimization as it prevents unnecessary updates for resend packets. However, ensure that the getIsResend() method is correctly implemented and that the isResend flag is properly set when a packet is being resent. Also, verify that this change does not affect the functionality of the system in any unexpected ways.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Details

Commits Files that changed from the base of the PR and between 8d0741d and 1372d77.
Files selected for processing (2)
  • docs/network/rfc/RFC.md (1 hunks)
  • src/Nitwork/ANitwork.hpp (1 hunks)
Additional comments (Suppressed): 4
docs/network/rfc/RFC.md (1)
  • 295-299: The change in the server's response to the client's ready action is significant. Ensure that the server-side logic has been updated to only send a start wave action when all clients are ready, and that this change is compatible with the client-side logic.
src/Nitwork/ANitwork.hpp (3)
  • 43-58: The new code introduces a check for packet.getIsResend(). If the packet is not a resend, it updates the packet ID and header. This is a good performance optimization as it avoids unnecessary operations for resending packets. However, ensure that the getIsResend() method is correctly implemented and the isResend flag is properly set when a packet is being resent.

  • 46-46: The use of std::any_cast can throw a std::bad_any_cast exception if the contained value in packet.body is not of type T. Ensure that the type of packet.body is always T when this function is called, or add error handling for the std::bad_any_cast exception.

  • 48-58: The code creates a new header_s struct and assigns it to data.header. This is fine if data.header is not used elsewhere in the code after this point. If it is, this could lead to unexpected behavior. Verify that this is the intended behavior.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Details

Commits Files that changed from the base of the PR and between 1372d77 and 6585df1.
Files selected for processing (9)
  • src/Nitwork/ANitwork.cpp (2 hunks)
  • src/Nitwork/ANitwork.hpp (3 hunks)
  • src/Nitwork/Nitwork.h (1 hunks)
  • src/Nitwork/NitworkClient.cpp (1 hunks)
  • src/Nitwork/NitworkClient.hpp (1 hunks)
  • src/Nitwork/NitworkServer.cpp (3 hunks)
  • src/Nitwork/NitworkServer.hpp (2 hunks)
  • src/main_client.cpp (1 hunks)
  • src/main_server.cpp (2 hunks)
Files skipped from review due to trivial changes (1)
  • src/Nitwork/Nitwork.h
Additional comments (Suppressed): 16
src/Nitwork/NitworkClient.cpp (1)
  • 25-28: The function signature of startClient has been changed. Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, verify that the order of arguments passed to ANitwork::start is correct.
src/main_client.cpp (1)
  • 64-70: The function signature of start() has been changed to startClient(). Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, the order of parameters has been changed. Verify that the new order of parameters is correctly used in all function calls.
-    .start(std::stoi(av[2]), DEFAULT_THREAD_NB, TICKS_PER_SECOND, av[1])
+    .startClient(std::stoi(av[2]), av[1], DEFAULT_THREAD_NB, TICKS_PER_SECOND)
src/Nitwork/ANitwork.cpp (2)
  • 188-193: The new code introduces a setIsResend(true) call before adding the packet to the send queue. This is a good performance optimization as it prevents unnecessary updates for resend packets. However, ensure that the setIsResend method correctly sets the isResend member variable and that this variable is checked before updating packet ID and header information.

  • 317-319: The function signature for addPacketToSend remains unchanged, but the context shows that a function getEndpointSender has been removed. Ensure that this function is not used elsewhere in the codebase, or that its functionality has been replaced appropriately.

src/Nitwork/ANitwork.hpp (4)
  • 29-29: The stop method has been moved from public to protected. Ensure that this change does not break any code that calls this method from outside the class.

  • 33-33: The start method has been moved from public to protected and the override keyword has been removed. Ensure that this change does not break any code that calls this method from outside the class or relies on it being an override of a base class method.

  • 48-60: The new code checks if the packet is a resend before updating the packet ID and header. This is a good performance optimization as it avoids unnecessary updates for resend packets.

  • 79-83: The getEndpointSender method has been removed. Ensure that this change does not break any code that calls this method.

src/Nitwork/NitworkServer.hpp (2)
  • 24-28: The function signature for startServer has been changed. Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, the ip parameter has been removed and a new parameter nbPlayer has been added. Verify if this change is intentional and if it doesn't break any existing functionality.

  • 87-87: A new member variable _maxNbPlayer has been added. This seems to be used for limiting the number of players. Ensure that this variable is being properly initialized and used where necessary.

src/Nitwork/NitworkClient.hpp (1)
  • 24-29: The function signature for start has been changed to startClient and the order of parameters has been modified. Ensure that all calls to this function throughout the codebase have been updated to match the new signature and parameter order.
-            bool start(
-                 int port,
-                 int threadNb          = DEFAULT_THREAD_NB,
-                int tick              = TICKS_PER_SECOND,
-                const std::string &ip = "") final;
+            bool startClient(
+                int port,
+                const std::string &ip,
+                int threadNb          = DEFAULT_THREAD_NB,
+                int tick              = TICKS_PER_SECOND);
src/Nitwork/NitworkServer.cpp (3)
  • 25-33: The function signature for startServer has been changed and a new parameter nbPlayer has been added. Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, the IP parameter has been removed, verify if this change doesn't affect the functionality.

  • 116-122: The maximum number of clients (MAX_CLIENTS) has been replaced with _maxNbPlayer. This change allows for a dynamic number of maximum players, which can be set when starting the server. Ensure that _maxNbPlayer is always properly set before this function is called to prevent unexpected behavior.

  • 135-144: The server now checks if the number of connected clients is less than _maxNbPlayer before sending a star wave message. If there are not enough clients, the function returns early. This change ensures that the game starts only when all clients are ready. Make sure this change doesn't affect other parts of the code that rely on the star wave message.

src/main_server.cpp (2)
  • 31-48: The checkArgs function now checks for two arguments instead of one, and the usage message has been updated accordingly. The function now uses a vector to store the arguments and checks if each argument is a number using the isNumber function. It also checks if the first argument is a valid port number and if the second argument is greater than 0. This is a good approach, but the error messages could be more specific to help users understand what went wrong. For example, the function could specify which argument is invalid and what the valid range for each argument is.
-    Logger::error("Invalid argument");
+    Logger::error("Invalid argument: " + arg + ". Both arguments must be numbers.");

-    Logger::error("Invalid port or playerNb");
+    Logger::error("Invalid port or player number. Port must be between " + std::to_string(PORT_MIN) + " and " + std::to_string(PORT_MAX) + ". Player number must be greater than 0.");
  • 61-63: The startServer function now takes two arguments instead of one. The first argument is the port number, and the second argument is the number of players. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Details

Commits Files that changed from the base of the PR and between 6585df1 and dd7b3af.
Files selected for processing (4)
  • src/Nitwork/ANitwork.hpp (3 hunks)
  • src/Nitwork/NitworkClient.hpp (1 hunks)
  • src/Nitwork/NitworkServer.cpp (3 hunks)
  • src/Nitwork/NitworkServer.hpp (2 hunks)
Additional comments (Suppressed): 9
src/Nitwork/NitworkClient.hpp (1)
  • 22-29: The start function has been renamed to startClient and the order of parameters has been changed. The ip parameter is now required and is no longer the last parameter. Ensure that all calls to this function throughout the codebase have been updated to match the new signature and parameter order.
src/Nitwork/ANitwork.hpp (3)
  • 34-34: The start function has been marked as final, which means it cannot be overridden in derived classes. Ensure that this change does not affect any subclasses that were previously overriding this function.

  • 50-61: The sendData function has been modified to update the packet ID and header only if the packet is not a resend. This is a good approach to avoid unnecessary operations for resending packets. However, ensure that the isResend flag is properly managed in the code where packets are being sent.

  • 79-85: The getEndpointSender function has been removed. If this function was used elsewhere in the codebase, those references will need to be updated or removed.

src/Nitwork/NitworkServer.hpp (2)
  • 24-28: The function signature for startServer has been changed. The ip parameter has been removed and a new nbPlayer parameter has been added. Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, verify that the removal of the ip parameter does not affect the functionality of the server.

  • 87-87: A new member variable _maxNbPlayer has been introduced. This variable is used to store the maximum number of players that can connect to the server. Ensure that this variable is properly initialized and updated throughout the codebase.

src/Nitwork/NitworkServer.cpp (3)
  • 25-29: The function signature of startServer has been changed. Ensure that all calls to this function throughout the codebase have been updated to match the new signature. Also, the ip parameter has been removed and replaced with nbPlayer. Verify that this change does not affect the functionality of the server.

  • 112-118: The check for the maximum number of clients has been updated to use the _maxNbPlayer variable instead of the MAX_CLIENTS constant. This change allows for a dynamic maximum number of players. However, ensure that _maxNbPlayer is always properly initialized before this check to avoid unexpected behavior.

  • 131-137: A new condition has been added to check if the number of connected clients is less than _maxNbPlayer. This change seems to be in line with the new feature of having a dynamic maximum number of players. However, ensure that this condition does not interfere with the expected flow of the program.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Details

Commits Files that changed from the base of the PR and between dd7b3af and 91d58ff.
Files selected for processing (2)
  • src/main_client.cpp (3 hunks)
  • src/main_server.cpp (2 hunks)
Additional comments (Suppressed): 7
src/main_server.cpp (2)
  • 21-24: The isNumber function is a good addition to validate if a string is a number. It uses the std::all_of function from the STL and the ::isdigit function to check if all characters in the string are digits.

  • 56-56: The startServer function now takes an additional argument for the number of players. Ensure that all calls to this function throughout the codebase have been updated to match the new signature.

src/main_client.cpp (5)
  • 8-10: The inclusion of the header files "Logger.hpp", "NitworkClient.hpp", and "Registry.hpp" remains unchanged. No issues found.

  • 16-19: The isNumber function is a new addition. It checks if a string is a number by checking if all characters are digits. This is a more efficient and safer way to validate numeric strings compared to the previous method.

  • 21-37: The checkArgs function has been updated to use the isNumber function for port validation. This is a more efficient and safer way to validate the port number. The function now also checks if the IP address is empty, which is a good addition for error handling.

  • 39-42: The main function signature has been updated to use const char **av instead of char **av. This is a good practice as it prevents accidental modification of the command-line arguments.

  • 47-53: The start method of NitworkClient has been renamed to startClient and now takes the IP address as a separate parameter. Ensure that all calls to this method throughout the codebase have been updated to match the new signature.

@Saverio976 Saverio976 merged commit 24a2884 into dev Oct 15, 2023
@Saverio976 Saverio976 deleted the fix/Nitwork-dont-change-resent-packet-id branch October 17, 2023 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants