Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

876895 Posts in 32835 Topics- by 24277 Members - Latest Member: aetherX

May 18, 2013, 02:12:40 AM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Byte Issue
Pages: [1]
Print
Author Topic: Byte Issue  (Read 397 times)
Maud'Dib Atreides
Level 4
****


Obsessed with space


View Profile WWW Email
« on: June 13, 2012, 06:08:32 PM »

Ok, this is rather embarrassing to ask as it's really newblike, but I think it's necessary to ask it here.

I chose Unity as the icon for this topic because I really couldn't find a C# icon.

This is C#.

I'm sending data from a client and server.

My original plan was to send this:

A 10 character string.

IIBBBBBBBB
II represents [Player ID Ranging from 0-99]
The B's represent 0 or 1.

But I was thinking about optimizing to send it as:

2 Byte Array

Because the player ID can be converted to a byte since it can't exceed 100 and will never reach 256

And since the B's are either 0 or 1, and there's eight of them, then it's 2^8 which is 256 possible values for B, which can be translated to a byte

I was reading that in C# a string takes 20 bytes as an object and 2 bytes for each character.

This means that I'd be rocketing 40 bytes each second between client and server.

I've read that calculating bytes for string data is fairly complicated, but would this be accurate? If this is correct, I'd be more than happy to translate 40 bytes to a 2 byte array.
« Last Edit: June 13, 2012, 06:15:17 PM by # » Logged

Guy: Give me all of your money.
Chap: You can't talk to me that way, I'M BRITISH!
Guy: Well, You can't talk to me that way, I'm brutish.
Chap: Somebody help me, I'm about to lose 300 pounds!
Guy: Why's that a bad thing?
Chap: I'M BRITISH.
Danmark
Level 7
**



View Profile
« Reply #1 on: June 13, 2012, 07:49:37 PM »

While it's generally a good practice to minimize the amount of data sent over a network, you have to consider the savings against the packet overhead. Supposedly Asynchronous Transfer Mode, with its fixed-size payloads, is still used over many Internet links, so you're not necessarily saving anything at all. You're really splitting hairs worrying about a small payload sent once per second.

In any case, what you're trying to do is trivial. Read up on bitwise operators.
Logged
rivon
Level 10
*****



View Profile
« Reply #2 on: June 14, 2012, 12:30:51 AM »

If you're sending the stuff only once per second then it's definitely not needed. Just send the string.
Logged
Laurent C
Level 0
**


View Profile
« Reply #3 on: June 15, 2012, 09:04:39 AM »


Are you using 8 booleans for player ID?

Anyway, here's a quick look at what your packets should look like.
Let's say you're sending an ascii string (8-bit). You can either send length before the string or place a zero at the end.

The binary for sending "TEST" should be either:
- 04 54 45 53 54 (Length before string)
- 54 45 53 54 00 (Zero after string)


You may also want different operation codes. Since most games hardly ever use more than 256 we can just use a single byte for the enum.

Let's say player "Player1" joins the game.
(01) (00) (07) (50 6C 61 79 65 72 31)

I am using parenthesis for each variable we're sending.
First we have an operation code, I chose 0x01 to be for player joining.
Second we have player ID, in this case player 0.
third we have our string length.
fourth we have our string "Player1".


Now that our player joined. It's much easier to deal with player ID than string because:
- You don't have to search for player name each time.
- Much less data is sent each time.
- Player name may change during gameplay. (Optional)
- Player name is unknown. Server sent other packets concerning that player before sending the name.
- 2 or more players have the same name.

Everything I listed above does not affect performance much but can lead to some game breaking bugs.


I hope it helps you understand how strings are sent between clients and server.

Quote
I was reading that in C# a string takes 20 bytes as an object and 2 bytes for each character.
The 20 bytes are used to define where the string is stored in memory and some other information not required by data streams.
Logged
Maud'Dib Atreides
Level 4
****


Obsessed with space


View Profile WWW Email
« Reply #4 on: June 15, 2012, 09:52:21 AM »


Are you using 8 booleans for player ID?

Anyway, here's a quick look at what your packets should look like.
Let's say you're sending an ascii string (8-bit). You can either send length before the string or place a zero at the end.

The binary for sending "TEST" should be either:
- 04 54 45 53 54 (Length before string)
- 54 45 53 54 00 (Zero after string)


You may also want different operation codes. Since most games hardly ever use more than 256 we can just use a single byte for the enum.

Let's say player "Player1" joins the game.
(01) (00) (07) (50 6C 61 79 65 72 31)

I am using parenthesis for each variable we're sending.
First we have an operation code, I chose 0x01 to be for player joining.
Second we have player ID, in this case player 0.
third we have our string length.
fourth we have our string "Player1".


Now that our player joined. It's much easier to deal with player ID than string because:
- You don't have to search for player name each time.
- Much less data is sent each time.
- Player name may change during gameplay. (Optional)
- Player name is unknown. Server sent other packets concerning that player before sending the name.
- 2 or more players have the same name.

Everything I listed above does not affect performance much but can lead to some game breaking bugs.


I hope it helps you understand how strings are sent between clients and server.

Quote
I was reading that in C# a string takes 20 bytes as an object and 2 bytes for each character.
The 20 bytes are used to define where the string is stored in memory and some other information not required by data streams.

No no I have the data transfers functional.. I'm using Lidgren.Network, so things are pretty functional header and footer wise ( I really don't even use string lengths and so forth, it prevents me from getting my hands dirty).

The real question was about the necessity of optimizing data for sending to clients.

I've tried what everyone said about not worrying about things, the game runs fairly decent, latency is small.

Thanks
Logged

Guy: Give me all of your money.
Chap: You can't talk to me that way, I'M BRITISH!
Guy: Well, You can't talk to me that way, I'm brutish.
Chap: Somebody help me, I'm about to lose 300 pounds!
Guy: Why's that a bad thing?
Chap: I'M BRITISH.
Laurent C
Level 0
**


View Profile
« Reply #5 on: June 15, 2012, 05:16:04 PM »

You don't need to optimize packets until you're noticing slow down really.

My point was to deal directly with player ID instead of sending player name for every action. It's more for clean code than optimization. The player name is only a propriety.

You don't need to make a stream class from scratch but understanding what's going on will help you estimate the bandwidth required for a full server and fix ahead of time instead of having to rebuild.
Logged
Maud'Dib Atreides
Level 4
****


Obsessed with space


View Profile WWW Email
« Reply #6 on: June 15, 2012, 05:36:16 PM »

You don't need to optimize packets until you're noticing slow down really.

My point was to deal directly with player ID instead of sending player name for every action. It's more for clean code than optimization. The player name is only a propriety.

You don't need to make a stream class from scratch but understanding what's going on will help you estimate the bandwidth required for a full server and fix ahead of time instead of having to rebuild.



>.> You are not reading my comments in their entirety... agh

I was already working with player ID's instead of player names....

Quote
IIBBBBBBBB
II represents [Player ID Ranging from 0-99]

Thanks for your help anyways, but this was solved a while back...
Logged

Guy: Give me all of your money.
Chap: You can't talk to me that way, I'M BRITISH!
Guy: Well, You can't talk to me that way, I'm brutish.
Chap: Somebody help me, I'm about to lose 300 pounds!
Guy: Why's that a bad thing?
Chap: I'M BRITISH.
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic