SocketWrapper泛型类
来源:互联网 发布:java中demo是什么意思 编辑:程序博客网 时间:2024/06/08 07:18
该类封装了一个泛型E(Socket)对象,并且保存了其一些设置,如lastAccess,keptAlive等信息
/* * 实体 */protected volatile E socket;/* * 最后一次访问时间 */protected volatile long lastAccess = System.currentTimeMillis();protected long timeout = -1;protected boolean error = false;protected long lastRegistered = 0;protected volatile int keepAliveLeft = 100;private boolean comet = false;protected boolean async = false;protected boolean keptAlive = false;private boolean upgraded = false;private boolean secure = false;/* * Used if block/non-blocking is set at the socket level. The client is * responsible for the thread-safe use of this field via the locks provided. */private volatile boolean blockingStatus = true;private final Lock blockingStatusReadLock;private final WriteLock blockingStatusWriteLock;/* * In normal servlet processing only one thread is allowed to access the * socket at a time. That is controlled by a lock on the socket for both * read and writes). When HTTP upgrade is used, one read thread and one * write thread are allowed to access the socket concurrently. In this case * the lock on the socket is used for reads and the lock below is used for * writes. */private final Object writeThreadLock = new Object();/** * 构造函数,初始化socket和blockingStatusReadLock和blockingStatusWriteLock * * @param socket */public SocketWrapper(E socket) {this.socket = socket;ReentrantReadWriteLock lock = new ReentrantReadWriteLock();this.blockingStatusReadLock = lock.readLock();this.blockingStatusWriteLock = lock.writeLock();}/** * 获取保存的实体对象 * @return */public E getSocket() {return socket;}public boolean isComet() {return comet;}public void setComet(boolean comet) {this.comet = comet;}/** * 异步 async的get * @return */public boolean isAsync() {return async;}/** * 异步async的set * @param async */public void setAsync(boolean async) {this.async = async;}public boolean isUpgraded() {return upgraded;}public void setUpgraded(boolean upgraded) {this.upgraded = upgraded;}/** * secure的set * @return */public boolean isSecure() {return secure;}/** * secure的get * @param secure */public void setSecure(boolean secure) {this.secure = secure;}/** * 获取最后访问时间 * @return */public long getLastAccess() {return lastAccess;}/** * 如果该对象实体socket为不为异步的,将当前时间毫秒赋给lastAccess */public void access() {/** * 异步超时时间是基于在调用startAsync和complete/dispatch这两个方法之间的时间, * 所以不需要更新最后访问时间当使用异步处理器读或写的时候. */// Async timeouts are based on the time between the call to startAsync()// and complete() / dispatch() so don't update the last access time// (that drives the timeout) on every read and write when using async// processing.if (!isAsync()) {access(System.currentTimeMillis());}}/** * 访问该对象 * * @param access */public void access(long access) {lastAccess = access;}/** * 设置超时时间 * @param timeout */public void setTimeout(long timeout) {this.timeout = timeout;}/** * 获取超时时间 * @return */public long getTimeout() {return this.timeout;}/** * 获取error * @return */public boolean getError() {return error;}/** * 设置error * @param error */public void setError(boolean error) {this.error = error;}public void setKeepAliveLeft(int keepAliveLeft) {this.keepAliveLeft = keepAliveLeft;}public int decrementKeepAlive() {return (--keepAliveLeft);}/** * 获取keptAlive * @return */public boolean isKeptAlive() {return keptAlive;}/** * set * @param keptAlive */public void setKeptAlive(boolean keptAlive) {this.keptAlive = keptAlive;}public boolean getBlockingStatus() {return blockingStatus;}public void setBlockingStatus(boolean blockingStatus) {this.blockingStatus = blockingStatus;}public Lock getBlockingStatusReadLock() {return blockingStatusReadLock;}public WriteLock getBlockingStatusWriteLock() {return blockingStatusWriteLock;}public Object getWriteThreadLock() {return writeThreadLock;}/** * 重置 * @param socket * @param timeout */public void reset(E socket, long timeout) {async = false;blockingStatus = true;comet = false;error = false;keepAliveLeft = 100;lastAccess = System.currentTimeMillis();this.socket = socket;this.timeout = timeout;upgraded = false;}/** * Overridden for debug purposes. No guarantees are made about the format of * this message which may vary significantly between point releases. * <p> * {@inheritDoc} */@Overridepublic String toString() {return super.toString() + ":" + String.valueOf(socket);}
0 0
- SocketWrapper泛型类
- 泛型类
- 泛型类
- 泛型类
- 泛型类
- 泛型类
- 泛型类
- **泛型类。**
- 泛型类
- 泛型类
- 泛型类
- 泛型类
- 泛型类
- 泛型类
- 泛型类
- 泛型类
- Dictionary 泛型类
- Dictionary 泛型类
- STL源码剖析—序列容器
- Java之美[从菜鸟到高手演变]之Quartz任务调度快速入门
- 第15周项目三在OJ上玩指针(2)
- 四轴加速度计滤波
- 26解决发送过来的号码和安全号码不一致的问题
- SocketWrapper泛型类
- StringBuffer和StringBuilder的使用区别
- 汉字2级字库2
- Android BLE 蓝牙低功耗教程,中央BluetoothGatt和周边BluetoothGattServer的实现
- notepad++ 中增加lua语法检查快捷键
- bzoj3781: 小B的询问 莫队算法
- 关于Struts中*.do的问题
- 低功耗蓝牙4.0BLE编程-nrf51822开发(1)
- Ajax中的XMLHttpRequest