-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhotonPacketHandler.cs
98 lines (95 loc) · 2.66 KB
/
PhotonPacketHandler.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.IO;
using System.Net;
using ExitGames.Client.Photon;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Transport;
namespace AODPSo
{
internal class PhotonPacketHandler
{
public PhotonPacketHandler(PacketHandler p)
{
this._eventHandler = p;
}
public void PacketHandler(Packet packet)
{
Protocol16 protocol16 = new Protocol16();
UdpDatagram udp = packet.Ethernet.IpV4.Udp;
if (udp.SourcePort != 5056 && udp.DestinationPort != 5056)
{
return;
}
BinaryReader p = new BinaryReader(udp.Payload.ToMemoryStream());
IPAddress.NetworkToHostOrder((int)p.ReadUInt16());
p.ReadByte();
byte commandCount = p.ReadByte();
IPAddress.NetworkToHostOrder(p.ReadInt32());
IPAddress.NetworkToHostOrder(p.ReadInt32());
int commandHeaderLength = 12;
int signifierByteLength = 1;
for (int commandIdx = 0; commandIdx < (int)commandCount; commandIdx++)
{
try
{
byte commandType = p.ReadByte();
p.ReadByte();
p.ReadByte();
p.ReadByte();
int commandLength = IPAddress.NetworkToHostOrder(p.ReadInt32());
IPAddress.NetworkToHostOrder(p.ReadInt32());
switch (commandType)
{
case 4:
goto postPositionUpdate;
case 5:
goto prePositionUpdate;
case 6:
break;
case 7:
p.BaseStream.Position += 4L;
commandLength -= 4;
break;
default:
goto prePositionUpdate;
}
p.BaseStream.Position += (long)signifierByteLength;
byte messageType = p.ReadByte();
int operationLength = commandLength - commandHeaderLength - 2;
StreamBuffer payload = new StreamBuffer(p.ReadBytes(operationLength));
switch (messageType)
{
case 2:
{
OperationRequest requestData = protocol16.DeserializeOperationRequest(payload);
this._eventHandler.OnRequest(requestData.OperationCode, requestData.Parameters);
goto postPositionUpdate;
}
case 3:
{
OperationResponse responseData = protocol16.DeserializeOperationResponse(payload);
this._eventHandler.OnResponse(responseData.OperationCode, responseData.ReturnCode, responseData.Parameters);
goto postPositionUpdate;
}
case 4:
{
EventData eventData = protocol16.DeserializeEventData(payload);
this._eventHandler.OnEvent(eventData.Code, eventData.Parameters);
goto postPositionUpdate;
}
default:
p.BaseStream.Position += (long)operationLength;
goto postPositionUpdate;
}
prePositionUpdate:
p.BaseStream.Position += (long)(commandLength - commandHeaderLength);
postPositionUpdate:;
}
catch (Exception)
{
}
}
}
private PacketHandler _eventHandler;
}
}