Ejemplo n.º 1
0
        public static void When(this WebSocket socket, Action <byte[]> received, Action <WebSocketCloseStatus> closed)
        {
            var key = socket.GetHashCode();

            if (received != null)
            {
                if (received_actions.ContainsKey(key))
                {
                    var temp = Delegate.Combine(received_actions[key], received);
                    received_actions[key] = temp;
                }
                else
                {
                    received_actions[key] = received;
                }
            }
            if (closed != null)
            {
                if (closed_actions.ContainsKey(key))
                {
                    var temp = Delegate.Combine(closed_actions[key], closed);
                    closed_actions[key] = temp;
                }
                else
                {
                    closed_actions[key] = closed;
                }
            }
            Run(socket);
        }
Ejemplo n.º 2
0
        public static void ClearActions(this WebSocket socket)
        {
            try
            {
                var key = socket.GetHashCode();
                if (received_actions.TryRemove(key, out Delegate a))
                {
                    a = null;
                }
                if (closed_actions.TryRemove(key, out a))
                {
                    a = null;
                }

                if (task_tokens.TryRemove(key, out CancellationTokenSource t))
                {
                    t.Cancel();
                    t.Dispose();
                }
            }
            catch { }
        }
Ejemplo n.º 3
0
 public static void RemoveAction(this WebSocket socket, Delegate action)
 {
     try
     {
         var key = socket.GetHashCode();
         if (action is Action <byte[]> )
         {
             if (received_actions.ContainsKey(key))
             {
                 var temp = Delegate.RemoveAll(received_actions[key], action);
                 received_actions[key] = temp;
             }
         }
         else
         {
             if (closed_actions.ContainsKey(key))
             {
                 var temp = Delegate.RemoveAll(closed_actions[key], action);
                 closed_actions[key] = temp;
             }
         }
     }
     catch { }
 }
Ejemplo n.º 4
0
        private static void Run(WebSocket socket)
        {
            var key = socket.GetHashCode();

            if (task_tokens.ContainsKey(key))
            {
                return;
            }

            var cts     = new CancellationTokenSource();
            var context = SynchronizationContext.Current;

            Task.Run(async() =>
            {
                while (true)
                {
                    try
                    {
                        if (cts.IsCancellationRequested)
                        {
                            //if (closed_actions.ContainsKey(key))
                            //{
                            //    var action = closed_actions[key];
                            //    context.Post(_ =>
                            //    {
                            //        action.DynamicInvoke(WebSocketCloseStatus.InternalServerError);
                            //    }, null);
                            //}
                            break;
                        }

                        var data = await socket.ReceiveAsync();

                        if (received_actions.ContainsKey(key))
                        {
                            var action = received_actions[key];
                            context.Post(_ =>
                            {
                                action.DynamicInvoke(data);
                            }, null);
                        }

                        if (socket.CloseStatus.HasValue && closed_actions.ContainsKey(key))
                        {
                            var action = closed_actions[key];
                            context.Post(_ =>
                            {
                                action.DynamicInvoke(socket.CloseStatus);
                            }, null);
                            ClearActions(socket);
                            break;
                        }
                    }
                    catch
                    {
                        if (closed_actions.ContainsKey(key))
                        {
                            var state  = socket.CloseStatus.HasValue ? socket.CloseStatus.Value : WebSocketCloseStatus.Empty;
                            var action = closed_actions[key];
                            context.Post(_ =>
                            {
                                action.DynamicInvoke(state);
                            }, null);
                        }
                        ClearActions(socket);
                        break;
                    }
                }
            }, cts.Token).ConfigureAwait(false);
            task_tokens[key] = cts;
        }