Package com.barchart.udt
Class SocketUDT
java.lang.Object
com.barchart.udt.SocketUDT
UDT native socket wrapper
note: current implementation supports IPv4 only (no IPv6)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum number of connections queued in listening mode byaccept()static final intBlock size used bysendFile(File, long, long)static final intMaximum number sockets that can participate in aSelectorUDT.select()operation; see epoll.h to confirm current limitstatic final intMinimum timeout of aSelectorUDT.select()operations.static final intinfinite message time to live;static booleanHelper value that can be checked from CCC class and force JNI library loadprivate intjava copy of underlying native accept queue size parameterprivate InetSocketAddresslocal end point; loaded by JNI byhasLoadedLocalSocketAddress()protected static final org.slf4j.Loggerprivate booleanprivate intprivate final MonitorUDTprivate InetSocketAddressremote end point; loaded by JNI byhasLoadedRemoteSocketAddress()static final intJNI Signature that must match between java code and c++ code on all platforms; failure to match will abort native library load, as an indication of inconsistent build.private final intnative address family; read by JNIprivate final intnative descriptor; read by JNI; see udt.h "typedef int UDTSOCKET;"static final intinfinite timeout:static longzero timeout:private final TypeUDTstatic final intUDT::select() sizeArray/sizeBuffer index offset for EXCEPTION reportstatic final intUDT::select() sizeArray/sizeBuffer index offset for READ intereststatic final intUDT::select() sizeArray/sizeBuffer size count or number of arrays/buffersstatic final intUDT::select() sizeArray/sizeBuffer index offset for WRITE interest -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaccept()protected SocketUDTaccept0()voidbind(InetSocketAddress localSocketAddress) protected voidbind0(InetSocketAddress localSocketAddress) static voidcleanup()Cleans up global JNI references and the UDT library.voidClear error status on a socket, if any.protected voidvoidclose()Close socket if not already closed.protected voidclose0()voidconnect(InetSocketAddress remoteSocketAddress) Connect to remote UDT socket.protected voidconnect0(InetSocketAddress remoteSocketAddress) protected static voidepollAdd0(int epollID, int socketID, int epollOpt) protected static intprotected static voidepollRelease0(int epollID) protected static voidepollRemove0(int epollID, int socketID) protected static voidepollUpdate0(int epollID, int socketID, int epollMask) update epoll maskprotected static intepollVerify0(int epollID, int socketID) query epoll maskprotected static intepollWait0(int epollID, IntBuffer readBuffer, IntBuffer writeBuffer, IntBuffer sizeBuffer, long millisTimeout) booleanNote: equality is based onsocketID.protected voidfinalize()NOTE: catch all exceptions; else prevents GCgetError()Error object wrapper.intError code set by last operation on a socket.protected intNative error message set by last operation on a socket.protected Stringintintbooleandefault isOrdered value used by sendmsg modeintdefault timeToLive value used by sendmsg mode<T> Tprotected ObjectgetOption0(int code, Class<?> klaz) intGet maximum receive buffer size.intbooleanCheck if local bind address is set to reuse mode.intGet maximum send buffer size.protected static intVerify that java code and c++ code builds are consistent.intGet time to linger on close (seconds).intGet "any blocking operation" timeout setting.protected intinthashCode()Note: usessocketIDas hash code.protected booleanLoadlocalSocketAddressvalue.protected booleanLoadremoteSocketAddressvalue.intid()native socket descriptor id; assigned by udt libraryprotected static voidCall this after loading native library.protected intinitInstance0(int typeCode) used by default constructorprotected intinitInstance1(int socketUDT) used by accept() internallybooleanCheck if socket is in strict blocking mode.booleanisBound()Check if socket is bound.booleanisClosed()Check if socket is closed.booleanCheck ifKindUDT.CONNECTORsocket is connected.booleanCheck if socket is in strict non-blocking mode.booleanisOpen()Check if socket is open.booleanvoidlisten(int queueSize) protected voidlisten0(int queueSize) monitor()performance monitor; updated byupdateMonitor(boolean)in JNIintreceive(byte[] array) receive into byte[] array uptoarray.lengthbytesintreceive(byte[] array, int position, int limit) receive into byte[] array uptosize=limit-positionbytesintreceive(ByteBuffer buffer) receive into; uptoinvalid reference
java.nio.channels.DirectByteBufferBuffer.remaining()bytesprotected static intreceive0(int socketID, int socketType, byte[] array) receive into a complete byte arrayprotected static intreceive1(int socketID, int socketType, byte[] array, int position, int limit) receive into a portion of a byte arrayprotected static intreceive2(int socketID, int socketType, ByteBuffer buffer, int position, int limit) receive into ainvalid reference
java.nio.channels.DirectByteBufferlongreceiveFile(File file, long offset, long length) Receive file from remote peer.protected static longreceiveFile0(int socketID, String path, long offset, long length, int block) Receive file.static intselectEpoll(int epollId, IntBuffer readBuffer, IntBuffer writeBuffer, IntBuffer sizeBuffer, long millisTimeout) Basic access to UDT socket readiness selection feature.intsend(byte[] array) send from byte[] array uptosize=array.lengthbytesintsend(byte[] array, int position, int limit) send from byte[] array uptosize=limit-positionbytesintsend(ByteBuffer buffer) send fromDirectByteBuffer, uptoBuffer.remaining()bytesprotected static intsend0(int socketID, int socketType, int timeToLive, boolean isOrdered, byte[] array) send from a complete byte[] array; wrapper for UDT::send(), UDT::sendmsg()protected static intsend1(int socketID, int socketType, int timeToLive, boolean isOrdered, byte[] array, int arayPosition, int arrayLimit) send from a portion of a byte[] array; wrapper for UDT::send(), UDT::sendmsg()protected static intsend2(int socketID, int socketType, int timeToLive, boolean isOrdered, ByteBuffer buffer, int bufferPosition, int bufferLimit) send fromDirectByteBuffer; wrapper for UDT::send(), UDT::sendmsg()longSend file to remote peer.protected static longSend file.voidsetBlocking(boolean block) Configure socket in strict blocking / strict non-blocking mode.voidApply default settings for message mode.voidsetMessageIsOdered(boolean isOrdered) default isOrdered value used by sendmsg modevoidsetMessageTimeTolLive(int timeToLive) default timeToLive value used by sendmsg mode<T> voidprotected voidsetOption0(int code, Class<?> klaz, Object value) voidsetReceiveBufferSize(int size) Set maximum receive buffer size.voidsetRendezvous(boolean isOn) voidsetReuseAddress(boolean on) voidsetSendBufferSize(int size) Set maximum send buffer size.voidsetSoLinger(boolean on, int linger) voidsetSoTimeout(int millisTimeout) call timeout (milliseconds); Set a timeout on blocking Socket operations: ServerSocket.accept(); SocketInputStream.read(); DatagramSocket.receive(); Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.status()returns native status of underlying native UDT socketprotected static voidCall this before unloading native library.protected static voidprotected static voidprotected static voidtestDirectIntBufferAccess0(IntBuffer buffer) protected static voidtestDirectIntBufferLoad0(IntBuffer buffer) protected static voidprotected static voidtestFillArray0(byte[] array) protected static voidtestFillBuffer0(ByteBuffer buffer) protected static voidtestGetSetArray0(int[] array, boolean isReturn) protected static voidtestInvalidClose0(int socketID) protected static voidtestIterateArray0(Object[] array) protected static voidtestIterateSet0(Set<Object> set) protected static int[]testMakeArray0(int size) toString()Show current monitor status.Show current socket options.type()message/stream socket type; read by JNIvoidupdateMonitor(boolean makeClear) Load updated statistics values intomonitorobject.protected voidupdateMonitor0(boolean makeClear)
-
Field Details
-
DEFAULT_ACCEPT_QUEUE_SIZE
public static final int DEFAULT_ACCEPT_QUEUE_SIZEMaximum number of connections queued in listening mode byaccept()- See Also:
-
DEFAULT_FILE_BLOCK_SIZE
public static final int DEFAULT_FILE_BLOCK_SIZEBlock size used bysendFile(File, long, long)- See Also:
-
DEFAULT_MAX_SELECTOR_SIZE
public static final int DEFAULT_MAX_SELECTOR_SIZEMaximum number sockets that can participate in aSelectorUDT.select()operation; see epoll.h to confirm current limit- See Also:
-
DEFAULT_MIN_SELECTOR_TIMEOUT
public static final int DEFAULT_MIN_SELECTOR_TIMEOUTMinimum timeout of aSelectorUDT.select()operations.- See Also:
-
INFINITE_TTL
public static final int INFINITE_TTLinfinite message time to live;- See Also:
-
INIT_OK
Helper value that can be checked from CCC class and force JNI library load -
log
protected static final org.slf4j.Logger log -
SIGNATURE_JNI
JNI Signature that must match between java code and c++ code on all platforms; failure to match will abort native library load, as an indication of inconsistent build.- See Also:
-
TIMEOUT_INFINITE
public static final int TIMEOUT_INFINITEinfinite timeout:blocking send/receive
epoll wait
- See Also:
-
TIMEOUT_NONE
public static long TIMEOUT_NONEzero timeout:epoll wait
-
UDT_EXCEPT_INDEX
UDT::select() sizeArray/sizeBuffer index offset for EXCEPTION report- See Also:
-
UDT_READ_INDEX
UDT::select() sizeArray/sizeBuffer index offset for READ interest- See Also:
-
UDT_SIZE_COUNT
UDT::select() sizeArray/sizeBuffer size count or number of arrays/buffers- See Also:
-
UDT_WRITE_INDEX
UDT::select() sizeArray/sizeBuffer index offset for WRITE interest- See Also:
-
listenQueueSize
private volatile int listenQueueSizejava copy of underlying native accept queue size parameter- See Also:
-
localSocketAddress
local end point; loaded by JNI byhasLoadedLocalSocketAddress() -
messageIsOrdered
private volatile boolean messageIsOrdered -
messageTimeTolive
private volatile int messageTimeTolive -
monitor
-
remoteSocketAddress
remote end point; loaded by JNI byhasLoadedRemoteSocketAddress() -
socketAddressFamily
native address family; read by JNITODO add support for AF_INET6
-
socketID
native descriptor; read by JNI; see udt.h "typedef int UDTSOCKET;" -
type
-
-
Constructor Details
-
SocketUDT
"Primary" socket. Default constructor; will applysetDefaultMessageSendMode()- Parameters:
type- UDT socket type- Throws:
ExceptionUDT
-
SocketUDT
"Secondary" socket. Made byaccept0(), will applysetDefaultMessageSendMode()- Parameters:
socketID- UDT socket descriptor;- Throws:
ExceptionUDT
-
-
Method Details
-
cleanup
Cleans up global JNI references and the UDT library.The behavior of SocketUDT class after a call to cleanup is undefined, so it should only ever be called once you are done and you are ready for the class loader to unload the JNI library
- Throws:
ExceptionUDT
-
epollAdd0
- Throws:
ExceptionUDT- See Also:
-
epollCreate0
- Returns:
- epoll id
- Throws:
ExceptionUDT- See Also:
-
epollRelease0
- Throws:
ExceptionUDT- See Also:
-
epollRemove0
- Throws:
ExceptionUDT- See Also:
-
epollUpdate0
update epoll mask- Throws:
ExceptionUDT
-
epollVerify0
query epoll mask- Throws:
ExceptionUDT
-
epollWait0
protected static int epollWait0(int epollID, IntBuffer readBuffer, IntBuffer writeBuffer, IntBuffer sizeBuffer, long millisTimeout) throws ExceptionUDT - Throws:
ExceptionUDT- See Also:
-
getSignatureJNI0
protected static int getSignatureJNI0()Verify that java code and c++ code builds are consistent.- See Also:
-
initClass0
Call this after loading native library.- Throws:
ExceptionUDT- See Also:
-
receive0
receive into a complete byte array- Throws:
ExceptionUDT- See Also:
-
receive1
protected static int receive1(int socketID, int socketType, byte[] array, int position, int limit) throws ExceptionUDT receive into a portion of a byte array- Throws:
ExceptionUDT- See Also:
-
receive2
protected static int receive2(int socketID, int socketType, ByteBuffer buffer, int position, int limit) throws ExceptionUDT receive into ainvalid reference
java.nio.channels.DirectByteBuffer- Throws:
ExceptionUDT- See Also:
-
receiveFile0
protected static long receiveFile0(int socketID, String path, long offset, long length, int block) throws ExceptionUDT Receive file.- Throws:
ExceptionUDT- See Also:
-
selectEpoll
public static int selectEpoll(int epollId, IntBuffer readBuffer, IntBuffer writeBuffer, IntBuffer sizeBuffer, long millisTimeout) throws ExceptionUDT Basic access to UDT socket readiness selection feature. Based oninfo exchange.Timeout is in milliseconds.invalid reference
java.nio.DirectIntBuffer- Parameters:
millisTimeout- http://udt.sourceforge.net/udt4/doc/epoll.htm "Finally, for epoll_wait, negative timeout value will make the function to wait until an event happens. If the timeout value is 0, then the function returns immediately with any sockets associated an IO event. If timeout occurs before any event happens, the function returns 0".- Returns:
invalid input: '<'0: should not happen
=0: timeout, no ready sockets
>0: total number or reads, writes, exceptions- Throws:
ExceptionUDT- See Also:
-
send0
protected static int send0(int socketID, int socketType, int timeToLive, boolean isOrdered, byte[] array) throws ExceptionUDT send from a complete byte[] array; wrapper for UDT::send(), UDT::sendmsg()- Throws:
ExceptionUDT- See Also:
-
send1
protected static int send1(int socketID, int socketType, int timeToLive, boolean isOrdered, byte[] array, int arayPosition, int arrayLimit) throws ExceptionUDT send from a portion of a byte[] array; wrapper for UDT::send(), UDT::sendmsg()- Throws:
ExceptionUDT- See Also:
-
send2
protected static int send2(int socketID, int socketType, int timeToLive, boolean isOrdered, ByteBuffer buffer, int bufferPosition, int bufferLimit) throws ExceptionUDT send fromDirectByteBuffer; wrapper for UDT::send(), UDT::sendmsg()- Throws:
ExceptionUDT- See Also:
-
sendFile0
protected static long sendFile0(int socketID, String path, long offset, long length, int block) throws ExceptionUDT Send file.- Throws:
ExceptionUDT- See Also:
-
stopClass0
Call this before unloading native library.- Throws:
ExceptionUDT- See Also:
-
testCrashJVM0
protected static void testCrashJVM0() -
testDirectByteBufferAccess0
-
testDirectIntBufferAccess0
-
testDirectIntBufferLoad0
-
testEmptyCall0
protected static void testEmptyCall0() -
testFillArray0
protected static void testFillArray0(byte[] array) -
testFillBuffer0
-
testGetSetArray0
protected static void testGetSetArray0(int[] array, boolean isReturn) -
testInvalidClose0
- Throws:
ExceptionUDT
-
testIterateArray0
-
testIterateSet0
-
testMakeArray0
protected static int[] testMakeArray0(int size) -
accept
- Returns:
- null : no incoming connections (non-blocking mode only)
non null : newly accepted SocketUDT (both blocking and non-blocking) - Throws:
ExceptionUDT
-
accept0
- Throws:
ExceptionUDT- See Also:
-
bind
public void bind(InetSocketAddress localSocketAddress) throws ExceptionUDT, IllegalArgumentException - Throws:
ExceptionUDTIllegalArgumentException
-
bind0
- Throws:
ExceptionUDT- See Also:
-
clearError
public void clearError()Clear error status on a socket, if any.- See Also:
-
clearError0
protected void clearError0()- See Also:
-
close
Close socket if not already closed.- Throws:
ExceptionUDT- See Also:
-
close0
- Throws:
ExceptionUDT- See Also:
-
connect
Connect to remote UDT socket.Can be blocking or non blocking call; depending on
OptionUDT.Is_Receive_SynchronousTiming: UDT uses hard coded connect timeout:
normal socket: 3 seconds
rendezvous socket: 30 seconds; when
OptionUDT.Is_Randezvous_Connect_Enabledis true- Throws:
ExceptionUDT- See Also:
-
connect0
- Throws:
ExceptionUDT- See Also:
-
equals
Note: equality is based onsocketID. -
finalize
protected void finalize()NOTE: catch all exceptions; else prevents GCNOTE: do not leak "this" references; else prevents GC
-
getError
Error object wrapper.- Returns:
- error status set by last socket operation
-
getErrorCode
public int getErrorCode()Error code set by last operation on a socket.- See Also:
-
getErrorCode0
protected int getErrorCode0()- See Also:
-
getErrorMessage
Native error message set by last operation on a socket.- See Also:
-
getErrorMessage0
- See Also:
-
getListenQueueSize
public int getListenQueueSize()- See Also:
-
getLocalInetAddress
- Returns:
- null : not bound
not null : valid address; result ofbind(InetSocketAddress)
-
getLocalInetPort
public int getLocalInetPort()- Returns:
- 0 : not bound
>0 : valid port; result ofbind(InetSocketAddress)
-
getLocalSocketAddress
- Returns:
- null: not bound;
not null: local UDT socket address to which the the socket is bound - Throws:
ExceptionUDT- See Also:
-
getMessageIsOdered
public boolean getMessageIsOdered()default isOrdered value used by sendmsg mode- See Also:
-
getMessageTimeTolLive
public int getMessageTimeTolLive()default timeToLive value used by sendmsg mode- See Also:
-
getOption
- Throws:
ExceptionUDT- See Also:
-
getOption0
- Throws:
ExceptionUDT- See Also:
-
getReceiveBufferSize
Get maximum receive buffer size. Reflects minimum of protocol-level (UDT) and kernel-level(UDP) settings.- Throws:
ExceptionUDT- See Also:
-
getRemoteInetAddress
- Returns:
- null : not connected
not null : valid address; result ofconnect(InetSocketAddress)
-
getRemoteInetPort
public int getRemoteInetPort()- Returns:
- 0 : not connected
>0 : valid port ; result ofconnect(InetSocketAddress)
-
getRemoteSocketAddress
- Returns:
- null : not connected;
not null: remote UDT peer socket address to which this socket is connected - Throws:
ExceptionUDT- See Also:
-
getReuseAddress
Check if local bind address is set to reuse mode.- Throws:
ExceptionUDT- See Also:
-
getSendBufferSize
Get maximum send buffer size. Reflects minimum of protocol-level (UDT) and kernel-level(UDP) settings.- Throws:
ExceptionUDT- See Also:
-
getSoLinger
Get time to linger on close (seconds).- Throws:
ExceptionUDT- See Also:
-
getSoTimeout
Get "any blocking operation" timeout setting. Returns milliseconds; zero return means "infinite"; negative means invalid- Throws:
ExceptionUDT- See Also:
-
getStatus0
protected int getStatus0()- See Also:
-
hashCode
public int hashCode()Note: usessocketIDas hash code. -
hasLoadedLocalSocketAddress
protected boolean hasLoadedLocalSocketAddress()LoadlocalSocketAddressvalue.- See Also:
-
hasLoadedRemoteSocketAddress
protected boolean hasLoadedRemoteSocketAddress()LoadremoteSocketAddressvalue.- See Also:
-
id
public int id()native socket descriptor id; assigned by udt library -
initInstance0
used by default constructor- Throws:
ExceptionUDT
-
initInstance1
used by accept() internally- Throws:
ExceptionUDT
-
isBlocking
public boolean isBlocking()Check if socket is in strict blocking mode. (JDK semantics)- Returns:
- true : socket is valid and both send and receive are set to blocking mode; false : at least one channel is set to non-blocking mode or socket is invalid;
- See Also:
-
isBound
public boolean isBound()Check if socket is bound. (JDK semantics)- Returns:
- true :
bind(InetSocketAddress)was successful
false : otherwise
-
isClosed
public boolean isClosed()Check if socket is closed. A convenience !isOpen();- See Also:
-
isConnected
public boolean isConnected()Check ifKindUDT.CONNECTORsocket is connected. (JDK semantics)- Returns:
- true :
connect(InetSocketAddress)was successful
false : otherwise
-
isNonBlocking
public boolean isNonBlocking()Check if socket is in strict non-blocking mode.- Returns:
- true : socket is valid and both send and receive are set to NON blocking mode; false : at least one channel is set to blocking mode or socket is invalid;
- See Also:
-
isOpen
public boolean isOpen()Check if socket is open. (JDK semantics). The status of underlying UDT socket is mapped into JDK expected categories- See Also:
-
isRendezvous
public boolean isRendezvous() -
listen
- Parameters:
queueSize- maximum number of queued clients- Throws:
ExceptionUDT- See Also:
-
listen0
- Throws:
ExceptionUDT- See Also:
-
monitor
performance monitor; updated byupdateMonitor(boolean)in JNI- See Also:
-
receive
receive into byte[] array uptoarray.lengthbytes- Returns:
-1: nothing received (non-blocking only)
=0: timeout expired (blocking only)
>0: normal receive, byte count- Throws:
ExceptionUDT- See Also:
-
receive
receive into byte[] array uptosize=limit-positionbytes- Returns:
-1: nothing received (non-blocking only)
=0: timeout expired (blocking only)
>0: normal receive, byte count- Throws:
ExceptionUDT- See Also:
-
receive
receive into; uptoinvalid reference
java.nio.channels.DirectByteBufferBuffer.remaining()bytes- Returns:
-1: nothing received (non-blocking only)
=0: timeout expired (blocking only)
>0: normal receive, byte count- Throws:
ExceptionUDT- See Also:
-
receiveFile
Receive file from remote peer.- Throws:
ExceptionUDT- See Also:
-
send
send from byte[] array uptosize=array.lengthbytes- Parameters:
array- array to send- Returns:
-1: no buffer space (non-blocking only)
=0: timeout expired (blocking only)
>0: normal send, actual sent byte count- Throws:
ExceptionUDT- See Also:
-
send
send from byte[] array uptosize=limit-positionbytes- Parameters:
array- array to sendposition- start of array portion to sendlimit- finish of array portion to send- Returns:
-1: no buffer space (non-blocking only)
=0: timeout expired (blocking only)
>0: normal send, actual sent byte count- Throws:
ExceptionUDT- See Also:
-
send
send fromDirectByteBuffer, uptoBuffer.remaining()bytes- Parameters:
buffer- buffer to send- Returns:
-1: no buffer space (non-blocking only)
=0: timeout expired (blocking only)
>0: normal send, actual sent byte count- Throws:
ExceptionUDT- See Also:
-
sendFile
Send file to remote peer.- Throws:
ExceptionUDT- See Also:
-
setBlocking
Configure socket in strict blocking / strict non-blocking mode.- Parameters:
block- true : set both send and receive to blocking mode; false : set both send and receive to non-blocking mode- Throws:
ExceptionUDT- See Also:
-
setDefaultMessageSendMode
public void setDefaultMessageSendMode()Apply default settings for message mode.IsOdered = true;
TimeTolLive = INFINITE_TTL; -
setMessageIsOdered
public void setMessageIsOdered(boolean isOrdered) default isOrdered value used by sendmsg mode- See Also:
-
setMessageTimeTolLive
public void setMessageTimeTolLive(int timeToLive) default timeToLive value used by sendmsg mode- See Also:
-
setOption
- Throws:
ExceptionUDT- See Also:
-
setOption0
- Throws:
ExceptionUDT- See Also:
-
setReceiveBufferSize
Set maximum receive buffer size. Affects both protocol-level (UDT) and kernel-level(UDP) settings.- Throws:
ExceptionUDT
-
setRendezvous
- Throws:
ExceptionUDT
-
setReuseAddress
- Throws:
ExceptionUDT
-
setSendBufferSize
Set maximum send buffer size. Affects both protocol-level (UDT) and kernel-level(UDP) settings- Throws:
ExceptionUDT- See Also:
-
setSoLinger
- Throws:
ExceptionUDT
-
setSoTimeout
call timeout (milliseconds); Set a timeout on blocking Socket operations: ServerSocket.accept(); SocketInputStream.read(); DatagramSocket.receive(); Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. A timeout of zero is interpreted as an infinite timeout.- Throws:
ExceptionUDT
-
status
returns native status of underlying native UDT socket -
toString
-
toStringMonitor
Show current monitor status. -
toStringOptions
Show current socket options. -
type
message/stream socket type; read by JNI -
updateMonitor
Load updated statistics values intomonitorobject. Must call this methos only on connected socket.- Parameters:
makeClear- true : reset all statistics with this call; false : keep collecting statistics, load updated values.- Throws:
ExceptionUDT- See Also:
-
updateMonitor0
- Throws:
ExceptionUDT- See Also:
-