我正在开发一个实时应用程序,该应用程序要求每隔5ms通过WiFi直接连接从从电话到主机(组所有者)传输约512字节的数据。平均传输延迟大约为15ms,这不是很好,但是最大的问题是我注意到一种相当一致的周期模式,即每2到3秒大约有100ms的延迟,再加上每分钟大约9秒的周期,吞吐量大大降低。请查看图表scatter graph of delay over time。我还附加了我的套接字编程代码。主机和从机内部时钟都在1ms错误内同步(不必担心时钟漂移)。 请让我知道您是否也经历了这些结果,以及在此问题上是否可以做些改进。有趣的是,通常会有0个数据丢失,并且当一个数据包被延迟之前,所有数据包也都被延迟了(从图中很容易看出),即使这是UDP。我读到某个地方,即使使用UDP,也需要在MAC层确认数据包,这可能会导致大约100毫秒的延迟。
以下是从站(发送方)上的代码:
public class DataTransmissionServiceV3 {
public static final String TAG = "DataTransmissionService";
private long mClockOffset;
private DatagramChannel mDatagramChannel;
private boolean running;
public void start(InetAddress remoteAddress, long clockOffset) {
if (!running) {
running = true;
mClockOffset = clockOffset;
Thread t = new Thread(new TransmissionTask(remoteAddress));
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}
}
public void close() {
running = false;
if (mDatagramChannel != null) {
mDatagramChannel.socket().close();
}
}
private class TransmissionTask implements Runnable {
private int mCount;
private final ByteBuffer mByteBuffer = ByteBuffer.allocateDirect(
Constants.DATA_PACKET_SIZE);
private final byte[] mBytes = new byte[Constants.DATA_PACKET_SIZE];
private final InetAddress mRemoteAddress;
public TransmissionTask(final InetAddress remoteAddress) {
mRemoteAddress = remoteAddress;
}
@Override
public void run() {
try {
mDatagramChannel = DatagramChannel.open();
mDatagramChannel.socket().connect(new InetSocketAddress(mRemoteAddress,
Constants.TRANS_MASTER_SERVER_PORT));
mDatagramChannel.configureBlocking(false);
} catch (IOException ioe) {
Log.e(TAG, "Exception while connecting", ioe);
}
while (running) {
TimeUtils.busyWait(5000000); // busy wait for 5ms
send();
}
}
private void send() {
mCount++;
DataPacket.setSequenceNumber(mBytes, mCount);
DataPacket.setTimestamp(mBytes, TimeUtils.getTimestamp() + mClockOffset); // synchronized timestamp to master device to measure delay
mByteBuffer.clear();
mByteBuffer.put(mBytes);
mByteBuffer.flip();
try {
mDatagramChannel.write(mByteBuffer);
} catch (Exception e) {
Log.e(TAG, "Exception while sending data packet", e);
running = false;
}
}
}
}
以下是主机(接收方)上的代码:
public class DataTransmissionServerV3 {
public static final String TAG = "DataTransmissionServer";
private DatagramChannel mDatagramChannel;
private boolean mRunning;
private Handler mHandler;
public DataTransmissionServerV3(Handler handler) {
mHandler = handler;
}
public void start(InetAddress localAddress) {
if (!mRunning) {
mRunning = true;
Thread t = new Thread(new ReceiveDataTask(localAddress));
t.setPriority(Thread.MAX_PRIORITY);
t.start();
}
}
public void close() {
mRunning = false;
if (mDatagramChannel != null) {
mDatagramChannel.socket().close();
}
}
private class ReceiveDataTask implements Runnable {
private final InetAddress mLocalAddress;
private final ByteBuffer mByteBuffer = ByteBuffer.allocateDirect(
Constants.DATA_PACKET_SIZE);
private final byte[] mBytes = new byte[Constants.DATA_PACKET_SIZE];
private int mCount;
public ReceiveDataTask(final InetAddress localAddress) {
mLocalAddress = localAddress;
}
@Override
public void run() {
try {
bind();
while (mRunning) {
receive();
}
} catch (IOException ioe) {
Log.e(TAG, "Exception while binding datagram socket", ioe);
}
}
private void bind() throws IOException {
mDatagramChannel = DatagramChannel.open();
mDatagramChannel.socket().bind(new InetSocketAddress(mLocalAddress,
Constants.TRANS_MASTER_SERVER_PORT));
mDatagramChannel.configureBlocking(true);
}
private boolean receive() {
mByteBuffer.clear();
try {
SocketAddress isa = mDatagramChannel.receive(mByteBuffer);
long t2 = TimeUtils.getTimestamp();
if (isa != null) {
mByteBuffer.flip();
mByteBuffer.get(mBytes);
mCount++;
TransmissionStat stat = TransmissionStat.get(mBytes, mCount, t2);
handlePacket(stat); // a statistic that is saved to file for later analysis (ignore this)
return true;
}
} catch (IOException ioe) {
Log.e(TAG, "Exception while receiving data", ioe);
mRunning = false;
}
return false;
}
private void handlePacket(TransmissionStat stat) {
Message msg = mHandler.obtainMessage();
msg.what = Constants.TRANSMISSION_PACKET_RECEIVED_CODE;
msg.obj = stat;
msg.sendToTarget();
}
}