Client LuaCsForBarotrauma
NetworkExtensions.cs
1 using System;
2 using System.Runtime.CompilerServices;
3 using Lidgren.Network;
4 
5 namespace Barotrauma.Networking
6 {
7  internal static class WriteOnlyMessageExtensions
8  {
9 #if CLIENT
10  public static IWriteMessage WithHeader(this IWriteMessage msg, ClientPacketHeader header)
11  {
12  msg.WriteByte((byte)header);
13  return msg;
14  }
15 #elif SERVER
16  public static IWriteMessage WithHeader(this IWriteMessage msg, ServerPacketHeader header)
17  {
18  msg.WriteByte((byte)header);
19  return msg;
20  }
21 #endif
22  public static void WriteNetSerializableStruct(this IWriteMessage msg, INetSerializableStruct serializableStruct)
23  {
24  serializableStruct.Write(msg);
25  }
26 
27  public static NetOutgoingMessage ToLidgren(this IWriteMessage msg, NetPeer peer)
28  {
29  NetOutgoingMessage outMsg = peer.CreateMessage();
30  outMsg.Write(msg.Buffer, 0, msg.LengthBytes);
31  return outMsg;
32  }
33  }
34 
35  internal static class NetIncomingMessageExtensions
36  {
37  public static T ReadHeader<T>(this NetIncomingMessage msg) where T : Enum
38  {
39  byte header = msg.ReadByte();
40  return Unsafe.As<byte, T>(ref header);
41  }
42 
43  public static IReadMessage ToReadMessage(this NetIncomingMessage msg)
44  {
45  return new ReadWriteMessage(msg.Data, 0, msg.LengthBits, copyBuf: false);
46  }
47  }
48 
49  internal static class DeliveryMethodExtensions
50  {
51  public static NetDeliveryMethod ToLidgren(this DeliveryMethod deliveryMethod) =>
52  deliveryMethod switch
53  {
54  DeliveryMethod.Unreliable => NetDeliveryMethod.Unreliable,
55  DeliveryMethod.Reliable => NetDeliveryMethod.ReliableOrdered,
56  _ => NetDeliveryMethod.Unreliable
57  };
58 
59  public static Steamworks.P2PSend ToSteam(this DeliveryMethod deliveryMethod) =>
60  deliveryMethod switch
61  {
62  DeliveryMethod.Reliable => Steamworks.P2PSend.Reliable,
63  _ => Steamworks.P2PSend.Unreliable
64  };
65  }
66 }