www.zhblog.net

Openfire 包路由 PacketRouter 分析

9C38B484-1668-4f54-8E72-AA757D0F59B5.png

openfire 中 PacketRouter 处理所有的传入的数据包

@Override

public void route(IQ packet) {

    iqRouter.route(packet);

}



@Override

public void route(Message packet) {

    messageRouter.route(packet);

}



@Override

public void route(Presence packet) {

    presenceRouter.route(packet);

}


实际根据 packet 的类型不同,路由到具体 Router:

IQRouter

MessageRouter

PresenceRouter


每次 route(),都会在之前和之后调用拦截器:

// Invoke the interceptors before we process the read packet

InterceptorManager.getInstance().invokeInterceptors(packet, session, true, false);

// Invoke the interceptors after we have processed the read packet

InterceptorManager.getInstance().invokeInterceptors(packet, session, true, true);


在 PacketRouterImpl 中也提供了一种处理任何 Packet 的方法:

@Override

public void route(Packet packet) {

    if (packet instanceof Message) {

        route((Message)packet);

    }

    else if (packet instanceof Presence) {

        route((Presence)packet);

    }

    else if (packet instanceof IQ) {

        route((IQ)packet);

    }

    else {

        throw new IllegalArgumentException();

    }

}


但可以看到,其实只处理了 

IQ

Message

Presence

三种包。


另一个实现包 SessionPacketRouter  也是类似,都是转发到具体了 Router:

@Override

public void route(Packet packet) {

    // Security: Don't allow users to send packets on behalf of other users

    packet.setFrom(session.getAddress());

    if(packet instanceof IQ) {

        route((IQ)packet);

    }

    else if(packet instanceof Message) {

        route((Message)packet);

    }

    else if(packet instanceof Presence) {

        route((Presence)packet);

    }

}



@Override

public void route(IQ packet) {

    packet.setFrom(session.getAddress());

    router.route(packet);

    session.incrementClientPacketCount();

}



@Override

public void route(Message packet) {

    packet.setFrom(session.getAddress());

    router.route(packet);

    session.incrementClientPacketCount();

}



@Override

public void route(Presence packet) {

    packet.setFrom(session.getAddress());

    router.route(packet);

    session.incrementClientPacketCount();

}


判断 Packet 的类型,然后转发到相应的 Router。

 

 

展开阅读全文

评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 心情