-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClipBlazorFacade.cs
143 lines (123 loc) · 4.41 KB
/
ClipBlazorFacade.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using BlazorQueue;
using ClipHost.ServiceModel;
using ClipHost.ServiceModel.CreateClipFrameEventModels;
using ClipHost.ServiceModel.CreateFrameEventModels;
using ClipHost.ServiceModel.CreateStreamFrameEventModels;
using ClipHunta2;
using Microsoft.AspNetCore.SignalR.Client;
using Serilog;
using ServiceStack;
using System.ComponentModel;
using System.Diagnostics;
public class ClipBlazorFacade : BlazorInstanceTransmitter //, IProcessClipFacade
{
private readonly CancellationTokenSource _tokenSource;
private readonly JsonApiClient apiClient;
private readonly BackgroundWorker _uploader;
public ClipBlazorFacade(HubConnectionInfo parentConnectionInfo, JsonApiClient ApiClient, bool isRoot = false) : base(parentConnectionInfo,
isRoot)
{
_tokenSource = new CancellationTokenSource();
if (Connection == null) return;
Connection!.On<string, string, int>("Clip", Clip);
Connection!.On<string, int>("Watch", Watch);
Connection!.Closed += ConnectionOnClosed;
apiClient = ApiClient;
_uploader = new BackgroundWorker();
_uploader.DoWork += _uploader_DoWork;
_uploader.RunWorkerAsync();
}
private async void _uploader_DoWork(object? sender, DoWorkEventArgs e)
{
while (!_tokenSource.IsCancellationRequested)
{
await Task.Delay(500);
var events = EventRouterTask.GetEvents();
if (events == null) continue;
if (events.Length == 0) continue;
var streamEvents = events.Where(a => a.streamDefinition.StreamCaptureType == StreamCaptureType.Stream).ToArray();
var clipEvents = events.Where(a => a.streamDefinition.StreamCaptureType == StreamCaptureType.Clip).ToArray();
if (streamEvents.Length > 0)
while (!uploadEvents(streamEvents))
{
await Task.Delay(2700); // retry every 2.7 second until server comes back --
}
if (clipEvents.Length > 0)
while (!uploadEventsclip(clipEvents))
{
await Task.Delay(2700); // retry every 2.7 second until server comes back --
}
}
}
private bool uploadEventsclip((StreamDefinition streamDefinition, InternalFrameEvent frameEvent)[] events)
{
try
{
apiClient.Post(new CreateClipFrameEventRequest()
{
ClipFrameEvent = events.Select(BlazorDtoHelper.ToTransferClipDto).ToArray()
});
return true;
}
catch (Exception ex)
{
Log.Logger.Error(ex, "Big ole error"); //todo: write better error messages
}
return false;
}
private bool uploadEvents((StreamDefinition streamDefinition, InternalFrameEvent frameEvent)[] events)
{
try
{
apiClient.Post(new CreateStreamFrameEventRequest()
{
StreamFrameEvent = events.Select(BlazorDtoHelper.ToTransferStreamDto).ToArray()
});
return true;
}
catch (Exception ex)
{
Log.Logger.Error(ex, "Big ole error"); //todo: write better error messages
}
return false;
}
private async Task ConnectionOnClosed(Exception? arg)
{
_tokenSource.Cancel();
}
public async Task Watch(string streamer, int twitchStreamId)
{
try
{
await ClipBlazorFacadeHelper.Watch(streamer, twitchStreamId, apiClient);
await Connection!.SendAsync("StreamFinished", twitchStreamId);
}//todo: catch connection error and write to log
catch (Exception ex)
{
await Connection!.SendAsync("StreamError", twitchStreamId, ex.Message, ex.StackTrace);
}
}
public async Task Clip(string streamer, string clipId, int twitchClipId)
{
try
{
await ClipBlazorFacadeHelper.Clip(streamer, twitchClipId, clipId, apiClient);
await Connection!.SendAsync("ClipFinished", twitchClipId);
}//todo: catch connection error and write to log
catch (Exception ex)
{
await Connection!.SendAsync("ClipError", twitchClipId, ex.Message, ex.StackTrace);
}
}
public async Task Running()
{
try
{
await Task.Delay(-1, _tokenSource.Token);
}
catch
{
// ignored
}
}
};