Package com.germancoding.packetapi
Class PacketHandler
- java.lang.Object
- 
- com.germancoding.packetapi.PacketHandler
 
- 
 public class PacketHandler extends java.lang.Object
- 
- 
Field SummaryFields Modifier and Type Field Description static intDATA_TIMEOUTTimeout (in MS) after which KeepAlive packets should be send.static intHANDSHAKE_ID_REQUESTHandshake ID used in the sendHandshake() method.static intHANDSHAKE_ID_RESPONSEHandshake ID used when replying to a handshake packet.protected java.io.InputStreaminjava.util.logging.Loggerloggerprotected java.io.OutputStreamoutstatic intPROTOCOL_VERSIONApplications can change this value if they want.
 - 
Constructor SummaryConstructors Constructor Description PacketHandler(java.io.InputStream in, java.io.OutputStream out, java.lang.String connectionName, PacketListener listener)Creates a new PacketHandler instance.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanautomaticPacketProcessing()booleanautoSendKeepAlive()voidclose()Closes the connection, the I/O streams and notfies the other side that we are closing this connection.java.util.List<Packet>getCachedPackets()Returns all packets that have not been processed yet.java.lang.StringgetConnectionName()DefaultPacketListenergetDefaultPacketListener()longgetLastPacketReceived()longgetLastPacketSend()PacketListenergetListener()PacketgetNewPacketInstance(short id)DataReadergetReader()intgetRemoteProtocolVersion()DataSendergetSender()booleanisClosed()booleanisHandshakeSend()booleanisInstantFlush()booleanisVersionApproved()voidnotifyOnDefaultPackets(boolean notify)Sets whether this library callsPacketListener.onPacketReceived()when default packets are received.voidonConnectionClosed(java.lang.String message, boolean expected)Notifies this instance that the connection is closed (or will be closed now) for an external reason.voidonConnectionFail(java.lang.Exception e)Notifies this instance that the connection has failed.voidonPacketReceived(Packet packet)Called when a new packet was received.voidonUnknownPacketReceived(short id)voidregisterPacket(java.lang.Class<? extends Packet> packetClass)Registers a new packet.voidsendHandshake()Sends a handshake to exchange version numbers.voidsendHandshake(int id)Sends a handshake using the given id as the handshake id.voidsendPacket(Packet p)Sends the given packet by adding it to the sending queue.voidsetAutomaticPacketProcessing(boolean on)voidsetAutoSendKeepAlive(boolean autoSendKeepAlive)voidsetConnectionName(java.lang.String connectionName)protected voidsetDefaultPacketListener(DefaultPacketListener defaultPacketListener)voidsetHandshakeSend(boolean handshakeSend)voidsetInstantFlush(boolean instantFlush)voidsetLastPacketReceived(long lastPacketReceived)voidsetLastPacketSend(long lastPacketSend)voidsetListener(PacketListener listener)voidsetReader(DataReader s)voidsetRemoteProtocolVersion(int remoteProtocolVersion)voidsetSender(DataSender s)voidsetVersionApproved(boolean versionApproved)booleanshouldSendKeepAlive()voidshutdown()Shuts down this PacketHandler instance silently without touching the underlying streams or sockets.
 
- 
- 
- 
Field Detail- 
PROTOCOL_VERSIONpublic static int PROTOCOL_VERSION Applications can change this value if they want. Default is 1
 - 
HANDSHAKE_ID_REQUESTpublic static final int HANDSHAKE_ID_REQUEST Handshake ID used in the sendHandshake() method. The other side will respond to that packet. Default is 0- See Also:
- Constant Field Values
 
 - 
HANDSHAKE_ID_RESPONSEpublic static final int HANDSHAKE_ID_RESPONSE Handshake ID used when replying to a handshake packet. The other side will not respond to that packet. Default is 1- See Also:
- Constant Field Values
 
 - 
DATA_TIMEOUTpublic static int DATA_TIMEOUT Timeout (in MS) after which KeepAlive packets should be send. Default is 20.000 ms
 - 
