获取监听端口的流量数据
GroupStat
TioConfig对象有个GroupStat成员,定义如下
public GroupStat groupStat = null;
GroupStat定义
GroupStat有如下一些字段和方法(去掉了简单的getter和setter)
/**
* 关闭了多少连接
*/
public final AtomicLong closed = new AtomicLong();
/**
* 接收到的消息包
*/
public final AtomicLong receivedPackets = new AtomicLong();
/**
* 接收到的消息字节数
*/
public final AtomicLong receivedBytes = new AtomicLong();
/**
* 处理了的消息包数
*/
public final AtomicLong handledPackets = new AtomicLong();
/**
* 处理消息包耗时,单位:毫秒
*/
public final AtomicLong handledPacketCosts = new AtomicLong();
/**
* 处理了多少字节
*/
public final AtomicLong handledBytes = new AtomicLong();
/**
* 发送了的消息包数
*/
public final AtomicLong sentPackets = new AtomicLong();
/**
* 发送了的字节数
*/
public final AtomicLong sentBytes = new AtomicLong();
/**
* 本IP已接收了多少次TCP数据包
*/
public final AtomicLong receivedTcps = new AtomicLong();
/**
* 平均每次TCP接收到的字节数,这个可以用来监控慢攻击,配置PacketsPerTcpReceive定位慢攻击
*/
public double getBytesPerTcpReceive() {
if (receivedTcps.get() == 0) {
return 0;
}
double ret = (double) receivedBytes.get() / (double) receivedTcps.get();
return ret;
}
/**
* 平均每次TCP接收到的业务包数,这个可以用来监控慢攻击,此值越小越有攻击嫌疑
*/
public double getPacketsPerTcpReceive() {
if (receivedTcps.get() == 0) {
return 0;
}
double ret = (double) receivedPackets.get() / (double) receivedTcps.get();
return ret;
}
/**
* 处理packet平均耗时,单位:毫秒
* @return
*/
public double getHandledCostsPerPacket() {
if (handledPackets.get() > 0) {
return handledPacketCosts.get() / handledPackets.get();
}
return 0;
}
对于服务器端的groupStat,它是在ServerTioConfig类中的初始化代码在构造函数中,如下
this.groupStat = new ServerGroupStat();
对于客户端的groupStat,它是在ClientTioConfig类中的初始化代码在构造函数中,如下
this.groupStat = new ClientGroupStat();
获取GroupStat
GroupStat groupStat = tioConfig.groupStat;
//如果确认是服务器端,则可以用强转方式获得ServerGroupStat对象
ServerGroupStat serverGroupStat = (ServerGroupStat)tioConfig.groupStat;
//如果确认是客户端,则可以用强转方式获得ClientGroupStat对象
ClientGroupStat clientGroupStat = (ClientGroupStat)tioConfig.groupStat;