예제 #1
0
        public override bool ReadData(ReadBuffer reader)
        {
            if (Data == null)
            {
                // header
                var status   = (OperationStatus)reader.ReadInt32();
                var dataSize = reader.ReadInt32();

                Data = new byte[dataSize];
                return(false);
            }

            var operationStatus = (OperationStatus)reader.ReadInt32();

            // check?
            var offset     = reader.ReadInt32();
            var dataLength = reader.ReadInt32();

            int readOffset;

            reader.ReserveForReading(dataLength, out readOffset);

            unsafe
            {
                var data       = Data;
                var readBuffer = reader.GetBuffer();
                fixed(byte *readBufferPtr = readBuffer)
                fixed(byte *targetDataPtr = data)
                {
                    DataManipulation.Copy(readBufferPtr + readOffset, targetDataPtr + _offset, (uint)dataLength);
                    _offset += dataLength;
                }
            }

            return(_offset == Data.Length);
        }
예제 #2
0
 public void ReadCommand(ReadBuffer reader)
 {
     Status = (OperationStatus)reader.ReadInt32();
     Type   = (ReaderType)reader.ReadInt32();
     Config = (ReaderConfig)reader.ReadInt32();
 }
예제 #3
0
        protected async override void ProcessReceivedData(ReadBuffer receiveBuffer)
        {
            var opCode = (OperationCode)receiveBuffer.ReadInt32();

            // TODO: validate operation type when processing
            //var operationType = (OperationStatus)_dataReader.ReadInt32();

            // MAKE SURE TO read all data from receiveBuffer before awaiting async ooperation!
            switch (opCode)
            {
            case OperationCode.OpenReader:
            {
                var openRequest = new OpenReader();
                openRequest.ReadCommand(receiveBuffer);

                // handle open request + start receiving & resending frames
                switch (openRequest.Type)
                {
                case ReaderType.Body:
                {
                    var bodyReader = await _sensor.OpenBodyFrameReaderAsync();

                    if (bodyReader != null)
                    {
                        bodyReader.FrameArrived -= BodyReader_FrameArrived;
                        bodyReader.FrameArrived += BodyReader_FrameArrived;
                        openRequest.SetResponse(OperationStatus.ResponseSuccess);
                    }
                    else
                    {
                        openRequest.SetResponse(OperationStatus.ResponseFail);
                    }
                    break;
                }

                case ReaderType.BodyIndex:
                {
                    var bodyIndexReader = await _sensor.OpenBodyIndexFrameReaderAsync();

                    if (bodyIndexReader != null)
                    {
                        bodyIndexReader.FrameArrived -= BodyIndexReader_FrameArrived;
                        bodyIndexReader.FrameArrived += BodyIndexReader_FrameArrived;
                        openRequest.SetResponse(OperationStatus.ResponseSuccess);
                    }
                    else
                    {
                        openRequest.SetResponse(OperationStatus.ResponseFail);
                    }
                    break;
                }

                case ReaderType.Color:
                {
                    var colorReader = await _sensor.OpenColorFrameReaderAsync(openRequest.Config);

                    if (colorReader != null)
                    {
                        colorReader.FrameArrived -= ColorReader_FrameArrived;
                        colorReader.FrameArrived += ColorReader_FrameArrived;
                        openRequest.SetResponse(OperationStatus.ResponseSuccess);
                    }
                    else
                    {
                        openRequest.SetResponse(OperationStatus.ResponseFail);
                    }
                    break;
                }

                case ReaderType.Depth:
                {
                    var depthReader = await _sensor.OpenDepthFrameReaderAsync();

                    if (depthReader != null)
                    {
                        depthReader.FrameArrived -= DepthReader_FrameArrived;
                        depthReader.FrameArrived += DepthReader_FrameArrived;
                        openRequest.SetResponse(OperationStatus.ResponseSuccess);
                    }
                    else
                    {
                        openRequest.SetResponse(OperationStatus.ResponseFail);
                    }
                    break;
                }

                default:
                {
                    openRequest.SetResponse(OperationStatus.ResponseFailNotAvailable);
                    break;
                }
                }

                // send response should happen before subscription (possible race condition when frame gets send before reply to request gets transmitted)
                SendRequestCommand(openRequest);
                break;
            }

            case OperationCode.CloseReader:
            {
                var closeRequest = new CloseReader();
                closeRequest.ReadCommand(receiveBuffer);

                switch (closeRequest.Type)
                {
                case ReaderType.Body:
                    await _sensor.BodyReader?.CloseAsync();

                    break;

                case ReaderType.BodyIndex:
                    await _sensor.BodyIndexReader?.CloseAsync();

                    break;

                case ReaderType.Color:
                    await _sensor.ColorReader?.CloseAsync();

                    break;

                case ReaderType.Depth:
                    await _sensor.DepthReader?.CloseAsync();

                    break;

                default:
                    closeRequest.SetResponse(OperationStatus.ResponseFailNotAvailable);
                    SendRequestCommand(closeRequest);
                    return;
                }

                closeRequest.SetResponse(OperationStatus.ResponseSuccess);
                SendRequestCommand(closeRequest);
                break;
            }

            case OperationCode.CloseSensor:
            {
                // close all opened readers but keep kinect open / listen for connections
                _connection.Dispose();
                _connection = null;

                await _sensor.BodyReader?.CloseAsync();

                await _sensor.BodyIndexReader?.CloseAsync();

                await _sensor.ColorReader?.CloseAsync();

                await _sensor.DepthReader?.CloseAsync();

                return;
            }

            case OperationCode.UserFrameTransfer:
            {
                FramePacket activeReceive;
                if (_activeFrameReceives.TryGetValue(ReaderType.UserDefined, out activeReceive))
                {
                    // todo: verify that operation status is not push init
                }
                else
                {
                    activeReceive = new CustomFramePacket();
                    _activeFrameReceives[ReaderType.UserDefined] = activeReceive;
                }

                var finishedReading = activeReceive.ReadData(receiveBuffer);
                if (finishedReading)
                {
                    _activeFrameReceives.Remove(ReaderType.UserDefined);
                    OnCustomDataReceived((CustomFramePacket)activeReceive);
                }
                break;
            }
            }
        }
