Skip to main content
deleted 9 characters in body
Source Link
  1. TRANSPORT. This is a module that takes care of reading data from network. Transport is for example TCP or UDP, but it might be also serial or something else. You should have functions in this module to connect, read, write and disconnect.

  2. PROTOCOL. This is a way in which you are parsing data (not your game logic data yet, just whole messages). In example you can have "sign delimeter" protocol, which reads data from transport and creates messages from it by splitting data where "delimeter sign" occurs.

    123%456%789%

TRANSPORT. This is a module that takes care of reading data from network. Transport is for example TCP or UDP, but it might be also serial or something else. You should have functions in this module to connect, read, write and disconnect.

PROTOCOL. This is a way in which you are parsing data (not your game logic data yet, just whole messages). In example you can have "sign delimeter" protocol, which reads data from transport and creates messages from it by splitting data where "delimeter sign" occurs.

123%456%789%

this data read from transport would be converted to 3 messages [123,456, 789] if your delimeter is %. Special case of this protocol is "line receiver" where delimeter sign is "\n".

  1. PARSER. Messages that you get from protocol should be parsed. Parsing is based on some type of identifier sent in message. I have separated ID into type and subtype. My ID is 2 bytes long, and I store type id in 4 first bits, and subtype id in rest 12 bits. How to do this you have to read elsewhere. This way your switch (ifs) is more optimized. Store these identifiers at the top of parser module. In example:

     TYPE_SYSTEM = 1
     SUBTYPE_LOGIN_REQUEST = 1
    
     if type == TYPE_SYSTEM:
         if subtype == SUBTYPE_LOGIN_REQUEST:
             do_something()
    

PARSER. Messages that you get from protocol should be parsed. Parsing is based on some type of identifier sent in message. I have separated ID into type and subtype. My ID is 2 bytes long, and I store type id in 4 first bits, and subtype id in rest 12 bits. How to do this you have to read elsewhere. This way your switch (ifs) is more optimized. Store these identifiers at the top of parser module. In example:

    TYPE_SYSTEM = 1
    SUBTYPE_LOGIN_REQUEST = 1

    if type == TYPE_SYSTEM:
        if subtype == SUBTYPE_LOGIN_REQUEST:
            do_something()
  1. TRANSPORT. This is a module that takes care of reading data from network. Transport is for example TCP or UDP, but it might be also serial or something else. You should have functions in this module to connect, read, write and disconnect.

  2. PROTOCOL. This is a way in which you are parsing data (not your game logic data yet, just whole messages). In example you can have "sign delimeter" protocol, which reads data from transport and creates messages from it by splitting data where "delimeter sign" occurs.

    123%456%789%

this data read from transport would be converted to 3 messages [123,456, 789] if your delimeter is %. Special case of this protocol is "line receiver" where delimeter sign is "\n".

  1. PARSER. Messages that you get from protocol should be parsed. Parsing is based on some type of identifier sent in message. I have separated ID into type and subtype. My ID is 2 bytes long, and I store type id in 4 first bits, and subtype id in rest 12 bits. How to do this you have to read elsewhere. This way your switch (ifs) is more optimized. Store these identifiers at the top of parser module. In example:

     TYPE_SYSTEM = 1
     SUBTYPE_LOGIN_REQUEST = 1
    
     if type == TYPE_SYSTEM:
         if subtype == SUBTYPE_LOGIN_REQUEST:
             do_something()
    

TRANSPORT. This is a module that takes care of reading data from network. Transport is for example TCP or UDP, but it might be also serial or something else. You should have functions in this module to connect, read, write and disconnect.

PROTOCOL. This is a way in which you are parsing data (not your game logic data yet, just whole messages). In example you can have "sign delimeter" protocol, which reads data from transport and creates messages from it by splitting data where "delimeter sign" occurs.

123%456%789%

this data read from transport would be converted to 3 messages [123,456, 789] if your delimeter is %. Special case of this protocol is "line receiver" where delimeter sign is "\n".

PARSER. Messages that you get from protocol should be parsed. Parsing is based on some type of identifier sent in message. I have separated ID into type and subtype. My ID is 2 bytes long, and I store type id in 4 first bits, and subtype id in rest 12 bits. How to do this you have to read elsewhere. This way your switch (ifs) is more optimized. Store these identifiers at the top of parser module. In example:

    TYPE_SYSTEM = 1
    SUBTYPE_LOGIN_REQUEST = 1

    if type == TYPE_SYSTEM:
        if subtype == SUBTYPE_LOGIN_REQUEST:
            do_something()
