NetworkDesign: Difference between revisions

Jump to navigation Jump to search
Content deleted Content added
imported>Hendrik Brummermann
No edit summary
imported>Hendrik Brummermann
No edit summary
Line 2: Line 2:
{{Navigation for Marauroa Developers}}
{{Navigation for Marauroa Developers}}



{{Likely Outdated}}


Please note: This page explains the low level network communication. You don't need to bother with these implementation details if you want to use Marauroa to write a game. We document the network design anyway for contributor to Marauroa itself. And it is helpful for people porting Marauroa to other programming languages.
Please note: This page explains the low level network communication. You don't need to bother with these implementation details if you want to use Marauroa to write a game. We document the network design anyway for contributor to Marauroa itself. And it is helpful for people porting Marauroa to other programming languages.
Line 17: Line 15:


[[Image:messages-connected.png|thumb|Messages used to securely login.]]
[[Image:messages-connected.png|thumb|Messages used to securely login.]]
{{br}}


=== State logged in ===
=== State logged in ===


[[Image:messages-loggedin.png|thumb|Selecting a character and transmitting world meta data.]]
[[Image:messages-loggedin.png|thumb|Selecting a character and transmitting world meta data.]]
{{br}}


=== State in game ===
=== State in game ===


[[Image:messages-game.png|thumb|Messages sent while the game is active.]]
[[Image:messages-game.png|thumb|Messages sent while the game is active.]]
{{br}}


=== Logging Out ===
=== Logging Out ===
Line 30: Line 31:
[[Image:messages-logout.png|thumb|The client sends a logout request and the server accepts or denies it.]]
[[Image:messages-logout.png|thumb|The client sends a logout request and the server accepts or denies it.]]


{{br}}


== Transmitting Messages over TCP ==

== Basic idea behind Networking ==
The idea behind arianne's network protocol is to use a single stream of TCP packets between the server and the clients. Different kinds of in-game actions create different types of messages that are then interpreted at the opposite side in to meaningful data.
The idea behind arianne's network protocol is to use a single stream of TCP packets between the server and the clients. Different kinds of in-game actions create different types of messages that are then interpreted at the opposite side in to meaningful data.


Line 78: Line 79:
* Finalizing the manager
* Finalizing the manager


The read operation is a blocking type operation so we have two options, either Polling (i.e. continually checking if data is there) or Blocking (i.e. only processing when data is actually available, otherwise sleeping).
The read operation is a blocking type operation so we have two options, either polling (i.e. continually checking if data is there) or blocking (i.e. only processing when data is actually available, otherwise sleeping).


We choose blocking because we don't want to waste CPU time polling the network for messages, we just want to sleep until messages are available. Hence we create a Thread to read from the Network, let's call it NetworkManagerRead.
(<i>Polling is "the sequential interrogation of devices for various purposes, such as avoiding contention, determining operational status, or determining readiness to send or receive data." - Wikipedia ( http://en.wikipedia.org/wiki/Polling )</i>)

We choose Blocking because we don't want to waste CPU time Polling the network for messages, we just want to sleep until messages are available. Hence we create a Thread to read from the Network, let's call it Network Manager Read.


Writing messages to the network can be simply coded as a method of Network Manager, as write is an operation that is non blocking by nature.
Writing messages to the network can be simply coded as a method of Network Manager, as write is an operation that is non blocking by nature.


The Network Manager opens a Socket from which it will receive all the messages from the network. It will also write all the outbound messages to the network from this same socket. Note: Both write and read use the same Socket. (
The NetworkManager opens a Socket from which it will receive all the messages from the network. It will also write all the outbound messages to the network from this same socket. Note: Both write and read use the same Socket.


To encapsulate all this we create both the Read and Write methods as inner classes of Network Manager.
To encapsulate all this we create both the Read and Write methods as inner classes of Network Manager.
Line 107: Line 106:
{
{
get from pendingToSendMessages
get from pendingToSendMessages
build UDP from message
serialize message
send socket
send socket
}
}
Line 115: Line 114:
As you can see, messages are stored in a list when they are received. Hence access to the list must be synchronized.
As you can see, messages are stored in a list when they are received. Hence access to the list must be synchronized.


Now lets get back to the interface as exposed to other objects. The Write method is immediate, just call it with the message to send, making sure that you have correctly filled SourceAddress and ClientID. The message will then be sent to the Client.
Now lets get back to the interface as exposed to other objects. The write method is immediate, just call it with the message to send, making sure that you have correctly filled SourceAddress and ClientID. The message will then be sent to the Client.

It is important to notice that as the transport is based on UDP there is no guarantee that the message is correctly sent. This situation is part of the protocol itself.


The Read method is blocking, when you call the Read method it either returns a message from the queue or if the queue is empty the thread blocks (sleeps) until one arrives.
The read method is blocking, when you call the read method it either returns a message from the queue or if the queue is empty the thread blocks (sleeps) until one arrives.


That is the basic idea of the Network Manager; however, the manager gets a bit more complex as a result of the need to support Server to Client messages that are bigger than one UDP package. Note that the manager just sends the stream of packets once and doesn't confirm if any of the messages are received. Please refer to [[NetworkDesign#UDP_Packet_Format | UDP Packet Format]] for more info about it.
That is the basic idea of the Network Manager. Note that the manager just sends the stream of packets once and doesn't confirm if any of the messages are received. TCP takes care of that.