-
Notifications
You must be signed in to change notification settings - Fork 2
/
CrowdRequest.cs
79 lines (66 loc) · 1.89 KB
/
CrowdRequest.cs
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
using System.Net.Sockets;
using System.Text;
using Newtonsoft.Json;
namespace ControlValley
{
public class CrowdRequest
{
public static readonly int RECV_BUF = 4096;
public static readonly int RECV_TIME = 5000000;
public string code;
public int id;
public string type;
public string viewer;
public static CrowdRequest Recieve(ControlClient client, Socket socket)
{
byte[] buf = new byte[RECV_BUF];
string content = "";
int read = 0;
do
{
if (!client.IsRunning()) return null;
if (socket.Poll(RECV_TIME, SelectMode.SelectRead))
{
read = socket.Receive(buf);
if (read < 0) return null;
content += Encoding.ASCII.GetString(buf);
}
else
CrowdResponse.KeepAlive(socket);
} while (read == 0 || (read == RECV_BUF && buf[RECV_BUF - 1] != 0));
return JsonConvert.DeserializeObject<CrowdRequest>(content);
}
public enum Type
{
REQUEST_TEST,
REQUEST_START,
REQUEST_STOP,
REQUEST_KEEPALIVE=255
}
public string GetReqCode()
{
return this.code;
}
public int GetReqID()
{
return this.id;
}
public Type GetReqType()
{
string value = this.type;
if (value == "1")
return Type.REQUEST_START;
else if (value == "2")
return Type.REQUEST_STOP;
return Type.REQUEST_TEST;
}
public string GetReqViewer()
{
return this.viewer;
}
public bool IsKeepAlive()
{
return id == 0 && type == "255";
}
}
}