[SOLVED]Networking (buffer)

Discussion in 'Programming' started by Andrea N, Apr 11, 2018.

  1. Andrea N

    Andrea N Member

    Joined:
    Apr 4, 2018
    Posts:
    37
    Hello,
    i'm trying to make a multiplayer online game with server and client.
    I wached some youtube tutorial and it's all ok, all works fine, the data that i send from the client to the server is correct.
    I added some changes, i added some variables to sent from the client to the server using buffer, but when i received the informations from the server, the variables have wrong values.

    I copy here my example code, the "Variable" are obviously all differents (this from the client):

    //update coordinates
    buffer_seek(global.buffer, buffer_seek_start, 0);
    buffer_write(global.buffer, buffer_f64, 7);
    buffer_write(global.buffer, buffer_string, playerUsername);
    buffer_write(global.buffer, buffer_u32, Variable);
    buffer_write(global.buffer, buffer_f32, Variable);
    buffer_write(global.buffer, buffer_f32, Variable);
    ...
    ....
    ....
    many lines later
    network_send_packet(obj_controller.socket, global.buffer, scr_getBufferSize());

    And this form the server:

    case 7:
    var playerUsername= buffer_read(buffer, buffer_string);
    var Variable= buffer_read(buffer, buffer_u32);
    var Variable= buffer_read(buffer, buffer_f32);
    var Variable= buffer_read(buffer,buffer_f32);
    var Variable= buffer_read(buffer, buffer_f32);
    var Variable= buffer_read(buffer, buffer_f32);
    ....
    ....
    .....
    many lines later
    if (file_exists(playerUsername + ".ini"))
    {
    // Update player data
    ini_open(playerUsername + ".ini");
    ini_write_real("a", "aa", Varible);
    ini_write_real("a", "ab", Varible);
    ...
    ....
    ....
    many lines later
    }
    ini_close();
    break;


    The server so receives the varibles from the client. I save them to a .ini file! But i have a problem, i have about 26 variables to send and for the last 9 i receive wrong information!
    What's the problem?
    Have i to use different buffers?
    Am I sending too much information at the same time?
    Can someone explain to me why i receive wrong informations?
    For wrong informations i mean that i send (for example), playerlife=50, and i receive a casual number.

    I hope I explained myself.
    Thanks all for the answers.
     
  2. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,469
    Most likely you are reading something not in the same order (or with a different data type) than how you have written it, and thus the reading position becomes off and mixes up the rest.

    I also have question as to why you are writing your message ID (?) as a 64-bit floating-point value (in other words, as something designed to have precise fractions or big magnitudes)
     
  3. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,522
    If you use tcp you may send too much info so the protocol split the send and you receive a parts and it mess up the read. Try send in two different messages.

    Also check so the buffer writing and reading correspond.

    You may try write a f32 or a string value with u32 or u8 buffer write. Ex 1.1 to a u8 buffer will give a strange value on reading.

    You maybe use the f16 that is not supported.

    You may have a antivirus that mess up the travel from kernel to the game.

    It may also be that the bug in f and u64 bit buffer is back again.
     
  4. PNelly

    PNelly Member

    Joined:
    Jun 20, 2016
    Posts:
    256
    I believe this is only an issue when using the raw* networking functions
     
  5. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,522
    Sorry no. I tested it locally from GM to GM with the network send (not the raw* ones) function and it has the same "issue". TCP is a stream protocol (it can split whenever it want).
     
  6. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,469
    TCP is a stream protocol but GMS packet header includes a length field and it accumulates (or at least supposed to) until there's enough data to process an individual packet.
     
  7. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,522
    That is the problem :)
    Tested in both GMS1.4 and 2 and the same bug exists. It's hard to reproduce so a bug report is no use. But the splited buffers with GMs header info in the middle of them tell a sad story.
    UDP work fine however.
     
  8. Andrea N

    Andrea N Member

    Joined:
    Apr 4, 2018
    Posts:
    37
    Now i check the order but i think i ordered the varibles the right way. I understood that i need to use the same data type for the client and for the server! I used 64bitfloating value because i tried all the data type for all the variables (
    I did not understand very well how much memory the variables need).

    Now i put an image of my real code (to the left the server and to the right the client).
    An image that show my variables when i'm playing, and my ini file.

    The variables room, x and y are ok, the others have all casual values, i don't understand where i'm wrong.

    Another quest, i use tile set and i see the square, (look the client_player image), how can i remove them?
    Thanks for the attention.
     

    Attached Files:

  9. Andrea N

    Andrea N Member

    Joined:
    Apr 4, 2018
    Posts:
    37
    Yes i'm using tcp, i followed this tutorial:
    Do you think UDP works better? (obviously if I didn't make mistakes myself).
     
  10. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,522
    The server try to use the wrong buffers.

    buffer_u8 can only hold a real number from 0 to 255.
    var my_number=35; // OK
    var my_next_number=290; // SUPER FAIL
    var my_3_number=-34; // MEGA SUPER FAIL

    So if you try <0 or 255< it wont store it correctly.
    Ex if you collect 256 money you will get a strange number instead. Because 256 is bigger that 255 (max of a buffer_u8)
    You need to keep track how big the number you send.

    On the server use show_debug_message(sprite_index) and print out the rest of the values and you see what I mean.

    Also note that sprite_index is a dynamic value and will change if you add, remove or rearrange the source tree in GM. So if you save the sprite_index and then you add a sprite in your project and then load the sprite_index it may point to a different sprite. Save the sprite name instead.
     
    Last edited: Apr 13, 2018
  11. Andrea N

    Andrea N Member

    Joined:
    Apr 4, 2018
    Posts:
    37
    Sorry i don't understand how to use show_debug_message(). I tried to send the information one by one to understand what caused the first problem, and it's the varible player stamina (for now)! When i'm playing i draw the variable and the value of stamina is 100 so i think buffer_u32 is ok.
    Now i try to send only variable "stamina" and watch what happens.
    If you have any questions about the other variables iniside the .ini file, them were written first (during the login).
    I'm stuck here!! Grrr
    Thanks.
     

    Attached Files:

  12. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,522
    Ex on the server you do this:
    Code:
    buffer_write(global.buffer, buffer_u8, sprite_index);
    
    Check the value with:
    Code:
    buffer_write(global.buffer, buffer_u8, sprite_index);
    Show_debug_message("sprite_index:" + string(sprite_index))
    
    My guess is that you use the wrong buffer type for some of the values. All values that come to the client as wrong use wrong buffer type.
     
  13. Andrea N

    Andrea N Member

    Joined:
    Apr 4, 2018
    Posts:
    37
    Now i tried with the varibale "experience" and the value is the same of the varible "stamina" i used show_debug_message.
     

    Attached Files:

  14. Andrea N

    Andrea N Member

    Joined:
    Apr 4, 2018
    Posts:
    37
    Oh sorry i used show debug message on client!!
     
  15. Andrea N

    Andrea N Member

    Joined:
    Apr 4, 2018
    Posts:
    37
    Done
    Done! And the value is as the ini file!
     

    Attached Files:

  16. The-any-Key

    The-any-Key Member

    Joined:
    Feb 2, 2017
    Posts:
    1,522
    Update GM. You run 1.4.1567 and there is some bugs in the buffers writer in that one.
     
  17. Andrea N

    Andrea N Member

    Joined:
    Apr 4, 2018
    Posts:
    37
    Solved with the update! Thanks :D
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice