This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
MessageType.cs
162 lines (142 loc) · 4.94 KB
/
MessageType.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
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
using System;
using System.Runtime.InteropServices;
namespace Improbable.Gdk.Core.NetworkStats
{
public enum WorldCommand
{
CreateEntity = 0,
DeleteEntity = 1,
ReserveEntityIds = 2,
EntityQuery = 3
}
public enum Direction
{
Incoming = 0,
Outgoing = 1
}
public enum MessageType
{
Update = 0,
CommandRequest = 1,
CommandResponse = 2,
WorldCommandRequest = 3,
WorldCommandResponse = 4
}
/// <summary>
/// Describes a type of a message.
/// </summary>
/// <remarks>
/// Implemented as a C-style union. Can be thought of as a sum type where the discriminants are:
/// Update, CommandRequest, CommandResponse, WorldCommandRequest, WorldCommandResponse
/// </remarks>
[StructLayout(LayoutKind.Explicit)]
public struct MessageTypeUnion : IEquatable<MessageTypeUnion>
{
[FieldOffset(offset: 0)] private MessageType Type;
[FieldOffset(sizeof(MessageType))] private uint UpdateInfo;
[FieldOffset(sizeof(MessageType))] private (uint, uint) CommandInfo;
[FieldOffset(sizeof(MessageType))] private WorldCommand WorldCommand;
public static MessageTypeUnion Update(uint componentId)
{
return new MessageTypeUnion
{
Type = MessageType.Update,
UpdateInfo = componentId
};
}
public static MessageTypeUnion CommandRequest(uint componentId, uint commandIndex)
{
return new MessageTypeUnion
{
Type = MessageType.CommandRequest,
CommandInfo = (componentId, commandIndex)
};
}
public static MessageTypeUnion CommandResponse(uint componentId, uint commandIndex)
{
return new MessageTypeUnion
{
Type = MessageType.CommandResponse,
CommandInfo = (componentId, commandIndex)
};
}
public static MessageTypeUnion WorldCommandRequest(WorldCommand worldCommand)
{
return new MessageTypeUnion
{
Type = MessageType.WorldCommandRequest,
WorldCommand = worldCommand
};
}
public static MessageTypeUnion WorldCommandResponse(WorldCommand worldCommand)
{
return new MessageTypeUnion
{
Type = MessageType.WorldCommandResponse,
WorldCommand = worldCommand
};
}
public bool Equals(MessageTypeUnion other)
{
if (Type != other.Type)
{
return false;
}
switch (Type)
{
case MessageType.Update:
return UpdateInfo == other.UpdateInfo;
case MessageType.CommandRequest:
return CommandInfo == other.CommandInfo;
case MessageType.CommandResponse:
return CommandInfo == other.CommandInfo;
case MessageType.WorldCommandRequest:
return WorldCommand == other.WorldCommand;
case MessageType.WorldCommandResponse:
return WorldCommand == other.WorldCommand;
default:
throw new ArgumentOutOfRangeException();
}
}
public override bool Equals(object obj)
{
return obj is MessageTypeUnion other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = (int) Type;
switch (Type)
{
case MessageType.Update:
hashCode = (hashCode * 397) ^ (int) UpdateInfo;
break;
case MessageType.CommandRequest:
hashCode = (hashCode * 397) ^ CommandInfo.GetHashCode();
break;
case MessageType.CommandResponse:
hashCode = (hashCode * 397) ^ CommandInfo.GetHashCode();
break;
case MessageType.WorldCommandRequest:
hashCode = (hashCode * 397) ^ (int) WorldCommand;
break;
case MessageType.WorldCommandResponse:
hashCode = (hashCode * 397) ^ (int) WorldCommand;
break;
default:
throw new ArgumentOutOfRangeException();
}
return hashCode;
}
}
public static bool operator ==(MessageTypeUnion left, MessageTypeUnion right)
{
return left.Equals(right);
}
public static bool operator !=(MessageTypeUnion left, MessageTypeUnion right)
{
return !left.Equals(right);
}
}
}