Osc.OscMessageToPacket C# (CSharp) Method

OscMessageToPacket() private static method

Creates an array of bytes from a single OscMessage. Used internally.
Can specify where in the array of bytes the OscMessage should be put.
private static OscMessageToPacket ( OscMessage, oscM, byte packet, int start, int length ) : int
oscM OscMessage, The OscMessage to be turned into an array of bytes.
packet byte The array of bytes to be populated with the OscMessage.
start int The start index in the packet where the OscMessage should be put.
length int The length of the array of bytes.
return int
    private static int OscMessageToPacket(OscMessage oscM, byte[] packet, int start, int length)
    {
      int index = start;
      index = InsertString(oscM.Address, packet, index, length);
      //if (oscM.Values.Count > 0)
      {
        StringBuilder tag = new StringBuilder();
        tag.Append(",");
        int tagIndex = index;
        index += PadSize(2 + oscM.Values.Count);

        foreach (object o in oscM.Values)
        {
          if (o is int)
          {
            int i = (int)o;
            tag.Append("i");
            packet[index++] = (byte)((i >> 24) & 0xFF);
            packet[index++] = (byte)((i >> 16) & 0xFF);
            packet[index++] = (byte)((i >> 8) & 0xFF);
            packet[index++] = (byte)((i) & 0xFF);
          }
          else
          {
            if (o is float)
            {
              float f = (float)o;
              tag.Append("f");
              byte[] buffer = new byte[4];
              MemoryStream ms = new MemoryStream(buffer);
              BinaryWriter bw = new BinaryWriter(ms);
              bw.Write(f);
              packet[index++] = buffer[3];
              packet[index++] = buffer[2];
              packet[index++] = buffer[1];
              packet[index++] = buffer[0];
            }
            else
            {
              if (o is string)
              {
                tag.Append("s");
                index = InsertString(o.ToString(), packet, index, length);
              }
              else
              {
                tag.Append("?");
              }
            }
          }
        }
        InsertString(tag.ToString(), packet, tagIndex, length);
      }
      return index;
    }

Same methods

Osc::OscMessageToPacket ( OscMessage, oscM, byte packet, int length ) : int

Usage Example

Ejemplo n.º 1
0
    /// <summary>
    /// Send an individual OSC message.  Internally takes the OscMessage object and
    /// serializes it into a byte[] suitable for sending to the PacketIO.
    /// </summary>
    /// <param name="oscMessage">The OSC Message to send.</param>
    public void Send(OscMessage oscMessage)
    {
        byte[] packet = new byte[1000];
        int    length = Osc.OscMessageToPacket(oscMessage, packet, 1000);

        OscPacketIO.SendPacket(packet, length);
    }