5.7. Debug Support - Latency Emulation

Description

PiaTransport comes with a latency emulation feature. By intentionally lengthening the latency that occurs in communication, the latency emulation feature creates a pseudo-environment mimicking a poor network environment or a communication partner located a long way away.

Internal Operation

When the latency emulation feature is enabled, the PiaTransport send and receive threads temporarily save packets in buffers for latency emulation. At this point, Pia sets the delay time for each individual packet using a random number. Packets are sent when this delay time is exceeded, so that the packets are sent and received later than normal.

Buffer Settings

As explained above, the latency emulation feature requires buffers for temporarily storing packets. The number of these buffers is specified on the application side by member variables of the transport::Transport::DebugSetting structure, and then the transport::Transport::SetDebugSetting() function is called. For applications that use a lot of communications, or when setting an extended maximum delay period, the buffers are used up by these activities. You must prepare large buffers. If the size of buffers is inadequate, the packets that could not be saved in a buffer are discarded and the apparent rate of packet loss is high.

Code 5-30. Buffer Settings for Latency Emulation
// Set the parameters to the member variables of the Transport::DebugSetting structure.
nn::pia::transport::Transport::DebugSetting debugSetting;
debugSetting.sendThreadLatencyEmulationPacketNum = 128; // Each buffer uses about 1.5 KB of memory.
debugSetting.receiveThreadLatencyEmulationPacketNum = 128; // Each buffer uses about 1.5 KB of memory.
debugSetting.bPacketLossEmulation = false; // Disables the packet loss emulation feature.


// When you use the Transport::SetDebugSetting() function for its debugging features, you must call it before calling the Transport::CreateInstance() function.
nn::pia::transport::Transport::SetDebugSetting(debugSetting);

 
// Set the appropriate values in each of the setting member variables.
nn::pia::transport::Transport::Setting setting;
...
...
 
// Create a Transport class instance.
nn::pia::transport::Transport::GetInstance()->CreateInstance(setting);

In the latency emulation feature, you can set a minimum delay time and a maximum delay time. The delay times in this range are determined based on uniform random numbers for each individual packet sent and received.

Code 5-31. Setting Minimum and Maximum Delay Times
nn::pia::transport::ThreadStreamManager* pMgr = nn::pia::transport::ThreadStreamManager::GetInstance();
PIA_SAMPLE_ASSERT(PIA_IS_VALID_POINTER(pMgr));
const s32 SEND_MIN = 0;
const s32 SEND_MAX = 500;
const s32 RECV_MIN = 0;
const s32 RECV_MAX = 500;
pMgr->SetSendThreadLatencyEmulation(SEND_MIN, SEND_MAX); // Send latency setting (in milliseconds).
pMgr->SetReceiveThreadLatencyEmulation(RECV_MIN, RECV_MAX); // Receive latency setting (in milliseconds).

Note:

To measure latency between stations, you can use functions such as transport::Station::GetRtt().