forked from judero01col/GMap.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileHttpHost.cs
More file actions
158 lines (141 loc) · 5.02 KB
/
Copy pathTileHttpHost.cs
File metadata and controls
158 lines (141 loc) · 5.02 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
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using GMap.NET.MapProviders;
namespace GMap.NET.Internals
{
internal class TileHttpHost
{
volatile bool _listen;
TcpListener _server;
int _port;
readonly byte[] _responseHeaderBytes;
public TileHttpHost()
{
string response = "HTTP/1.0 200 OK\r\nContent-Type: image\r\nConnection: close\r\n\r\n";
_responseHeaderBytes = Encoding.ASCII.GetBytes(response);
}
public void Stop()
{
if (_listen)
{
_listen = false;
if (_server != null)
{
_server.Stop();
}
}
}
public void Start(int port)
{
if (_server == null)
{
this._port = port;
_server = new TcpListener(IPAddress.Any, port);
}
else
{
if (this._port != port)
{
Stop();
this._port = port;
_server = null;
_server = new TcpListener(IPAddress.Any, port);
}
else
{
if (_listen)
{
return;
}
}
}
_server.Start();
_listen = true;
var t = new Thread(() =>
{
Debug.WriteLine("TileHttpHost: " + _server.LocalEndpoint);
while (_listen)
{
try
{
if (!_server.Pending())
{
Thread.Sleep(111);
}
else
{
ThreadPool.QueueUserWorkItem(ProcessRequest, _server.AcceptTcpClient());
}
}
catch (Exception ex)
{
Debug.WriteLine("TileHttpHost: " + ex);
}
}
Debug.WriteLine("TileHttpHost: stoped");
});
t.Name = "TileHost";
t.IsBackground = true;
t.Start();
}
void ProcessRequest(object p)
{
try
{
using (var c = p as TcpClient)
{
using (var s = c.GetStream())
{
using (var r = new StreamReader(s, Encoding.UTF8))
{
string request = r.ReadLine();
if (!string.IsNullOrEmpty(request) && request.StartsWith("GET"))
{
//Debug.WriteLine("TileHttpHost: " + request);
// http://localhost:88/88888/5/15/11
// GET /8888888888/5/15/11 HTTP/1.1
var rq = request.Split(' ');
if (rq.Length >= 2)
{
var ids = rq[1].Split(new[] {'/'}, StringSplitOptions.RemoveEmptyEntries);
if (ids.Length == 4)
{
int dbId = int.Parse(ids[0]);
int zoom = int.Parse(ids[1]);
int x = int.Parse(ids[2]);
int y = int.Parse(ids[3]);
var pr = GMapProviders.TryGetProvider(dbId);
if (pr != null)
{
Exception ex;
var img = GMaps.Instance.GetImageFrom(pr, new GPoint(x, y), zoom, out ex);
if (img != null)
{
using (img)
{
s.Write(_responseHeaderBytes, 0, _responseHeaderBytes.Length);
img.Data.WriteTo(s);
}
}
}
}
}
}
}
}
c.Close();
}
}
catch (Exception ex)
{
Debug.WriteLine("TileHttpHost, ProcessRequest: " + ex);
}
//Debug.WriteLine("disconnected");
}
}
}