xi_file_re xi_file_unlock(xint32 fd, xint64 spos, xint64 len) { OVERLAPPED overlapped; const DWORD offsetLow = (DWORD)(spos & 0xFFFFFFFF); const DWORD offsetHigh = (DWORD)((spos >> 0x20) & 0x7FFFFFFF); const DWORD nNumberOfBytesToUnlockLow = (DWORD)(len & 0xFFFFFFFF); const DWORD nNumberOfBytesToUnlockHigh = (DWORD)((len >> 0x20) & 0x7FFFFFFF); xg_fd_t *fdesc; if (fd < 0) { return XI_FILE_RV_ERR_ARGS; } fdesc = xg_fd_get(fd); xi_mem_set(&overlapped, 0, sizeof(overlapped)); overlapped.Offset = offsetLow; overlapped.OffsetHigh = offsetHigh; if (!UnlockFileEx(fdesc->desc.f.fd, /* [in] file handle to lock */ (DWORD) 0, /* [in] reserved */ nNumberOfBytesToUnlockLow, /* [in] number of bytes to lock (low) */ nNumberOfBytesToUnlockHigh, /* [in] number of bytes to lock (high) */ (LPOVERLAPPED) & overlapped)) { /* [in] contains file offset to lock start */ return XI_FILE_RV_ERR_ARGS; } return XI_FILE_RV_OK; }
xi_file_re xi_pathname_absolute(xchar *pathbuf, xuint32 pblen, const xchar *pathname) { xchar ch; xchar *path_name = (char*) pathname; xchar *cp = pathbuf; xi_mem_set(pathbuf, 0, pblen); if (*path_name != '/' && *path_name != '\\' && *(path_name + 1) != ':') { xi_pathname_get(pathbuf, pblen); while (*cp) { cp++; } *(cp++) = XI_SEP_FILE_C; } while ((ch = *(path_name++))) { switch (ch) { /*case '~':{ char *p = getenv("HOME");; while (*p)*(cp++) = *(p++); break; }*/ case '\\': case '/': *(cp++) = XI_SEP_FILE_C; break; case '.': { switch (*path_name) { case '.': // we encounter '..' path_name += 2; //skip the following "./" if (*(cp - 1) == XI_SEP_FILE_C) //if cano_path now is "/xx/yy/" --cp; //skip backward the tail '/' *cp = '\0'; cp = xi_strrchr(pathbuf, XI_SEP_FILE_C); //up a level ++cp; break; case '\\': case '/': ++path_name; break; default: *(cp++) = ch; } break; } default: *(cp++) = ch; break; } } *cp = '\0'; return XI_FILE_RV_OK; }
//static jboolean NetworkInterface_isLoopbackImpl(JNIEnv* env, jclass, jstring name) { JNIEXPORT jboolean JNICALL Java_java_net_NetworkInterface_isLoopbackImpl(JNIEnv* env, jclass, jstring name) { //return hasFlag(env, name, IFF_LOOPBACK); xchar buf[64]; jsize nameLength = env->GetStringLength(name); if (nameLength >= 64) { jniThrowSocketExceptionMsg(env, "java/net/SocketException", "ifname is too long!!", 0); return false; } xi_mem_set(buf, 0, sizeof(buf)); env->GetStringUTFRegion(name, 0, nameLength, buf); if (xi_strcmp(buf, "lo") == 0) { return JNI_TRUE; } else { return JNI_FALSE; } }
xi_file_re xi_file_lock(xint32 fd, xint64 spos, xint64 len, xint32 bexcl, xint32 bwait) { DWORD dwFlags = 0; OVERLAPPED overlapped; const DWORD offsetLow = (DWORD)(spos & 0xFFFFFFFF); const DWORD offsetHigh = (DWORD)((spos >> 0x20) & 0x7FFFFFFF); const DWORD nNumberOfBytesToLockLow = (DWORD)(len & 0xFFFFFFFF); const DWORD nNumberOfBytesToLockHigh = (DWORD)((len >> 0x20) & 0x7FFFFFFF); xg_fd_t *fdesc; if (fd < 0) { return XI_FILE_RV_ERR_ARGS; } fdesc = xg_fd_get(fd); if (!bwait) { dwFlags |= LOCKFILE_FAIL_IMMEDIATELY; } if (bexcl) { dwFlags |= LOCKFILE_EXCLUSIVE_LOCK; } xi_mem_set(&overlapped, 0, sizeof(overlapped)); overlapped.Offset = offsetLow; overlapped.OffsetHigh = offsetHigh; if (!LockFileEx(fdesc->desc.f.fd, /* [in] file handle to lock */ dwFlags, /* [in] flags describing lock type */ (DWORD) 0, /* [in] reserved */ nNumberOfBytesToLockLow, /* [in] number of bytes to lock (low) */ nNumberOfBytesToLockHigh, /* [in] number of bytes to lock (high) */ (LPOVERLAPPED) & overlapped)) { /* [in] contains file offset to lock start */ return XI_FILE_RV_ERR_ARGS; } return XI_FILE_RV_OK; }
static xvoid * tc_client_thread(xvoid *args) { xint32 t = 1; xchar *tcname = "xi_socket.h-clt"; xssize ret; xchar buf[1024]; xint32 clt_sock = -1; xi_sock_addr_t toaddr = { XI_SOCK_FAMILY_INET, XI_SOCK_TYPE_STREAM, XI_SOCK_PROTO_IP, { '\0' }, 9876 }; UNUSED(args); log_print(XDLOG, "Client Thread - started >>>>>>>>>>>>>>>\n\n"); log_print(XDLOG, " (%s:%02d) xi_socket_open ############\n", tcname, t++); clt_sock = xi_socket_open(toaddr.family, toaddr.type, toaddr.proto); if (clt_sock < 0) { log_print(XDLOG, " - result : failed!!!\n\n"); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (socket=%d)\n\n", clt_sock); log_print(XDLOG, " (%s:%02d) xi_socket_connect #########\n", tcname, t++); xi_strcpy(toaddr.host, "127.0.0.1"); ret = xi_socket_connect(clt_sock, toaddr); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (svr=%s)\n\n", toaddr.host); log_print(XDLOG, " (%s:%02d) xi_socket_send #############\n", tcname, t++); xi_strcpy(buf, "Hello, SVR!!"); ret = xi_socket_send(clt_sock, buf, xi_strlen(buf)); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (wbytes=%d/msg=%s)\n\n", ret, buf); log_print(XDLOG, " (%s:%02d) xi_socket_recv #############\n", tcname, t++); xi_mem_set(buf, 0, sizeof(buf)); ret = xi_socket_recv(clt_sock, buf, sizeof(buf)); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (rbytes=%d/msg=%s)\n\n", ret, buf); log_print(XDLOG, " (%s:%02d) xi_file_write #############\n", tcname, t++); xi_strcpy(buf, "client file write!"); ret = xi_file_write(clt_sock, buf, xi_strlen(buf)); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (wbytes=%d/msg=%s)\n\n", ret, buf); log_print(XDLOG, " (%s:%02d) xi_file_read #############\n", tcname, t++); xi_mem_set(buf, 0, sizeof(buf)); ret = xi_file_read(clt_sock, buf, sizeof(buf)); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (rbytes=%d/msg=%s)\n\n", ret, buf); log_print(XDLOG, " (%s:%02d) xi_socket_shutdown ########\n", tcname, t++); ret = xi_socket_shutdown(clt_sock, XI_SOCK_SHUTDOWN_RDWR); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass.\n\n"); log_print(XDLOG, " (%s:%02d) xi_socket_close ###########\n", tcname, t++); ret = xi_socket_close(clt_sock); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass.\n\n"); log_print(XDLOG, "Client Thread - ended <<<<<<<<<<<<<<<<<\n\n"); return NULL; }
static xvoid * tc_server_thread(xvoid *args) { xint32 t = 1; xchar *tcname = "xi_socket.h-svr"; xssize ret; xchar buf[1024]; xint32 svr_sock = -1; xint32 client = -1; xi_sock_addr_t lsnaddr = { XI_SOCK_FAMILY_INET, XI_SOCK_TYPE_STREAM, XI_SOCK_PROTO_IP, { '\0' }, 9876 }; xi_sock_addr_t laddr; xi_sock_addr_t fromaddr; UNUSED(args); log_print(XDLOG, "Server Thread - started >>>>>>>>>>>>>>>\n\n"); log_print(XDLOG, " (%s:%02d) xi_socket_open ############\n", tcname, t++); svr_sock = xi_socket_open(lsnaddr.family, lsnaddr.type, lsnaddr.proto); if (svr_sock < 0) { log_print(XDLOG, " - result : failed!!!\n\n"); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (socket=%d)\n\n", svr_sock); log_print(XDLOG, " (%s:%02d) xi_socket_opt_set #########\n", tcname, t++); ret = xi_socket_opt_set(svr_sock, XI_SOCK_OPT_REUSEADDR, TRUE); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass.\n\n"); log_print(XDLOG, " (%s:%02d) xi_socket_bind ############\n", tcname, t++); xi_strcpy(lsnaddr.host, "0.0.0.0"); ret = xi_socket_bind(svr_sock, lsnaddr); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass.\n\n"); log_print(XDLOG, " (%s:%02d) xi_socket_listen ##########\n", tcname, t++); ret = xi_socket_listen(svr_sock, 10); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass.\n\n"); log_print(XDLOG, " (%s:%02d) xi_socket_get_local #######\n", tcname, t++); ret = xi_socket_get_local(svr_sock, &laddr); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (addr=%s,port=%d)\n\n", laddr.host, laddr.port); log_print(XDLOG, " (%s:%02d) xi_socket_accept ##########\n", tcname, t++); client = xi_socket_accept(svr_sock, &fromaddr); if (client < 0) { log_print(XDLOG, " - result : accept failed!!!\n\n"); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (client=%d, from=%s)\n\n", client, fromaddr.host); log_print(XDLOG, " (%s:%02d) xi_socket_recv ############\n", tcname, t++); xi_mem_set(buf, 0, sizeof(buf)); ret = xi_socket_recv(client, buf, sizeof(buf)); if (ret < 0) { log_print(XDLOG, " - result : recv failed!!!\n\n"); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (rbytes=%d/msg=%s)\n\n", ret, buf); log_print(XDLOG, " (%s:%02d) xi_socket_send ############\n", tcname, t++); xi_strcpy(buf, "connected!"); ret = xi_socket_send(client, buf, xi_strlen(buf)); if (ret < 0) { log_print(XDLOG, " - result : send failed!!!\n\n"); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (wbytes=%d/msg=%s)\n\n", ret, buf); log_print(XDLOG, " (%s:%02d) xi_file_read #############\n", tcname, t++); xi_mem_set(buf, 0, sizeof(buf)); ret = xi_file_read(client, buf, sizeof(buf)); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (rbytes=%d/msg=%s)\n\n", ret, buf); log_print(XDLOG, " (%s:%02d) xi_file_write #############\n", tcname, t++); xi_strcpy(buf, "server file write!"); ret = xi_file_write(client, buf, xi_strlen(buf)); if (ret < 0) { log_print(XDLOG, " - result : failed!!! (ret=%d)\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass. (wbytes=%d/msg=%s)\n\n", ret, buf); log_print(XDLOG, " (%s:%02d) wait for client-end #######\n", tcname, t++); xi_thread_sleep(2000); log_print(XDLOG, " (%s:%02d) xi_socket_close ###########\n", tcname, t++); ret = xi_socket_close(client); if (ret != XI_SOCK_RV_OK) { log_print(XDLOG, " - result : client close err=%d!!!\n\n", ret); TC_EXIT_ERROR; } ret = xi_socket_close(svr_sock); if (ret != XI_SOCK_RV_OK) { log_print(XDLOG, " - result : svr_sock close err=%d!!!\n\n", ret); TC_EXIT_ERROR; } log_print(XDLOG, " - result : pass.\n\n", ret); log_print(XDLOG, " (%s:%02d) signal to main thread #####\n", tcname, t++); _g_trun = FALSE; log_print(XDLOG, "Server Thread - ended <<<<<<<<<<<<<<<<<\n\n"); return NULL; }
void monitorInit(Monitor *mon) { xi_mem_set(mon, 0, sizeof(Monitor)); xi_thread_mutex_create(&mon->lock, "Mon"); }