-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUtils.cs
More file actions
166 lines (152 loc) · 6.51 KB
/
Utils.cs
File metadata and controls
166 lines (152 loc) · 6.51 KB
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
/*
Copyright (C) <2007-2017> <Kay Diefenthal>
SatIp.RtspSample is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SatIp.RtspSample is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SatIp.RtspSample. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace SatIp.RtspSample
{
public class Utils
{
public static List<M3uService> GetStationsFromLocalFile_m3u(string fileName)
{
using (StreamReader reader = File.OpenText(fileName))
{
string[] strArray = reader.ReadToEnd().Split(new[] { "\n", "\r", "\n\r", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
var list = new List<M3uService>();
if (strArray[0].Trim().ToUpper() == "#EXTM3U")
{
var name = string.Empty;
var parameters = new string[15];
for (int i = 0; i < strArray.Length; i++)
{
if (strArray[i].StartsWith("#EXTINF", StringComparison.CurrentCultureIgnoreCase))
{
var strArray2 = strArray[i].Split(new[] { ":", "," }, StringSplitOptions.None);
if (strArray2.Length > 2)
{
name = strArray2[2];
parameters = strArray[++i].Split('&');
}
list.Add(new M3uService(name, parameters));
}
else if (strArray[i].StartsWith("# ", StringComparison.CurrentCultureIgnoreCase))
{
name = strArray[i].Substring(2);
}
}
}
return list;
}
}
public static int Convert2BytesToInt(byte[] buffer, int offset)
{
int temp = (int)buffer[offset];
temp = (temp * 256) + buffer[offset + 1];
return (temp);
}
public static int Convert3BytesToInt(byte[] buffer, int offset)
{
int temp = (int)buffer[offset];
temp = (temp * 256) + buffer[offset + 1];
temp = (temp * 256) + buffer[offset + 2];
return (temp);
}
public static int Convert4BytesToInt(byte[] buffer, int offset)
{
int temp =(int)buffer[offset];
temp = (temp * 256) + buffer[offset + 1];
temp = (temp * 256) + buffer[offset + 2];
temp = (temp * 256) + buffer[offset + 3];
return (temp);
}
public static long Convert4BytesToLong(byte[] buffer, int offset)
{
long temp = 0;
for (int index = 0; index < 4; index++)
temp = (temp * 256) + buffer[offset + index];
return (temp);
}
public static long Convert8BytesToLong(byte[] buffer, int offset)
{
long temp = 0;
for (int index = 0; index < 8; index++)
temp = (temp * 256) + buffer[offset + index];
return (temp);
}
public static string ConvertBytesToString(byte[] buffer, int offset, int length)
{
StringBuilder reply = new StringBuilder(4);
for (int index = 0; index < length; index++)
reply.Append((char)buffer[offset + index]);
return (reply.ToString());
}
public static DateTime NptTimestampToDateTime(long nptTimestamp) { return NptTimestampToDateTime((uint)((nptTimestamp >> 32) & 0xFFFFFFFF), (uint)(nptTimestamp & 0xFFFFFFFF),null); }
public static DateTime NptTimestampToDateTime(uint seconds, uint fractions, DateTime? epoch )
{
ulong ticks =(ulong)((seconds * TimeSpan.TicksPerSecond) + ((fractions * TimeSpan.TicksPerSecond) / 0x100000000L));
if (epoch.HasValue) return epoch.Value + TimeSpan.FromTicks((Int64)ticks);
return (seconds & 0x80000000L) == 0 ? UtcEpoch2036 + TimeSpan.FromTicks((Int64)ticks) : UtcEpoch1900 + TimeSpan.FromTicks((Int64)ticks);
}
//When the First Epoch will wrap (The real Y2k)
public static DateTime UtcEpoch2036 = new DateTime(2036, 2, 7, 6, 28, 16, DateTimeKind.Utc);
public static DateTime UtcEpoch1900 = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static DateTime UtcEpoch1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>
/// Split SSDP Response in a Key Value Dictonary
/// </summary>
/// <param name="searchResponse"></param>
/// <returns></returns>
public static Dictionary<string, string> ProcessSsdpResponse(string searchResponse)
{
var reader = new StringReader(searchResponse);
var line = string.Empty;
var values = new Dictionary<string, string>();
while ((line = reader.ReadLine()) != null)
{
if (line == "")
{
continue;
}
var colon = line.IndexOf(':');
if (colon < 1)
{
return null;
}
var name = line.Substring(0, colon).Trim();
var value = line.Substring(colon + 1).Trim();
if (string.IsNullOrEmpty(name))
{
return null;
}
values[name.ToLowerInvariant()] = value;
}
return values;
}
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("Local IP Address Not Found!");
}
}
}