public static OscMessage GetMessage(string address) { //Debug.Log( "Pool lossy hash count: " + _messageStacks.Count ); OscMessage message; // Compute lossy hash and try to get message from pool. int hash = OscStringHash.Pack(address); Stack <OscMessage> stack; if (_messageStacks.TryGetValue(hash, out stack) && stack.Count > 0) { message = stack.Pop(); message.Clear(); // Only set address if it differs. Ordinal (raw byte) comparison. if (string.Compare(message.address, address, StringComparison.Ordinal) != 0) { message.address = address; } } else { //Debug.Log( Time.frameCount + ": OscPool.GetMessage CREATED MESSAGE! for " + address + " with hash " + hash + " when there was " + _messageStacks.Count + " stacks available.\n" ); //foreach( KeyValuePair<int, Stack<OscMessage>> pair in _messageStacks ) { // Debug.Log( "\tHash: " + pair.Key + ", message count: " + pair.Value.Count + "\n" ); //} message = new OscMessage(address); } return(message); }
// Undocumented on purpose. public int GetAddressHash() { // Return cached when possible. if (!_dirtyAddressHash) { return(_addressHash); } // Compute and return. _addressHash = OscStringHash.Pack(_address); _dirtyAddressHash = false; return(_addressHash); }
void UpdateMappings() { // Create or clear collections. if (_regularMappingLookup == null) { _regularMappingLookup = new Dictionary <int, Dictionary <string, OscMapping> >(); } else { _regularMappingLookup.Clear(); } if (_specialPatternMappings == null) { _specialPatternMappings = new List <OscMapping>(); } else { _specialPatternMappings.Clear(); } // Add mappings. foreach (OscMapping mapping in _mappings) { mapping.SetDirty(); if (mapping.hasSpecialPattern) { _specialPatternMappings.Add(mapping); } else { int hash = OscStringHash.Pack(mapping.address); Dictionary <string, OscMapping> mappingLookup; if (!_regularMappingLookup.TryGetValue(hash, out mappingLookup)) { mappingLookup = new Dictionary <string, OscMapping>(); _regularMappingLookup.Add(hash, mappingLookup); } mappingLookup.Add(mapping.address, mapping); } } // Update flag. _dirtyMappings = false; }
// Undocumented on purpose. public static bool TryReadFrom(byte[] data, ref int index, int size, ref OscMessage message) { int beginIndex = index; // If we are not provided with a message, then read the lossy hash and try reuse from the pool. if (message == null) { int hash = OscStringHash.Pack(data, index); message = OscPool.GetMessage(hash); } else { if (message._argInfo.Count > 0) { message.Clear(); // Ensure that arguments are cleared. } } // Address. string address = message._address; if (!StringOscData.TryReadFrom(data, ref index, ref address)) { Debug.Log(OscDebug.FailedReadingBytesWarning(message)); return(false); } message._address = address; // Tag prefix. if (data[index] != OscConst.tagPrefixByte) { StringBuilder sb = OscDebug.BuildText(message); sb.Append("Read failed. Tag prefix missing.\n"); Debug.LogWarning(sb.ToString()); return(false); } index++; // Argument tags. for (int i = index; i < data.Length && data[i] != 0; i++) { message._argInfo.Add(new OscArgInfo(data[i], 0)); } index += message._argInfo.Count; // Followed by at least one trailing zero, multiple of four bytes. index += 4 - (index % 4); //Debug.Log( "READ: Args data start index: " + index ); // Argument data info. int argDataByteCount = 0; for (int i = 0; i < message._argInfo.Count; i++) { byte tagByte = message._argInfo[i].tagByte; int argByteCount = 0; switch (tagByte) { case OscConst.tagNullByte: case OscConst.tagImpulseByte: case OscConst.tagTrueByte: case OscConst.tagFalseByte: break; case OscConst.tagFloatByte: case OscConst.tagIntByte: case OscConst.tagCharByte: case OscConst.tagColorByte: case OscConst.tagMidiByte: argByteCount = 4; break; case OscConst.tagDoubleByte: case OscConst.tagLongByte: case OscConst.tagTimetagByte: argByteCount = 8; break; case OscConst.tagStringByte: case OscConst.tagSymbolByte: argByteCount = StringOscData.EvaluateByteCount(data, index + argDataByteCount); break; case OscConst.tagBlobByte: BlobOscData.TryEvaluateByteCount(data, index + argDataByteCount, out argByteCount); break; default: StringBuilder sb = OscDebug.BuildText(message); sb.Append("Read failed. Tag '"); sb.Append((char)tagByte); sb.Append("' is not supported\n"); Debug.LogWarning(sb.ToString()); return(false); } message._argInfo[i] = new OscArgInfo(tagByte, argByteCount); //Debug.Log( "i; " + i + ", info: " + message._argInfo[i] ); argDataByteCount += argByteCount; } // AdaptiveSet data list. if (message._argData.Capacity < argDataByteCount) { message._argData.Capacity = argDataByteCount; } // Read data. for (int i = 0; i < argDataByteCount; i++) { message._argData.Add(data[index++]); } // Cache byte count. message._cachedSize = index - beginIndex; message._dirtySize = false; return(true); }