loggerpublic java.util.logging.Logger logger 
 - 
inprotected java.io.InputStream in 
 - 
outprotected java.io.OutputStream out 
 
- 
 - 
Constructor Detail- 
PacketHandlerpublic PacketHandler(java.io.InputStream in, java.io.OutputStream out, java.lang.String connectionName, PacketListener listener)Creates a new PacketHandler instance. The instance will use the given I/O streams to send and receive data.
 
 Note about timeouts: When the I/O stream reads from a UDP socket (or any other protocol that deals with timeouts), beware of the socket timeout value! If thePacketHandlerruns into a timeout, the connection will be marked as failed and will be terminated. To avoid this, set the timeout to zero or set it to something higher then theDATA_TIMEOUTand use the keep-alive function.- Parameters:
- in- The InputStream to read data from.
- out- The OutputStream to send data to.
- connectionName- Optional: Give the connection a name to identify it. Can be- null.
- listener- A listener which is notified when something happens (A packet arrived, the connection failed...). Can be- nullif the application does not want to listen to incoming data.
 
 
- 
 - 
Method Detail- 
registerPacketpublic void registerPacket(java.lang.Class<? extends Packet> packetClass) throws java.lang.Exception Registers a new packet. After registering the packet type can be send and received.- Parameters:
- packetClass- The class of the new packet.
- Throws:
- java.lang.Exception- If reflection fails, like when there is no nullary constructor.
 
 - 
getNewPacketInstancepublic Packet getNewPacketInstance(short id) 
 - 
sendPacketpublic void sendPacket(Packet p) Sends the given packet by adding it to the sending queue. Sendings packets that are not registered is possible, but is not recommended. Calling this method has the same effect as callinggetSender().sendPacket(p)- Parameters:
- p- The packet to send.
 
 - 
onConnectionFailpublic void onConnectionFail(java.lang.Exception e) Notifies this instance that the connection has failed. This method notifies the listeners and closes the connection- Parameters:
- e- An exception describing why the connection has failed.
 
 - 
onConnectionClosedpublic void onConnectionClosed(java.lang.String message, boolean expected)Notifies this instance that the connection is closed (or will be closed now) for an external reason.- Parameters:
- message- Message why the connection was closed.
- expected- Whether this was expected (like there was a close packet) or not (like when the underlying socket is closed without notification)
 
 - 
closepublic void close() Closes the connection, the I/O streams and notfies the other side that we are closing this connection.
 - 
getConnectionNamepublic java.lang.String getConnectionName() - Returns:
- The name of this connection.
 
 - 
setConnectionNamepublic void setConnectionName(java.lang.String connectionName) 
 - 
getReaderpublic DataReader getReader() - Returns:
- The reader which is reading data from the InputStream.
 
 - 
getSenderpublic DataSender getSender() - Returns:
- The sender which is sending data to the OutputStream.
 
 - 
getListenerpublic PacketListener getListener() - Returns:
- The application/user listener.
 
 - 
setListenerpublic void setListener(PacketListener listener) 
 - 
getDefaultPacketListenerpublic DefaultPacketListener getDefaultPacketListener() - Returns:
- The listener used by the PacketAPI for default packets.
 
 - 
setDefaultPacketListenerprotected void setDefaultPacketListener(DefaultPacketListener defaultPacketListener) 
 - 
onPacketReceivedpublic void onPacketReceived(Packet packet) Called when a new packet was received.- Parameters:
- packet- The packet just received.
 
 - 
onUnknownPacketReceivedpublic void onUnknownPacketReceived(short id) 
 - 
isClosedpublic boolean isClosed() - Returns:
- Whether the connection was closed (using the close() function of this class) or not.
 
 - 
sendHandshakepublic void sendHandshake() Sends a handshake to exchange version numbers. You have to call this method if you want to use state attributes like isVersionApproved().
 - 
sendHandshakepublic void sendHandshake(int id) Sends a handshake using the given id as the handshake id.- Parameters:
- id- The handshake id.
 
 - 
