int main(int argc, char *argv[]) {
    int i;

    init();                                 /* Init some variables (like malloc timestamp string, encrypt text string, etc.) */

    check_par(argc, argv);                  /* Check command arguments number */
    open_config(argv);                      /* Open config file and check if it failed */
    open_log(argv);                         /* Open log file and check if it failed */

    get_ipaddr();                           /* Get server IP address */

    create_socket();                        /* Create a socket */
    bind_socket();                          /* Bind the socket */
    listen_socket();                        /* Listen at the socket */

    print_server_info();                    /* Print server information */

    while (TRUE) {                          /* Read until the end of file */
        if (read_flag) {
            if (fscanf(fcfg, "%s", enc_txt) == EOF) {
                finish_flag = 1;
                break;
            } else {
                fscanf(fcfg, "%s", dec_txt);
            }
        }
        read_flag = 0;

        init_select();                      /* Select function */
        if (select_func() == -1) break;

        for (i = 0; i < max_fds + 1; i++) {
            if (FD_ISSET(i, &rfds)) {
                if (i == sockfd) {                              /* If have a new client connect */
                    if (accept_new_cli() == -1) break;          /* Try to accept new client */
                    if (check_connect() == -1) break;           /* Check connect message from client */
                    if (print_client_info() == -1) break;       /* Print the information of client side */
                    store_client_ip();                          /* Store the client ip address */
                    break;
                } else {                                        /* If have new message from client side */
                    client_ip = get_host_by_sockfd(i);          /* Get the client ip address by socket */
                    recv_socket_msg(i, recv_mark);              /* Get the message from socket */
                    handle_client_msg(i);                       /* Handle client message (SUCCESS_MSG, FAILURE_MSG, DISPATCH_MSG, etc.) */
                    break;
                }
            }
            if (main_flag == EXIT_FAILURE) break;
        }
        if (main_flag == EXIT_FAILURE) break;
    }

    remained_cli = ask_clients_quit();                          /* Ask clients quit and count the remain clients number */
    wait_clients_quit();                                        /* Wait for all clients quit */
    quit_server();                                              /* Clean up and quit server, also print the message to log */

    return main_flag;
}
Esempio n. 2
0
int handle_msg(half_stream* stream, int msg_type, mysql_session* sess){
    int ret = 0;
    switch(msg_type){
        case MYSQL_CLIENT_MSG:
            ret = handle_client_msg(stream, sess);
            break;
        case MYSQL_SERVER_MSG:
            ret = handle_server_msg(stream, sess);
            break;
        default:
            DEBUG_ASSERT(0);
            break;
    }

    sess->ignore_flag = (ret == SESSION_IGNORE_NEXT);
    return ret;
}
static void recv_client_msg(fd_set *readfds,fd_set *writefds)
{
    //printf("==============new message coming:====================\n");
    int i = 0, n = 0,j=0;
    int clifd,clifdw,rc;
    char buf[MAXLINE] = {0};
    int msgid;
    char tempwrite[MAXLINE]={0};
    for (i = 0;i <= svrCliMng->cli_cnt;i++) 
    {
        clifd = svrCliMng->clifds[i];
        if (clifd < 0) 
        {
            continue;
        }

        if (FD_ISSET(clifd, readfds)) 
        {
            //receive the msg from clients
            n = read(clifd, buf, MAXLINE);
            printf("message is:%s\n", buf);
            printf("length of message is:%d\n",n );
            if (n <= 0) 
            {
                FD_CLR(clifd, &svrCliMng->allfds);
                close(clifd);
                svrCliMng->clifds[i] = -1;
                continue;
            }
            msgid=handle_client_msg(clifd, buf);
            //give it a try

            for(j=0;j<= svrCliMng->cli_cnt;j++)//this part is originally creative
            {
                if(j==i)continue;
                clifdw=svrCliMng->clifds[j];
                if(clifdw<0)continue;

                if(FD_ISSET(clifdw,writefds))
                {
                printf("now sycronizing\n");
                //syncronize the msg to all the other clients online
                sprintf(tempwrite,"%d\t%s\n",msgid,buf);
                rc=write(clifdw,tempwrite,strlen(tempwrite));
                printf("tempwrite is:%s\n",tempwrite);
                if(rc>0)
                {
                    tempwrite[rc]='\0';
                    printf("sycronized!\n");
                }
                else
                {
                    perror("sycronize error!\n");
                }
                // sleep(5);
                }
             
            }
            
        }
                    

    }
}