• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Buffer wasn't recieved

marasovec

Member
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;
}
 
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?
 

marasovec

Member
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?
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
 
J

jaydee

Guest
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;
 }
}
 

marasovec

Member
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;
 }
}
It works! Thanks for the code.
 
Last edited:

PNelly

Member
It works! Thanks for the code.
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:

marasovec

Member
Thanks for the code but it says "Attempting to read from outside the buffer, returning 0"
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 you're life will be a little easier if you stay away from those.
Thanks for a tip. I'll try it :)
 
Top