예제 #4
0
 public void ReadCommand(ReadBuffer reader)
 {
     // todo: check operation type
     Status = (OperationStatus)reader.ReadInt32();
     Type   = (ReaderType)reader.ReadInt32();
 }
예제 #5
0
        protected override void ProcessReceivedData(ReadBuffer receiveBuffer)
        {
            var opCode = (OperationCode)receiveBuffer.ReadInt32();

            // TODO: validate operation type when processing
            //var operationType = (OperationStatus)_dataReader.ReadInt32();

            switch (opCode)
            {
            // process responses
            case OperationCode.OpenReader:
            {
                // find first unprocessed request
                var openResponse = new OpenReader();
                openResponse.ReadCommand(receiveBuffer);
                HandleCommandResponse(openResponse);
                break;
            }

            case OperationCode.CloseReader:
            {
                var closeResponse = new CloseReader();
                closeResponse.ReadCommand(receiveBuffer);
                HandleCommandResponse(closeResponse);
                break;
            }

            case OperationCode.BodyFrameTransfer:
            {
                FramePacket activeReceive;
                if (_activeFrameReceives.TryGetValue(ReaderType.Body, out activeReceive))
                {
                    // todo: verify that operation status is not push init
                }
                else
                {
                    activeReceive = new BodyFramePacket();
                    _activeFrameReceives[ReaderType.Body] = activeReceive;
                }

                var finishedReading = activeReceive.ReadData(receiveBuffer);
                if (finishedReading)
                {
                    _activeFrameReceives.Remove(ReaderType.Body);
                    var subs = BodyFrameArrived;
                    if (subs != null)
                    {
                        Task.Run(() => subs(this, (BodyFramePacket)activeReceive));
                    }
                }
                break;
            }

            case OperationCode.DepthFrameTransfer:
            {
                FramePacket activeReceive;
                if (_activeFrameReceives.TryGetValue(ReaderType.Depth, out activeReceive))
                {
                    // todo: verify that operation status is not push init
                }
                else
                {
                    activeReceive = new DepthFramePacket();
                    _activeFrameReceives[ReaderType.Depth] = activeReceive;
                }

                var finishedReading = activeReceive.ReadData(receiveBuffer);
                if (finishedReading)
                {
                    _activeFrameReceives.Remove(ReaderType.Depth);
                    var subs = DepthFrameArrived;
                    if (subs != null)
                    {
                        Task.Run(() => subs(this, (DepthFramePacket)activeReceive));
                    }
                }
                break;
            }

            case OperationCode.BodyIndexFrameTransfer:
            {
                FramePacket activeReceive;
                if (_activeFrameReceives.TryGetValue(ReaderType.BodyIndex, out activeReceive))
                {
                    // todo: verify that operation status is not push init
                }
                else
                {
                    activeReceive = new BodyIndexFramePacket();
                    _activeFrameReceives[ReaderType.BodyIndex] = activeReceive;
                }

                var finishedReading = activeReceive.ReadData(receiveBuffer);
                if (finishedReading)
                {
                    _activeFrameReceives.Remove(ReaderType.BodyIndex);
                    var subs = BodyIndexFrameArrived;
                    if (subs != null)
                    {
                        Task.Run(() => subs(this, (BodyIndexFramePacket)activeReceive));
                    }
                }
                break;
            }

            case OperationCode.ColorFrameTransfer:
            {
                FramePacket activeReceive;
                if (_activeFrameReceives.TryGetValue(ReaderType.Color, out activeReceive))
                {
                    // todo: verify that operation status is not push init
                }
                else
                {
                    activeReceive = new ColorFramePacket();
                    _activeFrameReceives[ReaderType.Color] = activeReceive;
                }

                var finishedReading = activeReceive.ReadData(receiveBuffer);
                if (finishedReading)
                {
                    _activeFrameReceives.Remove(ReaderType.Color);
                    var subs = ColorFrameArrived;
                    if (subs != null)
                    {
                        Task.Run(() => subs(this, (ColorFramePacket)activeReceive));
                    }
                }
                break;
            }

            case OperationCode.UserFrameTransfer:
            {
                FramePacket activeReceive;
                if (_activeFrameReceives.TryGetValue(ReaderType.UserDefined, out activeReceive))
                {
                    // todo: verify that operation status is not push init
                }
                else
                {
                    activeReceive = new CustomFramePacket();
                    _activeFrameReceives[ReaderType.UserDefined] = activeReceive;
                }

                var finishedReading = activeReceive.ReadData(receiveBuffer);
                if (finishedReading)
                {
                    _activeFrameReceives.Remove(ReaderType.UserDefined);
                    OnCustomDataReceived((CustomFramePacket)activeReceive);
                }
                break;
            }
            }
        }