Esempio n. 1
0
        private void ReceivedBytes(ref byte[] packetBytes, ref byte[] readBuffer)
        {
            // Fetch bytes from serial port
            //int bytesToRead = Math.Min(serialPort.BytesToRead, readBuffer.Length); // Don't use BytesToRead as it is not mono compatible
            int bytesToRead = readBuffer.Length;

            bytesToRead = serialPort.Read(readBuffer, 0, bytesToRead);

            Statistics?.BytesReceived.Increment(bytesToRead);

            int processed = 0;

            do
            {
                int packetLength;

                processed += reader.ProcessBytes(readBuffer, processed, bytesToRead - processed, ref packetBytes, out packetLength);

                if (packetLength <= 0)
                {
                    continue;
                }

                OscPacket oscPacket = OscPacket.Read(packetBytes, packetLength);

                if (Statistics != null && oscPacket.Error != OscPacketError.None)
                {
                    Statistics.ReceiveErrors.Increment(1);
                }

                PacketRecived?.Invoke(oscPacket);
            }while (processed < bytesToRead);
        }
Esempio n. 2
0
        private void Receive_Callback(IAsyncResult ar)
        {
            try
            {
                // create an empty origin
                EndPoint origin = UseIPv6 ? Helper.EmptyEndPointIPv6 : Helper.EmptyEndPoint;

                int count = Socket.EndReceiveFrom(ar, ref origin);

                Statistics?.BytesReceived.Increment(count);

                OscPacket message = OscPacket.Read(buffer, count, (IPEndPoint)origin);

                if (Statistics != null && message.Error != OscPacketError.None)
                {
                    Statistics.ReceiveErrors.Increment(1);
                }

                if (receiveQueue.Count < messageBufferSize)
                {
                    receiveQueue.Enqueue(message);

                    messageReceived.Set();
                }

                //lock (syncLock)
                //{
                //    if (this.count < receiveQueue.Length)
                //    {
                //        receiveQueue[writeIndex] = message;

                //        writeIndex = NextWriteIndex;

                //        this.count++;

                //        // if this was the first message then signal
                //        if (this.count == 1)
                //        {
                //            messageReceived.Set();
                //        }
                //    }
                //}
            }
            catch
            {
            }

            if (State == OscSocketState.Connected)
            {
                // create an empty origin
                EndPoint origin = UseIPv6 ? Helper.EmptyEndPointIPv6 : Helper.EmptyEndPoint;

                Socket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags, ref origin, Receive_Callback, null);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Read a single packet from the stream at the current position
        /// </summary>
        /// <returns>An osc packet</returns>
        public OscPacket Read()
        {
            switch (Format)
            {
            case OscPacketFormat.Binary:
                int length = Helper.ReadInt32(binaryReader);

                byte[] bytes = new byte[length];

                binaryReader.Read(bytes, 0, length);

                return(OscPacket.Read(bytes, length));

            case OscPacketFormat.Slip:
                byte[] packetBytes  = new byte[slipReader.BufferSize];
                int    packetLength = 0;

                do
                {
                    if (slipByteCount - slipByteIndex == 0)
                    {
                        slipByteIndex = 0;

                        slipByteCount = binaryReader.Read(slipByteCache, 0, slipByteCache.Length);
                    }

                    slipByteIndex += slipReader.ProcessBytes(slipByteCache, slipByteIndex, slipByteCount - slipByteIndex, ref packetBytes, out packetLength);
                }while (packetLength <= 0 && EndOfStream == false);

                return(packetLength > 0 ? OscPacket.Read(packetBytes, packetLength) : OscMessage.ParseError);

            case OscPacketFormat.String:
                string    line = stringReader.ReadLine();
                OscPacket packet;

                if (OscPacket.TryParse(line, out packet) == false)
                {
                    StringBuilder sb = new StringBuilder();

                    sb.AppendLine(line);

                    while (EndOfStream == false)
                    {
                        sb.Append(stringReader.ReadLine());

                        if (OscPacket.TryParse(sb.ToString(), out packet) == true)
                        {
                            return(packet);
                        }

                        sb.AppendLine();
                    }

                    return(OscMessage.ParseError);
                }

                return(packet);

            default:
                throw new Exception($@"Invalid OSC stream format ""{Format}"".");
            }
        }