Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WeGameP2PReader.cs
Go to the documentation of this file.
1using System;
3using rail;
4
6
7public class WeGameP2PReader
8{
9 public class ReadResult
10 {
11 public byte[] Data;
12
13 public uint Size;
14
15 public uint Offset;
16
17 public ReadResult(byte[] data, uint size)
18 {
19 Data = data;
20 Size = size;
21 Offset = 0u;
22 }
23 }
24
25 public delegate bool OnReadEvent(byte[] data, int size, RailID user);
26
27 public object RailLock = new object();
28
29 private const int BUFFER_SIZE = 4096;
30
32
34
36
37 private OnReadEvent _readEvent;
38
39 private RailID _local_id;
40
41 public void ClearUser(RailID id)
42 {
44 {
46 }
47 }
48
49 public bool IsDataAvailable(RailID id)
50 {
52 {
54 {
55 return false;
56 }
58 if (queue.Count == 0 || queue.Peek().Size == 0)
59 {
60 return false;
61 }
62 return true;
63 }
64 }
65
66 public void SetReadEvent(OnReadEvent method)
67 {
69 }
70
71 private bool IsPacketAvailable(out uint size)
72 {
73 //IL_001b: Unknown result type (might be due to invalid IL or missing references)
74 //IL_0021: Expected O, but got Unknown
76 {
77 IRailNetwork obj = rail_api.RailFactory().RailNetworkHelper();
78 RailID val = new RailID();
80 return obj.IsDataReady(val, ref size);
81 }
82 }
83
84 private RailID GetLocalPeer()
85 {
86 return _local_id;
87 }
88
89 public void SetLocalPeer(RailID rail_id)
90 {
91 //IL_000f: Unknown result type (might be due to invalid IL or missing references)
92 //IL_0019: Expected O, but got Unknown
93 if ((RailComparableID)(object)_local_id == (RailComparableID)null)
94 {
95 _local_id = new RailID();
96 }
97 ((RailComparableID)_local_id).id_ = ((RailComparableID)rail_id).id_;
98 }
99
100 private bool IsValid()
101 {
102 if ((RailComparableID)(object)_local_id != (RailComparableID)null)
103 {
104 return ((RailComparableID)_local_id).IsValid();
105 }
106 return false;
107 }
108
109 public void ReadTick()
110 {
111 //IL_0072: Unknown result type (might be due to invalid IL or missing references)
112 //IL_0079: Expected O, but got Unknown
113 //IL_00a1: Unknown result type (might be due to invalid IL or missing references)
114 //IL_00a7: Invalid comparison between Unknown and I4
115 if (!IsValid())
116 {
117 return;
118 }
120 {
121 while (_deletionQueue.Count > 0)
122 {
124 }
125 uint size;
126 while (IsPacketAvailable(out size))
127 {
128 byte[] array = ((_bufferPool.Count != 0) ? _bufferPool.Dequeue() : new byte[Math.Max(size, 4096u)]);
129 RailID val = new RailID();
130 bool flag;
131 lock (RailLock)
132 {
133 flag = (int)rail_api.RailFactory().RailNetworkHelper().ReadData(GetLocalPeer(), val, array, size) == 0;
134 }
135 if (!flag)
136 {
137 continue;
138 }
139 if (_readEvent == null || _readEvent(array, (int)size, val))
140 {
142 {
144 }
145 _pendingReadBuffers[val].Enqueue(new ReadResult(array, size));
146 }
147 else
148 {
150 }
151 }
152 }
153 }
154
155 public int Receive(RailID user, byte[] buffer, int bufferOffset, int bufferSize)
156 {
157 uint num = 0u;
159 {
161 {
162 return 0;
163 }
165 while (queue.Count > 0)
166 {
167 ReadResult readResult = queue.Peek();
168 uint num2 = Math.Min((uint)bufferSize - num, readResult.Size - readResult.Offset);
169 if (num2 == 0)
170 {
171 return (int)num;
172 }
173 Array.Copy(readResult.Data, readResult.Offset, buffer, bufferOffset + num, num2);
174 if (num2 == readResult.Size - readResult.Offset)
175 {
176 _bufferPool.Enqueue(queue.Dequeue().Data);
177 }
178 else
179 {
180 readResult.Offset += num2;
181 }
182 num += num2;
183 }
184 return (int)num;
185 }
186 }
187}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
bool ICollection< KeyValuePair< TKey, TValue > >. Remove(KeyValuePair< TKey, TValue > keyValuePair)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static byte Max(byte val1, byte val2)
Definition Math.cs:738
delegate bool OnReadEvent(byte[] data, int size, RailID user)
int Receive(RailID user, byte[] buffer, int bufferOffset, int bufferSize)
void SetReadEvent(OnReadEvent method)
Dictionary< RailID, Queue< ReadResult > > _pendingReadBuffers