コード例 #1
0
  private int send(SocketChannel socketChannel, ByteBuffer dataBuffer) throws IOException {
    int sendSize = socketChannel.write(dataBuffer);

    if (sendSize > 0) {
      channelContext.getStatVo().setCurrentSendTime(SystemTimer.currentTimeMillis());
      channelContext.getStatVo().setSentBytes(sendSize + channelContext.getStatVo().getSentBytes());
      StatVo.getAllSentBytes().addAndGet(sendSize);

      getProcessedMsgByteCount().addAndGet(sendSize);
    }

    return sendSize;
  }
コード例 #2
0
  /**
   * @param data 要发送的数据
   * @param socketChannel 发往的通道
   * @return 数据发送成功:true;失败:false
   * @throws IOException
   */
  private boolean sendData(byte[] data, SocketChannel socketChannel) throws IOException {
    try {
      ByteBuffer dataBuffer = ByteBuffer.wrap(data);

      long sendSize = send(socketChannel, dataBuffer);
      long allSendSize = sendSize;
      long starttime = SystemTimer.currentTimeMillis();

      while (dataBuffer.hasRemaining()
          && socketChannel.isOpen()) // 还有数据没写干净,一般是网络不好导致,也可能是发送和接收量太大。
      {
        sendSize = send(socketChannel, dataBuffer);

        if (sendSize == 0) {
          Thread.sleep(5);
        } else {
          allSendSize += sendSize;
        }

        if (DebugUtils.isNeedDebug(channelContext)) {
          try {
            long endtime = SystemTimer.currentTimeMillis();
            long costtime = (endtime - starttime);
            double speed = -1;
            if (costtime > 0) {
              speed = allSendSize / costtime;
            }

            log.error(
                "{} bytes was sent, {}/{}({}%), cost time: {} ms, {}bytes/ms,{}",
                sendSize,
                allSendSize,
                data.length,
                (allSendSize * 100) / data.length,
                costtime,
                speed,
                channelContext);
          } catch (Exception e) {
            log.error(e.getLocalizedMessage(), e);
          }
        }
      }
      StatVo.getAllSentMsgCount().incrementAndGet();
      this.getProcessedMsgCount().incrementAndGet();
      long endtime = SystemTimer.currentTimeMillis();

      if (DebugUtils.isNeedDebug(channelContext)) {
        try {
          long costtime = (endtime - starttime);
          double speed = -1;
          if (costtime > 0) {
            speed = allSendSize / costtime;
          }
          log.error("cost time: {} ms, {}bytes/ms, {}", costtime, speed, channelContext);

          log.error(
              "ok sent to "
                  + channelContext
                  + ",total num["
                  + StatVo.getAllSentMsgCount().get()
                  + "],num to this ["
                  + getProcessedMsgCount().get()
                  + "],waiting for send to this ["
                  + getMsgQueue().size()
                  + "]");
        } catch (Exception e) {
          log.error(e.getLocalizedMessage(), e);
        }
      }

      return true;
    } catch (InterruptedException e) {
      return false;
    }
  }