Skip to content

Commit d45796c

Browse files
15 implement network statistics (#17)
* #15 done * + fix and resolve to merge live code * + fix var1 to bytes
1 parent ae83ce9 commit d45796c

22 files changed

+242
-3
lines changed

EzyClient.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using com.tvd12.ezyfoxserver.client.config;
77
using com.tvd12.ezyfoxserver.client.constant;
88
using com.tvd12.ezyfoxserver.client.request;
9+
using com.tvd12.ezyfoxserver.client.statistics;
910

1011
namespace com.tvd12.ezyfoxserver.client
1112
{
@@ -74,5 +75,6 @@ public interface EzyClient
7475

7576
EzyHandlerManager getHandlerManager();
7677

77-
}
78+
EzyStatistics getNetworkStatistics();
79+
}
7880
}

EzyTcpClient.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using com.tvd12.ezyfoxserver.client.request;
1010
using com.tvd12.ezyfoxserver.client.socket;
1111
using static com.tvd12.ezyfoxserver.client.constant.EzyConnectionStatuses;
12+
using com.tvd12.ezyfoxserver.client.statistics;
1213

1314
namespace com.tvd12.ezyfoxserver.client
1415
{
@@ -26,6 +27,7 @@ public class EzyTcpClient :
2627
protected readonly EzyClientConfig config;
2728
protected readonly EzyPingManager pingManager;
2829
protected readonly EzyHandlerManager handlerManager;
30+
protected readonly EzyStatistics networkStatistics;
2931
protected readonly EzyRequestSerializer requestSerializer;
3032

3133
protected EzyConnectionStatus status;
@@ -45,6 +47,7 @@ public EzyTcpClient(EzyClientConfig config)
4547
this.pingManager = new EzySimplePingManager(config.getPing());
4648
this.pingSchedule = new EzyPingSchedule(this);
4749
this.handlerManager = new EzySimpleHandlerManager(this);
50+
this.networkStatistics = new EzySimpleStatistics();
4851
this.requestSerializer = new EzySimpleRequestSerializer();
4952
this.settingUp = new EzySimpleSetup(handlerManager);
5053
this.unloggableCommands = newUnloggableCommands();
@@ -65,6 +68,7 @@ protected EzySocketClient newSocketClient()
6568
EzyTcpSocketClient client = newTcpSocketClient();
6669
client.setPingSchedule(pingSchedule);
6770
client.setPingManager(pingManager);
71+
client.setNetworkStatistics(networkStatistics);
6872
client.setHandlerManager(handlerManager);
6973
client.setReconnectConfig(config.getReconnect());
7074
client.setUnloggableCommands(unloggableCommands);
@@ -308,6 +312,11 @@ public virtual void udpSend(EzyCommand cmd, EzyArray data)
308312
{
309313
throw new InvalidOperationException("only support TCP, use EzyUTClient instead");
310314
}
311-
}
315+
316+
public EzyStatistics getNetworkStatistics()
317+
{
318+
return networkStatistics;
319+
}
320+
}
312321

313322
}

socket/EzySocketAdapter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Threading;
3+
4+
using com.tvd12.ezyfoxserver.client.statistics;
35
using com.tvd12.ezyfoxserver.client.util;
46
namespace com.tvd12.ezyfoxserver.client.socket
57
{
@@ -8,6 +10,7 @@ public abstract class EzySocketAdapter : EzyLoggable
810
protected volatile bool active;
911
protected volatile bool stopped;
1012
protected readonly Object adapterLock;
13+
protected EzyStatistics networkStatistics;
1114

1215
public EzySocketAdapter()
1316
{
@@ -84,5 +87,10 @@ public bool isStopped()
8487
return stopped;
8588
}
8689
}
90+
91+
public void setNetworkStatistics(EzyStatistics networkStatistics)
92+
{
93+
this.networkStatistics = networkStatistics;
94+
}
8795
}
8896
}

socket/EzySocketClient.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using com.tvd12.ezyfoxserver.client.handler;
1010
using com.tvd12.ezyfoxserver.client.manager;
1111
using com.tvd12.ezyfoxserver.client.util;
12+
using com.tvd12.ezyfoxserver.client.statistics;
13+
1214
using static com.tvd12.ezyfoxserver.client.constant.EzySocketStatuses;
1315

