Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IPCBase.cs
Go to the documentation of this file.
1using System;
3using System.IO;
5using System.Linq;
6using System.Text;
8
10
11public abstract class IPCBase
12{
14
16
18
19 private object _listLock = new object();
20
21 private volatile bool _haveDataToReadFlag;
22
23 protected volatile bool _pipeBrokenFlag;
24
26
28
30
31 public int BufferSize { get; set; }
32
44
45 public IPCBase()
46 {
47 BufferSize = 256;
48 }
49
50 protected void AddPackToList(List<byte> pack)
51 {
53 {
54 _producer.Add(pack);
56 }
57 }
58
60 {
61 List<List<byte>> list = null;
63 {
69 _haveDataToReadFlag = false;
70 return list;
71 }
72 }
73
74 protected bool HaveDataToRead()
75 {
77 }
78
79 public virtual void Reset()
80 {
83 _pipeStream = null;
84 }
85
86 public virtual void ProcessDataArriveEvent()
87 {
88 if (!HaveDataToRead())
89 {
90 return;
91 }
93 if (packList == null || _onDataArrive == null)
94 {
95 return;
96 }
97 foreach (List<byte> item in packList)
98 {
99 _onDataArrive(item.ToArray());
100 }
101 }
102
103 protected virtual bool BeginReadData()
104 {
105 bool result = false;
107 {
108 data = new byte[BufferSize],
109 CancelToken = _cancelTokenSrc.Token
110 };
111 WeGameHelper.WriteDebugString("BeginReadData");
112 try
113 {
114 if (_pipeStream != null)
115 {
117 result = true;
118 }
119 }
120 catch (IOException ex)
121 {
122 _pipeBrokenFlag = true;
123 WeGameHelper.WriteDebugString("BeginReadData Exception, {0}", ex.Message);
124 }
125 return result;
126 }
127
128 public virtual void ReadCallback(IAsyncResult result)
129 {
130 WeGameHelper.WriteDebugString("ReadCallback: " + Thread.CurrentThread.ManagedThreadId);
132 try
133 {
134 int num = _pipeStream.EndRead(result);
135 if (!iPCContent.CancelToken.IsCancellationRequested)
136 {
137 if (num > 0)
138 {
139 _totalData.AddRange(iPCContent.data.Take(num));
141 {
143 _totalData = new List<byte>();
144 }
145 }
146 }
147 else
148 {
149 WeGameHelper.WriteDebugString("IPCBase.ReadCallback.cancel");
150 }
151 }
152 catch (IOException ex)
153 {
154 _pipeBrokenFlag = true;
155 WeGameHelper.WriteDebugString("ReadCallback Exception, {0}", ex.Message);
156 }
158 {
159 _pipeBrokenFlag = true;
160 WeGameHelper.WriteDebugString("ReadCallback Exception, {0}", ex2.Message);
161 }
162 }
163
164 public virtual bool Send(string value)
165 {
166 byte[] bytes = Encoding.UTF8.GetBytes(value);
167 return Send(bytes);
168 }
169
170 public virtual bool Send(byte[] data)
171 {
172 bool result = false;
173 if (_pipeStream != null && _pipeStream.IsConnected)
174 {
175 try
176 {
177 _pipeStream.BeginWrite(data, 0, data.Length, SendCallback, null);
178 result = true;
179 }
180 catch (IOException ex)
181 {
182 _pipeBrokenFlag = true;
183 WeGameHelper.WriteDebugString("Send Exception, {0}", ex.Message);
184 }
185 }
186 return result;
187 }
188
189 protected virtual void SendCallback(IAsyncResult result)
190 {
191 try
192 {
193 if (_pipeStream != null)
194 {
195 _pipeStream.EndWrite(result);
196 }
197 }
198 catch (IOException ex)
199 {
200 _pipeBrokenFlag = true;
201 WeGameHelper.WriteDebugString("SendCallback Exception, {0}", ex.Message);
202 }
203 }
204}
void AddRange(IEnumerable< T > collection)
Definition List.cs:275
static ? Delegate Remove(Delegate? source, Delegate? value)
Definition Delegate.cs:463
static ? Delegate Combine(Delegate? a, Delegate? b)
Definition Delegate.cs:379
override void EndWrite(IAsyncResult asyncResult)
override void Dispose(bool disposing)
override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
override int EndRead(IAsyncResult asyncResult)
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
static Encoding UTF8
Definition Encoding.cs:526
static Thread CurrentThread
Definition Thread.cs:312
List< List< byte > > GetPackList()
Definition IPCBase.cs:59
CancellationTokenSource _cancelTokenSrc
Definition IPCBase.cs:27
virtual bool BeginReadData()
Definition IPCBase.cs:103
List< List< byte > > _consumer
Definition IPCBase.cs:15
virtual bool Send(byte[] data)
Definition IPCBase.cs:170
List< List< byte > > _producer
Definition IPCBase.cs:13
void AddPackToList(List< byte > pack)
Definition IPCBase.cs:50
virtual void ReadCallback(IAsyncResult result)
Definition IPCBase.cs:128
virtual bool Send(string value)
Definition IPCBase.cs:164
virtual void ProcessDataArriveEvent()
Definition IPCBase.cs:86
virtual void SendCallback(IAsyncResult result)
Definition IPCBase.cs:189
Action< byte[]> _onDataArrive
Definition IPCBase.cs:29
volatile bool _pipeBrokenFlag
Definition IPCBase.cs:23
volatile bool _haveDataToReadFlag
Definition IPCBase.cs:21
virtual Action< byte[]> OnDataArrive
Definition IPCBase.cs:34
static void WriteDebugString(string format, params object[] args)