Ejemplo n.º 1
0
        public HostWebSocket(int id, string url, IEnumerable <string> protocols, dynamic callback,
                             ScheduleTaskHandler scheduler) :
            base(scheduler)
        {
            _id = id;
            _cancellationTokenSource = new CancellationTokenSource();

            _socket = new ClientWebSocket();
            foreach (var protocol in protocols)
            {
                _socket.Options.AddSubProtocol(protocol);
            }
            //_socket.Options.UseDefaultCredentials = true;

            MemoryStream ms     = null;
            var          buffer = new byte[4096];

            Schedule($"{nameof(HostWebSocket)}[{id}]", _cancellationTokenSource.Token,
                     async cancellationToken =>
            {
                try
                {
                    await _socket.ConnectAsync(new Uri(url), cancellationToken);
                }
                catch (OperationCanceledException) { /* ignore */ }
                catch (WebSocketException e)
                {
                    callback("error", e.GetBaseException().Message);
                    return;
                }

                callback("open", null);

                while (true)
                {
                    var receiveTask = _socket.ReceiveAsync(new ArraySegment <byte>(buffer), cancellationToken);
                    var result      = await receiveTask;
                    ms = ms ?? new MemoryStream();
                    await ms.WriteAsync(buffer, 0, result.Count, cancellationToken);
                    if (result.EndOfMessage)
                    {
                        var data = Encoding.UTF8.GetString(ms.ToArray());
                        ms       = null;
                        try
                        {
                            callback("receive", data);
                        }
                        catch (Exception e)
                        {
                            ErrorLog?.Invoke(this, e, null);
                        }
                    }
                }
                // ReSharper disable once FunctionNeverReturns
            });
        }
Ejemplo n.º 2
0
 public TimerService(ScheduleTaskHandler scheduler) :
     base(scheduler)
 {
 }
Ejemplo n.º 3
0
        public Host(Action <string> loader,
                    ConsoleService console,
                    TaskScheduler taskScheduler)
        {
            _loader = loader ?? throw new ArgumentNullException(nameof(loader));

            if (taskScheduler == null)
            {
                throw new ArgumentNullException(nameof(taskScheduler));
            }

            var scheduler = _scheduler = Schedule;

            Console = console;
            Timer   = new TimerService(scheduler);
            Xhr     = new XhrService(scheduler);

            void Schedule(ILogSource source, string name, CancellationToken cancellationToken,
                          Func <CancellationToken, Task> onSchedule,
                          Action <Exception> onError,
                          Action <OperationCanceledException> onCancel,
                          Action onFinally)
            {
                var task =
                    AsyncTask.Create(name,
                                     async thisTask =>
                {
                    try
                    {
                        await onSchedule(cancellationToken);
                        TaskFinishing?.Invoke(this, thisTask);
                        thisTask.FlagSuccess();
                    }
                    catch (OperationCanceledException e)
                    {
                        try
                        {
                            source.WarnLog?.Invoke(source, e, name);
                            onCancel?.Invoke(e);
                        }
                        finally
                        {
                            TaskFinishing?.Invoke(this, thisTask);
                            thisTask.FlagCanceled(e.CancellationToken);
                        }
                    }
                    catch (Exception e)
                    {
                        try
                        {
                            source.ErrorLog?.Invoke(source, e, name);
                            onError?.Invoke(e);
                        }
                        finally
                        {
                            TaskFinishing?.Invoke(this, thisTask);
                            thisTask.FlagError(e);
                        }
                    }
                    finally
                    {
                        try
                        {
                            onFinally?.Invoke();
                        }
                        finally
                        {
                            TaskFinished?.Invoke(this, thisTask);
                        }
                    }
                });

                TaskStarting?.Invoke(this, task);
                task.Start(taskScheduler);
            }
        }