Exemple #1
0
 public void AddFrame(IVideoProvider source)
 {
     while (!threadQ.TryAdd(new VideoCopy(source), 1000))
     {
         if (!workerT.IsAlive)
         {
             throw new Exception("AVI Worker thread died!");
         }
     }
 }
Exemple #2
0
 /// <summary>
 /// 解压缩字节数据
 /// </summary>
 /// <param name="data">原字节数据</param>
 /// <returns>解压缩后字节数据</returns>
 public static byte[] Decompress(this byte[] data)
 {
     byte[] buffer;
     _bytesQueue.TryTake(out buffer, 100);
     buffer = buffer ?? new byte[3 * 1024];
     try
     {
         var ms  = new MemoryStream(data);
         var zip = new GZipStream(ms, CompressionMode.Decompress, true);
         using (var msreader = new MemoryStream())
         {
             while (true)
             {
                 var reader = zip.Read(buffer, 0, buffer.Length);
                 if (reader <= 0)
                 {
                     break;
                 }
                 msreader.Write(buffer, 0, reader);
             }
             zip.Close();
             ms.Close();
             msreader.Position = 0;
             return(msreader.ToArray());
         }
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
     finally
     {
         _bytesQueue.TryAdd(buffer, 100);
     }
 }
Exemple #3
0
 public void setVal(DateTime ts)
 {
     Console.WriteLine("try to add " + ts.ToLongTimeString());
     while (!col.TryAdd(ts))
     {
         deleteOlder();
     }
     ShowContent(" ++ ");
 }
Exemple #4
0
        private async void StartListen(CancellationToken token)
        {
            var buffer       = new byte[ReceiveChunkSize];
            var stringResult = new StringBuilder();

            try
            {
                while (WebSocket?.State == System.Net.WebSockets.WebSocketState.Open)
                {
                    WebSocketReceiveResult result;
                    do
                    {
                        stringResult.Clear();
                        result = await WebSocket?.ReceiveAsync(new ArraySegment <byte>(buffer), token);

                        if (result != null)
                        {
                            if (result.MessageType == WebSocketMessageType.Close)
                            {
                                await WebSocket?.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);

                                Closed(this);
                                OnClose?.Invoke(this, null);
                            }
                            else if (result.Count > 0 && buffer != null)
                            {
                                stringResult?.Append(Encoding.UTF8.GetString(buffer, 0, result.Count));
                            }
                        }
                    } while (result != null && !result.EndOfMessage);

                    if (stringResult.Length > 0)
                    {
                        messageCollection.TryAdd(stringResult.ToString());
                    }
                }
            }
            catch (Exception)
            {
                Closed(this);
                OnClose?.Invoke(this, null);
            }
            finally
            {
                WebSocket.Dispose();
            }
        }