xi_dir_t *xi_dir_open(const xchar *pathname) { WIN32_FIND_DATA ddat; xi_dir_t *dfd; xchar buf[XCFG_PATHNAME_MAX]; if (pathname == NULL) { return NULL; } dfd = xi_mem_alloc(sizeof(xi_dir_t)); if (dfd == NULL) { return NULL; } xi_strcpy(buf, pathname); xi_strcat(buf, "/*"); dfd->dfd = FindFirstFile(buf, &ddat); if (dfd->dfd == NULL) { xi_mem_free(dfd); return NULL; } xi_strcpy(dfd->pathname, pathname); return dfd; }
char *copyUtf8(char *string) { char *buff = xi_strcpy(sysMalloc(xi_strlen(string) + 1), string); char *found = findHashedUtf8(buff, TRUE); if(found != buff) sysFree(buff); return found; }
xi_file_re xi_pathname_get(xchar *pathbuf, xuint32 pblen) { xchar *buf; if (pathbuf == NULL || pblen == 0) { return XI_FILE_RV_ERR_ARGS; } buf = _getcwd(NULL, 0); if (buf == NULL) { return XI_FILE_RV_ERR_ARGS; } xi_strcpy(pathbuf, buf); free(buf); return XI_FILE_RV_OK; }
xint32 tc_xi_select_echosrv() { xint32 ret; xint32 srvsock; xi_sock_addr_t lsnaddr = { XI_SOCK_FAMILY_INET, XI_SOCK_TYPE_STREAM, XI_SOCK_PROTO_TCP, { '\0' }, 56789 }; tc_info(); // setup a listen socket srvsock = xi_socket_open(lsnaddr.family, lsnaddr.type, lsnaddr.proto); if (srvsock < 0) { log_error(XDLOG, "Failed to creat a socket!\n"); return -1; } // set socket options ret = xi_socket_opt_set(srvsock, XI_SOCK_OPT_REUSEADDR, TRUE); if (ret < 0) { log_error(XDLOG, "Failed to setsocketopt: %d\n", ret); return -1; } // setup a local addr xi_strcpy(lsnaddr.host, "0.0.0.0"); // bind to the listen socket ret = xi_socket_bind(srvsock, lsnaddr); if (ret < 0) { log_error(XDLOG, "Failed to bind a socket: %d\n", ret); return -1; } // listen ret = xi_socket_listen(srvsock, 10); if (ret < 0) { log_error(XDLOG, "Failed to listen to socket: %d\n", ret); return -1; } // start a server return tc_start_server(srvsock); }
xi_file_re xi_pathname_merge(xchar *pathbuf, xuint32 pblen, xi_pathname_t pathinfo) { xchar buf[XCFG_PATHNAME_MAX]; if (pathbuf == NULL) { return XI_FILE_RV_ERR_ARGS; } if (pathinfo.drive[0] != '\0') { xi_sprintf(buf, "%s:%s/%s", pathinfo.drive, pathinfo.dirname, pathinfo.basename); } else { xi_sprintf(buf, "%s/%s", pathinfo.dirname, pathinfo.basename); } if (xi_strlen(buf) > pblen) { return XI_FILE_RV_ERR_OVER; } xi_strcpy(pathbuf, buf); 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; }
xint32 xi_dir_read(xi_dir_t *dfd, xi_file_stat_t *s) { WIN32_FIND_DATA ddat; SYSTEMTIME st; LARGE_INTEGER fsize; xchar path[XCFG_PATHNAME_MAX]; xbool ret; xi_time_t xtime; xlong utc; if (dfd == NULL || s == NULL) { return XI_FILE_RV_ERR_ARGS; } ret = FindNextFile(dfd->dfd, &ddat); if (!ret) { switch (GetLastError()) { case ERROR_NO_MORE_FILES: return XI_FILE_RV_EOF; default: return XI_FILE_RV_ERR_ARGS; } } if (ddat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { s->type = XI_FILE_TYPE_DIR; } else { s->type = XI_FILE_TYPE_REG; } if (ddat.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { s->perm = 00400; } else { s->perm = 00600; } fsize.LowPart = ddat.nFileSizeLow; fsize.HighPart = ddat.nFileSizeHigh; s->size = fsize.QuadPart; s->blocks = s->size / 4096; FileTimeToSystemTime(&ddat.ftCreationTime, &st); xtime.year = st.wYear + 1601; xtime.mon = st.wMonth; xtime.day = st.wDay; xtime.hour = st.wHour; xtime.min = st.wMinute; xtime.sec = st.wSecond; xtime.msec = st.wMilliseconds; xi_clock_time2sec(&utc, xtime); s->created = (utc * 1000) + xtime.msec; FileTimeToSystemTime(&ddat.ftLastAccessTime, &st); xtime.year = st.wYear + 1601; xtime.mon = st.wMonth; xtime.day = st.wDay; xtime.hour = st.wHour; xtime.min = st.wMinute; xtime.sec = st.wSecond; xtime.msec = st.wMilliseconds; xi_clock_time2sec(&utc, xtime); s->accessed = (utc * 1000) + xtime.msec; FileTimeToSystemTime(&ddat.ftLastWriteTime, &st); xtime.year = st.wYear + 1601; xtime.mon = st.wMonth; xtime.day = st.wDay; xtime.hour = st.wHour; xtime.min = st.wMinute; xtime.sec = st.wSecond; xtime.msec = st.wMilliseconds; xi_clock_time2sec(&utc, xtime); s->modified = (utc * 1000) + xtime.msec; xi_sprintf(path, "%s/%s", dfd->pathname, ddat.cFileName); xi_strcpy(s->pathname, path); xi_strcpy(s->filename, ddat.cFileName); return 1; }
xi_file_re xi_file_fstat(xint32 fd, xi_file_stat_t *s) { BY_HANDLE_FILE_INFORMATION se; SYSTEMTIME st; LARGE_INTEGER fsize; xbool ret; xg_fd_t *fdesc; xi_time_t xtime; xlong utc; if (fd < 0 || s == NULL) { return XI_FILE_RV_ERR_ARGS; } fdesc = xg_fd_get(fd); ret = GetFileInformationByHandle(fdesc->desc.f.fd, &se); if (!ret) { return XI_FILE_RV_ERR_ARGS; } if (se.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { s->type = XI_FILE_TYPE_DIR; } else { s->type = XI_FILE_TYPE_REG; } if (se.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { s->perm = 00400; } else { s->perm = 00600; } fsize.LowPart = se.nFileSizeLow; fsize.HighPart = se.nFileSizeHigh; s->size = fsize.QuadPart; s->blocks = (s->size / 4096L) + 1; FileTimeToSystemTime(&se.ftCreationTime, &st); xtime.year = st.wYear + 1601; xtime.mon = st.wMonth; xtime.day = st.wDay; xtime.hour = st.wHour; xtime.min = st.wMinute; xtime.sec = st.wSecond; xtime.msec = st.wMilliseconds; xi_clock_time2sec(&utc, xtime); s->created = (utc * 1000) + xtime.msec; FileTimeToSystemTime(&se.ftLastAccessTime, &st); xtime.year = st.wYear + 1601; xtime.mon = st.wMonth; xtime.day = st.wDay; xtime.hour = st.wHour; xtime.min = st.wMinute; xtime.sec = st.wSecond; xtime.msec = st.wMilliseconds; xi_clock_time2sec(&utc, xtime); s->accessed = (utc * 1000) + xtime.msec; FileTimeToSystemTime(&se.ftLastWriteTime, &st); xtime.year = st.wYear + 1601; xtime.mon = st.wMonth; xtime.day = st.wDay; xtime.hour = st.wHour; xtime.min = st.wMinute; xtime.sec = st.wSecond; xtime.msec = st.wMilliseconds; xi_clock_time2sec(&utc, xtime); s->modified = (utc * 1000) + xtime.msec; xi_strcpy(s->pathname, fdesc->desc.f.path); xi_strcpy(s->filename, xi_pathname_basename(fdesc->desc.f.path)); return XI_FILE_RV_OK; }
xi_file_re xi_file_stat(const xchar* pathname, xi_file_stat_t *s) { WIN32_FILE_ATTRIBUTE_DATA se; SYSTEMTIME st; LARGE_INTEGER fsize; xbool ret; xi_time_t xtime; xlong utc; if (pathname == NULL || s == NULL) { return XI_FILE_RV_ERR_ARGS; } ret = GetFileAttributesEx(pathname, GetFileExInfoStandard, &se); if (!ret) { return XI_FILE_RV_ERR_ARGS; } if (se.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { s->type = XI_FILE_TYPE_DIR; } else { s->type = XI_FILE_TYPE_REG; } if (se.dwFileAttributes & FILE_ATTRIBUTE_READONLY) { s->perm = 00400; } else { s->perm = 00600; } fsize.LowPart = se.nFileSizeLow; fsize.HighPart = se.nFileSizeHigh; s->size = fsize.QuadPart; s->blocks = (s->size / 4096L) + 1; FileTimeToSystemTime(&se.ftCreationTime, &st); xtime.year = st.wYear + 1601; xtime.mon = st.wMonth; xtime.day = st.wDay; xtime.hour = st.wHour; xtime.min = st.wMinute; xtime.sec = st.wSecond; xtime.msec = st.wMilliseconds; xi_clock_time2sec(&utc, xtime); s->created = (utc * 1000) + xtime.msec; FileTimeToSystemTime(&se.ftLastAccessTime, &st); xtime.year = st.wYear + 1601; xtime.mon = st.wMonth; xtime.day = st.wDay; xtime.hour = st.wHour; xtime.min = st.wMinute; xtime.sec = st.wSecond; xtime.msec = st.wMilliseconds; xi_clock_time2sec(&utc, xtime); s->accessed = (utc * 1000) + xtime.msec; FileTimeToSystemTime(&se.ftLastWriteTime, &st); xtime.year = st.wYear + 1601; xtime.mon = st.wMonth; xtime.day = st.wDay; xtime.hour = st.wHour; xtime.min = st.wMinute; xtime.sec = st.wSecond; xtime.msec = st.wMilliseconds; xi_clock_time2sec(&utc, xtime); s->modified = (utc * 1000) + xtime.msec; xi_strcpy(s->pathname, pathname); xi_strcpy(s->filename, xi_pathname_basename(pathname)); return XI_FILE_RV_OK; }