Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NetServerSocialModule.cs
Go to the documentation of this file.
1using System;
4using System.Text;
5using rail;
7using Terraria.Net;
9
11
13{
14 private SocketConnectionAccepted _connectionAcceptedCallback;
15
16 private bool _acceptingClients;
17
19
20 private RailCallBackHelper _callbackHelper = new RailCallBackHelper();
21
23
24 private bool _serverConnected;
25
26 private RailID _serverID = new RailID();
27
28 private Action _ipcConnetedAction;
29
31
33 {
34 //IL_0001: Unknown result type (might be due to invalid IL or missing references)
35 //IL_000b: Expected O, but got Unknown
36 //IL_0017: Unknown result type (might be due to invalid IL or missing references)
37 //IL_0021: Expected O, but got Unknown
38 _lobby._lobbyCreatedExternalCallback = OnLobbyCreated;
39 }
40
42 {
43 }
44
45 private bool AcceptAnUserSession(RailID local_peer, RailID remote_peer)
46 {
47 //IL_0043: Unknown result type (might be due to invalid IL or missing references)
48 //IL_0049: Invalid comparison between Unknown and I4
49 bool result = false;
50 WeGameHelper.WriteDebugString("AcceptAnUserSession server:" + ((RailComparableID)local_peer).id_ + " remote:" + ((RailComparableID)remote_peer).id_);
51 IRailNetwork val = rail_api.RailFactory().RailNetworkHelper();
52 if (val != null)
53 {
54 result = (int)val.AcceptSessionRequest(local_peer, remote_peer) == 0;
55 }
56 return result;
57 }
58
60 {
61 IRailPlayer val = rail_api.RailFactory().RailPlayer();
62 if (val != null)
63 {
64 val.TerminateSessionOfPlayer(remote_id);
65 }
66 }
67
68 private bool CloseNetWorkSession(RailID remote_peer)
69 {
70 //IL_0018: Unknown result type (might be due to invalid IL or missing references)
71 //IL_001e: Invalid comparison between Unknown and I4
72 bool result = false;
73 IRailNetwork val = rail_api.RailFactory().RailNetworkHelper();
74 if (val != null)
75 {
76 result = (int)val.CloseSession(_serverID, remote_peer) == 0;
77 }
78 return result;
79 }
80
81 private RailID GetServerID()
82 {
83 //IL_001d: Unknown result type (might be due to invalid IL or missing references)
84 RailID val = null;
85 IRailGameServer server = _lobby.GetServer();
86 if (server != null)
87 {
88 val = server.GetGameServerRailID();
89 }
90 return (RailID)(((object)val) ?? ((object)new RailID()));
91 }
92
105
106 public void OnConnected()
107 {
108 _serverConnected = true;
109 if (_ipcConnetedAction != null)
110 {
112 }
113 _ipcConnetedAction = null;
114 WeGameHelper.WriteDebugString("IPC connected");
115 }
116
118 {
120 {
121 WeGameHelper.WriteDebugString(" - Ignoring connection from " + ((RailComparableID)data.remote_peer).id_ + " while _acceptionClients is false.");
122 return;
123 }
124 if (!_mode.HasFlag(ServerMode.FriendsOfFriends) && !IsWeGameFriend(data.remote_peer))
125 {
126 WeGameHelper.WriteDebugString("Ignoring connection from " + ((RailComparableID)data.remote_peer).id_ + ". Friends of friends is disabled.");
127 return;
128 }
129 WeGameHelper.WriteDebugString("pass wegame friend check");
130 AcceptAnUserSession(data.local_peer, data.remote_peer);
131 _connectionStateMap[data.remote_peer] = ConnectionState.Authenticating;
132 if (_connectionAcceptedCallback != null)
133 {
134 _connectionAcceptedCallback(new SocialSocket(new WeGameAddress(data.remote_peer, "")));
135 }
136 }
137
139 {
140 WeGameHelper.WriteDebugString("CreateSessionFailed, local:{0}, remote:{1}", ((RailComparableID)data.local_peer).id_, ((RailComparableID)data.remote_peer).id_);
141 CloseAndUpdateUserState(data.remote_peer);
142 }
143
145 {
146 //IL_0012: Unknown result type (might be due to invalid IL or missing references)
147 //IL_0018: Invalid comparison between Unknown and I4
148 bool result = false;
149 IRailFriends val = rail_api.RailFactory().RailFriends();
150 if (val != null)
151 {
152 result = (int)val.GetFriendsList(list) == 0;
153 }
154 return result;
155 }
156
157 private void OnWegameMessage(IPCMessage message)
158 {
159 IPCMessageType cmd = message.GetCmd();
160 if (cmd == IPCMessageType.IPCMessageTypeNotifyFriendList)
161 {
162 message.Parse<WeGameFriendListInfo>(out var value);
164 }
165 }
166
168 {
169 _wegameFriendList = friendListInfo._friendList;
170 WeGameHelper.WriteDebugString("On update friend list - " + DumpFriendListString(friendListInfo._friendList));
171 }
172
173 private bool IsWeGameFriend(RailID id)
174 {
175 bool result = false;
176 if (_wegameFriendList != null)
177 {
179 {
180 if ((RailComparableID)(object)wegameFriend.friend_rail_id == (RailComparableID)(object)id)
181 {
182 result = true;
183 break;
184 }
185 }
186 }
187 return result;
188 }
189
191 {
192 //IL_0039: Unknown result type (might be due to invalid IL or missing references)
194 foreach (RailFriendInfo item in list)
195 {
196 stringBuilder.AppendLine($"friend_id: {((RailComparableID)item.friend_rail_id).id_}, type: {item.friend_type}, online: {((object)(EnumRailPlayerOnLineState)(ref item.online_state.friend_online_state)).ToString()}, playing: {item.online_state.game_define_game_playing_state}");
197 }
198 return stringBuilder.ToString();
199 }
200
201 private bool IsActiveUser(RailID user)
202 {
204 {
205 return _connectionStateMap[user] != ConnectionState.Inactive;
206 }
207 return false;
208 }
209
211 {
212 //IL_0008: Unknown result type (might be due to invalid IL or missing references)
213 //IL_000d: Unknown result type (might be due to invalid IL or missing references)
214 //IL_001c: Unknown result type (might be due to invalid IL or missing references)
215 RailID remote_rail_id = data.remote_rail_id;
216 RailResult result = ((EventBase)data).result;
218 {
219 if ((int)result == 0)
220 {
221 WeGameHelper.WriteDebugString("UpdateUserStateBySessionAuthResult Auth Success");
223 }
224 else
225 {
226 WeGameHelper.WriteDebugString("UpdateUserStateBySessionAuthResult Auth Failed");
228 }
229 }
230 }
231
232 private bool TryAuthUserByRecvData(RailID user, byte[] data, int length)
233 {
234 WeGameHelper.WriteDebugString("TryAuthUserByRecvData user:{0}", ((RailComparableID)user).id_);
235 if (length < 3)
236 {
237 WeGameHelper.WriteDebugString("Failed to validate authentication packet: Too short. (Length: " + length + ")");
238 return false;
239 }
240 int num = (data[1] << 8) | data[0];
241 if (num != length)
242 {
243 WeGameHelper.WriteDebugString("Failed to validate authentication packet: Packet size mismatch. (" + num + "!=" + length + ")");
244 return false;
245 }
246 if (data[2] != 93)
247 {
248 WeGameHelper.WriteDebugString("Failed to validate authentication packet: Packet type is not correct. (Type: " + data[2] + ")");
249 return false;
250 }
251 return true;
252 }
253
254 private bool OnPacketRead(byte[] data, int size, RailID user)
255 {
256 if (!IsActiveUser(user))
257 {
258 WeGameHelper.WriteDebugString("OnPacketRead IsActiveUser false");
259 return false;
260 }
262 if (connectionState == ConnectionState.Authenticating)
263 {
264 if (!TryAuthUserByRecvData(user, data, size))
265 {
267 }
268 else
269 {
271 }
272 return false;
273 }
274 return connectionState == ConnectionState.Connected;
275 }
276
277 private void OnAuthSuccess(RailID remote_peer)
278 {
279 //IL_0055: Unknown result type (might be due to invalid IL or missing references)
281 {
283 int num = 3;
284 byte[] array = new byte[num];
285 array[0] = (byte)((uint)num & 0xFFu);
286 array[1] = (byte)((uint)(num >> 8) & 0xFFu);
287 array[2] = 93;
288 rail_api.RailFactory().RailNetworkHelper().SendReliableData(_serverID, remote_peer, array, (uint)num);
289 }
290 }
291
293 {
294 //IL_0038: Unknown result type (might be due to invalid IL or missing references)
295 //IL_003e: Invalid comparison between Unknown and I4
296 //IL_0053: Unknown result type (might be due to invalid IL or missing references)
297 //IL_005d: Expected O, but got Unknown
298 //IL_0040: Unknown result type (might be due to invalid IL or missing references)
299 //IL_0046: Invalid comparison between Unknown and I4
300 //IL_0060: Unknown result type (might be due to invalid IL or missing references)
301 //IL_006a: Expected O, but got Unknown
302 //IL_0048: Unknown result type (might be due to invalid IL or missing references)
303 //IL_004e: Invalid comparison between Unknown and I4
304 //IL_006d: Unknown result type (might be due to invalid IL or missing references)
305 //IL_0077: Expected O, but got Unknown
306 WeGameHelper.WriteDebugString("OnRailEvent,id=" + ((object)(RAILEventID)(ref event_id)).ToString() + " ,result=" + ((object)(RailResult)(ref data.result)).ToString());
307 if ((int)event_id != 3006)
308 {
309 if ((int)event_id != 16001)
310 {
311 if ((int)event_id == 16002)
312 {
314 }
315 }
316 else
317 {
319 }
320 }
321 else
322 {
324 }
325 }
326
327 private void OnLobbyCreated(RailID lobbyID)
328 {
333 Action action = delegate
334 {
336 {
337 _serverID = ((RailComparableID)lobbyID).id_.ToString()
338 };
340 iPCMessage.Build(IPCMessageType.IPCMessageTypeReportServerID, t);
341 WeGameHelper.WriteDebugString("Send serverID to game client - " + _client.SendMessage(iPCMessage));
342 };
344 {
345 action();
346 return;
347 }
349 WeGameHelper.WriteDebugString("report server id fail, no connection");
350 }
351
352 private void RegisterRailEvent()
353 {
354 //IL_002e: Unknown result type (might be due to invalid IL or missing references)
355 //IL_0035: Unknown result type (might be due to invalid IL or missing references)
356 //IL_003d: Unknown result type (might be due to invalid IL or missing references)
357 //IL_0047: Expected O, but got Unknown
358 RAILEventID[] array = (RAILEventID[])(object)new RAILEventID[4]
359 {
360 (RAILEventID)16001,
361 (RAILEventID)16002,
362 (RAILEventID)3006,
363 (RAILEventID)3005
364 };
365 foreach (RAILEventID val in array)
366 {
367 _callbackHelper.RegisterCallback(val, new RailEventCallBackHandler(OnRailEvent));
368 }
369 }
370
371 public override void Initialize()
372 {
374 _mode |= ServerMode.Lobby;
377 if (Program.LaunchParameters.ContainsKey("-lobby"))
378 {
379 _mode |= ServerMode.Lobby;
380 string text = Program.LaunchParameters["-lobby"];
381 if (!(text == "private"))
382 {
383 if (text == "friends")
384 {
385 _mode |= ServerMode.FriendsCanJoin;
386 _lobby.Create(inviteOnly: false);
387 }
388 else
389 {
390 Console.WriteLine(Language.GetTextValue("Error.InvalidLobbyFlag", "private", "friends"));
391 }
392 }
393 else
394 {
395 _lobby.Create(inviteOnly: true);
396 }
397 }
398 if (Program.LaunchParameters.ContainsKey("-friendsoffriends"))
399 {
400 _mode |= ServerMode.FriendsOfFriends;
401 }
402 _client.Init("WeGame.Terraria.Message.Client", "WeGame.Terraria.Message.Server");
403 _client.OnConnected += OnConnected;
404 _client.OnMessage += OnWegameMessage;
405 CoreSocialModule.OnTick += _client.Tick;
406 _client.Start();
407 }
408
409 public override ulong GetLobbyId()
410 {
411 return ((RailComparableID)_serverID).id_;
412 }
413
414 public override void OpenInviteInterface()
415 {
416 }
417
418 public override void CancelJoin()
419 {
420 }
421
422 public override bool CanInvite()
423 {
424 return false;
425 }
426
427 public override void LaunchLocalServer(Process process, ServerMode mode)
428 {
429 }
430
431 public override bool StartListening(SocketConnectionAccepted callback)
432 {
433 _acceptingClients = true;
435 return false;
436 }
437
438 public override void StopListening()
439 {
440 _acceptingClients = false;
441 }
442
443 public override void Connect(RemoteAddress address)
444 {
445 }
446
447 public override void Close(RemoteAddress address)
448 {
449 RailID remote_peer = RemoteAddressToRailId(address);
451 }
452}
static void WriteLine()
Definition Console.cs:733
static ? Delegate Combine(Delegate? a, Delegate? b)
Definition Delegate.cs:379
static string GetTextValue(string key)
Definition Language.cs:15
static Dictionary< string, string > LaunchParameters
Definition Program.cs:29
void Create(bool inviteOnly)
Definition Lobby.cs:75
IRailGameServer GetServer()
Definition Lobby.cs:149
void Init(string clientName, string serverName)
override void Close(RemoteAddress address)
bool GetRailFriendList(List< RailFriendInfo > list)
bool OnPacketRead(byte[] data, int size, RailID user)
override bool StartListening(SocketConnectionAccepted callback)
void UpdateFriendList(WeGameFriendListInfo friendListInfo)
override void LaunchLocalServer(Process process, ServerMode mode)
bool TryAuthUserByRecvData(RailID user, byte[] data, int length)
void UpdateUserStateBySessionAuthResult(GameServerStartSessionWithPlayerResponse data)
bool AcceptAnUserSession(RailID local_peer, RailID remote_peer)
void OnCreateSessionFailed(CreateSessionFailed data)
string DumpFriendListString(List< RailFriendInfo > list)
override void Connect(RemoteAddress address)
void OnCreateSessionRequest(CreateSessionRequest data)
void OnRailEvent(RAILEventID event_id, EventBase data)
RailID RemoteAddressToRailId(RemoteAddress address)
ConcurrentDictionary< RailID, ConnectionState > _connectionStateMap
static void WriteDebugString(string format, params object[] args)
void SetReadEvent(OnReadEvent method)
delegate void Action()