Net protocol

From X-Moto
Jump to: navigation, search

Architecture

Server side

  • 2 ports : port 4130 TCP, and port 4130 UDP

Client side

  • 1 or 2 ports if possible : random ports
  • go throw nat/firewall

Main packet

Format:

[size of the subheader+subpacket written in ascii]\n
===> beginning of the subheader
[source written in ascii]\n
[subsource written in ascii]\n
[actionType]\n
===> beginning of the subpacket
[subpacket]\n
  • source : -1 for the server, or the uniq number associated to the client
  • subsource : the client subsource (0, 1, 2 or 3), for the player on the client machine
  • action type : name of the netAction
  • subpacket : specific to the netAction. \n is added at each subpacket so that it's more readable on a network sniffer, moreover, it can be used to be replaced by \0 to get the char.

For each NetAction, you have these two functions: send and NetAction(void* data, int len). When you send, you don't have to add the \n while it's added after. When you received, the len value includes the \n which is in data so that you can use it to retrieve the data easily (by replacing it by \0)

Sub packets

NA_clientInfos

[protocol version]\n
[udp bind key]\n
  • the protocol version is an integer
  • the udp bind key cannot contains a \n

When connecting, the client generates a random key that he sends via tcp (via clientInfos) and udp (via udpBind) so that the server can bind both connections. The protocol version is compared to the one used by the server, if the both value are not the same, the server remove the client (and send a serverError packet).

NA_udpBind

[udp bind key]\n
  • the udp bind key cannot contains a \n

NA_udpBindQuery

\n

Once the server received the clientInfos packet, he sends a NA_udpBindQuery to the client to ask him to send its udp bind key via udp.

NA_chatMessage

[message]\n
  • message can contain any char on several lines

Example to show how send and read works :

// data contains the \n of the protocol. It's included into len :
NA_chatMessage::NA_chatMessage(void* data, unsigned int len) {
  ((char*)data)[len-1] = '\0';
  m_msg = std::string((char*)data);
  ((char*)data)[len-1] = '\n';
}

// don't send the ending \n while it's added automatically
void NA_chatMessage::send(TCPsocket* i_tcpsd,
                          UDPsocket* i_udpsd, UDPpacket* i_sendPacket,
                          IPaddress* i_udpRemoteIP) {
 // force TCP
 NetAction::send(i_tcpsd, NULL, NULL, NULL, m_msg.c_str(), m_msg.size()); // don't send the 0
}

NA_serverError

[message]\n

NA_frame

[SerializedBikeState]\n

Contains the image of the bike at a given time (littleendian order, or bigendian, i don't remember).

struct SerializedBikeState {
  unsigned char cFlags;             /* State flags */
  float fGameTime;                  /* Game time */
  float fFrameX,fFrameY;            /* Frame position */
  float fMaxXDiff,fMaxYDiff;        /* Addressing space around the frame */

  unsigned short nRearWheelRot;     /* Encoded rear wheel matrix */
  unsigned short nFrontWheelRot;    /* Encoded front wheel matrix */
  unsigned short nFrameRot;         /* Encoded frame matrix */

  unsigned char cBikeEngineRPM;     /* Maps to a float between 400 and 5000 */

  signed char cRearWheelX,cRearWheelY;     /* Rear wheel position */
  signed char cFrontWheelX,cFrontWheelY;   /* Front wheel position */
  signed char cElbowX,cElbowY;             /* Elbow position */
  signed char cShoulderX,cShoulderY;       /* Shoulder position */
  signed char cLowerBodyX,cLowerBodyY;     /* Ass position */
  signed char cKneeX,cKneeY;               /* Knee position */
};

NA_changeName

NA_playingLevel

[level id]\n

Indicate that you've playing an other level or empty to indicate that you've not playing.

NA_changeClients

Scenario

  • the client connects to the server via TCP
  • the client sends a NA_clientInfos netAction via TCP
  • the server sends a NA_udpBindQuery netAction to the client via TCP
  • the client sends 3 NA_udpBind netActions via UDP to the server
  • the server know which tcp connection is bind to which udp connection (several clients could have the same ip) and can send packets via the udp way.
  • the server sends the connected client list with a NA_changeClients netAction

Examples

25
7
0
message
hi everybody