Original Post
hey,
as in title is it possible to send a dynamic array through sockets with WinSock2 ?
as in title is it possible to send a dynamic array through sockets with WinSock2 ?
im interested in c++
ok so is there any way to send a dynamic array in the struct i'm sending ?
Yes, use a serialization library .
[/quote]
is boost a good library for this ?
btw currently i'm using a static array to send the players data from server to clients:
struct data
{
Player array[50];
}
But this will send an array with 50 elements even if less people are logged in the game.
That's why i want to try and put a dynamic array, but i read that serialization greatly increases
the size of the struct so at the end is it worth it ?
is boost a good library for this ?
btw currently i'm using a static array to send the players data from server to clients:
struct data
{
Player array[50];
}
But this will send an array with 50 elements even if less people are logged in the game.
That's why i want to try and put a dynamic array, but i read that serialization greatly increases
the size of the struct so at the end is it worth it ?
[/quote]
Where did you read that?
btw currently i'm using a static array to send the players data from server to clients:
struct data
{
Player array[50];
}
But this will send an array with 50 elements even if less people are logged in the game.
That's why i want to try and put a dynamic array, but i read that serialization greatly increases
the size of the struct so at the end is it worth it ?
[/quote]
Where did you read that?
[/quote]
on some random forums.. so serialization doesn't increase the size of the struct ?
Data myData;
send( &myData, sizeof(Data) );//send the whole struct
///.... on the other end ....
Data myData;
receive( &myData, sizeof(Data) );//read the whole structstruct Data { int numPlayers; Player array[50]; }
Data myData;
myData.numPlayers = 42;
send( &myData.numPlayers, sizeof(int) );//send the length so the other end knows how much of the array to expect
send( &myData.array[0], sizeof(Player)*myData.numPlayers );//only send the part of the array that matters
///.... on the other end ....
Data myData;
receive( &myData.numPlayers, sizeof(int) );//read the length in first
receive( &myData.array[0], sizeof(Player)*myData.numPlayers );//now we know how much data to read
... so serialization doesn't increase the size of the struct ?
[/quote]
Forget about structs. What you need to worry about is the amount of data on the wire. The data on the wire is a serialised form of the data in memory. Directly sending a struct is an extremely basic form of serialisation - one where the memory and serialised representations are identical. Though basic, It is not the most efficient.
A structure will often contain "padding bytes". These bytes are placed so that data can be efficiently accessed by the processor (or in some architectures, so that it can be accessed without triggering an error!). It is not necessary to send such padding on the wire. Another example is data ranges. Say you have a Player structure with a health member variable of type "int". On common toolchains, you will have a four byte variable. However, your game might only ever have health values in the range 0 to 100. Thus, you could efficiently encode the health in a single byte.
Serialisation can go beyond just bytes. You can serialise boolean values to individual bits. The aforementioned health value needs only 7 bits to encode the full range.
Finally, one you get out of the mentality of "sending" a "struct" to "serialise to a byte stream and send that", you can easily add compression as part of the serialisation process, which might save more space depending on the nature of the data.
Combining these, a hand rolled serialisation scheme will almost certainly be as space efficient, if not more so, than directly sending the source structure. A serialisation library, one with space efficiency as a design goal, should also be able to compete with the source structures on size.
... i read that serialization greatly increases the size of the struct so at the end is it worth it ?
[/quote]
Where did you read that?
[/quote]
on some random forums...
[/quote]
What you read on "some random forums" was probably referring to generalised and/or automatic serialisation. Such tools may not designed for space optimisation. For example, they may not allow you to express domain specific knowledge such as the range of a "health" variable, etc.
They have different design goals. For example they may not require you to list all possible messages/serialised forms up front. They may support deserialising more than one "version" of the data.
What you read on "some random forums" was probably referring to generalised and/or automatic serialisation. Such tools may not designed for space optimisation. For example, they may not allow you to express domain specific knowledge such as the range of a "health" variable, etc.
They may not support bit level access though. I'm not aware of a library that supports that.
[/quote]
I was thinking of Raknet, which, while not a standalone serialisation library, provides related functionality through its BitStream class.
Actually, if we are recommending libraries to the OP, Raknet is a reasonably popular networking library that solves these problems in a high level way.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more