Esempio n. 1
0
static void	create_client(t_server *server)
{
  t_client	*cli;

  cli = xmalloc(sizeof(t_client));
  cli->fd = server->cfd;
  cli->is_graphic = 0;
  cli->team = NULL;
  add_client_list(cli, &(server->awaiting_clients));
  add_client_response(&cli, "BIENVENUE\n");
}
Esempio n. 2
0
/**
 * @brief Handle an incoming RTSP connection
 *
 * @param loop The event loop where the incoming connection was triggered
 * @param w The watcher that triggered the incoming connection
 * @param revents Unused
 *
 * This function takes care of all the handling of an incoming RTSP
 * client connection:
 *
 * @li accept the new socket;
 *
 * @li checks that there is space for new connections for the current
 *     fork;
 *
 * @li creates and sets up the @ref RTSP_Client object.
 *
 * The newly created instance is deleted by @ref
 * client_ev_disconnect_handler.
 *
 * @internal This function should be used as callback for an ev_io
 *           listener.
 */
void rtsp_client_incoming_cb(struct ev_loop *loop, ev_io *w,
                             int revents)
{
    Sock *sock = w->data;
    feng *srv = sock->data;
    Sock *client_sock = NULL;
    ev_io *io;
    ev_async *async;
    ev_timer *timer;
    RTSP_Client *rtsp;


    client_port_pair *clients=NULL;
    pid_t pid;


    if ( (client_sock = Sock_accept(sock, NULL)) == NULL )
        return;

    if (srv->connection_count >= ONE_FORK_MAX_CONNECTION) {
        Sock_close(client_sock);
        return;
    }

    if(!( clients = new_child_port(srv, 
                                   get_remote_host(client_sock), 
                                   get_remote_port(client_sock)))){
        Sock_close(client_sock);
        return;
    }
    pid = fork();
    if(pid==0){
        
        /*clean the context of parent*/
        clients->pid = getpid();
        current_client = clients;
        //free_child_port(clients);


        feng_ports_cleanup(srv);
        
        feng_stop_child_watcher(srv);
        
        demuxer_stsw_global_init();
        

        //void fnc_log_change_child();
        if(srv->srvconf.log_type == FNC_LOG_FILE){
            //child process can't write to parent log file
            fnc_log_uninit();            
        }
        


        rtsp = g_slice_new0(RTSP_Client);
        rtsp->sock = client_sock;
        rtsp->input = g_byte_array_new();
        rtsp->out_queue = g_queue_new();
        rtsp->srv = srv;
        rtsp->cached_resource = NULL;

        srv->connection_count++;
        client_sock->data = srv;
        
        /*install read handler*/
        io = &rtsp->ev_io_read;
        io->data = rtsp;
        ev_io_init(io, rtsp_read_cb, Sock_fd(client_sock), EV_READ);
        ev_io_start(srv->loop, io);
        
        /* configure the write handler*/
        /* to be started/stopped when necessary */
        io = &rtsp->ev_io_write;
        io->data = rtsp;
        ev_io_init(io, rtsp_write_cb, Sock_fd(client_sock), EV_WRITE);
        fnc_log(FNC_LOG_INFO, "Incoming RTSP connection accepted on socket: %d\n",
            Sock_fd(client_sock));
        
        /* install async event handler for destroy */
        async = &rtsp->ev_sig_disconnect;
        async->data = rtsp;
        ev_async_init(async, client_ev_disconnect_handler);
        ev_async_start(srv->loop, async);
        
        /* configure a check timer, 
         * After play, this timer would be started */
        timer = &rtsp->ev_timeout;
        timer->data = rtsp;
        ev_init(timer, client_ev_timeout);
        timer->repeat = STREAM_TIMEOUT;

        

    }else if(pid > 0){
        Sock_close(client_sock);   
        srv->connection_count++;

        clients->pid = pid;
        fnc_log(FNC_LOG_INFO, 
            "The child process (pid:%d) for rtsp connection (%s:%hu) is created\n", 
            pid,
            clients->host, 
            clients->port);        
        fnc_log(FNC_LOG_INFO, "Connection reached: %d\n", srv->connection_count);
        add_client_list(clients);        
     
        return;
    }else{
        Sock_close(client_sock);
        free_child_port(clients);
        fnc_log(FNC_LOG_ERR, "fork faild\n");
        return;
    }

}