All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.voovan.http.server.WebServerHandler Maven / Gradle / Ivy

package org.voovan.http.server;

import org.voovan.Global;
import org.voovan.http.message.Request;
import org.voovan.http.message.Response;
import org.voovan.http.server.context.WebContext;
import org.voovan.http.server.context.WebServerConfig;
import org.voovan.http.server.exception.RouterNotFound;
import org.voovan.http.websocket.WebSocketFrame;
import org.voovan.http.websocket.WebSocketTools;
import org.voovan.network.IoHandler;
import org.voovan.network.IoSession;
import org.voovan.network.exception.SendMessageException;
import org.voovan.tools.ByteBufferChannel;
import org.voovan.tools.TEnv;
import org.voovan.tools.hashwheeltimer.HashWheelTask;
import org.voovan.tools.log.Logger;

import java.nio.ByteBuffer;
import java.nio.channels.InterruptedByTimeoutException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * WebServer Socket 事件处理类
 *
 * @author helyho
 *
 * Voovan Framework.
 * WebSite: https://github.com/helyho/Voovan
 * Licence: Apache v2 License
 */
public class WebServerHandler implements IoHandler {
	private HttpDispatcher		httpDispatcher;
	private WebSocketDispatcher	webSocketDispatcher;
	private WebServerConfig webConfig;
	private List keepAliveSessionList;


	public class SessionParam{
		public static final int TYPE = 0x1111;
		public static final int HTTP_REQUEST = 0x2222;
		public static final int HTTP_RESPONSE = 0x3333;
		public static final int KEEP_ALIVE = 0x4444;
		public static final int KEEP_ALIVE_TIMEOUT = 0x5555;
	}

	public WebServerHandler(WebServerConfig webConfig, HttpDispatcher httpDispatcher, WebSocketDispatcher webSocketDispatcher) {
		this.httpDispatcher = httpDispatcher;
		this.webSocketDispatcher = webSocketDispatcher;
		this.webConfig = webConfig;
		keepAliveSessionList = new ArrayList();

		initKeepAliveTimer();

	}

	/**
	 * 获取属性
	 * @param session       会话对象
	 * @param sessionParam  会话参数枚举
	 * @param            范型
	 * @return  参数
	 */
	public static  T getAttribute(IoSession session, int sessionParam){
		return (T)session.getAttribute(sessionParam);
	}

	/**
	 *
	 * @param session       会话对象
	 * @param sessionParam  会话参数枚举
	 * @param value  参数
	 * @param            范型
	 */
	public static  void  setAttribute(IoSession session, int sessionParam, T value){
		session.setAttribute(sessionParam, value);
	}

	/**
	 * 初始化连接保持 Timer
	 */
	public void initKeepAliveTimer(){

		Global.getHashWheelTimer().addTask(new HashWheelTask() {

			@Override
			public void run() {

				long currentTimeValue = System.currentTimeMillis();
				//遍历所有的 session
				for(int i=0; i 0) {

			if (!keepAliveSessionList.contains(session)) {
				keepAliveSessionList.add(session);
			}
			//更新会话超时时间
			refreshTimeout(session);
		} else {
			if (keepAliveSessionList.contains(session)) {
				keepAliveSessionList.remove(session);
			}
			session.close();
		}
	}

	@Override
	public void onException(IoSession session, Exception e) {
		//忽略远程连接断开异常 和 超时断开异常
		if(!(e instanceof InterruptedByTimeoutException)){
			Logger.error("Http Server Error",e);
		}
		session.close();
	}

	@Override
	public void onIdle(IoSession session) {

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy