aboutsummaryrefslogtreecommitdiff
path: root/RSSDP/SsdpHelper.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2016-11-14 14:32:43 -0500
committerLuke Pulverenti <luke.pulverenti@gmail.com>2016-11-14 14:32:43 -0500
commit180ab02edc62ad6e73ece198bd2f9887468eb159 (patch)
tree76bb3f26c72115e75250d73e23a51cdab931e7ba /RSSDP/SsdpHelper.cs
parentc2e9df41dc288c3a57e98cb8d89c6c49ecb59814 (diff)
update msearch
Diffstat (limited to 'RSSDP/SsdpHelper.cs')
-rw-r--r--RSSDP/SsdpHelper.cs88
1 files changed, 88 insertions, 0 deletions
diff --git a/RSSDP/SsdpHelper.cs b/RSSDP/SsdpHelper.cs
new file mode 100644
index 000000000..2eacf3c11
--- /dev/null
+++ b/RSSDP/SsdpHelper.cs
@@ -0,0 +1,88 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+using MediaBrowser.Model.Net;
+using MediaBrowser.Model.Text;
+
+namespace RSSDP
+{
+ public class SsdpHelper
+ {
+ private readonly ITextEncoding _encoding;
+
+ public SsdpHelper(ITextEncoding encoding)
+ {
+ _encoding = encoding;
+ }
+
+ public SsdpMessageInfo ParseSsdpResponse(byte[] data)
+ {
+ using (var ms = new MemoryStream(data))
+ {
+ using (var reader = new StreamReader(ms, _encoding.GetASCIIEncoding()))
+ {
+ var proto = (reader.ReadLine() ?? string.Empty).Trim();
+ var method = proto.Split(new[] { ' ' }, 2)[0];
+ var headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
+ for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
+ {
+ line = line.Trim();
+ if (string.IsNullOrEmpty(line))
+ {
+ break;
+ }
+ var parts = line.Split(new[] { ':' }, 2);
+
+ if (parts.Length >= 2)
+ {
+ headers[parts[0]] = parts[1].Trim();
+ }
+ }
+
+ return new SsdpMessageInfo
+ {
+ Method = method,
+ Headers = headers,
+ Message = data
+ };
+ }
+ }
+ }
+
+ public static string BuildMessage(string header, Dictionary<string, string> values)
+ {
+ var builder = new StringBuilder();
+
+ const string argFormat = "{0}: {1}\r\n";
+
+ builder.AppendFormat("{0}\r\n", header);
+
+ foreach (var pair in values)
+ {
+ builder.AppendFormat(argFormat, pair.Key, pair.Value);
+ }
+
+ builder.Append("\r\n");
+
+ return builder.ToString();
+ }
+ }
+
+ public class SsdpMessageInfo
+ {
+ public string Method { get; set; }
+
+ public IpEndPointInfo EndPoint { get; set; }
+
+ public Dictionary<string, string> Headers { get; set; }
+
+ public IpEndPointInfo LocalEndPoint { get; set; }
+ public byte[] Message { get; set; }
+
+ public SsdpMessageInfo()
+ {
+ Headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
+ }
+ }
+}