Changeset 111 for dotnet

Show
Ignore:
Timestamp:
11/27/06 23:22:47 (2 years ago)
Author:
davidthings
Message:

Added BLOBS to the set of OSC messages processable.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • dotnet/trunk/MakeControllerOsc/Osc.cs

    r88 r111  
    217217    /// serializes them into a byte[] suitable for sending to the PacketExchange. 
    218218    /// </summary> 
    219     /// <param name="oms">The OSC Message to send.</param>    
     219    /// <param name="oms">The OSC Messages (as an ArrayList) to send.</param>    
    220220    public void Send(ArrayList oms) 
    221221    { 
     
    266266      { 
    267267        s.Append(" "); 
    268         s.Append(o.ToString()); 
     268        if (o is byte[]) 
     269        { 
     270          byte[] byteArray = (byte[])o; 
     271          s.Append("<"); 
     272          bool first = true; 
     273          foreach (byte b in byteArray) 
     274          { 
     275            if (!first) 
     276              s.Append(" "); 
     277            else 
     278              first = false; 
     279            s.Append( String.Format( "{0:X2}",b));  
     280          } 
     281          s.Append(">");  
     282        } 
     283        else 
     284          s.Append(o.ToString()); 
    269285      } 
    270286      return s.ToString(); 
     
    288304        string s = (string)sE.Current; 
    289305        // Console.WriteLine("  <" + s + ">"); 
     306        // Check for a string - as indicated by an open quote 
    290307        if (s.StartsWith("\"")) 
    291308        { 
     309          // Get the whole string 
    292310          StringBuilder quoted = new StringBuilder(); 
    293311          bool looped = false; 
     
    320338        else 
    321339        { 
    322           if (s.Length > 0) 
    323           { 
    324             try 
    325             { 
    326               int i = int.Parse(s); 
    327               // Console.WriteLine("  i:" + i); 
    328               oM.Values.Add(i); 
    329             } 
    330             catch 
     340          if ( s.StartsWith( "<" ) ) 
     341          { 
     342            ArrayList byteList = new ArrayList(); 
     343            if (s.Length > 1) 
     344              ExtractBytesFromString(byteList, s.Substring(1)); 
     345            while (sE.MoveNext()) 
     346            { 
     347              string a = (string)sE.Current; 
     348 
     349              ExtractBytesFromString(byteList, a); 
     350 
     351              if ( a.EndsWith( ">" ) ) 
     352                break; 
     353            } 
     354 
     355            byte[] byteArray = new byte[ byteList.Count ]; 
     356            int index = 0; 
     357            foreach ( byte b in byteList ) 
     358              byteArray[ index++ ] = (byte)b; 
     359 
     360            oM.Values.Add( byteArray ); 
     361          } 
     362          else 
     363          { 
     364            if (s.Length > 0) 
    331365            { 
    332366              try 
    333367              { 
    334                 float f = float.Parse(s); 
    335                 // Console.WriteLine("  f:" + f); 
    336                 oM.Values.Add(f); 
     368                int i = int.Parse(s); 
     369                // Console.WriteLine("  i:" + i); 
     370                oM.Values.Add(i); 
    337371              } 
    338372              catch 
    339373              { 
    340                 // Console.WriteLine("  s:" + s); 
    341                 oM.Values.Add(s); 
     374                try 
     375                { 
     376                  float f = float.Parse(s); 
     377                  // Console.WriteLine("  f:" + f); 
     378                  oM.Values.Add(f); 
     379                } 
     380                catch 
     381                { 
     382                  // Console.WriteLine("  s:" + s); 
     383                  oM.Values.Add(s); 
     384                } 
    342385              } 
    343386            } 
    344  
    345387          } 
    346388        } 
     
    467509              else 
    468510              { 
    469                 tag.Append("?"); 
     511                if ( o is byte[] ) 
     512                { 
     513                  tag.Append("b"); 
     514                  byte[] byteArray = (byte[])o; 
     515                  int len = byteArray.Length; 
     516                  packet[index++] = (byte)((len >> 24) & 0xFF); 
     517                  packet[index++] = (byte)((len >> 16) & 0xFF); 
     518                  packet[index++] = (byte)((len >> 8) & 0xFF); 
     519                  packet[index++] = (byte)((len) & 0xFF); 
     520                  Array.Copy(byteArray, 0, packet, index, len); 
     521                  index += len; 
     522                  int pad = len % 4; 
     523                  if (pad != 0) 
     524                  { 
     525                    pad = 4 - pad; 
     526                    while (pad-- > 0) 
     527                      packet[index++] = 0; 
     528                  } 
     529                } 
     530                else 
     531                  tag.Append("?"); 
    470532              } 
    471533            } 
     
    539601              index += PadSize(s.Length + 1); 
    540602              oscM.Values.Add(s); 
     603              break; 
     604            } 
     605          case 'b': 
     606            { 
     607              byte[] b = ExtractBlob(packet, index, length); 
     608              index += PadSize(b.Length); 
     609              oscM.Values.Add(b); 
    541610              break; 
    542611            } 
     
    567636 
    568637    /// <summary> 
    569     /// Removes a string from a packet.  Used internally. 
     638    /// Extracts a string from a packet.  Used internally. 
    570639    /// </summary> 
    571640    /// <param name="packet">The packet of bytes to be parsed.</param> 
     
    580649        sb.Append((char)packet[index++]); 
    581650      return sb.ToString(); 
     651    } 
     652 
     653    /// <summary> 
     654    /// Extracts a blob from a packet.  Used internally. 
     655    /// </summary> 
     656    /// <param name="packet">The packet of bytes to be parsed.</param> 
     657    /// <param name="start">The index of where to start looking in the packet.</param> 
     658    /// <param name="length">The length of the packet.</param> 
     659    /// <returns>The string</returns> 
     660    private static byte[] ExtractBlob(byte[] packet, int start, int length) 
     661    { 
     662      int index = start; 
     663      int blobSize = ( packet[index++] << 24 ) + ( packet[index++] << 16 ) + ( packet[index++] << 8 ) + packet[index++]; 
     664      byte[] b = new byte[ blobSize ]; 
     665      int blobIndex = 0; 
     666      while (blobSize-- > 0 && index < length) 
     667      { 
     668        b[ blobIndex ] = packet[ index ]; 
     669        blobIndex++; 
     670        index++; 
     671      } 
     672      return b; 
    582673    } 
    583674 
     
    623714        return rawSize + (4 - pad); 
    624715    } 
     716 
     717    private static void ExtractBytesFromString(ArrayList byteList, string s) 
     718    { 
     719      int nibbleCount = 0; 
     720      int v = 0; 
     721      // look at each character 
     722      foreach (char c in s) 
     723      { 
     724        // if the character is 0-9, a-f or A-F add it to v left shifting whatever was in there by a nibble 
     725        if (c >= '0' && c <= '9') 
     726        { 
     727          v = ( v << 4 ) + (int)(c - '0'); 
     728          nibbleCount++; 
     729        } 
     730        else 
     731        { 
     732          if (c >= 'a' && c <= 'f') 
     733          { 
     734            v = (v << 4) + (int)(c - 'a') + 10; 
     735            nibbleCount++; 
     736          } 
     737          else 
     738          { 
     739            if (c >= 'A' && c <= 'F') 
     740            { 
     741              v = (v << 4) + (int)(c - 'A') + 10; 
     742              nibbleCount++; 
     743            } 
     744            else 
     745            { 
     746              // if there is a digit in v, and we just got a non-digit then we're done 
     747              if ( nibbleCount > 0 ) 
     748                nibbleCount++; 
     749            } 
     750          } 
     751        } 
     752        if (nibbleCount == 2) 
     753        { 
     754          byteList.Add((byte)v); 
     755          v = 0; 
     756          nibbleCount = 0; 
     757        } 
     758      } 
     759      // if at the end of it all, we were left with a hanging nibble, save it 
     760      if (nibbleCount > 0) 
     761        byteList.Add((byte)v); 
     762    } 
    625763  } 
    626764}