Terraria ModLoader  0.11.5
A framework for Terraria mods
ModPacket.cs
Go to the documentation of this file.
1 using System;
2 using System.IO;
3 using Terraria.Localization;
4 
5 namespace 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:447
void Send(int toClient=-1, int ignoreClient=-1)
Sends all the information you&#39;ve written between client and server. If the toClient parameter is non-...
Definition: ModPacket.cs:27
This class inherits from BinaryWriter. This means that you can use all of its writing functions to se...
Definition: ModPacket.cs:13
void SendCallback(object state)
Definition: ModPacket.cs:47
static int[] txMsgType
Definition: ModNet.cs:446