Buffer wasn't recieved

Discussion in 'Programming' started by marasovec, Apr 1, 2018.

  1. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    343
    The first buffer MSG_MOVE works but MSG_INFO does not. The weird thing is the non working buffer is made from the working buffer. There are many buffers like these two but MSG_INFO is the only one that doesn't work.

    CLIENT:

    send_buffer = buffer_create(64, buffer_fixed, 1);
    buffer_seek(send_buffer, buffer_seek_start, 0);
    buffer_write(send_buffer, buffer_u8, MSG_MOVE);
    buffer_write(send_buffer, buffer_u16, round(obj_player.x));
    buffer_write(send_buffer, buffer_u16, round(obj_player.y));
    buffer_write(send_buffer, buffer_u16, obj_player.visible);
    buffer_write(send_buffer, buffer_string, obj_client.name);

    network_send_raw(global.socket, send_buffer, buffer_tell(send_buffer));

    send_buffer = buffer_create(64, buffer_fixed, 1);
    buffer_seek(send_buffer, buffer_seek_start, 0);
    buffer_write(send_buffer, buffer_u8, MSG_INFO);
    buffer_write(send_buffer, buffer_string, "level1");

    network_send_raw(global.socket, send_buffer, buffer_tell(send_buffer));

    SERVER:

    message_id = buffer_read(buffer, buffer_u8);
    switch(message_id)
    {
    case MSG_MOVE:

    var xx = buffer_read(buffer, buffer_u16);
    var yy = buffer_read(buffer, buffer_u16);
    var vis = buffer_read(buffer, buffer_u16);
    var name = buffer_read(buffer, buffer_string);
    break;

    case MSG_INFO:

    var level = buffer_read(buffer, buffer_string);
    obj_sync.current_level = level;
    show_message("recieved");
    break;
    }
     
  2. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,240
    What are the values of MSG_MOVE and MSG_INFO? Are they unsigned integers? Are they the same on both the server and client?

    I presume the show_message("recieved") never happens? Are you totally positive the other messages are being handled correctly?
     
  3. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    343
    MSG_MOVE = 4, MSG_INFO = 11, they are not unsigned, they are the same on client and server.
    When I put the message_show into MSG_MOVE it will show the "recieved" sign but when I put the message_show into MSG_INFO nothing happends
     
  4. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,240
    Are you sure the MSG_INFO message is being sent in the first place? Better put a show_message("sent") in that event.
     
  5. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    343
    I already did that. It always show the message.
     
  6. jaydee

    jaydee Member

    Joined:
    Dec 27, 2016
    Posts:
    75
    Since this TCP, it is stream based - not datagram based. This means that when receiving:
    a) Packets that were sent separately on the senders side may be concated on the receiving side.
    b) Packets that are sent as a whole on the senders side may take multiple read calls on the receiving side.

    My first guess is you are doing nothing to handle either of these cases, and your MSG_INFO packet is being concated together with the MSG_MOVE packet (or another packet). This is a likely cause of the error if they are being sent at the same time.

    Try this solution when receiving packets: (Note this doesn't handle case b, only case a)
    Code:
    // Get length of buffer
    buffer_seek(buffer, buffer_seek_end, 0);
    var buffer_size = buffer_tell(buffer);
    
    // Seek to start of buffer
    buffer_seek(buffer, buffer_seek_start, 0);
    
    // Loop until buffer has been read in its entirety
    while(buffer_tell(buffer) < buffer_size)
    {
     // Read packet
     var msg = buffer_read(buffer, buffer_u8);
     switch(msg)
     {
      case MSG_MOVE:
       //
       break;
      case MSG_INFO:
       //
       break;
      default:
       // Skip to end of buffer to break loop to prevent infinite loop after receiving unexpected data
       buffer_seek(buffer, buffer_seek_end, 0);
       break;
     }
    }
     
    PNelly likes this.
  7. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    343
    It works! Thanks for the code.
     
    Last edited: Apr 1, 2018
  8. PNelly

    PNelly Member

    Joined:
    Jun 20, 2016
    Posts:
    256
    You can avoid both of the pitfalls pointed out by @jaydee by not using the *raw networking functions, which are primarily needed only when you need to talk to another program that's not made wtih GMS. If both your server and client are built in GM your life will be a little easier if you stay away from those.
     
    Last edited: Apr 1, 2018
  9. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    343
    Thanks for a tip. I'll try it :)
     

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