-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathServerEventNetworkLayer.cs
209 lines (184 loc) · 8.96 KB
/
ServerEventNetworkLayer.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Channels;
using System.Threading.Tasks;
using AltV.Net.EntitySync.Events;
namespace AltV.Net.EntitySync.ServerEvent
{
public class ServerEventNetworkLayer : NetworkLayer
{
public override event ConnectionConnectEventDelegate OnConnectionConnect;
public override event ConnectionDisconnectEventDelegate OnConnectionDisconnect;
public override event ClientSubscribeEntityDelegate OnClientSubscribeEntity;
public override event ClientUnsubscribeEntityDelegate OnClientUnsubscribeEntity;
public override event EntityCreateEventDelegate OnEntityCreate;
public override event EntityRemoveEventDelegate OnEntityRemove;
public override event EntityPositionUpdateEventDelegate OnEntityPositionUpdate;
public override event EntityDataUpdateEventDelegate OnEntityDataUpdate;
public override event EntityNetOwnerUpdateEventDelegate OnEntityNetOwnerUpdate;
private readonly struct ServerEntityEvent
{
public readonly byte EventType;
public readonly IEntity Entity;
public readonly Vector3 Position;
public readonly IDictionary<string, object> ChangedData;
public readonly bool NetOwner;
public ServerEntityEvent(byte eventType, IEntity entity, Vector3 position,
IDictionary<string, object> changedData, bool netOwner)
{
EventType = eventType;
Entity = entity;
Position = position;
ChangedData = changedData;
NetOwner = netOwner;
}
public ServerEntityEvent(byte eventType, IEntity entity) : this(eventType,
entity, Vector3.Zero, null, false)
{
}
}
private readonly IDictionary<IClient, Channel<ServerEntityEvent>> serverEventChannels =
new Dictionary<IClient, Channel<ServerEntityEvent>>();
public ServerEventNetworkLayer(ulong threadCount, IClientRepository clientRepository) : base(threadCount,
clientRepository)
{
Alt.OnPlayerConnect += (player, reason) =>
{
var playerClient = new PlayerClient(threadCount, player.Id.ToString(), player);
player.SetEntitySyncClient(playerClient);
clientRepository.Add(playerClient);
Task.Factory.StartNew(async obj =>
{
var currPlayerClient = (PlayerClient) obj;
var channel = Channel.CreateUnbounded<ServerEntityEvent>(new UnboundedChannelOptions
{SingleReader = true});
lock (serverEventChannels)
{
serverEventChannels[currPlayerClient] = channel;
}
var channelReader = channel.Reader;
while (await channelReader.WaitToReadAsync())
{
while (channelReader.TryRead(out var entityEvent))
{
try
{
switch (entityEvent.EventType)
{
case 1:
if (entityEvent.ChangedData != null)
{
currPlayerClient.Emit("entitySync:create", entityEvent.Entity.Id,
entityEvent.Entity.Type, entityEvent.Entity.Position,
entityEvent.ChangedData);
}
else
{
currPlayerClient.Emit("entitySync:create", entityEvent.Entity.Id,
entityEvent.Entity.Type, entityEvent.Entity.Position);
}
break;
case 2:
currPlayerClient.Emit("entitySync:remove", entityEvent.Entity.Id,
entityEvent.Entity.Type);
break;
case 3:
currPlayerClient.Emit("entitySync:updatePosition", entityEvent.Entity.Id,
entityEvent.Entity.Type, entityEvent.Position);
break;
case 4:
currPlayerClient.Emit("entitySync:updateData", entityEvent.Entity.Id,
entityEvent.Entity.Type, entityEvent.ChangedData);
break;
case 5:
currPlayerClient.Emit("entitySync:clearCache", entityEvent.Entity.Id,
entityEvent.Entity.Type);
break;
case 6:
currPlayerClient.Emit("entitySync:netOwner", entityEvent.Entity.Id,
entityEvent.Entity.Type, entityEvent.NetOwner);
break;
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
}, playerClient);
};
Alt.OnPlayerDisconnect += (player, reason) =>
{
var client = clientRepository.Remove(player.Id.ToString());
if (client == null) return;
Channel<ServerEntityEvent> channel;
lock (serverEventChannels)
{
if (!serverEventChannels.Remove(client, out channel)) return;
}
channel.Writer.Complete();
};
}
public override void SendEvent(IClient client, in EntityCreateEvent entityCreate)
{
Channel<ServerEntityEvent> channel;
lock (serverEventChannels)
{
if (!serverEventChannels.TryGetValue(client, out channel)) return;
}
channel.Writer.TryWrite(new ServerEntityEvent(1, entityCreate.Entity, Vector3.Zero, entityCreate.Data,
false));
}
public override void SendEvent(IClient client, in EntityRemoveEvent entityRemove)
{
Channel<ServerEntityEvent> channel;
lock (serverEventChannels)
{
if (!serverEventChannels.TryGetValue(client, out channel)) return;
}
channel.Writer.TryWrite(new ServerEntityEvent(2, entityRemove.Entity));
}
public override void SendEvent(IClient client, in EntityPositionUpdateEvent entityPositionUpdate)
{
Channel<ServerEntityEvent> channel;
lock (serverEventChannels)
{
if (!serverEventChannels.TryGetValue(client, out channel)) return;
}
channel.Writer.TryWrite(new ServerEntityEvent(3, entityPositionUpdate.Entity,
entityPositionUpdate.Position, null, false));
}
public override void SendEvent(IClient client, in EntityDataChangeEvent entityDataChange)
{
Channel<ServerEntityEvent> channel;
lock (serverEventChannels)
{
if (!serverEventChannels.TryGetValue(client, out channel)) return;
}
channel.Writer.TryWrite(new ServerEntityEvent(4, entityDataChange.Entity,
Vector3.Zero, entityDataChange.Data, false));
}
public override void SendEvent(IClient client, in EntityClearCacheEvent entityClearCache)
{
Channel<ServerEntityEvent> channel;
lock (serverEventChannels)
{
if (!serverEventChannels.TryGetValue(client, out channel)) return;
}
channel.Writer.TryWrite(new ServerEntityEvent(5, entityClearCache.Entity,
Vector3.Zero, null, false));
}
public override void SendEvent(IClient client, in EntityNetOwnerChangeEvent entityNetOwnerChange)
{
Channel<ServerEntityEvent> channel;
lock (serverEventChannels)
{
if (!serverEventChannels.TryGetValue(client, out channel)) return;
}
channel.Writer.TryWrite(new ServerEntityEvent(6, entityNetOwnerChange.Entity, Vector3.Zero, null,
entityNetOwnerChange.State));
}
}
}