Skip to content

Commit eebe867

Browse files
committed
fix bug
1 parent 7ce6c65 commit eebe867

File tree

4 files changed

+44
-42
lines changed

4 files changed

+44
-42
lines changed

TrafficStatistics/Relay/TcpPipe.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class Handler
3232
private Socket _local;
3333
private Socket _remote;
3434
private bool _closed = false;
35-
private bool _localShutdown = false;
36-
private bool _remoteShutdown = false;
3735
public const int RecvSize = 16384;
3836
// remote receive buffer
3937
private byte[] remoteRecvBuffer = new byte[RecvSize];
@@ -118,10 +116,7 @@ private void PipeRemoteReceiveCallback(IAsyncResult ar)
118116
}
119117
else
120118
{
121-
//Console.WriteLine("bytesRead: " + bytesRead.ToString());
122-
_remote.Shutdown(SocketShutdown.Send);
123-
_remoteShutdown = true;
124-
CheckClose();
119+
this.Close();
125120
}
126121
}
127122
catch (Exception e)
@@ -148,9 +143,7 @@ private void PipeConnectionReceiveCallback(IAsyncResult ar)
148143
}
149144
else
150145
{
151-
_local.Shutdown(SocketShutdown.Send);
152-
_localShutdown = true;
153-
CheckClose();
146+
this.Close();
154147
}
155148
}
156149
catch (Exception e)
@@ -198,14 +191,6 @@ private void PipeConnectionSendCallback(IAsyncResult ar)
198191
}
199192
}
200193

201-
private void CheckClose()
202-
{
203-
if (_localShutdown && _remoteShutdown)
204-
{
205-
this.Close();
206-
}
207-
}
208-
209194
public void Close()
210195
{
211196
lock (this)

TrafficStatistics/Relay/UdpRelay.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public State()
1818
}
1919
}
2020

21+
private const int SIP_UDP_CONNRESET = -1744830452;
22+
2123
private Socket _local;
2224
private UDPPipe _pipe;
2325

@@ -42,6 +44,8 @@ public void Start()
4244
// Create a TCP/IP socket.
4345
_local = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
4446
_local.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
47+
// Fix WinSock library bug, See https://support.microsoft.com/en-us/kb/263823
48+
_local.IOControl(SIP_UDP_CONNRESET, new byte[] { 0, 0, 0, 0 }, null);
4549
// Bind the socket to the local endpoint and listen for incoming connections.
4650
_local.Bind(_localEP);
4751

TrafficStatistics/Traffic.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public Traffic(Traffic t)
1818

1919
public Traffic(Traffic t1, Traffic t2)
2020
{
21-
if (t1.inbound >= t2.inbound)
21+
if (t2.inbound > 0 && t1.inbound >= t2.inbound)
2222
inbound = t1.inbound - t2.inbound;
23-
if (t1.outbound >= t2.outbound)
23+
if (t2.outbound > 0 && t1.outbound >= t2.outbound)
2424
outbound = t1.outbound - t2.outbound;
2525
}
2626

TrafficStatistics/TrafficStatisticsForm.cs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public partial class TrafficStatisticsForm : Form
1818

1919
private IRelay relay;
2020
private Traffic rawTrafficStatistics = new Traffic();
21-
private LinkedList<TrafficLog> trafficLogList;
21+
private LinkedList<TrafficLog> trafficLogList = new LinkedList<TrafficLog>();
2222

