Convert C ++ struct to C # for use with UDP communication

I am writing a program that will communicate with a C ++ program through UDP. Another program has already been written (not by me). I got a .h file that defines two structures that are used for data.

EDIT: This means that I cannot change the data format. I need to be able to read and write in accordance with C ++. H structs files!

How do I do this in C #? I will send and receive data in this format.

struct mdata
{
  uint32_t  mark_kupnr;
  uint16_t  mark_provnr;
  uint16_t  markriktning;
  uint16_t  xpos;
  uint16_t  ypos;
};

typedef struct  
{
  uint32_t      kupnr;
  uint16_t      lngd;
  uint16_t      bredd;
  uint16_t      tjocklek;
  char          slagkraft;
  uint8_t       antal;
  struct mdata  mark[10];
} markdata;

EDIT: I tried creating the appropriate structures in C #, but didn't work

By the way, I work on Windows, and the C ++ program works on Linux. The "specification" says that the data should be small Endian.

struct mdata
{
  UInt32    mark_kupnr;
  UInt16    mark_provnr;
  UInt16    markriktning;
  UInt16    xpos;
  UInt16    ypos;
};

typedef struct  
{
  UInt32        kupnr;
  UInt16        lngd;
  UInt16        bredd;
  UInt16        tjocklek;
  char          slagkraft;
  byte          antal;
  // here I have some trouble
  mdata[]   mark[10]; //???
} markdata;
+4
3

, fixed. , char # , char ++, , sbyte.

:

struct markdata
{
  UInt32        kupnr;
  UInt16        lngd;
  UInt16        bredd;
  UInt16        tjocklek;
  sbyte         slagkraft;
  byte          antal;
  fixed   mdata mark[10];
}

, , . , , , , .

+2

, , .

Google Protocol Buffers /. Apache Thrift. , ( ). , , Microsoft IDL. .

++ STL ( ), JSON / JSON . , RapidJSON ++ , RapidJSON.

+1

++ , IBM-PC, # -. , .

#, , 1 , # little-endian ++. WORD (16 ) DWORD (32 )

// BYTE SWAPING IN WORD and DWORD

#define WORD_SWAP(x)    ((((x) >> 8) & 0x00ff) | (((x) << 8) & 0xff00))

#define DWORD_SWAP(x)   ((((x) >> 24) & 0x000000ff) | \
                         (((x) >> 8) & 0x0000ff00) | \
                         (((x) << 8) & 0x00ff0000) | \
                         (((x) << 24) & 0xff000000))

#. .

PS: , .

Endianness,

#

// Swaps uint16_t
ushort Swap(ushort x)
{
     return (ushort)((((x) >> 8) & 0x00ff) | (((x) << 8) & 0xff00));
}

// Swaps uint32_t
uint Swap(uint x)
{
     return ((((x) >> 24) & 0x000000ff) |
             (((x) >> 8) & 0x0000ff00) |
             (((x) << 8) & 0x00ff0000) |
             (((x) << 24) & 0xff000000));
}

# client UDPClient . .

struct mdata
{
  UInt32  mark_kupnr;
  UInt16  mark_provnr;
  UInt16  markriktning;
  UInt16  xpos;
  UInt16  ypos;
}

// example of mdata read with sockets
mbata data;
var bytes = client.Receive()

data.mark_kupnr = Swap(BitConverter.ToUInt32(bytes, 0));
data.mark_provnr = Swap(BitConverter.ToUInt16(bytes, 4));
data.markriktning = Swap(BitConverter.ToUInt16(bytes, 6));
data.xpos = Swap(BitConverter.ToUInt16(bytes, 8));
data.ypos = Swap(BitConverter.ToUInt16(bytes, 10));

, UInt32 UInt16 . List<byte>.AddRange(variable_byte_array);, UDPClient.Send(formed_array);.

++ [0][1][2][3][4][5][6][7][8][9]. mdata 10 0 9.

// read first data
typedef struct  
{
  UInt32        kupnr;
  UInt16        lngd;
  UInt16        bredd;
  UInt16        tjocklek;
  sbyte          slagkraft;
  byte          antal;
  // now read mkdata 10 times
  struct mdata  mark[10];
} markdata; // we have done with markdata

. , () . ()

mdata​​p >

void Receive(UDPClient client)
{
    var bytes = client.Receive()

    mark_kupnr = Swap(BitConverter.ToUInt32(bytes, 0));
    mark_provnr = Swap(BitConverter.ToUInt16(bytes, 4));
    markriktning = Swap(BitConverter.ToUInt16(bytes, 6));
    xpos = Swap(BitConverter.ToUInt16(bytes, 8));
    ypos = Swap(BitConverter.ToUInt16(bytes, 10));
}

void Send(UDPClient client)
{
    var listOfBytes = new List<byte>();

    listOfBytes.AddRange(BitConverter.GetBytes(Swap(mark_kupnr)));
    listOfBytes.AddRange(BitConverter.GetBytes(Swap(mark_provnr)));
    listOfBytes.AddRange(BitConverter.GetBytes(Swap(markriktning)));
    listOfBytes.AddRange(BitConverter.GetBytes(Swap(xpos)));
    listOfBytes.AddRange(BitConverter.GetBytes(Swap(ypos)));

    client.Send(listOfBytes.ToArray(), listOfBytes.Count);
}

void Receive(byte[] bytes)
{      
    mark_kupnr = Swap(BitConverter.ToUInt32(bytes, 0));
    mark_provnr = Swap(BitConverter.ToUInt16(bytes, 4));
    markriktning = Swap(BitConverter.ToUInt16(bytes, 6));
    xpos = Swap(BitConverter.ToUInt16(bytes, 8));
    ypos = Swap(BitConverter.ToUInt16(bytes, 10));
}

void Send(out byte[] bytes)
{
    var listOfBytes = new List<byte>();

    listOfBytes.AddRange(BitConverter.GetBytes(Swap(mark_kupnr)));
    listOfBytes.AddRange(BitConverter.GetBytes(Swap(mark_provnr)));
    listOfBytes.AddRange(BitConverter.GetBytes(Swap(markriktning)));
    listOfBytes.AddRange(BitConverter.GetBytes(Swap(xpos)));
    listOfBytes.AddRange(BitConverter.GetBytes(Swap(ypos)));

    bytes = listOfBytes.ToArray();
}

markdata, , , . .

mdata.

0

Source: https://habr.com/ru/post/1616222/


All Articles