Changeset 6283c63bb120fef59d85d0f8fbf16d89c6c99224

Show
Ignore:
Timestamp:
27/02/03 16:07:48 (6 years ago)
Author:
Laurent Aimar <fenrir@videolan.org>
git-committer:
Laurent Aimar <fenrir@videolan.org> 1046358468 +0000
git-parent:

[e853c4ee5d5caf4e7cbb70359385c89df1f6bf7d]

git-author:
Laurent Aimar <fenrir@videolan.org> 1046358468 +0000
Message:
  • httpd: clean up, kick up unused connection (it waits 10s), use select

to way (instead of a hard coded sleep ;)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • modules/misc/httpd.c

    r626d8be r6283c63  
    33 ***************************************************************************** 
    44 * Copyright (C) 2001-2003 VideoLAN 
    5  * $Id: httpd.c,v 1.3 2003/02/25 17:17:43 fenrir Exp $ 
     5 * $Id: httpd.c,v 1.4 2003/02/27 15:07:48 fenrir Exp $ 
    66 * 
    77 * Authors: Laurent Aimar <fenrir@via.ecp.fr> 
     
    2626 *****************************************************************************/ 
    2727#include <stdlib.h> 
     28 
     29#include <sys/time.h> 
    2830#include <sys/types.h> 
    2931#include <sys/stat.h> 
     32 
    3033#include <string.h> 
    3134#include <errno.h> 
     
    6770#endif 
    6871 
    69 #define LISTEN_BACKLOG  100 
    70 #define HTTPD_MAX_CONNECTION    1024 
    71  
     72#define LISTEN_BACKLOG          100 
     73#define HTTPD_MAX_CONNECTION    512 
     74#define HTTPD_CONNECTION_MAX_UNUSED 10000000 
    7275 
    7376#define FREE( p ) if( p ) { free( p); (p) = NULL; } 
     
    176179    struct httpd_connection_s *p_prev; 
    177180 
    178     struct sockaddr_in sock; 
    179     int    fd; 
     181    struct  sockaddr_in sock; 
     182    int     fd; 
     183    mtime_t i_last_activity_date; 
    180184 
    181185    int    i_state; 
     
    958962    p += sprintf( p, "<h2><center>Admin page</center></h2>\n" ); 
    959963 
     964    /* general */ 
     965    p += sprintf( p, "<h3>General state</h3>\n" ); 
     966    p += sprintf( p, "<ul>\n" ); 
     967    p += sprintf( p, "<li>Connection count: %d</li>\n", p_httpt->i_connection_count ); 
     968    //p += sprintf( p, "<li>Total bandwith: %d</li>\n", -1 ); 
     969    /*p += sprintf( p, "<li></li>\n" );*/ 
     970    p += sprintf( p, "</ul>\n" ); 
    960971    /* host list */ 
    961972    vlc_mutex_lock( &p_httpt->host_lock ); 
     
    10541065    p_con->i_state  = HTTPD_CONNECTION_RECEIVING_REQUEST; 
    10551066    p_con->fd       = fd; 
     1067    p_con->i_last_activity_date = mdate(); 
     1068 
    10561069    p_con->sock     = *p_sock; 
    10571070    p_con->psz_file = NULL; 
     
    14031416 
    14041417    httpd_connection_t *p_con; 
    1405     vlc_bool_t         b_wait; 
    14061418 
    14071419    msg_Info( p_httpt, "httpd started" ); 
     
    14251437    while( !p_httpt->b_die ) 
    14261438    { 
     1439        struct timeval  timeout; 
     1440        fd_set          fds_read; 
     1441        fd_set          fds_write; 
     1442        int             i_handle_max = 0; 
     1443        int             i_ret; 
    14271444        int i; 
    14281445        if( p_httpt->i_host_count <= 0 ) 
     
    14311448            continue; 
    14321449        } 
     1450 
     1451        /* we will create a socket set with host and connection */ 
     1452        FD_ZERO( &fds_read ); 
     1453        FD_ZERO( &fds_write ); 
     1454 
     1455        vlc_mutex_lock( &p_httpt->host_lock ); 
     1456        vlc_mutex_lock( &p_httpt->connection_lock ); 
     1457        for( i = 0; i < p_httpt->i_host_count; i++ ) 
     1458        { 
     1459            FD_SET( p_httpt->host[i]->fd, &fds_read ); 
     1460            i_handle_max = __MAX( i_handle_max, p_httpt->host[i]->fd ); 
     1461        } 
     1462        for( p_con = p_httpt->p_first_connection; p_con != NULL; ) 
     1463        { 
     1464            /* no more than 10s of inactivity */ 
     1465            if( p_con->i_last_activity_date + (mtime_t)HTTPD_CONNECTION_MAX_UNUSED < mdate() ) 
     1466            { 
     1467                httpd_connection_t *p_next = p_con->p_next; 
     1468 
     1469                msg_Dbg( p_httpt,  "close unused connection" ); 
     1470                httpd_ConnnectionClose( p_httpt, p_con ); 
     1471                p_con = p_next; 
     1472                continue; 
     1473            } 
     1474 
     1475            if( p_con->i_state == HTTPD_CONNECTION_SENDING_STREAM && p_con->i_stream_pos + HTTPD_STREAM_PACKET >= p_con->p_file->i_buffer_pos ) 
     1476            { 
     1477                p_con = p_con->p_next; 
     1478                continue; 
     1479            } 
     1480 
     1481            if( p_con->i_state == HTTPD_CONNECTION_RECEIVING_REQUEST ) 
     1482            { 
     1483                FD_SET( p_con->fd, &fds_read ); 
     1484            } 
     1485            else 
     1486            { 
     1487                FD_SET( p_con->fd, &fds_write ); 
     1488            } 
     1489            i_handle_max = __MAX( i_handle_max, p_con->fd ); 
     1490 
     1491            p_con = p_con->p_next; 
     1492        } 
     1493        vlc_mutex_unlock( &p_httpt->host_lock ); 
     1494        vlc_mutex_unlock( &p_httpt->connection_lock ); 
     1495 
     1496        /* we will wait 0.5s */ 
     1497        timeout.tv_sec = 0; 
     1498        timeout.tv_usec = 500*1000; 
     1499 
     1500        i_ret = select( i_handle_max + 1,  
     1501                        &fds_read, 
     1502                        &fds_write, 
     1503                        NULL, 
     1504                        &timeout ); 
     1505        if( i_ret == -1 && errno != EINTR ) 
     1506        { 
     1507            msg_Warn( p_httpt, "cannot select sockets" ); 
     1508            msleep( 1000 ); 
     1509            continue; 
     1510        } 
     1511        if( i_ret <= 0 ) 
     1512        { 
     1513//            msg_Dbg( p_httpt, "waiting..." ); 
     1514            continue; 
     1515        } 
     1516 
    14331517        vlc_mutex_lock( &p_httpt->host_lock ); 
    14341518        /* accept/refuse new connection */ 
     
    14881572                { 
    14891573                    uint8_t *ptr; 
    1490  
     1574                    p_con->i_last_activity_date = mdate(); 
    14911575                    p_con->i_buffer += i_len; 
    14921576 
     
    15281612                else if( i_len > 0 ) 
    15291613                { 
     1614                    p_con->i_last_activity_date = mdate(); 
    15301615                    p_con->i_buffer += i_len; 
    15311616 
     
    16101695                    else if( i_send > 0 ) 
    16111696                    { 
     1697                        p_con->i_last_activity_date = mdate(); 
    16121698                        p_con->i_stream_pos += i_send; 
    16131699                    } 
     
    16231709        }   /* for over connection */ 
    16241710 
    1625 #if 0 
    1626         b_wait = VLC_TRUE; 
    1627         /* update position for stream based file */ 
    1628         for( i = 0; i < p_httpt->i_file_count; i++ ) 
    1629         { 
    1630             if( p_httpt->file[i]->b_stream ) 
    1631             { 
    1632                 p_httpt->file[i]->i_buffer += __MIN( p_httpt->file[i]->i_buffer_valid - p_httpt->file[i]->i_buffer, 
    1633                                                      HTTPD_STREAM_PACKET ); 
    1634                 if( p_httpt->file[i]->i_buffer < p_httpt->file[i]->i_buffer_valid ) 
    1635                 { 
    1636                     /* there is data */ 
    1637                     b_wait = VLC_FALSE; 
    1638                 } 
    1639             } 
    1640         } 
    1641 #endif 
    16421711        vlc_mutex_unlock( &p_httpt->file_lock ); 
    1643         /*if( b_wait )*/ msleep( 10 ); 
    16441712    } 
    16451713    msg_Info( p_httpt, "httpd stopped" );