Пример #1
0
void request_answer(int sock) {
 int start, end;
 struct timeval t1, t2;
 query q;

 gettimeofday(&t1, 0);

 query_init(&q);

 socket_set_timeout(sock, 5, 5);

 /* read query, read interval start, read interval end */

 if(!request_read_query(sock, &q) || !request_read_integer(sock, &start) || !request_read_integer(sock, &end)) {
  query_delete(&q);

  close(sock);

  return;
 }

 query_prepare(&q);

 request_send_result(sock, &q, start, end);

 query_delete(&q);

 close(sock);

 gettimeofday(&t2, 0);

 /* printf("%i milliseconds\n", ((t2.tv_sec - t1.tv_sec) * 1000000 + t2.tv_usec - t1.tv_usec) / 1000); */

 return;
}
Пример #2
0
int
hstcpcli::set_timeout(int send_timeout, int recv_timeout)
{
  String err;
  sargs.send_timeout = send_timeout;
  sargs.recv_timeout = recv_timeout;
  if (socket_set_timeout(fd, sargs, err) != 0) {
    set_error(-1, err);
  }
  return error_code;
}
Пример #3
0
/*
---------------------------------------
    打开 UDPv4 通讯接口
---------------------------------------
*/
static bool_t
qst_com_udpv4 (
  __CR_IN__ void_t*     parm,
  __CR_IN__ uint_t      argc,
  __CR_IN__ ansi_t**    argv
    )
{
    uint_t      port;
    socket_t    netw;

    /* 参数解析 <目标地址> <端口号> */
    if (argc < 3)
        return (FALSE);
    port = str2intxA(argv[2]);
    if (port > 65535)
        return (FALSE);

    sQstComm*   ctx = (sQstComm*)parm;

    /* 关闭当前接口并打开 UDPv4 连接 */
    netw = client_udp_open(argv[1], (int16u)port);
    if (netw == NULL)
        return (FALSE);
    socket_set_timeout(netw, QCOM_SNDTOUT, QCOM_CUTDOWN);

    /* 设置工作参数 */
    qst_com_close(parm, argc, argv);
    ctx->comm.obj.netw = netw;
    ctx->comm.send = qst_udpv4_send;

    /* 启动接收线程 */
    ctx->comm.thrd = thread_new(0, qst_udpv4_main, parm, FALSE);
    if (ctx->comm.thrd == NULL) {
        socket_close(netw);
        return (FALSE);
    }
    TRY_FREE(ctx->comm.title);
    ctx->comm.title = str_fmtA(" - UDPv4 \"%s\", %u", argv[1], port);
    qst_update_title(ctx);
    return (TRUE);
}
Пример #4
0
/*
=======================================
    主程序
=======================================
*/
int main (int argc, char *argv[])
{
    CR_NOUSE(argc);
    CR_NOUSE(argv);

    /* 建立 CrHack 系统 */
    if (!set_app_type(CR_APP_CUI))
        return (QST_ERROR);
    SetConsoleTitleA(WIN_TITLE);

    sint_t  x1, y1;
    uint_t  ww, hh;

    /* 初始化窗口 */
    s_hcui = GetStdHandle(STD_OUTPUT_HANDLE);
    if (s_hcui == INVALID_HANDLE_VALUE)
        return (QST_ERROR);
    s_hwnd = GetConsoleWindow();
    if (s_hwnd == NULL)
        return (QST_ERROR);
    misc_desk_init(WIN_ICONF, &x1, &y1, &ww, &hh,
                   QSRV_DEF_WIDTH, QSRV_DEF_HEIGHT);
    misc_cui_setwin(s_hwnd, s_hcui, x1, y1, ww, hh);

    sINIu*  ini;
    uint_t  temp;
    ansi_t* text;
    int16u  port = QST_DEF_PORT;
    ansi_t* addr = QST_DEF_ADDR;
    uint_t  count = QST_DEF_CLIENT;

    /* 读取参数 */
    text = file_load_as_strA(QST_CFG_STARTUP);
    if (text != NULL) {
        ini = ini_parseU(text);
        mem_free(text);
        text = NULL;
        if (ini != NULL) {
            text = ini_key_stringU("serv::bind", ini);
            if (text != NULL)
                addr = text;
            temp = ini_key_intxU("serv::port", QST_DEF_PORT, ini);
            if (temp > 1024 && temp < 32768)
                port = (int16u)temp;
            temp = ini_key_intxU("serv::count", QST_DEF_CLIENT, ini);
            if (temp > 0 && temp <= QST_MAX_CLIENT)
                count = temp;
            ini_closeU(ini);
        }
    }

    socket_t    serv;
    sQstWork*   work;

    /* 建立工作单元组 */
    work = qst_wrk_create(count);
    if (work == NULL)
        return (QST_ERROR);

    /* 初始化网络 */
    if (!socket_init())
        return (QST_ERROR);
    serv = server_tcp_open(addr, port);
    TRY_FREE(text);
    if (serv == NULL)
        return (QST_ERROR);

    /* 初始化锁 */
    mtlock_init(&s_qst_lck_shw);
    mtlock_init(&s_qst_lck_wrk);

    int16u  logo_color;

    /* 建立颜色值 */
    logo_color = cui_make_attr(0, CR_CUI_TEXT_LIGHT |
                        CR_CUI_TEXT_GREEN | CR_CUI_TEXT_RED |
                        CR_CUI_TEXT_BLUE);
    s_qst_clr_cin = cui_make_attr(0, CR_CUI_TEXT_LIGHT |
                        CR_CUI_TEXT_GREEN | CR_CUI_TEXT_RED);
    s_qst_clr_say = cui_make_attr(0, CR_CUI_TEXT_LIGHT |
                        CR_CUI_TEXT_GREEN);
    s_qst_clr_out = cui_make_attr(0, CR_CUI_TEXT_LIGHT |
                        CR_CUI_TEXT_RED);
    /* 工作循环 */
    cui_set_color(logo_color);
    printf("######################################\n");
    printf("##    QUESTLAB BROADCAST SERVER     ##\n");
    printf("######################################\n");
    while (!s_quit)
    {
        ansi_t*     ptr;
        bool_t      okay;
        ansi_t*     name;
        socket_t    netw;

        /* 接受一个客户端 */
        netw = server_tcp_accept(serv);
        if (netw == NULL)
            continue;

        /* 接收客户端名称 */
        socket_set_timeout(netw, -1, QST_TCP_TOUT);
        name = netw_cmd_recv(netw);
        if (name == NULL) {
            socket_close(netw);
            continue;
        }

        /* 客户端名称过滤 */
        if (str_lenA(name) > QST_MAX_NAME) {
            netw_ack_send(netw, FALSE);
            netw_cli_close(netw);
            mem_free(name);
            continue;
        }
        ptr = name;
        okay = TRUE;
        while (*ptr != NIL) {
            if (is_cntrlA(*ptr++)) {
                okay = FALSE;
                break;
            }
        }
        if (!okay) {
            netw_ack_send(netw, FALSE);
            netw_cli_close(netw);
            mem_free(name);
            continue;
        }

        /* 启动一个工作单元 */
        if (!qst_wrk_wakeup(work, name, netw, qst_srv_main)) {
            netw_cli_close(netw);
            mem_free(name);
            continue;
        }
    }
    return (QST_OKAY);
}
Пример #5
0
/*
=======================================
    WinMain 程序入口
=======================================
*/
int WINAPI
WinMain (
  __CR_IN__ HINSTANCE   curt_app,
  __CR_IN__ HINSTANCE   prev_app,
  __CR_IN__ LPSTR       cmd_line,
  __CR_IN__ int         cmd_show
    )
{
    CR_NOUSE(prev_app);
    CR_NOUSE(cmd_line);
    CR_NOUSE(cmd_show);

    /* 只允许一个例程 */
    if (misc_is_running(EXE_XNAME))
        return (QST_ERROR);

    /* 建立 CrHack 系统 */
    if (!set_app_type(CR_APP_GUI))
        return (QST_ERROR);
    mem_zero(&s_wrk_ctx, sizeof(s_wrk_ctx));

    sint_t  x1, y1;
    uint_t  ww, hh;

    /* 生成一个可变大小的窗口 */
    mtlock_init(&s_wrk_ctx.lock);
    qst_load_cfg(&s_wrk_ctx.cfgs);
    misc_desk_init(WIN_ICONF, &x1, &y1, &ww, &hh,
                   QV2D_DEF_WIDTH, QV2D_DEF_HEIGHT);
    if (ww < QV2D_DEF_WIDTH)  ww = QV2D_DEF_WIDTH;
    if (hh < QV2D_DEF_HEIGHT) hh = QV2D_DEF_HEIGHT;
    s_wrk_ctx.hwnd = (HWND)window_open(curt_app, (void_t*)WindowProc,
                        x1, y1, ww, hh, WIN_TITLE, WIN_CLASS, (ansi_t*)101,
                                    CR_WSTYLE_NORMAL);
    if (s_wrk_ctx.hwnd == NULL)
        return (QST_ERROR);
    SetWindowLongPtr(s_wrk_ctx.hwnd, GWL_STYLE,
        GetWindowLongPtr(s_wrk_ctx.hwnd, GWL_STYLE) & (~WS_MAXIMIZEBOX));

    iGFX2*  draw;
    sIMAGE* imgs;

    /* 创建 GDI 绘制对象 (只支持32位色屏幕) */
    draw = (iGFX2*)create_gdi_canvas(s_wrk_ctx.hwnd, 0, 0, FALSE);
    if (draw == NULL) {
        window_kill(s_wrk_ctx.hwnd, curt_app, WIN_CLASS);
        return (QST_ERROR);
    }
    imgs = CR_VCALL(draw)->lock(draw);
    if (imgs == NULL || imgs->fmt != CR_ARGB8888) {
        window_kill(s_wrk_ctx.hwnd, curt_app, WIN_CLASS);
        return (QST_ERROR);
    }
    CR_VCALL(draw)->unlock(draw);
    CR_VCALL(draw)->clear(draw, s_wrk_ctx.cfgs.bkcolor, 0);
    s_wrk_ctx.draw = draw;

    /* 初始化网络 */
    if (!socket_init()) {
        window_kill(s_wrk_ctx.hwnd, curt_app, WIN_CLASS);
        return (QST_ERROR);
    }
    s_wrk_ctx.netw = netw_cli_open(EXE_XNAME);
    if (s_wrk_ctx.netw == NULL) {
        window_kill(s_wrk_ctx.hwnd, curt_app, WIN_CLASS);
        return (QST_ERROR);
    }
    /* 读取需要超时, 不然线程无法退出 */
    socket_set_timeout(s_wrk_ctx.netw, -1, QST_TCP_TOUT);

    thrd_t  thrd;

    /* 生成工作线程 */
    s_wrk_ctx.send = TRUE;
    s_wrk_ctx.quit = FALSE;
    s_wrk_ctx.cur_busy = LoadCursor(NULL, IDC_WAIT);
    s_wrk_ctx.cur_free = LoadCursor(NULL, IDC_ARROW);
    s_wrk_ctx.res_loader = res_loader_get();
    if (s_wrk_ctx.res_loader->init != NULL)
        s_wrk_ctx.res_loader->init(s_wrk_ctx.netw, NULL);
    qst_load_filter(&s_wrk_ctx);
    thrd = thread_new(0, qst_v2d_main, &s_wrk_ctx, FALSE);
    if (thrd == NULL) {
        window_kill(s_wrk_ctx.hwnd, curt_app, WIN_CLASS);
        return (QST_ERROR);
    }

    /* 消息循环 */
    while (!s_wrk_ctx.quit)
    {
        MSG     msg;

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
            if (msg.message == WM_QUIT) {
                s_wrk_ctx.quit = TRUE;
                break;
            }
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else {
            qst_do_keyboard(&s_wrk_ctx);
        }
    }
    thread_wait(thrd);
    thread_del(thrd);
    window_kill(s_wrk_ctx.hwnd, curt_app, WIN_CLASS);
    netw_cli_close(s_wrk_ctx.netw);
    return (QST_OKAY);
}
Пример #6
0
/*
=======================================
    WinMain 程序入口
=======================================
*/
int WINAPI
WinMain (
  __CR_IN__ HINSTANCE   curt_app,
  __CR_IN__ HINSTANCE   prev_app,
  __CR_IN__ LPSTR       cmd_line,
  __CR_IN__ int         cmd_show
    )
{
    uint_t      argc;
    ansi_t**    argv;

    CR_NOUSE(prev_app);
    CR_NOUSE(cmd_show);

    /* 只允许一个例程 */
    if (misc_is_running(EXE_XNAME))
        return (QST_ERROR);

    /* 建立 CrHack 系统 */
    if (!set_app_type(CR_APP_GUI))
        return (QST_ERROR);
    mem_zero(&s_wrk_ctx, sizeof(s_wrk_ctx));

    int qt_argc;

    /* 获取命令行参数, 不包括进程文件名 */
    argv = misc_get_param(cmd_line, &argc);
    qt_argc = (int)argc;

    QApplication    qt_app(qt_argc, argv);

    qt_app.setApplicationName("QstComm");
    qt_app.setOrganizationName("QuestLAB");

    sint_t  x1, y1;
    uint_t  ww, hh;

    /* 生成一个可变大小的窗口 */
    mtlock_init(&s_wrk_ctx.lock);
    qst_load_cfg(&s_wrk_ctx.cfgs);
    misc_desk_init(WIN_ICONF, &x1, &y1, &ww, &hh,
                   QCOM_DEF_WIDTH, QCOM_DEF_HEIGHT);
    if (ww < QCOM_DEF_WIDTH)  ww = QCOM_DEF_WIDTH;
    if (hh < QCOM_DEF_HEIGHT) hh = QCOM_DEF_HEIGHT;

    RECT            w_rect;
    sint_t          fw, fh;
    QMainWindow     qt_win;

    /* Qt 里的宽高都不包括边框
       需要自己用 Win32 API 获取 */
    qt_win.setWindowFlags(qt_win.windowFlags()
        & (~Qt::WindowMaximizeButtonHint));
    qt_win.move(x1, y1);
    qt_win.resize(ww, hh);
    s_wrk_ctx.hwnd = (HWND)qt_win.winId();
    if (!GetWindowRect(s_wrk_ctx.hwnd, &w_rect))
        return (QST_ERROR);
    fw = w_rect.right - w_rect.left - ww;
    fh = w_rect.bottom - w_rect.top - hh;
    qt_win.setMinimumSize(QCOM_DEF_WIDTH  - fw,
                          QCOM_DEF_HEIGHT - fh);
    qt_win.resize(ww - fw, hh - fh);

    QWidget*        cent = new QWidget (&qt_win);
    CTextEdit*      edit = new CTextEdit (cent);
    QHBoxLayout*    hori = new QHBoxLayout (cent);

    /* 创建窗体里的控件 */
    hori->setSpacing(6);
    hori->setContentsMargins(8, 8, 8, 8);
    hori->addWidget(edit);
    qt_win.setCentralWidget(cent);

    CTextOper   oper(&qt_win, edit);

    s_wrk_ctx.oper = (void_t*)(&oper);
    SetClassLongPtr(s_wrk_ctx.hwnd, GCLP_HICON, (LONG_PTR)
                    LoadIconA(curt_app, (ansi_t*)101));

    /* 初始化 ANSI 上下文 */
    if (!qst_csi_init())
        return (QST_ERROR);
    qst_set_viewer(&s_wrk_ctx);

    /* 初始化网络 */
    if (!socket_init())
        return (QST_ERROR);
    s_wrk_ctx.netw = netw_cli_open(EXE_XNAME);
    if (s_wrk_ctx.netw == NULL)
        return (QST_ERROR);

    /* 读取需要超时, 不然线程无法退出 */
    socket_set_timeout(s_wrk_ctx.netw, -1, QST_TCP_TOUT);

    thrd_t  thrd;

    /* 生成工作线程 */
    s_wrk_ctx.quit = FALSE;
    s_wrk_ctx.comm.quit = FALSE;
    s_wrk_ctx.comm.text = TRUE;
    s_wrk_ctx.comm.rtype = "text";
    s_wrk_ctx.comm.stype = "text";
    s_wrk_ctx.comm.title = NULL;
    s_wrk_ctx.comm.render = qst_txt_show;
    s_wrk_ctx.page = get_sys_codepage();
    qst_update_title(&s_wrk_ctx);
    thrd = thread_new(0, qst_com_main, &s_wrk_ctx, FALSE);
    if (thrd == NULL)
        return (QST_ERROR);
    sio_init();

    /* 开始 Qt 流程 */
    qt_win.show();
    qt_app.exec();

    /* 关闭线程直接退出 */
    if (!s_wrk_ctx.quit)
        s_wrk_ctx.quit = TRUE;
    thread_wait(thrd);
    thread_del(thrd);
    netw_cli_close(s_wrk_ctx.netw);
    sio_free();
    qst_csi_free();
    return (QST_OKAY);
}
int handle_input_line(int socket_fd, const char *input_line, int had_cr, int had_lf)
{
  static const char *seperator_seperator = ";";
  static const char *terminator_terminator = "\n";
  const char *this_stSettings_iTimeOut_str_s = ".THIS.stSettings.iTimeOut=";

  static unsigned int counter;

  const char **my_argv = NULL;
  int argc = create_argv(input_line, had_cr, had_lf, (const char*** )&my_argv);
  const char *argv1 = (argc > 1) ? my_argv[1] : "";
  int is_EAT_cmd = strchr(input_line, ';') != NULL;

  if (!strncmp(argv1, this_stSettings_iTimeOut_str_s, strlen(this_stSettings_iTimeOut_str_s))) {
    const char *myarg_1 = &argv1[strlen(this_stSettings_iTimeOut_str_s)];
    int timeout;
    int nvals;
    nvals = sscanf(myarg_1, "%d", &timeout);
    if (nvals == 1) {
      int res = socket_set_timeout(socket_fd, timeout);
      cmd_buf_printf("%s%s%s",
                     res ? "Error" : "OK",
                     seperator_seperator,
                     terminator_terminator);
    } else {
      cmd_buf_printf("Error nvals=%d %s%s",
                     nvals,
                     seperator_seperator,
                     terminator_terminator);
    }
  }
  else if (is_EAT_cmd) {
    cmd_EAT(argc, my_argv);
  }
  else if ((argc > 1) && (0 == strcmp(argv1, "bye"))) {
    fprintf(stdlog, "%s/%s:%d bye\n", __FILE__, __FUNCTION__, __LINE__);
    return 1;
  }
  else if ((argc > 1) && (0 == strcmp(argv1, "kill"))) {
    exit(0);
  }
  else if (cmd_TCPsim(argc, my_argv)) {
    ; /* TCPsim command */
  }
  else if (cmd_IcePAP(argc, my_argv)) {
    ; /* IcePAP command */
  }
  else if (argv1[0] == 'h' ||
           argv1[0] == '?') {
    fd_printf_crlf(socket_fd, had_cr,
                   "Valid commands :\n");
    fd_printf_crlf(socket_fd, had_cr,
                   "bye            : Bye\n"
                   "kill           : exit(0)\n");
  }
  else if (argc > 2){
    fd_printf_crlf(socket_fd, had_cr,"error(%s:%d): invalid command (%s)\n",
                   __FILE__, __LINE__,  argv1);
  }
  else if (argc == 1) {
    /* Just a return, print a prompt */
  }
  {
    int i;
    for (i=0; i < argc; i++)
    {
      free((void *)my_argv[i]);
    }
    free(my_argv);
  }
  if (PRINT_STDOUT_BIT2()) {
    fprintf(stdlog, "%s/%s:%d (%u)\n",
            __FILE__, __FUNCTION__, __LINE__,
            counter++);
  }
  {
    int flags = had_cr ? PRINT_ADD_CR : 0;
    const char *buf = get_buf();

    fd_printf_crlf(socket_fd, flags, "%s", buf);
    clear_buf();
  }

  return 0;
}