added 924 characters in body
Source Link

I'm writing game in HTML5 using websockets and my design should apply well for you too.

I've separated few things.

  1. TRANSPORT. This is a module that takes care of reading data from network. Transport is for example TCP or UDP, but it might be also serial or something else. You should have functions in this module to connect, read, write and disconnect.

  2. PROTOCOL. This is a way in which you are parsing data (not your game logic data yet, just whole messages). In example you can have "sign delimeter" protocol, which reads data from transport and creates messages from it by splitting data where "delimeter sign" occurs.

    123%456%789%

this data read from transport would be converted to 3 messages [123,456, 789] if your delimeter is %. Special case of this protocol is "line receiver" where delimeter sign is "\n".

For binary packets you should create protocol that creates messages based on size of packet.

(3 1 3 4)

Let's assume that this is 4 bytes long data. Your protocol should read first byte (3) so it knows that this packet has 3 bytes. Then you read next 3 bytes so this protocol gets message (1 3 4). You can also have sizes constant for types, but it's not a good idea if you want to send strings.

  1. PARSER. Messages that you get from protocol should be parsed. Parsing is based on some type of identifier sent in message. I have separated ID into type and subtype. My ID is 2 bytes long, and I store type id in 4 first bits, and subtype id in rest 12 bits. How to do this you have to read elsewhere. This way your switch (ifs) is more optimized. Store these identifiers at the top of parser module. In example:

     TYPE_SYSTEM = 1
     SUBTYPE_LOGIN_REQUEST = 1
    
     if type == TYPE_SYSTEM:
         if subtype == SUBTYPE_LOGIN_REQUEST:
             do_something()
    

In the end you should remember about design patterns MODEL, CONTROLLER, VIEW. On server you just need MODELS and CONTROLLERS. Parsers are controllers, while transport and protocol are models, you just use them in controllers. They should have no dependancy. Every model should be encapsulated.

Once again I'll explain flow of work in shorter form.

You separated modules that should have no dependancies on each other:

  • transport,
  • protocol,
  • game logics,

Controllers:

  • main controller,
  • main_parser,
  • specific_parsers.

In main controller in a loop you read data using transport model, that data is saved to buffer. Buffer is then passed to protocol model. If buffer has enough data then protocol returns list of messages that it created from data. This list of messages is passed to main parser controller. Main parser based on type and subtype calls specific parser and passes it data from message. Specific parser use functions from game logic model. And that's all about it.

I've separated main parser and specific parsers because in one module it would take too much space and it would be troublesome to find function you want later when you'll write like 100 parser functions.

Good luck.

I'm writing game in HTML5 using websockets and my design should apply well for you too.

I've separated few things.

  1. TRANSPORT. This is a module that takes care of reading data from network. Transport is for example TCP or UDP, but it might be also serial or something else. You should have functions in this module to connect, read, write and disconnect.

  2. PROTOCOL. This is a way in which you are parsing data (not your game logic data yet, just whole messages). In example you can have "sign delimeter" protocol, which reads data from transport and creates messages from it by splitting data where "delimeter sign" occurs.

    123%456%789%

this data read from transport would be converted to 3 messages [123,456, 789] if your delimeter is %. Special case of this protocol is "line receiver" where delimeter sign is "\n".

For binary packets you should create protocol that creates messages based on size of packet.

(3 1 3 4)

Let's assume that this is 4 bytes long data. Your protocol should read first byte (3) so it knows that this packet has 3 bytes. Then you read next 3 bytes so this protocol gets message (1 3 4). You can also have sizes constant for types, but it's not a good idea if you want to send strings.

  1. PARSER. Messages that you get from protocol should be parsed. Parsing is based on some type of identifier sent in message. I have separated ID into type and subtype. My ID is 2 bytes long, and I store type id in 4 first bits, and subtype id in rest 12 bits. How to do this you have to read elsewhere. This way your switch (ifs) is more optimized. Store these identifiers at the top of parser module. In example:

     TYPE_SYSTEM = 1
     SUBTYPE_LOGIN_REQUEST = 1
    
     if type == TYPE_SYSTEM:
         if subtype == SUBTYPE_LOGIN_REQUEST:
             do_something()
    

In the end you should remember about design patterns MODEL, CONTROLLER, VIEW. On server you just need MODELS and CONTROLLERS. Parsers are controllers, while transport and protocol are models, you just use them in controllers. They should have no dependancy. Every model should be encapsulated.

