Пример #1
0
        public Task StopAsync()
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(nameof(RosbridgeMessageDispatcher));
            }

            if (this.CurrentState != RosbridgeMessageDispatcherStates.Started)
            {
                throw new RosbridgeMessageDispatcherNotInStartedStateException();
            }

            this.CurrentState = RosbridgeMessageDispatcherStates.Stopping;

            return(Task.Run(async() =>
            {
                if (this.socket != null)
                {
                    await this.socket.DisconnectAsync();
                }

                if (this.receivingTask != null)
                {
                    await this.receivingTask;
                    this.receivingTask = null;
                }

                this.CurrentState = RosbridgeMessageDispatcherStates.Stopped;
            }));
        }
Пример #2
0
        public void Dispose()
        {
            if (this.disposed)
            {
                return;
            }

            this.disposed     = true;
            this.CurrentState = RosbridgeMessageDispatcherStates.Stopped;

            Task.Run(async() =>
            {
                if (this.socket != null)
                {
                    await this.socket.DisconnectAsync();
                    this.socket.Dispose();
                    this.socket = null;
                }

                if (this.receivingTask != null)
                {
                    await this.receivingTask;
                    this.receivingTask = null;
                }

                this.rosbridgeMessageSerializer = null;
            });

            GC.SuppressFinalize(this);
        }
Пример #3
0
        public Task StartAsync()
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(nameof(RosbridgeMessageDispatcher));
            }

            if (this.CurrentState != RosbridgeMessageDispatcherStates.Stopped)
            {
                throw new RosbridgeMessageDispatcherNotInStoppedStateException();
            }

            Task startingTask = Task.Run(async() =>
            {
                this.CurrentState = RosbridgeMessageDispatcherStates.Starting;

                try
                {
                    await this.socket.ConnectAsync();
                }
                catch
                {
                    this.CurrentState = RosbridgeMessageDispatcherStates.Stopped;
                    throw;
                }

                this.CurrentState = RosbridgeMessageDispatcherStates.Started;
            });

            this.receivingTask = startingTask.ContinueWith(async(socketStartingTask) =>
            {
                while (this.socket.IsConnected && this.CurrentState == RosbridgeMessageDispatcherStates.Started)
                {
                    byte[] serializedMessage = await this.socket.ReceiveAsync();

                    JObject jsonMessage = this.rosbridgeMessageSerializer.Deserialize(serializedMessage);

                    this.RosbridgeMessageReceived?.Invoke(this, new RosbridgeMessageReceivedEventArgs(jsonMessage));
                }
            });

            return(startingTask);
        }