Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (C) 2009-2013 Barchart, Inc.
*
* All rights reserved. Licensed under the OSI BSD License.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package com.barchart.udt;
import com.barchart.udt.anno.Native;
import com.barchart.udt.nio.KindUDT;
import com.barchart.udt.util.HelpUDT;
import dorkbox.network.util.NativeLoader;
import dorkbox.util.FileUtil;
import dorkbox.util.OS;
import dorkbox.util.OsType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.List;
import java.util.Set;
/**
* UDT native socket wrapper
*
* note: current implementation supports IPv4 only (no IPv6)
*/
public
class SocketUDT {
/**
* Maximum number of connections queued in listening mode by
* {@link #accept()}
*/
public static final int DEFAULT_ACCEPT_QUEUE_SIZE = 256;
/**
* Block size used by {@link #sendFile(File, long, long)}
*/
public static final int DEFAULT_FILE_BLOCK_SIZE = 1 * 1024 * 1024;
/**
* Maximum number sockets that can participate in a
* {@link com.barchart.udt.nio.SelectorUDT#select()} operation; see epoll.h
* to confirm current limit
*/
public static final int DEFAULT_MAX_SELECTOR_SIZE = 1024;
/**
* Minimum timeout of a {@link com.barchart.udt.nio.SelectorUDT#select()}
* operations.
*/
public static final int DEFAULT_MIN_SELECTOR_TIMEOUT = 10;
/**
* infinite message time to live;
*/
public static final int INFINITE_TTL = -1;
/**
* Helper value that can be checked from CCC class and force JNI library
* load
*/
@Native
public static boolean INIT_OK = false;
protected static final Logger log = LoggerFactory.getLogger(SocketUDT.class);
/**
* 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.
*/
@Native
public static final int SIGNATURE_JNI = 20150706; // VersionUDT.BUILDTIME;
/**
* infinite timeout:
*
* blocking send/receive
*
* epoll wait
*/
public static final int TIMEOUT_INFINITE = -1;
/**
* zero timeout:
*
* epoll wait
*/
public static long TIMEOUT_NONE = 0;
/**
* UDT::select() sizeArray/sizeBuffer index offset for EXCEPTION report
*/
@Native
public static final int UDT_EXCEPT_INDEX = 2;
/**
* UDT::select() sizeArray/sizeBuffer index offset for READ interest
*/
@Native
public static final int UDT_READ_INDEX = 0;
/**
* UDT::select() sizeArray/sizeBuffer size count or number of arrays/buffers
*/
@Native
public static final int UDT_SIZE_COUNT = 3;
/**
* UDT::select() sizeArray/sizeBuffer index offset for WRITE interest
*/
@Native
public static final int UDT_WRITE_INDEX = 1;
/**
* Native library loader.
*
* @throws RuntimeException
*/
static {
// we are either from a jar or as source
ProtectionDomain pDomain = SocketUDT.class.getProtectionDomain();
CodeSource cSource = pDomain.getCodeSource();
// file:/X:/workspace/XYZ/classes/ when it's in ide/flat
// jar:/X:/workspace/XYZ/jarname.jar when it's jar
URL loc = cSource.getLocation();
final String path = loc.getPath();
final boolean isContainer = path.endsWith(".jar") || path.endsWith(".box");
final OsType os = OS.get();
String osName = os.getName();
boolean loaded = false;
if (isContainer) {
// have to extract our correct file to temp then load it, ONLY if we are not already loaded!
String sourceFileName = "udt-core-2.3.2";
if (OS.isLinux()) {
sourceFileName += ".so";
}
else if (OS.isWindows()) {
sourceFileName += ".dll";
}
else {
sourceFileName += ".dylib";
}
try {
log.info("Loading release libraries.");
final String packageName = TypeUDT.class.getPackage()
.getName()
.replaceAll("\\.", "/");
sourceFileName = packageName + "/natives/" + osName + "/" + sourceFileName;
NativeLoader.loadLibrary(sourceFileName, "libnetty-UDT", TypeUDT.class);
log.info("Release libraries loaded.");
loaded = true;
} catch (Exception e) {
log.error(e.getMessage());
}
}
else {
try {
log.info("Loading release libraries.");
final URI uri = TypeUDT.class.getResource("natives/" + osName + "/")
.toURI();
final String host = uri.getPath();
File libPath = new File(host).getAbsoluteFile();
if (libPath.canRead()) {
List libs = FileUtil.parseDir(libPath, os.getLibraryNames());
for (File lib : libs) {
// load the libs in that dir (there will be only one)
System.load(lib.getAbsolutePath());
break;
}
log.info("Release libraries loaded.");
loaded = true;
}
} catch (final Throwable e) {
log.error("Release libraries missing: {}", e.getMessage());
}
}
if (!loaded) {
log.error("Failed to load UDT native library");
throw new RuntimeException("Failed to load UDT native library");
}
try {
initClass0();
} catch (final Throwable e) {
log.error("Failed to INIT native library", e);
throw new RuntimeException("init", e);
}
if (SIGNATURE_JNI != getSignatureJNI0()) {
log.error("Java/Native SIGNATURE inconsistent");
throw new RuntimeException("signature");
}
INIT_OK = true;
log.debug("native library load & init OK");
}
public static
void init() {
}
/**
* 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
*/
public static
void cleanup() throws ExceptionUDT {
stopClass0();
}
/**
* @see UDT::epoll_add_usock()
*/
protected static native
void epollAdd0( //
final int epollID, //
final int socketID, //
final int epollOpt //
) throws ExceptionUDT;
/**
* @return epoll id
* @see UDT::epoll_create()
*/
protected static native
int epollCreate0() throws ExceptionUDT;
/**
* @see UDT::epoll_release()
*/
protected static native
void epollRelease0(final int epollID) throws ExceptionUDT;
/**
* @see UDT::epoll_remove_usock()
*/
protected static native
void epollRemove0( //
final int epollID, final int socketID) throws ExceptionUDT;
/**
* update epoll mask
*/
protected static native
void epollUpdate0(int epollID, int socketID, int epollMask) throws ExceptionUDT;
/**
* query epoll mask
*/
protected static native
int epollVerify0(int epollID, int socketID) throws ExceptionUDT;
/**
* @see UDT::epoll_wait()
*/
protected static native
int epollWait0( //
final int epollID, //
final IntBuffer readBuffer, //
final IntBuffer writeBuffer, //
final IntBuffer sizeBuffer, //
final long millisTimeout) throws ExceptionUDT;
/**
* Verify that java code and c++ code builds are consistent.
*
* @see #SIGNATURE_JNI
*/
protected static native
int getSignatureJNI0();
/**
* Call this after loading native library.
*
* @see UDT::startup()
*/
protected static native
void initClass0() throws ExceptionUDT;
/**
* receive into a complete byte array
*
* @see UDT::recv()
* @see UDT::recvmsg()
*/
protected static native
int receive0(//
final int socketID, //
final int socketType, //
final byte[] array //
) throws ExceptionUDT;
/**
* receive into a portion of a byte array
*
* @see UDT::recv()
* @see UDT::recvmsg()
*/
protected static native
int receive1( //
final int socketID, //
final int socketType, //
final byte[] array, //
final int position, //
final int limit //
) throws ExceptionUDT;
/**
* receive into a {@link java.nio.channels.DirectByteBuffer}
*
* @see UDT::recv()
* @see UDT::recvmsg()
*/
protected static native
int receive2( //
final int socketID, //
final int socketType, //
final ByteBuffer buffer, //
final int position, //
final int limit //
) throws ExceptionUDT;
/**
* Receive file.
*
* @see UDT::recvfile
*/
protected static native
long receiveFile0( //
final int socketID, //
final String path, //
long offset, //
long length, //
int block //
) throws ExceptionUDT;
/**
* Basic access to UDT socket readiness selection feature. Based on
* {@link java.nio.DirectIntBuffer} info exchange.Timeout is in
* milliseconds.
*
* @param 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".
* @return <0 : should not happen
* =0 : timeout, no ready sockets
* >0 : total number or reads, writes, exceptions
* @see #epollWait0(int, IntBuffer, IntBuffer, IntBuffer, long)
*/
public static
int selectEpoll( //
final int epollId, //
final IntBuffer readBuffer, //
final IntBuffer writeBuffer, //
final IntBuffer sizeBuffer, //
final long millisTimeout) throws ExceptionUDT {
/** asserts are contracts */
assert readBuffer != null && readBuffer.isDirect();
assert writeBuffer != null && writeBuffer.isDirect();
assert sizeBuffer != null && sizeBuffer.isDirect();
return epollWait0( //
epollId, //
readBuffer, //
writeBuffer, //
sizeBuffer, //
millisTimeout //
);
}
/**
* send from a complete byte[] array;
*
* wrapper for UDT::send(), UDT::sendmsg()
*
* @see UDT::send()
* @see UDT::sendmsg()
*/
protected static native
int send0( //
final int socketID, //
final int socketType, //
final int timeToLive, //
final boolean isOrdered, //
final byte[] array //
) throws ExceptionUDT;
/**
* send from a portion of a byte[] array;
*
* wrapper for UDT::send(), UDT::sendmsg()
*
* @see UDT::send()
* @see UDT::sendmsg()
*/
protected static native
int send1( //
final int socketID, //
final int socketType, //
final int timeToLive, //
final boolean isOrdered, //
final byte[] array, // /
final int arayPosition, //
final int arrayLimit //
) throws ExceptionUDT;
/**
* send from {@link java.nio.DirectByteBuffer};
*
* wrapper for UDT::send(), UDT::sendmsg()
*
* @see UDT::send()
* @see UDT::sendmsg()
*/
protected static native
int send2( //
final int socketID, //
final int socketType, //
final int timeToLive, //
final boolean isOrdered, //
final ByteBuffer buffer, //
final int bufferPosition, //
final int bufferLimit //
) throws ExceptionUDT;
/**
* Send file.
*
* @see UDT::sendfile
*/
protected static native
long sendFile0( //
final int socketID, //
final String path, //
long offset, //
long length, //
int block //
) throws ExceptionUDT;
/**
* Call this before unloading native library.
*
* @see UDT::cleanup()
*/
protected static native
void stopClass0() throws ExceptionUDT;
// ###########################################
// ### used for development & testing only
// ###
protected static native
void testCrashJVM0();
protected static native
void testDirectByteBufferAccess0(ByteBuffer buffer);
protected static native
void testDirectIntBufferAccess0(IntBuffer buffer);
protected static native
void testDirectIntBufferLoad0(IntBuffer buffer);
protected static native
void testEmptyCall0();
protected static native
void testFillArray0(byte[] array);
protected static native
void testFillBuffer0(ByteBuffer buffer);
protected static native
void testGetSetArray0(int[] array, boolean isReturn);
protected static native
void testInvalidClose0(int socketID) throws ExceptionUDT;
protected static native
void testIterateArray0(Object[] array);
protected static native
void testIterateSet0(Set