/
HTSConnectionHandler.cs
234 lines (184 loc) · 8.06 KB
/
HTSConnectionHandler.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using MediaBrowser.Controller.Drawing;
using MediaBrowser.Controller.LiveTv;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TVHeadEnd.DataHelper;
using TVHeadEnd.HTSP;
namespace TVHeadEnd
{
class HTSConnectionHandler : HTSConnectionListener
{
private readonly ILogger _logger;
private HTSConnectionAsync _htsConnection;
private string _httpBaseUrl;
private string _tvhServerName;
private int _htspPort;
private string _userName;
private string _password;
// Data helpers
private readonly ChannelDataHelper _channelDataHelper;
private Dictionary<string, string> _headers = new Dictionary<string, string>();
private List<TaskCompletionSource<bool>> connectionOpenListeners = new List<TaskCompletionSource<bool>>();
private bool _connected;
private SemaphoreSlim ConnectionSemaphore = new SemaphoreSlim(1, 1);
public HTSConnectionHandler(ILogger logger, string baseUrl, TvHeadEndProviderOptions config)
{
_logger = logger;
//System.Diagnostics.StackTrace t = new System.Diagnostics.StackTrace();
_logger.Info("[TVHclient] HTSConnectionHandler()");
_channelDataHelper = new ChannelDataHelper(logger);
init(baseUrl, config);
}
private void init(string baseUrl, TvHeadEndProviderOptions config)
{
_tvhServerName = new Uri(baseUrl).Host;
_htspPort = config.HTSP_Port;
_userName = (config.Username ?? "").Trim();
_password = (config.Password ?? "").Trim();
_httpBaseUrl = baseUrl;
string authInfo = _userName + ":" + _password;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
_headers["Authorization"] = "Basic " + authInfo;
}
public Dictionary<string, string> GetHeaders()
{
return new Dictionary<string, string>(_headers);
}
public async Task EnsureConnection(CancellationToken cancellationToken)
{
await ConnectionSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
try
{
//_logger.Info("[TVHclient] HTSConnectionHandler.ensureConnection()");
if (_htsConnection == null || _htsConnection.needsRestart())
{
_connected = false;
_logger.Info("[TVHclient] HTSConnectionHandler.ensureConnection() : create new HTS-Connection");
Version version = Assembly.GetEntryAssembly().GetName().Version;
var connection = new HTSConnectionAsync(this, "TVHclient4Emby-" + version.ToString(), "" + HTSMessage.HTSP_VERSION, _logger);
_logger.Info("[TVHclient] HTSConnectionHandler.ensureConnection: Used connection parameters: " +
"TVH Server = '" + _httpBaseUrl + "'; " +
"HTSP Port = '" + _htspPort + "'; " +
"User = '" + _userName + "'; " +
"Password set = '" + (_password.Length > 0) + "'");
await connection.open(_tvhServerName, _htspPort, cancellationToken).ConfigureAwait(false);
await connection.Authenticate(_userName, _password, cancellationToken).ConfigureAwait(false);
_htsConnection = connection;
}
else if (_connected)
{
return;
}
var taskCompletionSource = new TaskCompletionSource<bool>();
lock (connectionOpenListeners)
{
connectionOpenListeners.Add(taskCompletionSource);
}
await taskCompletionSource.Task.ConfigureAwait(false);
}
finally
{
ConnectionSemaphore.Release();
}
}
public async Task<HTSMessage> SendMessage(HTSMessage message, CancellationToken cancellationToken)
{
await EnsureConnection(cancellationToken).ConfigureAwait(false);
return await _htsConnection.SendMessage(message, cancellationToken).ConfigureAwait(false);
}
public async Task<T> SendMessage<T>(HTSMessage message, Func<HTSMessage, T> responseHandler, CancellationToken cancellationToken)
{
var response = await SendMessage(message, cancellationToken).ConfigureAwait(false);
return responseHandler(response);
}
public List<ChannelInfo> BuildChannelInfos()
{
var uri = new Uri(_httpBaseUrl);
var builder = new UriBuilder(uri);
builder.UserName = _userName;
builder.Password = _password;
var urlWithAuth = builder.ToString();
return _channelDataHelper.BuildChannelInfos(urlWithAuth);
}
public String GetHttpBaseUrl()
{
return _httpBaseUrl;
}
public void onError(Exception ex)
{
_logger.ErrorException("[TVHclient] HTSConnectionHandler recorded a HTSP error: " + ex.Message, ex);
_htsConnection.stop();
_htsConnection = null;
}
private void OnInitialDataLoadComplete()
{
_connected = true;
var list = new List<TaskCompletionSource<bool>>();
lock (connectionOpenListeners)
{
list.AddRange(connectionOpenListeners);
connectionOpenListeners.Clear();
}
foreach (var t in list)
{
t.TrySetResult(true);
}
}
public void onMessage(HTSMessage response)
{
if (response != null)
{
switch (response.Method)
{
case "tagAdd":
case "tagUpdate":
case "tagDelete":
//_logger.Fatal("[TVHclient] tad add/update/delete" + response.ToString());
break;
case "channelAdd":
case "channelUpdate":
_channelDataHelper.Add(response);
break;
case "eventAdd":
case "eventUpdate":
case "eventDelete":
// should not happen as we don't subscribe for this events.
break;
//case "subscriptionStart":
//case "subscriptionGrace":
//case "subscriptionStop":
//case "subscriptionSkip":
//case "subscriptionSpeed":
//case "subscriptionStatus":
// _logger.Fatal("[TVHclient] subscription events " + response.ToString());
// break;
//case "queueStatus":
// _logger.Fatal("[TVHclient] queueStatus event " + response.ToString());
// break;
//case "signalStatus":
// _logger.Fatal("[TVHclient] signalStatus event " + response.ToString());
// break;
//case "timeshiftStatus":
// _logger.Fatal("[TVHclient] timeshiftStatus event " + response.ToString());
// break;
//case "muxpkt": // streaming data
// _logger.Fatal("[TVHclient] muxpkt event " + response.ToString());
// break;
case "initialSyncCompleted":
OnInitialDataLoadComplete();
break;
default:
//_logger.Fatal("[TVHclient] Method '" + response.Method + "' not handled in LiveTvService.cs");
break;
}
}
}
}
}