This commit is contained in:
地平线
2015-09-16 19:40:58 +08:00
parent 08eab09a01
commit 527bb92c8c

View File

@@ -9,6 +9,7 @@ import java.io.*;
import java.net.*; import java.net.*;
import java.nio.*; import java.nio.*;
import java.nio.channels.*; import java.nio.channels.*;
import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
/** /**
@@ -17,6 +18,8 @@ import java.util.concurrent.*;
*/ */
public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCloseable { public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCloseable {
protected Map<String, Object> attributes;
public abstract boolean isTCP(); public abstract boolean isTCP();
public abstract SocketAddress getRemoteAddress(); public abstract SocketAddress getRemoteAddress();
@@ -37,6 +40,29 @@ public abstract class AsyncConnection implements AsynchronousByteChannel, AutoCl
public abstract void dispose(); //同close 只是去掉throws IOException public abstract void dispose(); //同close 只是去掉throws IOException
public void setAttribute(String name, Object value) {
if (attributes == null) attributes = new HashMap<>();
attributes.put(name, value);
}
@SuppressWarnings("unchecked")
public final <T> T getAttribute(String name) {
return (T) (attributes == null ? null : attributes.get(name));
}
public final void removeAttribute(String name) {
if (attributes != null) attributes.remove(name);
}
public final Map<String, Object> getAttributes() {
return attributes;
}
public final void clearAttribute() {
if (attributes != null) attributes.clear();
}
//------------------------------------------------------------------------------------------------------------------------------
public static AsyncConnection create(final String protocol, final SocketAddress address) throws IOException { public static AsyncConnection create(final String protocol, final SocketAddress address) throws IOException {
return create(protocol, address, 0, 0); return create(protocol, address, 0, 0);
} }