tModLoader v0.11.8.9
A mod to make and play Terraria mods
ModPacket.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using Terraria.Localization;
4
5namespace Terraria.ModLoader
6{
13 public sealed class ModPacket : BinaryWriter
14 {
15 private byte[] buf;
16 private ushort len;
17 internal short netID = -1;
18
19 internal ModPacket(byte messageID, int capacity = 256) : base(new MemoryStream(capacity)) {
20 Write((ushort)0);
21 Write(messageID);
22 }
23
27 public void Send(int toClient = -1, int ignoreClient = -1) {
28 Finish();
29
30 if (Main.netMode == 1) {
31 Netplay.Connection.Socket.AsyncSend(buf, 0, len, SendCallback);
32 Main.txMsg++;
33 Main.txData += len;
34 if (netID > 0) {
35 ModNet.txMsgType[netID]++;
36 ModNet.txDataType[netID] += len;
37 }
38 }
39 else if (toClient != -1)
40 Netplay.Clients[toClient].Socket.AsyncSend(buf, 0, len, SendCallback);
41 else
42 for (int i = 0; i < 256; i++)
43 if (i != ignoreClient && Netplay.Clients[i].IsConnected() && NetMessage.buffer[i].broadcast)
44 Netplay.Clients[i].Socket.AsyncSend(buf, 0, len, SendCallback);
45 }
46
47 private void SendCallback(object state) { }
48
49 private void Finish() {
50 if (buf != null)
51 return;
52
53 if (OutStream.Position > ushort.MaxValue)
54 throw new Exception(Language.GetTextValue("tModLoader.MPPacketTooLarge", OutStream.Position, ushort.MaxValue));
55
56 len = (ushort)OutStream.Position;
57 Seek(0, SeekOrigin.Begin);
58 Write(len);
59 Close();
60 buf = ((MemoryStream)OutStream).GetBuffer();
61 }
62 }
63}
static int[] txDataType
Definition: ModNet.cs:458
static int[] txMsgType
Definition: ModNet.cs:457
This class inherits from BinaryWriter. This means that you can use all of its writing functions to se...
Definition: ModPacket.cs:14
void Send(int toClient=-1, int ignoreClient=-1)
Sends all the information you've written between client and server. If the toClient parameter is non-...
Definition: ModPacket.cs:27
void SendCallback(object state)
Definition: ModPacket.cs:47