|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectjava.lang.Thread
com.samskivert.util.LoopingThread
com.threerings.nio.conman.ConnectionManager
public abstract class ConnectionManager
Manages socket connections. It creates connection objects for each socket connection, but those connection objects interact closely with the connection manager because network I/O is done via a poll()-like mechanism rather than via threads.
ConnectionManager doesn't directly accept TCP connections; it expects
ServerSocketChannelAcceptor or an external entity to do so and call its
handleAcceptedSocket(java.nio.channels.SocketChannel) method
| Nested Class Summary | |
|---|---|
protected class |
ConnectionManager.OverflowQueue
Used to handle messages for a client whose network buffer has filled up because their outgoing network buffer has filled up. |
protected static interface |
ConnectionManager.PartialWriteHandler
Used to handle partial writes in writeMessage(com.threerings.nio.conman.Connection, byte[], com.threerings.nio.conman.ConnectionManager.PartialWriteHandler). |
| Nested classes/interfaces inherited from class java.lang.Thread |
|---|
Thread.State, Thread.UncaughtExceptionHandler |
| Field Summary | |
|---|---|
protected Queue<SocketChannel> |
_acceptedq
|
protected IntMap<Connection> |
_connections
Connections mapped by identifier. |
protected Queue<Connection> |
_deathq
|
protected Map<SelectionKey,NetEventHandler> |
_handlers
Maps selection keys to network event handlers. |
protected long |
_idleTime
|
protected long |
_lastDebugStamp
Used to periodically report connection manager activity when in debug mode. |
protected ConnectionManager.PartialWriteHandler |
_oflowHandler
Used to create an overflow queue on the first partial write. |
protected Map<Connection,ConnectionManager.OverflowQueue> |
_oflowqs
|
protected Runnable |
_onExit
A runnable to execute when the connection manager thread exits. |
protected ByteBuffer |
_outbuf
|
protected Queue<Tuple<Connection,byte[]>> |
_outq
|
protected int |
_selectLoopTime
Duration in milliseconds for which we wait for network events before checking our running flag to see if we should still be running. |
protected Selector |
_selector
|
protected SelectorIterable |
_selectorSelector
|
protected ConMgrStats |
_stats
Our current runtime stats. |
protected static byte[] |
ASYNC_CLOSE_REQUEST
Used to denote asynchronous close requests. |
protected static boolean |
DEBUG_REPORT
Whether or not debug reporting is activated . |
protected static long |
DEBUG_REPORT_INTERVAL
Report our activity every 30 seconds. |
protected static long |
LATENCY_GRACE
The number of milliseconds beyond the ping interval that we allow a client's network connection to be idle before we forcibly disconnect them. |
| Fields inherited from class com.samskivert.util.LoopingThread |
|---|
_running |
| Fields inherited from class java.lang.Thread |
|---|
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY |
| Constructor Summary | |
|---|---|
ConnectionManager(Lifecycle cycle,
long idleTime)
Creates a connection manager instance. |
|
| Method Summary | |
|---|---|
void |
closeConnection(Connection conn)
Queues a connection up to be closed on the conmgr thread. |
protected void |
connectionClosed(Connection conn)
Called by a connection when it discovers that it's closed. |
protected void |
connectionFailed(Connection conn,
IOException ioe)
Called by a connection if it experiences a network failure. |
protected void |
didShutdown()
|
ConMgrStats |
getStats()
Returns our current runtime statistics. |
protected abstract void |
handleAcceptedSocket(SocketChannel channel)
Adds a connection for the given socket to the managed set. |
protected void |
handleAcceptedSocket(SocketChannel channel,
Connection conn)
|
protected void |
handleIncoming(long iterStamp)
|
protected void |
handleIterateFailure(Exception e)
|
protected void |
iterate()
|
protected void |
noteWrite(int msgs,
int bytes)
Called by writeMessage(com.threerings.nio.conman.Connection, byte[], com.threerings.nio.conman.ConnectionManager.PartialWriteHandler) and friends when they write data over the network. |
protected void |
postAsyncClose(Connection conn)
Posts a fake message to this connection's outgoing message queue that will cause the connection to be closed when this message is reached. |
protected void |
processIncomingEvents(long iterStamp)
Checks for any network events on our set of sockets and passes those events down to their associated NetEventHandlers for processing. |
SelectionKey |
register(SelectableChannel chan,
int ops,
NetEventHandler netEventHandler)
Registers ops on chan on this manager's selector and hooks
netEventHandler up to receive events whenever the selection occurs. |
protected void |
sendOutgoingMessages(long iterStamp)
Writes all queued overflow and normal messages to their respective sockets. |
void |
setShutdownAction(Runnable onExit)
Instructs us to execute the specified runnable when the connection manager thread exits. |
void |
transferAcceptedSocket(SocketChannel channel)
Introduces a new active socket into Presents from off the ConnectionManager thread. |
protected void |
willStart()
|
protected boolean |
writeMessage(Connection conn,
byte[] data,
ConnectionManager.PartialWriteHandler pwh)
Writes a message out to a connection, passing the buck to the partial write handler if the entire message could not be written. |
| Methods inherited from class com.samskivert.util.LoopingThread |
|---|
isRunning, kick, run, shutdown |
| Methods inherited from class java.lang.Thread |
|---|
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Methods inherited from interface com.samskivert.util.Lifecycle.ShutdownComponent |
|---|
shutdown |
| Field Detail |
|---|
protected ConnectionManager.PartialWriteHandler _oflowHandler
protected Selector _selector
protected SelectorIterable _selectorSelector
protected Map<SelectionKey,NetEventHandler> _handlers
protected IntMap<Connection> _connections
protected Queue<Connection> _deathq
protected Queue<SocketChannel> _acceptedq
protected Queue<Tuple<Connection,byte[]>> _outq
protected ByteBuffer _outbuf
protected Map<Connection,ConnectionManager.OverflowQueue> _oflowqs
protected ConMgrStats _stats
protected long _lastDebugStamp
protected volatile Runnable _onExit
@Inject(optional=true) protected int _selectLoopTime
protected final long _idleTime
protected static final byte[] ASYNC_CLOSE_REQUEST
protected static final boolean DEBUG_REPORT
protected static final long DEBUG_REPORT_INTERVAL
protected static final long LATENCY_GRACE
| Constructor Detail |
|---|
public ConnectionManager(Lifecycle cycle,
long idleTime)
throws IOException
IOException| Method Detail |
|---|
public void setShutdownAction(Runnable onExit)
public ConMgrStats getStats()
public SelectionKey register(SelectableChannel chan,
int ops,
NetEventHandler netEventHandler)
throws IOException
ops on chan on this manager's selector and hooks
netEventHandler up to receive events whenever the selection occurs.
IOExceptionpublic void transferAcceptedSocket(SocketChannel channel)
public void closeConnection(Connection conn)
protected void willStart()
willStart in class LoopingThreadprotected void iterate()
iterate in class LoopingThreadprotected void handleIncoming(long iterStamp)
protected abstract void handleAcceptedSocket(SocketChannel channel)
protected void handleAcceptedSocket(SocketChannel channel,
Connection conn)
protected void processIncomingEvents(long iterStamp)
NetEventHandlers for processing.
protected void sendOutgoingMessages(long iterStamp)
protected boolean writeMessage(Connection conn,
byte[] data,
ConnectionManager.PartialWriteHandler pwh)
protected void noteWrite(int msgs,
int bytes)
writeMessage(com.threerings.nio.conman.Connection, byte[], com.threerings.nio.conman.ConnectionManager.PartialWriteHandler) and friends when they write data over the network.
protected void postAsyncClose(Connection conn)
protected void connectionFailed(Connection conn,
IOException ioe)
protected void connectionClosed(Connection conn)
protected void handleIterateFailure(Exception e)
handleIterateFailure in class LoopingThreadprotected void didShutdown()
didShutdown in class LoopingThread
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||