getCachedPacketspublic java.util.List<Packet> getCachedPackets() Returns all packets that have not been processed yet. ReturnsnullifautomaticPacketProcessing()is true.
 Calling this method will first pass the cached packets to the listeners before it returns them.
 IfautomaticPacketProcessing()is false, it is recommended to call this method frequently otherwise no packets will get processed.- Returns:
- All packets in the queue or an empty list. Only nullif packets are automatically processed and the queue is empty.
- See Also:
- automaticPacketProcessing()
 
 - 
automaticPacketProcessingpublic boolean automaticPacketProcessing() - Returns:
- Whether automatic packet processing is on. If true, packets will be passed directly to the listener after receiving. This is done on an async thread (called DataReader). 
 In some cases you may want to handle to the incoming packets on your (main) thread. To achieve this, set automatic packet processing to false and callgetCachedPackets()frequently to get your packets processed on your thread.
 Default is true.
 
 - 
setAutomaticPacketProcessingpublic void setAutomaticPacketProcessing(boolean on) 
 - 
isVersionApprovedpublic boolean isVersionApproved() - Returns:
- Whether the protocol version is approved. The protocol version is approved when both peers have the same protocol version.
 To request a version check, simply send a handshake packet by callingsendHandshake(). After receiving the response from the other side and comparing the version numbers, the version will be approved.
 
 - 
setVersionApprovedpublic void setVersionApproved(boolean versionApproved) 
 - 
isHandshakeSendpublic boolean isHandshakeSend() - Returns:
- Whether this side has send a handshake packet.
 Note: Handshake packets are not send automatically, you have to callsendHandshake().
 
 - 
setHandshakeSendpublic void setHandshakeSend(boolean handshakeSend) 
 - 
getRemoteProtocolVersionpublic int getRemoteProtocolVersion() - Returns:
- The protocol version of the other peer or -1 if the version used by the other peer is unknown (e.g no handshake was send)
 
 - 
setRemoteProtocolVersionpublic void setRemoteProtocolVersion(int remoteProtocolVersion) 
 - 
getLastPacketReceivedpublic long getLastPacketReceived() - Returns:
- The timestamp when the last packet was (successfull) received. 0 if no packet was received yet.
 
 - 
setLastPacketReceivedpublic void setLastPacketReceived(long lastPacketReceived) 
 - 
getLastPacketSendpublic long getLastPacketSend() - Returns:
- The timestamp when the last packet was send. 0 if no packet was send yet.
 
 - 
setLastPacketSendpublic void setLastPacketSend(long lastPacketSend) 
 - 
autoSendKeepAlivepublic boolean autoSendKeepAlive() - Returns:
- Whether the library automatically sends packets to keep the connection alive. Default is false.
 
 - 
setAutoSendKeepAlivepublic void setAutoSendKeepAlive(boolean autoSendKeepAlive) 
 - 
shouldSendKeepAlivepublic boolean shouldSendKeepAlive() - Returns:
- If no data is send or received by the application for some time, this value returns true.
- See Also:
- DATA_TIMEOUT
 
 - 
notifyOnDefaultPacketspublic void notifyOnDefaultPackets(boolean notify) Sets whether this library callsPacketListener.onPacketReceived()when default packets are received.- Parameters:
- notify- Whether to notify your PacketListener when we receive default packets. Normally, only the DefaultPacketListener gets notified.
 
 - 
isInstantFlushpublic boolean isInstantFlush() - Returns:
- If the OutputStream is immediatly flushed after a packet has been send. This is usually not needed, but COULD improve ping times. Default is false
 
 - 
setInstantFlushpublic void setInstantFlush(boolean instantFlush) 
 - 
shutdownpublic void shutdown() Shuts down this PacketHandler instance silently without touching the underlying streams or sockets.
 - 
setSenderpublic void setSender(DataSender s) throws java.lang.InterruptedException - Throws:
- java.lang.InterruptedException
 
 - 
setReaderpublic void setReader(DataReader s) throws java.lang.InterruptedException - Throws:
- java.lang.InterruptedException
 
 
- 
 
-