网页游戏的as通信需要一个策略文件 mina的过滤器加个fiter


import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 策略文件过滤器
 */
public class PolicyFilter extends IoFilterAdapter {

	private final static Logger logger = LoggerFactory.getLogger(PolicyFilter.class);

	private final static byte[] policyResponse = "<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>\0"
			.getBytes();
	private final static byte[] policyRequest = "<policy-file-request/>\0".getBytes();

	@Override
	public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
		if (message instanceof IoBuffer) {
			IoBuffer buff = (IoBuffer) message;
			int len = policyRequest.length;
			if (buff.remaining() == len) {
				byte[] arr = buff.array();
				boolean equal = true;
				for (int i = 0; i < len; i++) {
					if (arr[i] != policyRequest[i]) {
						equal = false;
						break;
					}
				}
				if (equal) {
					session.write(policyResponse);
					if (logger.isDebugEnabled()) {
						logger.debug("向会话[{}]返回策略文件", session.getId());
					}
				}
			}
		}
		super.messageReceived(nextFilter, session, message);
	}
}


你可能感兴趣的:(Mina)