Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NetManager.cs
Go to the documentation of this file.
2using System.IO;
4
5namespace Terraria.Net;
6
7public class NetManager
8{
9 private class PacketTypeStorage<T> where T : NetModule
10 {
11 public static ushort Id;
12
13 public static T Module;
14 }
15
17
18 public static readonly NetManager Instance = new NetManager();
19
21
22 private ushort _moduleCount;
23
24 private NetManager()
25 {
26 }
27
28 public void Register<T>() where T : NetModule, new()
29 {
30 T val = new T();
32 PacketTypeStorage<T>.Module = val;
35 }
36
38 {
39 return PacketTypeStorage<T>.Module;
40 }
41
42 public ushort GetId<T>() where T : NetModule
43 {
44 return PacketTypeStorage<T>.Id;
45 }
46
47 public void Read(BinaryReader reader, int userId, int readLength)
48 {
49 ushort num = reader.ReadUInt16();
50 if (_modules.ContainsKey(num))
51 {
52 _modules[num].Deserialize(reader, userId);
53 }
54 Main.ActiveNetDiagnosticsUI.CountReadModuleMessage(num, readLength);
55 }
56
57 public void Broadcast(NetPacket packet, int ignoreClient = -1)
58 {
59 for (int i = 0; i < 256; i++)
60 {
61 if (i != ignoreClient && Netplay.Clients[i].IsConnected())
62 {
63 SendData(Netplay.Clients[i].Socket, packet);
64 }
65 }
66 }
67
69 {
70 for (int i = 0; i < 256; i++)
71 {
72 if (i != ignoreClient && Netplay.Clients[i].IsConnected() && conditionToBroadcast(i))
73 {
74 SendData(Netplay.Clients[i].Socket, packet);
75 }
76 }
77 }
78
80 {
81 packet.Reader.BaseStream.Position = 3L;
82 Read(packet.Reader, Main.myPlayer, packet.Length);
84 Main.ActiveNetDiagnosticsUI.CountSentModuleMessage(packet.Id, packet.Length);
85 }
86
88 {
89 if (Main.netMode == 2)
90 {
92 }
93 else if (Main.netMode == 0)
94 {
96 }
97 }
98
100 {
101 if (Main.netMode == 1)
102 {
104 }
105 else if (Main.netMode == 0)
106 {
108 }
109 }
110
112 {
113 if (Main.netMode == 1)
114 {
117 }
118 else if (Main.netMode == 0)
119 {
121 }
122 }
123
125 {
127 }
128
130 {
132 }
133
135 {
136 if (Main.netMode != 0)
137 {
138 packet.ShrinkToFit();
139 try
140 {
141 socket.AsyncSend(packet.Buffer.Data, 0, packet.Length, SendCallback, packet);
142 }
143 catch
144 {
145 }
146 Main.ActiveNetDiagnosticsUI.CountSentModuleMessage(packet.Id, packet.Length);
147 }
148 }
149
150 public static void SendCallback(object state)
151 {
152 ((NetPacket)state).Recycle();
153 }
154}
virtual ushort ReadUInt16()
static int myPlayer
Definition Main.cs:1801
static int netMode
Definition Main.cs:2095
static INetDiagnosticsUI ActiveNetDiagnosticsUI
Definition Main.cs:2789
void SendToServer(NetPacket packet)
void BroadcastOrLoopback(NetPacket packet)
Definition NetManager.cs:87
NetModule GetModule< T >()
Definition NetManager.cs:37
void Broadcast(NetPacket packet, int ignoreClient=-1)
Definition NetManager.cs:57
static readonly NetManager Instance
Definition NetManager.cs:18
Dictionary< ushort, NetModule > _modules
Definition NetManager.cs:20
void Read(BinaryReader reader, int userId, int readLength)
Definition NetManager.cs:47
void SendToSelf(NetPacket packet)
Definition NetManager.cs:79
void SendData(ISocket socket, NetPacket packet)
delegate bool BroadcastCondition(int clientIndex)
static void SendCallback(object state)
void Broadcast(NetPacket packet, BroadcastCondition conditionToBroadcast, int ignoreClient=-1)
Definition NetManager.cs:68
void SendToServerOrLoopback(NetPacket packet)
Definition NetManager.cs:99
void SendToClient(NetPacket packet, int playerId)
void SendToServerAndSelf(NetPacket packet)
static RemoteClient[] Clients
Definition Netplay.cs:37
static RemoteServer Connection
Definition Netplay.cs:39