Example #1
0
        /// <summary>
        /// Opens the connection
        /// <exception cref="InvalidOperationException">Invalid operation is thrown when we are already connected</exception>
        /// <exception cref="ArgumentOutOfRangeException">ArgumentOutOfRangeException is thrown when the given mac is null or empty</exception>
        /// </summary>
        /// <param name="mac">The mac address to connect to</param>
        public void OpenConnection(string mac)
        {
            if (Connected)
            {
                throw new InvalidOperationException("Already connected!");
            }

            if (string.IsNullOrWhiteSpace(mac))
            {
                throw new ArgumentOutOfRangeException(nameof(mac));
            }

            Exception exception = null;

            // Connect in separate task
            _communicationTaskQueue.DoWorkInQueue(() =>
            {
                try
                {
                    _adapter.OpenConnection(mac);
                    Connected = true;
                }
                catch (Exception e)
                {
                    exception = e;
                }
            }, true);

            // If any exception occured, we throw it
            if (exception != null)
            {
                Connected = false;
                throw exception;
            }
        }
Example #2
0
        public void StartCamera([CallerMemberName] string memberName = "")
        {
            TxtCommunication.TxtInterface.LogMessage("Starting camera" + memberName);
            if (Connected)
            {
                //throw new InvalidOperationException("Already connected!");
                return;
            }

            RequestedStop = false;

            TxtCommunication.SendCommand(new CommandStartCamera(), new ResponseStartCamera());

            _networkingTaskQueue.DoWorkInQueue(ConnectToCameraServerMethod, true);
            _networkingTaskQueue.DoWorkInQueue(CameraReceiverMethod, false);
        }
Example #3
0
        public void OpenConnection()
        {
            if (Connected)
            {
                throw new InvalidOperationException("Already connected!");
            }

            _requestedStop = false;

            Exception exception = null;

            _networkingTaskQueue.DoWorkInQueue(() =>
            {
                try
                {
                    var ipEndPoint = new IPEndPoint(IPAddress.Parse(TxtInterface.Ip), TxtInterface.ControllerIpPort);
                    _socket        = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
                    {
                        SendTimeout    = TxtInterface.TcpTimeout,
                        ReceiveTimeout = TxtInterface.TcpTimeout
                    };
                    _socket.Connect(ipEndPoint);
                    Connected = _socket.Connected;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    exception = e;
                    Connected = false;
                }
            }, true);

            if (exception != null)
            {
                Connected = false;
                throw exception;
            }
        }
Example #4
0
        private void CameraReceiverMethod()
        {
            byte[] framedata = new byte[1];

            var responseCameraFrame = new ResponseCameraFrame();


            while (!RequestedStop)
            {
                try
                {
                    var responseBytes = new byte[responseCameraFrame.GetResponseLength()];

                    // Receive the first part of the frame. This part contains the informations like height, width or length
                    Receive(_socket, responseBytes, responseBytes.Length);

                    try
                    {
                        responseCameraFrame.FromByteArray(responseBytes);
                    }
                    catch (InvalidOperationException) // Error while receiving one frame. Close camera server
                    {
                        RequestedStop = true;
                        TxtCommunication.SendCommand(new CommandStopCamera(), new ResponseStopCamera());

                        DisconnectFromCameraServerMethod();
                        break;
                    }


                    // Use the existing framedata object and resize if needed
                    if (framedata.Length < responseCameraFrame.FrameSizeCompressed + 2)
                    {
                        Array.Resize(ref framedata, responseCameraFrame.FrameSizeCompressed + 2);
                    }


                    // Receive the second part of the frame. This part contains the compressed JPEG data
                    Receive(_socket, framedata, responseCameraFrame.FrameSizeCompressed);

                    // Add the missing EOI (End of image) tag
                    framedata[framedata.Length - 2] = 0xFF;
                    framedata[framedata.Length - 1] = 0xD9;

                    // Store the received frame in the responseCameraFrame object
                    responseCameraFrame.FrameData = framedata;

                    // Process the received frame in another thread queue so that we can continue receiving frames
                    ReceivedFrames.Enqueue(responseCameraFrame);
                    _frameProcessingTaskQueue.DoWorkInQueue(() =>
                    {
                        if (!ReceivedFrames.IsEmpty)
                        {
                            ResponseCameraFrame frame;
                            if (ReceivedFrames.TryDequeue(out frame) && !RequestedStop)
                            {
                                FrameReceivedEventArgs eventArgs = new FrameReceivedEventArgs(framedata, responseCameraFrame.FrameSizeCompressed + 2);
                                FrameReceived?.Invoke(this, eventArgs);
                            }
                        }
                    }, false);


                    // Send an acknowledge
                    _socket.Send(BitConverter.GetBytes(TxtInterface.AcknowledgeIdCameraOnlineFrame));
                }
                catch (Exception)
                {
                }
            }
        }