Good luck.

I'm writing game in HTML5 using websockets and my design should apply well for you too.

I've separated few things.

  1. TRANSPORT. This is a module that takes care of reading data from network. Transport is for example TCP or UDP, but it might be also serial or something else. You should have functions in this module to connect, read, write and disconnect.

  2. PROTOCOL. This is a way in which you are parsing data (not your game logic data yet, just whole messages). In example you can have "sign delimeter" protocol, which reads data from transport and creates messages from it by splitting data where "delimeter sign" occurs.

    123%456%789%

this data read from transport would be converted to 3 messages [123,456, 789] if your delimeter is %. Special case of this protocol is "line receiver" where delimeter sign is "\n".

For binary packets you should create protocol that creates messages based on size of packet.

(3 1 3 4)

Let's assume that this is 4 bytes long data. Your protocol should read first byte (3) so it knows that this packet has 3 bytes. Then you read next 3 bytes so this protocol gets message (1 3 4). You can also have sizes constant for types, but it's not a good idea if you want to send strings.

  1. PARSER. Messages that you get from protocol should be parsed. Parsing is based on some type of identifier sent in message. I have separated ID into type and subtype. My ID is 2 bytes long, and I store type id in 4 first bits, and subtype id in rest 12 bits. How to do this you have to read elsewhere. This way your switch (ifs) is more optimized. Store these identifiers at the top of parser module. In example:

     TYPE_SYSTEM = 1
     SUBTYPE_LOGIN_REQUEST = 1
    
     if type == TYPE_SYSTEM:
         if subtype == SUBTYPE_LOGIN_REQUEST:
             do_something()
    

In the end you should remember about design patterns MODEL, CONTROLLER, VIEW. On server you just need MODELS and CONTROLLERS. Parsers are controllers, while transport and protocol are models, you just use them in controllers. They should have no dependancy. Every model should be encapsulated.

Once again I'll explain flow of work in shorter form.

You separated modules that should have no dependancies on each other:

  • transport,
  • protocol,
  • game logics,

Controllers:

  • main controller,
  • main_parser,
  • specific_parsers.

In main controller in a loop you read data using transport model, that data is saved to buffer. Buffer is then passed to protocol model. If buffer has enough data then protocol returns list of messages that it created from data. This list of messages is passed to main parser controller. Main parser based on type and subtype calls specific parser and passes it data from message. Specific parser use functions from game logic model. And that's all about it.

I've separated main parser and specific parsers because in one module it would take too much space and it would be troublesome to find function you want later when you'll write like 100 parser functions.

Good luck.

Source Link

I'm writing game in HTML5 using websockets and my design should apply well for you too.

I've separated few things.

  1. TRANSPORT. This is a module that takes care of reading data from network. Transport is for example TCP or UDP, but it might be also serial or something else. You should have functions in this module to connect, read, write and disconnect.

  2. PROTOCOL. This is a way in which you are parsing data (not your game logic data yet, just whole messages). In example you can have "sign delimeter" protocol, which reads data from transport and creates messages from it by splitting data where "delimeter sign" occurs.

    123%456%789%

this data read from transport would be converted to 3 messages [123,456, 789] if your delimeter is %. Special case of this protocol is "line receiver" where delimeter sign is "\n".

For binary packets you should create protocol that creates messages based on size of packet.

(3 1 3 4)

Let's assume that this is 4 bytes long data. Your protocol should read first byte (3) so it knows that this packet has 3 bytes. Then you read next 3 bytes so this protocol gets message (1 3 4). You can also have sizes constant for types, but it's not a good idea if you want to send strings.

  1. PARSER. Messages that you get from protocol should be parsed. Parsing is based on some type of identifier sent in message. I have separated ID into type and subtype. My ID is 2 bytes long, and I store type id in 4 first bits, and subtype id in rest 12 bits. How to do this you have to read elsewhere. This way your switch (ifs) is more optimized. Store these identifiers at the top of parser module. In example:

     TYPE_SYSTEM = 1
     SUBTYPE_LOGIN_REQUEST = 1
    
     if type == TYPE_SYSTEM:
         if subtype == SUBTYPE_LOGIN_REQUEST:
             do_something()
    

In the end you should remember about design patterns MODEL, CONTROLLER, VIEW. On server you just need MODELS and CONTROLLERS. Parsers are controllers, while transport and protocol are models, you just use them in controllers. They should have no dependancy. Every model should be encapsulated.

Good luck.