private void initMetrics() { if (!storeStatistics) { return; } dgSendTime = new MeanValue("Datagram send time"); statistics.addMetric(dgSendTime); dgSendInterval = new MeanValue("Datagram send interval"); statistics.addMetric(dgSendInterval); throughput = new MeanThroughput("Throughput", session.getDatagramSize()); statistics.addMetric(throughput); }
/** * write the given data, waiting at most for the specified time if the queue is full * @param data * @param offset * @param length * @param timeout * @param units * @throws IOException - if data cannot be sent * @throws InterruptedException */ //protected void doWrite(byte[]data, int offset, int length, int timeout, TimeUnit units) /// <summary> /// write the given data, waiting at most for the specified time if the queue is full /// 写入给定数据,如果队列已满,将最多等待指定的超时时间 /// </summary> /// <param name="data"></param> /// <param name="offset"></param> /// <param name="length"></param> /// <param name="timeout"></param> public void doWrite(byte[] data, int offset, int length, int timeout) { int chunksize = session.getDatagramSize() - 24;//need some bytes for the header 1400-24=1376 每一次发送的最大字节数 ByteBuffer bb = new ByteBuffer(data, offset, length); long seqNo = 0; while (bb.Remaining() > 0) { try { int len = Math.Min(bb.Remaining(), chunksize); byte[] chunk = new byte[len]; bb.Get(ref chunk); DataPacket packet = new DataPacket(); seqNo = sender.getNextSequenceNumber(); packet.setPacketSequenceNumber(seqNo); packet.setSession(session); packet.setDestinationID(session.getDestination().getSocketID()); packet.setData(chunk); //put the packet into the send queue if (!sender.sendUdtPacket(packet, timeout)) { Log.Write(this.ToString(), "Queue full"); } Thread.Sleep(50); } catch (Exception exc) { Log.Write(this.ToString(), "write the given data, waiting at most for the specified time if the queue is full ", exc); } } if (length > 0) { active = true; } }