Пример #1
0
        public void Disconnect()
        {
            _connected = false;

            if (_client != null)
            {
                _client.Close();
                _client.Dispose();
                _client = null;
            }

            SetConnected(false);

            if (_sendThread != null)
            {
                if (!_sendThread.Join(DisconnectTimeout))
                {
                    _sendThread.Abort();
                }
                _sendThread = null;
            }

            if (_sendThread != null)
            {
                if (!_receiveThread.Join(DisconnectTimeout))
                {
                    _receiveThread.Abort();
                }
                _receiveThread = null;
            }

            _sendQueue.Clear();
            _previousData = null;
        }
Пример #2
0
        public void Send(DeoVrApiData newData)
        {
            if (!_connected)
            {
                return;
            }

            _sendQueue.Enqueue(newData);
        }
Пример #3
0
        private void InterpretData(DeoVrApiData data)
        {
            if (data == null)
            {
                return;
            }

            if (!_timeSource.CheckAccess())
            {
                _timeSource.Dispatcher.Invoke(() => InterpretData(data));
                return;
            }

            if (!string.IsNullOrEmpty(data.Path))
            {
                if (!String.Equals(data.Path, _previousData?.Path, StringComparison.InvariantCultureIgnoreCase))
                {
                    OnFileOpened(data.Path);
                }
            }

            if (data.PlaybackSpeed != null)
            {
                _timeSource.PlaybackRate = (float)data.PlaybackSpeed;
            }

            if (data.Duration != null)
            {
                _timeSource.SetDuration(TimeSpan.FromSeconds((float)data.Duration));
            }

            if (data.CurrentTime != null)
            {
                _timeSource.SetPosition(TimeSpan.FromSeconds((float)data.CurrentTime));
            }

            if (data.PlayerState != null)
            {
                bool isPlaying = (data.PlayerState == DeoVrPlayerState.Play);
                if (_timeSource.IsPlaying != isPlaying)
                {
                    if (isPlaying)
                    {
                        _timeSource.Play();
                    }
                    else
                    {
                        _timeSource.Pause();
                    }
                }
            }
        }
Пример #4
0
        private static void SendData(DeoVrApiData data, Stream stream)
        {
            byte[] result;

            if (data != null)
            {
                string jsonString   = JsonConvert.SerializeObject(data);
                int    stringLength = Encoding.UTF8.GetByteCount(jsonString);

                result = new byte[4 + stringLength];
                WriteInt32(stringLength, result, 0);
                Encoding.UTF8.GetBytes(jsonString, 0, jsonString.Length, result, 4);
            }
            else
            {
                result = new byte[4];
            }

            stream.Write(result, 0, result.Length);
            stream.Flush();
        }
Пример #5
0
        private void SendLoop(object arg)
        {
            Stream stream = (Stream)arg;

            try
            {
                while (_connected)
                {
                    DeoVrApiData data = _sendQueue.Dequeue(PingDelay);
                    SendData(data, stream);
                }

                _sendThread = null;
            }
            catch (ThreadAbortException)
            {
                Thread.ResetAbort();
            }
            catch (Exception)
            {
                Disconnect();
            }
        }
Пример #6
0
        private void ReceiveLoop(object arg)
        {
            Stream stream = (Stream)arg;

            stream.ReadTimeout = (int)PingDelay.TotalMilliseconds;

            try
            {
                DateTime lastReceiveTime = DateTime.UtcNow;
                byte[]   buffer          = new byte[1024 * 8];
                int      bufferPosition  = 0;

                while (_connected)
                {
                    if (DateTime.UtcNow - lastReceiveTime >= ConnectTimeout)
                    {
                        throw new Exception($"Connection timeout! (>= {ConnectTimeout.TotalSeconds:F2}s)");
                    }

                    const int headerLength = 4;

                    while (bufferPosition < headerLength)
                    {
                        int actuallyRead = stream.Read(buffer, bufferPosition, headerLength);
                        if (actuallyRead == 0)
                        {
                            return; // Socket Closed
                        }
                        bufferPosition += actuallyRead;
                    }

                    int messageLength = ReadInt32(buffer, 0);

                    while (bufferPosition < headerLength + messageLength)
                    {
                        int actuallyRead = stream.Read(buffer, bufferPosition, headerLength);
                        if (actuallyRead == 0)
                        {
                            return; // Socket Closed
                        }
                        bufferPosition += actuallyRead;
                    }

                    if (messageLength == 0)
                    {
                        // Ping
                        lastReceiveTime = DateTime.UtcNow;
                        continue;
                    }

                    string       jsonMsg = Encoding.UTF8.GetString(buffer, headerLength, messageLength);
                    DeoVrApiData data    = JsonConvert.DeserializeObject <DeoVrApiData>(jsonMsg);

                    if (data == null)
                    {
                        continue;
                    }

                    InterpretData(data);
                }
            }
            catch (ThreadAbortException)
            {
                Thread.ResetAbort();
                Disconnect();
            }
            catch (Exception)
            {
                Disconnect();
            }
        }