Device Monitoring Studio - Monitor, log and analyze data coming through PC ports and connections
Download Device Monitoring Studio Hide this button

Adding New Protocol

This section will guide you through creation of your own protocol and adding it to the network protocol chain. You may use the same steps to create and add protocols to other protocol chains.

Creating New Protocol Definition File

First, we will create an empty protocol definition file. Execute the Tools » Protocol Editor » New Protocol File command. You will be presented with a standard Windows Save File dialog. Do not change the location and enter the following name: mygame.h. An empty editor file will be opened.

Copy and paste the following text into the protocol definition file:

//Custom protocol 
//Network Game Sample

// Include standard headers
#include "std_netdefs.h"

#pragma pack(1)

enum MessageType : DWORD
{
  MSGID_CONNECT_PLAYER = 1,
  MSGID_DISCONNECT_PLAYER,
  MSGID_CREATE_PLAYER,
  MSGID_SEND_PLAYER,
  MSGID_SEND_PLAYER_POS,
  MSGID_SEND_PLAYER_CHARS,
  MSGID_LAST
};

struct pstring_a
{
  BYTE length;
  char text[length];
};

struct MyCreatePlayer
{
  pstring_a	PlayerName;
};

struct MySendPlayerAdded
{
  DWORD PlayerID;
};

struct MyConnect
{
  DWORD PlayerID;
};

struct MyDisConnect
{
  DWORD PlayerID;
};

struct MySendPlayerPos
{
  float x;
  float y;
  float z;
};

struct MySendPlayerCharacteristics
{
  WORD Strength;
  WORD Dexterity;
  WORD Health;
  WORD Mana;
};

[category(protocol)]
public struct MyGameProtocol
{
  MessageType MsgID;
  WORD Version;
  switch(MsgID)
  {
    case MSGID_CONNECT_PLAYER:
      MyConnect connect;
      break;
    case MSGID_DISCONNECT_PLAYER:
      MyDisConnect disconnect;
      break;
    case MSGID_CREATE_PLAYER:
      MyCreatePlayer create;
      break;
    case MSGID_SEND_PLAYER:
      MySendPlayerAdded send_added;
      break;

    default:
      if(MsgID > MSGID_SEND_PLAYER && MsgID < MSGID_LAST)
      {
        struct GameData
        {
          DWORD PlayerID;
          switch(MsgID)
          {
            case MSGID_SEND_PLAYER_POS:
              MySendPlayerPos player_pos;
              break;
            case MSGID_SEND_PLAYER_CHARS:
              MySendPlayerCharacteristics player_chars;
              break;
          }
       } game_data;
    }
    else
    {
      BYTE Unknown[packet_size - current_offset];
    }
  }
};

Save the file using the File » Save command.

Plugging New Protocol to Protocol Chain

Next step is to plug our new protocol into the protocol chain. We need to tell Device Monitoring Studio where and under what conditions this new protocol should be bound to monitored data.

Our game uses UDP for transport and has a specific UDP port. We will use this information to plug our game protocol. First, use the Protocols List tool window to open the UDP protocol. Expand the “Network” category, then “Protocols” category and double-click on the “Udp” item.

As you see, part of the Udp protocol definition includes a switch that selects next enclosed protocol. Modify the switch by adding the following text just before the default: line:

case MyPort:
  MyGameProtocol my_proto;
  break;

Then, scroll to the beginning of the udp.h file and add the following text (after all #include directives):

// Include the contents of the protocol definition file
#include "mygame.h"

// Define the port our game is using
const MyPort = 32323;

As soon as you save the udp.h file (using the File » Save command), Device Monitoring Studio will automatically compile the changes. If there are any compilation errors, they will be displayed in the Compile Errors tool window. Otherwise, next network monitoring session you start will automatically use your new protocol whenever UDP packet to port 32323 is captured.