2323
public TrafficStatisticsForm()
2424
{
@@ -86,6 +86,7 @@ private void TopMostheckBox_CheckedChanged(object sender, EventArgs e)
8686
private void ResetButton_Click(object sender, EventArgs e)
8787
{
8888
rawTrafficStatistics.reset();
89+
InitTrafficHistoricalList();
8990
}
9091

9192
private void timer1_Tick(object sender, EventArgs e)
@@ -97,6 +98,7 @@ private void timer2_Tick(object sender, EventArgs e)
9798
{
9899
try
99100
{
101+
timer2.Stop();
100102
UpdateTrafficList();
101103
if (TrafficChart.InvokeRequired)
102104
{
@@ -114,6 +116,10 @@ private void timer2_Tick(object sender, EventArgs e)
114116
{
115117
string s = ex.Message;
116118
}
119+
finally
120+
{
121+
timer2.Start();
122+
}
117123
}
118124

119125
private void CleanLogsButton_Click(object sender, EventArgs e)
@@ -197,27 +203,30 @@ private void AppendLog(string line)
197203

198204
private void InitTrafficHistoricalList()
199205
{
200-
if (trafficLogList == null)
201-
trafficLogList = new LinkedList<TrafficLog>();
202-
else
203-
trafficLogList.Clear();
204-
for (int i = 0; i < trafficLogSize; i++)
206+
lock(trafficLogList)
205207
{
206-
trafficLogList.AddLast(new TrafficLog());
208+
trafficLogList.Clear();
209+
for (int i = 0; i < trafficLogSize; i++)
210+
{
211+
trafficLogList.AddLast(new TrafficLog());
212+
}
207213
}
208214
}
209215

210216
private void UpdateTrafficList()
211217
{
212-
TrafficLog previous = trafficLogList.Last.Value;
213-
TrafficLog current = new TrafficLog(
214-
new Traffic(rawTrafficStatistics),
215-
new Traffic(rawTrafficStatistics, previous.raw)
216-
);
217-
trafficLogList.AddLast(current);
218-
219-
while (trafficLogList.Count > trafficLogSize) trafficLogList.RemoveFirst();
220-
while (trafficLogList.Count < trafficLogSize) trafficLogList.AddFirst(new TrafficLog());
218+
lock (trafficLogList)
219+
{
220+
TrafficLog previous = trafficLogList.Last.Value;
221+
TrafficLog current = new TrafficLog(
222+
new Traffic(rawTrafficStatistics),
223+
new Traffic(rawTrafficStatistics, previous.raw)
224+
);
225+
trafficLogList.AddLast(current);
226+
227+
while (trafficLogList.Count > trafficLogSize) trafficLogList.RemoveFirst();
228+
while (trafficLogList.Count < trafficLogSize) trafficLogList.AddFirst(new TrafficLog());
229+
}
221230
}
222231

223232
private List<float> rawInboundPoints = new List<float>();
@@ -226,21 +235,25 @@ private void UpdateTrafficList()
226235

227236
private void UpdateTrafficChart()
228237
{
238+
TrafficLog last;
229239
long maxSpeedValue = 0;
230240
rawInboundPoints.Clear();
231241
rawOutboundPoints.Clear();
232-
foreach (TrafficLog item in trafficLogList)
242+
lock(trafficLogList)
233243
{
234-
rawInboundPoints.Add(item.rawSpeed.inbound);
235-
rawOutboundPoints.Add(item.rawSpeed.outbound);
244+
last = trafficLogList.Last.Value;
245+
foreach (TrafficLog item in trafficLogList)
246+
{
247+
rawInboundPoints.Add(item.rawSpeed.inbound);
248+
rawOutboundPoints.Add(item.rawSpeed.outbound);
236249

237-
maxSpeedValue = Math.Max(maxSpeedValue,
238-
Math.Max(item.rawSpeed.inbound, item.rawSpeed.outbound)
239-
);
250+
maxSpeedValue = Math.Max(maxSpeedValue,
251+
Math.Max(item.rawSpeed.inbound, item.rawSpeed.outbound)
252+
);
253+
}
240254
}
241255

242256
FormattedSize maxSpeed = new FormattedSize(maxSpeedValue);
243-
TrafficLog last = trafficLogList.Last.Value;
244257
RawInboundSpeed.Text = new FormattedSize(last.rawSpeed.inbound) + "/s";
245258
RawOutboundSpeed.Text = new FormattedSize(last.rawSpeed.outbound) + "/s";
246259

0 commit comments

Comments
 (0)