public interface NIOSocket extends NIOAbstractSocket
The NIOSocket executes callbacks to a Socket observer to react to incoming packets and other events.
Modifier and Type | Method and Description |
---|---|
void |
closeAfterWrite()
Causes the socket to close after writing the current entries in the queue
(consequent entries will be thrown away).
|
long |
getBytesRead()
Return the total number of bytes read on this socket since
it was opened.
|
long |
getBytesWritten()
Return the total number of bytes written on this socket
since it was opened.
|
int |
getMaxQueueSize()
The current maximum queue size in bytes.
|
long |
getTimeOpen()
Return the time this socket has been open.
|
long |
getWriteQueueSize()
This method returns the number of bytes queued for dispatch.
|
void |
listen(SocketObserver socketObserver)
Opens the socket for reads.
|
void |
queue(java.lang.Runnable runnable)
Queue a runnable in the packet queue.
|
void |
setMaxQueueSize(int maxQueueSize)
Sets the maximum number of bytes allowed in the queue for this socket.
|
void |
setPacketReader(PacketReader packetReader)
Sets the packet reader for this socket.
|
void |
setPacketWriter(PacketWriter packetWriter)
Sets the packet writer for this socket.
|
java.net.Socket |
socket()
Allows access to the underlying socket.
|
boolean |
write(byte[] packet)
Write a packet of bytes asynchronously on this socket.
|
boolean |
write(byte[] packet,
java.lang.Object tag)
Write a packet of bytes asynchronously on this socket.
|
close, getAddress, getIp, getPort, getTag, isOpen, setTag
boolean write(byte[] packet)
The bytes will be sent to the PacketWriter belonging to this
socket for dispatch. However, if the queue is full (i.e. the new
queue size would exceed getMaxQueueSize()
),
the packet is discarded and the method returns false.
This method is thread-safe.
packet
- the packet to send.boolean write(byte[] packet, java.lang.Object tag)
The bytes will be sent to the PacketWriter belonging to this
socket for dispatch. However, if the queue is full (i.e. the new
queue size would exceed getMaxQueueSize()
),
the packet is discarded and the method returns false.
This method is thread-safe.
packet
- the packet to send.tag
- an optional tag to tag the packet (used in SocketObserver.packetSent(NIOSocket, Object)
).void queue(java.lang.Runnable runnable)
This method is thread-safe.
runnable
- the runnable to queue.long getBytesRead()
This method is thread-safe.
long getBytesWritten()
This method is thread-safe.
long getTimeOpen()
This method is thread-safe.
long getWriteQueueSize()
However, this number does not include the packet currently waiting to be written.
This method is thread-safe.
int getMaxQueueSize()
This method is thread-safe.
void setMaxQueueSize(int maxQueueSize)
This method is thread-safe.
maxQueueSize
- the new max queue size. A value less than 1 is an unbounded queue.void setPacketReader(PacketReader packetReader)
packetReader
- the packet reader to interpret the incoming byte stream.void setPacketWriter(PacketWriter packetWriter)
This method is thread-safe and all packets posted before the writer is changed is guaranteed to be serialized using the previous writer.
packetWriter
- the packet writer to interpret the incoming byte stream.void listen(SocketObserver socketObserver)
The socket observer will receive connects, disconnects and packets. If the socket was opened or disconnected before the observer was attached, the socket observer will still receive those callbacks.
This method is thread-safe, but may only be called once.
socketObserver
- the observer to receive packets and be notified of connects/disconnects.java.lang.IllegalStateException
- if the method already has been called.void closeAfterWrite()
Also see close()
if you want to immediately close the socket.
This method is thread-safe.
java.net.Socket socket()
Note that accessing streams or closing the socket will put this NIOSocket in an undefined state