cindy源码阅读(2)Packet

cindy传输的时候都是封装成pccket的,其实packet很简单,一个是socket的地址,一个是编码后的内容(一个buffer)
public interface Packet {

    /**
     * Get the socket address of the packet.
     * 
     * @return the socket address
     */
    SocketAddress getAddress();

    /**
     * Get the content of the packet.
     * 
     * @return content
     */
    Buffer getContent();

}

再看看packet的类结构图:


实际使用的是FuturePacket。既有优先级,也有原始的内容。还有一个future。
public FuturePacket(Object obj, Packet packet, int priority,
                DefaultFuture future) {


这个future的作用是在session的onWritable()方法体现的
  try {
                        checkSendPacket(currentSendPacket);
                    } catch (RuntimeException e) {
                        dispatchException(e);
                        DefaultFuture future = currentSendPacket.future;
                        currentSendPacket = null;
                        future.setSucceeded(false);
                        continue;
                    }


异常情况下会future设置为失败

dispatch(new Runnable() {

                            public void run() {
                                packet.future.setSucceeded(true);

开始发送的时候设置为成功。

而且可以看到cindy的发送都用的是packetSend packetSent。这些可以看出都是处理packet的,所以在cindy在packet是传输的介质。

猜你喜欢

转载自huangyunbin.iteye.com/blog/1866318
今日推荐