1416
namespace com.tvd12.ezyfoxserver.client.socket
@@ -31,6 +33,8 @@ public abstract class EzySocketClient : EzyLoggable , EzyISocketClient, EzySocke
3133
protected EzyDataHandlers dataHandlers;
3234
protected EzySocketReader socketReader;
3335
protected EzySocketWriter socketWriter;
36+
protected EzyStatistics networkStatistics;
37+
3438
protected EzyConnectionFailedReason connectionFailedReason;
3539
protected readonly EzyCodecFactory codecFactory;
3640
protected readonly EzyPacketQueue packetQueue;
@@ -101,6 +105,7 @@ protected void connect0(int sleepTime)
101105
{
102106
clearAdapters();
103107
createAdapters();
108+
addNetworkStatisticsAdapers();
104109
updateAdapters();
105110
closeSocket();
106111
packetQueue.clear();
@@ -146,6 +151,17 @@ protected void connect1(int sleepTime)
146151

147152
protected abstract void createAdapters();
148153

154+
public virtual void setNetworkStatistics(EzyStatistics networkStatistics)
155+
{
156+
this.networkStatistics = networkStatistics;
157+
}
158+
159+
protected void addNetworkStatisticsAdapers()
160+
{
161+
socketReader.setNetworkStatistics(networkStatistics);
162+
socketWriter.setNetworkStatistics(networkStatistics);
163+
}
164+
149165
protected void updateAdapters()
150166
{
151167
Object decoder = codecFactory.newDecoder(EzyConnectionType.SOCKET);

socket/EzySocketReader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ protected override void update()
4242
return;
4343
byte[] binary = EzyBytes.copyBytes(readBytes, bytesToRead);
4444
decoder.decode(binary, decodeBytesCallback);
45+
46+
networkStatistics.getSocketStats().getNetworkStats().addReadBytes(binary.Length);
47+
networkStatistics.getSocketStats().getNetworkStats().addReadPackets(1);
4548
}
4649

4750
}

socket/EzySocketWriter.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace com.tvd12.ezyfoxserver.client.socket
1+
using com.tvd12.ezyfoxserver.client.entity;
2+
3+
namespace com.tvd12.ezyfoxserver.client.socket
24
{
35
public abstract class EzySocketWriter : EzySocketAdapter
46
{
@@ -14,9 +16,13 @@ protected override void update()
1416
if (packet == null)
1517
return;
1618
int writtenBytes = writeToSocket(packet);
19+
1720
packet.release();
1821
if (writtenBytes <= 0)
1922
return;
23+
24+
networkStatistics.getSocketStats().getNetworkStats().addWrittenPackets(1);
25+
networkStatistics.getSocketStats().getNetworkStats().addWrittenBytes(writtenBytes);
2026
}
2127
}
2228

socket/EzyUTSocketClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using com.tvd12.ezyfoxserver.client.entity;
33
using com.tvd12.ezyfoxserver.client.constant;
4+
using com.tvd12.ezyfoxserver.client.statistics;
45

56
namespace com.tvd12.ezyfoxserver.client.socket
67
{
@@ -13,6 +14,12 @@ public EzyUTSocketClient() : base()
1314
this.udpClient = new EzyUdpSocketClient(codecFactory);
1415
}
1516

17+
public override void setNetworkStatistics(EzyStatistics networkStatistics)
18+
{
19+
base.setNetworkStatistics(networkStatistics);
20+
this.udpClient.setNetworkStatistics(networkStatistics);
21+
}
22+
1623
public void udpConnect(int port)
1724
{
1825
udpConnect(host, port);

socket/EzyUdpSocketClient.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using com.tvd12.ezyfoxserver.client.constant;
1111
using com.tvd12.ezyfoxserver.client.exception;
1212
using static com.tvd12.ezyfoxserver.client.constant.EzySocketStatuses;
13+
using com.tvd12.ezyfoxserver.client.statistics;
1314

1415
namespace com.tvd12.ezyfoxserver.client.socket
1516
{
@@ -25,6 +26,7 @@ public class EzyUdpSocketClient : EzyLoggable, EzyISocketClient
2526
protected readonly EzyResponseApi responseApi;
2627
protected readonly EzyCodecFactory codecFactory;
2728
protected readonly EzyValueStack<EzySocketStatus> socketStatuses;
29+
protected EzyStatistics networkStatistics;
2830

2931
public EzyUdpSocketClient(EzyCodecFactory codecFactory) {
3032
this.codecFactory = codecFactory;
@@ -76,6 +78,7 @@ protected void connect0()
7678
{
7779
clearAdapters();
7880
createAdapters();
81+
addNetworkStatisticsAdapers();
7982
updateAdapters();
8083
closeSocket();
8184
packetQueue.clear();
@@ -135,6 +138,17 @@ protected void createAdapters()
135138
this.socketWriter = new EzyUdpSocketWriter();
136139
}
137140

141+
public void setNetworkStatistics(EzyStatistics networkStatistics)
142+
{
143+
this.networkStatistics = networkStatistics;
144+
}
145+
146+
protected void addNetworkStatisticsAdapers()
147+
{
148+
socketReader.setNetworkStatistics(networkStatistics);
149+
socketWriter.setNetworkStatistics(networkStatistics);
150+
}
151+
138152
protected void updateAdapters()
139153
{
140154
Object decoder = codecFactory.newDecoder(EzyConnectionType.SOCKET);

socket/EzyUdpSocketReader.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ protected override void update()
4444
if (bytesToRead <= 0)
4545
return;
4646
handleReceivedBytes(binary);
47+
48+
networkStatistics.getSocketStats().getNetworkStats().addReadBytes(binary.Length);
49+
networkStatistics.getSocketStats().getNetworkStats().addReadPackets(1);
4750
}
4851
catch (SocketException e) {
4952
logger.warn("I/O error at socket-reader: " + e.Message);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace com.tvd12.ezyfoxserver.client.statistics
2+
{
3+
public interface EzyComponentStatistics
4+
{
5+
EzyNetworkStats getNetworkStats();
6+
}
7+
}

0 commit comments

Comments
 (0)