-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlobbyinfo.ashx
75 lines (73 loc) · 2.5 KB
/
lobbyinfo.ashx
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
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json.Linq;
public class Handler : IHttpHandler
{
static Exception _lastExcpetion = null;
static List<AllegNetLib.FMD_LS_LobbyMissionInfo> _latest = new List<AllegNetLib.FMD_LS_LobbyMissionInfo>();
public void ProcessRequest(HttpContext context)
{
if (context.Request.HttpMethod.Equals("post", StringComparison.CurrentCultureIgnoreCase)
// && context.Request.UserHostAddress == "23.99.5.217" // only let the lobby post
)
{
try
{
_lastExcpetion = null;
_latest.Clear();
using (var binaryReader = new System.IO.BinaryReader(context.Request.InputStream))
{
var bytes = binaryReader.ReadBytes(Convert.ToInt32(context.Request.InputStream.Length));
var avail = bytes.Length;
var offset = 0;
do
{
var len = BitConverter.ToUInt16(bytes, offset);
var oneSet = new byte[len];
Buffer.BlockCopy(bytes, offset, oneSet, 0, len);
offset += len;
_latest.Add(AzUnpack.FromLobbyInfo.Convert(oneSet));
avail -= len;
} while (avail > 0);
}
}
catch (Exception e)
{
_lastExcpetion = e;
throw;
}
}
else
{
context.Response.ContentType = "application/json";
string json = "";
if (_lastExcpetion == null)
{
json = Newtonsoft.Json.JsonConvert.SerializeObject(_latest.ToArray(), Newtonsoft.Json.Formatting.Indented);
}
else
{
json = JsonConvert.SerializeObject(_lastExcpetion, Formatting.Indented);
_lastExcpetion = null;
}
var bytes = System.Text.Encoding.ASCII.GetBytes(json);
context.Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(15));
context.Response.BinaryWrite(bytes);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}