@Override
  public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    Object o = e.getMessage();
    if (o instanceof FileRegionTransferCommand) {
      ChannelBuffer cb = ChannelBuffers.dynamicBuffer();
      ChannelFuture future = e.getFuture();

      final FileRegionTransferCommand command = (FileRegionTransferCommand) o;
      command.writeHeader(cb);
      Channels.write(ctx, future, cb);
      ChannelFuture fileFuture = Channels.write(ctx.getChannel(), command.getFileRegion());
      // Channels.write(ctx, future, command.getFileRegion());
      fileFuture.addListener(
          new ChannelFutureProgressListener() {

            public void operationComplete(ChannelFuture future) {
              command.getFileRegion().releaseExternalResources();
              System.out.print("ÉÏ´«Íê³É");
              System.out.println("  " + System.currentTimeMillis());
            }

            public void operationProgressed(
                ChannelFuture future, long amount, long current, long total) {
              // (String.format("%s: %d / %d (+%d)%n", path, current, total, amount));
            }
          });
    } else {
      super.writeRequested(ctx, e);
    }
  }
  /**
   * See {@link ThreadPoolExecutor#shutdownNow()} for how it handles the shutdown. If <code>true
   * </code> is given to this method it also notifies all {@link ChannelFuture}'s of the not
   * executed {@link ChannelEventRunnable}'s.
   *
   * <p>Be aware that if you call this with <code>false</code> you will need to handle the
   * notification of the {@link ChannelFuture}'s by your self. So only use this if you really have a
   * use-case for it.
   */
  public List<Runnable> shutdownNow(boolean notify) {
    if (!notify) {
      return super.shutdownNow();
    }
    Throwable cause = null;
    Set<Channel> channels = null;

    List<Runnable> tasks = super.shutdownNow();

    // loop over all tasks and cancel the ChannelFuture of the ChannelEventRunable's
    for (Runnable task : tasks) {
      if (task instanceof ChannelEventRunnable) {
        if (cause == null) {
          cause = new IOException("Unable to process queued event");
        }
        ChannelEvent event = ((ChannelEventRunnable) task).getEvent();
        event.getFuture().setFailure(cause);

        if (channels == null) {
          channels = new HashSet<Channel>();
        }

        // store the Channel of the event for later notification of the exceptionCaught event
        channels.add(event.getChannel());
      }
    }

    // loop over all channels and fire an exceptionCaught event
    if (channels != null) {
      for (Channel channel : channels) {
        Channels.fireExceptionCaughtLater(channel, cause);
      }
    }
    return tasks;
  }
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
      if (server) {
        Channels.write(channel, e.getMessage(), e.getRemoteAddress());
      } else {
        ChannelBuffer m = (ChannelBuffer) e.getMessage();
        byte[] actual = new byte[m.readableBytes()];
        m.getBytes(0, actual);

        int lastIdx = counter;
        for (int i = 0; i < actual.length; i++) {
          assertEquals(frames.getByte(ignoredBytes + i + lastIdx), actual[i]);
        }

        counter += actual.length;
      }
    }
  @Override
  protected void write0(final AbstractNioChannel channel) {

    boolean addOpWrite = false;
    boolean removeOpWrite = false;

    long writtenBytes = 0;

    final SendBufferPool sendBufferPool = this.sendBufferPool;
    final DatagramChannel ch = ((NioDatagramChannel) channel).getJdkChannel().getChannel();
    final Queue<MessageEvent> writeBuffer = channel.writeBufferQueue;
    final int writeSpinCount = channel.getConfig().getWriteSpinCount();
    synchronized (channel.writeLock) {
      // inform the channel that write is in-progress
      channel.inWriteNowLoop = true;

      // loop forever...
      for (; ; ) {
        MessageEvent evt = channel.currentWriteEvent;
        SendBuffer buf;
        if (evt == null) {
          if ((channel.currentWriteEvent = evt = writeBuffer.poll()) == null) {
            removeOpWrite = true;
            channel.writeSuspended = false;
            break;
          }

          channel.currentWriteBuffer = buf = sendBufferPool.acquire(evt.getMessage());
        } else {
          buf = channel.currentWriteBuffer;
        }

        try {
          long localWrittenBytes = 0;
          SocketAddress raddr = evt.getRemoteAddress();
          if (raddr == null) {
            for (int i = writeSpinCount; i > 0; i--) {
              localWrittenBytes = buf.transferTo(ch);
              if (localWrittenBytes != 0) {
                writtenBytes += localWrittenBytes;
                break;
              }
              if (buf.finished()) {
                break;
              }
            }
          } else {
            for (int i = writeSpinCount; i > 0; i--) {
              localWrittenBytes = buf.transferTo(ch, raddr);
              if (localWrittenBytes != 0) {
                writtenBytes += localWrittenBytes;
                break;
              }
              if (buf.finished()) {
                break;
              }
            }
          }

          if (localWrittenBytes > 0 || buf.finished()) {
            // Successful write - proceed to the next message.
            buf.release();
            ChannelFuture future = evt.getFuture();
            channel.currentWriteEvent = null;
            channel.currentWriteBuffer = null;
            evt = null;
            buf = null;
            future.setSuccess();
          } else {
            // Not written at all - perhaps the kernel buffer is full.
            addOpWrite = true;
            channel.writeSuspended = true;
            break;
          }
        } catch (final AsynchronousCloseException e) {
          // Doesn't need a user attention - ignore.
        } catch (final Throwable t) {
          buf.release();
          ChannelFuture future = evt.getFuture();
          channel.currentWriteEvent = null;
          channel.currentWriteBuffer = null;
          buf = null;
          evt = null;
          future.setFailure(t);
          fireExceptionCaught(channel, t);
        }
      }
      channel.inWriteNowLoop = false;

      // Initially, the following block was executed after releasing
      // the writeLock, but there was a race condition, and it has to be
      // executed before releasing the writeLock:
      //
      // https://issues.jboss.org/browse/NETTY-410
      //
      if (addOpWrite) {
        setOpWrite(channel);
      } else if (removeOpWrite) {
        clearOpWrite(channel);
      }
    }

    Channels.fireWriteComplete(channel, writtenBytes);
  }