void Socket::perform_client_socket() { /* attempt to get socket */ if ((sockfd = socket(socket_family(), socket_type(), 0)) < 0) { perror("socket fail"); } }
void Socket::perform_socket() { int optval = 1; /* attempt to get socket */ if ((sockfd = socket(socket_family(), socket_type(), 0)) < 0) perror("socket fail"); /* set SO_REUSEADDR for quick restarts */ if (socket_family() == AF_INET || socket_family() == AF_INET6) setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)); }
void run_server::listen(uint16_t port, double time_out) { ssock=socket_type(new server_socket()); if (!ssock->create(port)){ ostringstream oss; oss<<"unable to open port "<<port<<endl; ssock.reset(); throw std::runtime_error(oss.str()); } if (time_out>0 && !ssock->set_timeout(time_out)){ ostringstream oss; oss<<"unable to set timeout"<<port<<endl; ssock.reset(); throw std::runtime_error(oss.str()); } }
jint JNI_OnLoad(JavaVM* vm, void* reserved) { JNIEnv* env; if ((*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6) != JNI_OK) { return JNI_ERR; } else { // cache classes that are used within other jni methods for performance reasons jclass localClosedChannelExceptionClass = (*env)->FindClass(env, "java/nio/channels/ClosedChannelException"); if (localClosedChannelExceptionClass == NULL) { // pending exception... return JNI_ERR; } closedChannelExceptionClass = (jclass) (*env)->NewGlobalRef(env, localClosedChannelExceptionClass); if (closedChannelExceptionClass == NULL) { // out-of-memory! throwOutOfMemoryError(env, "Error allocating memory"); return JNI_ERR; } closedChannelExceptionMethodId = (*env)->GetMethodID(env, closedChannelExceptionClass, "<init>", "()V"); if (closedChannelExceptionMethodId == NULL) { throwRuntimeException(env, "Unable to obtain constructor of ClosedChannelException"); return JNI_ERR; } jclass localRuntimeExceptionClass = (*env)->FindClass(env, "java/lang/RuntimeException"); if (localRuntimeExceptionClass == NULL) { // pending exception... return JNI_ERR; } runtimeExceptionClass = (jclass) (*env)->NewGlobalRef(env, localRuntimeExceptionClass); if (runtimeExceptionClass == NULL) { // out-of-memory! throwOutOfMemoryError(env, "Error allocating memory"); return JNI_ERR; } jclass localIoExceptionClass = (*env)->FindClass(env, "java/io/IOException"); if (localIoExceptionClass == NULL) { // pending exception... return JNI_ERR; } ioExceptionClass = (jclass) (*env)->NewGlobalRef(env, localIoExceptionClass); if (ioExceptionClass == NULL) { // out-of-memory! throwOutOfMemoryError(env, "Error allocating memory"); return JNI_ERR; } jclass localInetSocketAddressClass = (*env)->FindClass(env, "java/net/InetSocketAddress"); if (localIoExceptionClass == NULL) { // pending exception... return JNI_ERR; } inetSocketAddressClass = (jclass) (*env)->NewGlobalRef(env, localInetSocketAddressClass); if (inetSocketAddressClass == NULL) { // out-of-memory! throwOutOfMemoryError(env, "Error allocating memory"); return JNI_ERR; } jclass localDatagramSocketAddressClass = (*env)->FindClass(env, "io/netty/channel/epoll/EpollDatagramChannel$DatagramSocketAddress"); if (localDatagramSocketAddressClass == NULL) { // pending exception... return JNI_ERR; } datagramSocketAddressClass = (jclass) (*env)->NewGlobalRef(env, localDatagramSocketAddressClass); if (datagramSocketAddressClass == NULL) { // out-of-memory! throwOutOfMemoryError(env, "Error allocating memory"); return JNI_ERR; } void *mem = malloc(1); if (mem == NULL) { throwOutOfMemoryError(env, "Error allocating native buffer"); return JNI_ERR; } jobject directBuffer = (*env)->NewDirectByteBuffer(env, mem, 1); if (directBuffer == NULL) { throwOutOfMemoryError(env, "Error allocating native buffer"); return JNI_ERR; } jclass cls = (*env)->GetObjectClass(env, directBuffer); // Get the method id for Buffer.position() and Buffer.limit(). These are used as fallback if // it is not possible to obtain the position and limit using the fields directly. posId = (*env)->GetMethodID(env, cls, "position", "()I"); if (posId == NULL) { // position method was not found.. something is wrong so bail out throwRuntimeException(env, "Unable to find method ByteBuffer.position()"); return JNI_ERR; } limitId = (*env)->GetMethodID(env, cls, "limit", "()I"); if (limitId == NULL) { // limit method was not found.. something is wrong so bail out throwRuntimeException(env, "Unable to find method ByteBuffer.limit()"); return JNI_ERR; } updatePosId = (*env)->GetMethodID(env, cls, "position", "(I)Ljava/nio/Buffer;"); if (updatePosId == NULL) { // position method was not found.. something is wrong so bail out throwRuntimeException(env, "Unable to find method ByteBuffer.position(int)"); return JNI_ERR; } // Try to get the ids of the position and limit fields. We later then check if we was able // to find them and if so use them get the position and limit of the buffer. This is // much faster then call back into java via (*env)->CallIntMethod(...). posFieldId = (*env)->GetFieldID(env, cls, "position", "I"); if (posFieldId == NULL) { // this is ok as we can still use the method so just clear the exception (*env)->ExceptionClear(env); } limitFieldId = (*env)->GetFieldID(env, cls, "limit", "I"); if (limitFieldId == NULL) { // this is ok as we can still use the method so just clear the exception (*env)->ExceptionClear(env); } jclass fileRegionCls = (*env)->FindClass(env, "io/netty/channel/DefaultFileRegion"); if (fileRegionCls == NULL) { // pending exception... return JNI_ERR; } fileChannelFieldId = (*env)->GetFieldID(env, fileRegionCls, "file", "Ljava/nio/channels/FileChannel;"); if (fileChannelFieldId == NULL) { throwRuntimeException(env, "Unable to obtain FileChannel field for DefaultFileRegion"); return JNI_ERR; } transferedFieldId = (*env)->GetFieldID(env, fileRegionCls, "transfered", "J"); if (transferedFieldId == NULL) { throwRuntimeException(env, "Unable to obtain transfered field for DefaultFileRegion"); return JNI_ERR; } jclass fileChannelCls = (*env)->FindClass(env, "sun/nio/ch/FileChannelImpl"); if (fileChannelCls == NULL) { // pending exception... return JNI_ERR; } fileDescriptorFieldId = (*env)->GetFieldID(env, fileChannelCls, "fd", "Ljava/io/FileDescriptor;"); if (fileDescriptorFieldId == NULL) { throwRuntimeException(env, "Unable to obtain fd field for FileChannelImpl"); return JNI_ERR; } jclass fileDescriptorCls = (*env)->FindClass(env, "java/io/FileDescriptor"); if (fileDescriptorCls == NULL) { // pending exception... return JNI_ERR; } fdFieldId = (*env)->GetFieldID(env, fileDescriptorCls, "fd", "I"); if (fdFieldId == NULL) { throwRuntimeException(env, "Unable to obtain fd field for FileDescriptor"); return JNI_ERR; } inetSocketAddrMethodId = (*env)->GetMethodID(env, inetSocketAddressClass, "<init>", "(Ljava/lang/String;I)V"); if (inetSocketAddrMethodId == NULL) { throwRuntimeException(env, "Unable to obtain constructor of InetSocketAddress"); return JNI_ERR; } socketType = socket_type(); datagramSocketAddrMethodId = (*env)->GetMethodID(env, datagramSocketAddressClass, "<init>", "(Ljava/lang/String;II)V"); if (datagramSocketAddrMethodId == NULL) { throwRuntimeException(env, "Unable to obtain constructor of DatagramSocketAddress"); return JNI_ERR; } jclass addressEntryClass = (*env)->FindClass(env, "io/netty/channel/epoll/EpollChannelOutboundBuffer$AddressEntry"); if (addressEntryClass == NULL) { // pending exception... return JNI_ERR; } readerIndexFieldId = (*env)->GetFieldID(env, addressEntryClass, "readerIndex", "I"); if (readerIndexFieldId == NULL) { // pending exception... return JNI_ERR; } writerIndexFieldId = (*env)->GetFieldID(env, addressEntryClass, "writerIndex", "I"); if (writerIndexFieldId == NULL) { // pending exception... return JNI_ERR; } memoryAddressFieldId = (*env)->GetFieldID(env, addressEntryClass, "memoryAddress", "J"); if (memoryAddressFieldId == NULL) { // pending exception... return JNI_ERR; } return JNI_VERSION_1_6; } }