RandyGaul / cute_headers

Collection of cross-platform one-file C/C++ libraries with no dependencies, primarily used for games
4.21k stars 264 forks source link

[Cute_Net.h] Knowing if a packet has been sent. #367

Open SaySayTakamura opened 8 months ago

SaySayTakamura commented 8 months ago

Hello there, my deepest apologies for the bothering, but i came to ask something and maybe suggest something.

I am quite new to this network thing, my apologies if i say or do something wrong.

I walked through the examples and after a while messing with them i went to create a class for both Server and Client and implement on my Debugger project.

Everything was fine until i went to test the client class. It works, but there's something wrong.

Look at this:

Client c;
c.Create(); //Default arguments are (IP: 127.0.0.1 | Port: XXXX) which is Localhost

bool done = false;

while(!done)
{
     c.Process(); //Poll events and updates the client.
     if(c.IsConnected())
     {
       //Following code taken from example
       std::string s = "Still alive";
       c.Send(s); //This functions gets the size of the string and has one version for each type i want to send (such as float, json and some others)

       c.Shutdown();
       done = true;
     }
}
printf("Goodbye <3"); //Shows that the program came to and end.

Ok, the concept for this code is, Create a client, connects to the localhost, sends an string to the server, then disconnect and then end the program.

Now let's go to the server output:

"New client with id (CLIENT ID), connected on index 0"
"Client disconnected on index 0"

Where is the message is the client send?

Problem is, i call to c.Shutdown() and done = true right after "sending" the message, which cancels the next call of c.Process() and with that the message i just sent didn't get processed.

Took me a while to find this out (yup, dumb me), but still something like:

bool cn_client_packet_pending(client) or bool cn_client_packet_send(client) would be very useful.

Example:

if(cn_client_packet_pending(client) == false)
{
   //Process code go here (c.Process, in my case)
}
else
{
  //Disconnect and close loop to end application
}

The second one would be quite harder to implement, since it would need a server response i suppose, but the first one i think it would be easier to do, i would implement it myself but i don't know how this would work and the nuances behind it.

Well, i think this is it, my deep apologies for the bothering and thanks for reading.

Have a great night.

RandyGaul commented 8 months ago

Hi there! Thanks for the suggestion. To clarify, you're mainly asking for a way to see if the "send queue" for your client is actually empty or not? This would be a very easy thing to add.

SaySayTakamura commented 8 months ago

Hi there! Thanks for the suggestion. To clarify, you're mainly asking for a way to see if the "send queue" for your client is actually empty or not? This would be a very easy thing to add.

Oh yeah, exactly what i had in mind, i remember seeing the packet_queue member on the client struct.

But i didn't know if what i have in mind is going to work:

//Returns true if there's anything pending, false when there's nothing yet to be sent
bool cn_client_packet_pending(client)
{
   cn_client_t c = client;
   return ((c.p_client.packet_queue.size_left == c.p_client.packet_queue.capacity-1) ? true : false)
}

In fact imma give it a try once i get home.

Also: my deepest apologies for the bothering and the delayed response (went to sleep after creating the issue)

Update: went to check the buffer size with Capacity and Size_Left members of the circular_buffer they don't change (this also goes for sizeof(), which was my first idea)

Output:

//First call
Buffer Size - 1048576, Size Left - 1048576, Sizeof - 32

Client sends message (c.Send())

//Second call
Buffer Size - 1048576, Size Left - 1048576, Sizeof - 32

Disconnect (c.Shutdown())
Application Close(done = true)
SaySayTakamura commented 7 months ago

Not exactly what i had in mind but it does the job:

if (c.IsConnected)
{
     std::string dt = "Hello There";
     static float i = 0;

      i += ElapsedTime(); //Directly calls ct_time()
      if(i > 0.5) //Default : 2
      {
            printf("Sending");
            c.send(dt);

             i = 0;
             done = true; // Closes the application.
      }
}

This code works but with certain delay between sends, so i need to adjust this for this to perform better.

Note: it's worth to mention that i took this snippet from the example, just out of nowhere haha.