Network Class and Portability

Hi everyone;
I think the Network module is done. Now I am just testing and making little changes. I am going to write some documentation soon on the wiki of the google code page.

One problem I got when developing the Network module was about the synchronization between the server and the clients. I am using C POSIX sockets and I had to know when my recv() should stop.
The idea was to send a little header before each message with its number of bytes. Another problem is portability. Unfortunately, different computeres can use different ways to represent a multibyte integer. So, if I use send() to send an integer from my Athlon 64 bits running ArchLinux to an Intel 32 bits running Windows, the number could be interpreted in a different way. To solve that, I am using network byte order. It is basically a common format of bytes. Each operational system should have functions to convert between network order to the system order.

For Linux and BSD you can use: (For Windows, you need to use another header, but the functions are the same)

#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);


And a nice way to remeber is:
htons() host to network short
htonl() host to network long
ntohs() network to host short
ntohl() network to host long


Now I am working on Core, which will manipulate the players. Borges is working on some nice models and a map to test. We hope to release an alpha version of the game soon.

3 comments:

  1. Have you ever considerated using Boost.Asio or other libs like ACE (http://www.cs.wustl.edu/~schmidt/ACE.html) or POCO (http://pocoproject.org/) instead worrying about size of integers?

    ReplyDelete
  2. Yes Marcos, I have. But, I am having fun and learning a lot making my own classes. And I am sure they will be as useful as other libs. Anyway, thanks for your advice ;)

    ReplyDelete