wxr
2020-07-06 23c075a9c27946773feccf05abc90489a6bf5203
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Foundation;
 
namespace com.hdl.on
{
    public class Server
    {
        public static int Port=22222;
        static System.Net.HttpListener listener;
 
        /// <summary>
        /// 启动监听及处理
        /// </summary>
        internal static void Start(System.Net.IPAddress ipAddress)
        {
            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    if (listener == null)
                    {
                        listener = new System.Net.HttpListener();
 
                        listener.AuthenticationSchemes = System.Net.AuthenticationSchemes.Anonymous;
 
                        listener.Prefixes.Add(string.Format("http://{0}:{1}/", ipAddress.ToString(), Port));
 
                        listener.Start();
 
                        beginGetContext(listener);
 
                        Shared.HDLUtils.WriteLine("音乐服务启动成功!");
                    }
                }
                catch (Exception e)
                {
                    Shared.HDLUtils.WriteLine("音乐服务启动失败!" + e.Message);
                }
                finally { }
            });
        }
 
        internal static void Stop()
        {
            if (listener != null)
            {
                try
                {
                    listener.Close();
                }catch{}
            }
            listener = null;
            Shared.HDLUtils.WriteLine("音乐服务停止!");
        }
        static string getTempPath()
        {
            var dirs = NSSearchPath.GetDirectories(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.User, true);
            if (dirs == null || dirs.Length == 0)
            {
                return "";
            }
            return dirs[0];
        }
        /// <summary>
        /// 开始异步接收http请求
        /// </summary>
        /// <param name="listerner"></param>
        static void beginGetContext(System.Net.HttpListener listerner)
        {
            try
            {
 
                if (listerner == null || !listerner.IsListening)
                {
                    return;
                }
                listerner.BeginGetContext(new System.AsyncCallback(getContextCallBack), null);
 
            }
            catch
            {
                System.Threading.Thread.Sleep(1);
                beginGetContext(listerner);
            }
        }
 
        /// <summary>
        /// 获取http请求,并处理
        /// </summary>
        /// <param name="ar"></param>
        static void getContextCallBack(System.IAsyncResult ar)
        {
            beginGetContext(listener);
            try
            {
                System.Net.HttpListenerContext context = listener.EndGetContext(ar);
                manager(context);
            }
            catch { }
 
        }
 
 
        static void manager(System.Net.HttpListenerContext httpListenerContext)
        {
            System.Net.HttpListenerResponse httpListenerResponse = null;
 
            try
            {
                #region
                httpListenerResponse = httpListenerContext.Response;
 
                httpListenerResponse.StatusCode = 200;//设置返回给客服端http状态代码
                httpListenerResponse.ContentEncoding = System.Text.Encoding.UTF8;
 
 
                var nameValueCollection = HttpUtility.ParseQueryString(System.IO.Path.GetFileName(httpListenerContext.Request.RawUrl));
                if (0 < nameValueCollection.Count && System.IO.File.Exists($"{getTempPath()}/{nameValueCollection[0]}"))
                {
                    var file = new System.IO.FileStream($"{getTempPath()}/{nameValueCollection[0]}", System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    try
                    {
                        while (file.CanRead)
                        {
                            var bytes = new byte[1024];
                            var len = file.Read(bytes, 0, bytes.Length);
                            httpListenerResponse.OutputStream.Write(bytes, 0, len);
                        }
                    }
                    finally
                    {
                        file.Close();
                    }
                }
                httpListenerResponse.OutputStream.Flush();
                #endregion
            }
            catch
            {
 
            }
            finally
            {
                #region
                try
                {
                    httpListenerContext.Request.InputStream.Close();
                }
                catch { }
 
                try
                {
                    httpListenerResponse.OutputStream.Close();
                }
                catch { }
 
 
                try
                {
                    httpListenerResponse.Close();
                }
                catch { }
 
                #endregion
            }
        }
    }
}