public static new bool FromBytes(byte[] data, ref int pos, int end, out OscPacket packet) { // Check header size int bundleByteSize = end - pos; if (bundleByteSize < headerByteSize) { Debug.LogWarning("[OSC io] OscBundle with invalid header was ignored." + Environment.NewLine); packet = null; return(false); } // Check prefix string prefixInput = Encoding.ASCII.GetString(data, pos, prefixByteSize); if (prefixInput != prefix) { Debug.LogWarning("[OSC io] OscBundle with invalid header was ignored." + Environment.NewLine); packet = null; return(false); } pos += prefixByteSize + 1; // + 1 to ensure bytes multiple of 4 // Get timetag byte[] oscNtpBytes = new byte[OscTimeTag.byteSize]; Array.Copy(data, pos, oscNtpBytes, 0, OscTimeTag.byteSize); OscTimeTag timeTag = new OscTimeTag(oscNtpBytes); pos += OscTimeTag.byteSize; // Create and fill bundle packet = new OscBundle(timeTag); OscBundle bundle = packet as OscBundle; while (pos < end) { int length = BitConverter.ToInt32(data, 0); pos += 4; int packetEnd = pos + length; OscPacket subPacket; if (pos < end && OscPacket.FromBytes(data, ref pos, packetEnd, out subPacket)) { bundle.Add(subPacket); } } return(true); }
void EndReceive(IAsyncResult asyncResult) { if (!_isReceiving) { return; } try { UdpState udpState = (UdpState)asyncResult.AsyncState; IPEndPoint endPoint = udpState.e; UdpClient udpClient = udpState.u; // TODO will this contain the souce of the datagram? //Debug.Log( endPoint.Address + " " + endPoint.Port ); // Get the data byte[] data = udpClient.EndReceive(asyncResult, ref endPoint); // Begin receiving again udpClient.BeginReceive(_callback, udpState); // Parse packet and forward it if (data != null && data.Length > 0) { OscPacket packet; if (OscPacket.FromBytes(data, out packet)) { OnOscPacketReceived(packet); } } else { // Ignore } } catch (Exception e) { if (e is ObjectDisposedException) { // Ignore. } else { Debug.LogWarning("[OscIn] Error occurred while receiving message." + Environment.NewLine + e.ToString()); } } }