Changeset 0878dc57d541b93055dd3c1e14a26b16acf563bc

Show
Ignore:
Timestamp:
17/07/03 16:28:13 (5 years ago)
Author:
Sam Hocevar <sam@videolan.org>
git-committer:
Sam Hocevar <sam@videolan.org> 1058452093 +0000
git-parent:

[aa935693f83221d868cd51522152ef3845222fd4]

git-author:
Sam Hocevar <sam@videolan.org> 1058452093 +0000
Message:
  • src/stream_output/announce.c:
    + Fixed Win32 port.
    + Speed optimizations in split().
    + More coding style fixes.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/stream_output/announce.c

    raa93569 r0878dc5  
    2929#include <string.h>                                            /* strerror() */ 
    3030 
     31#include <vlc/vlc.h> 
     32#include <vlc/sout.h> 
     33 
    3134#ifdef HAVE_UNISTD_H 
    3235#   include <unistd.h> 
    3336#endif 
    3437 
    35 #include <vlc/vlc.h> 
    36  
    37 #include <vlc/sout.h> 
     38#ifdef WIN32 
     39#   include <winsock2.h> 
     40#   include <ws2tcpip.h> 
     41#   ifndef IN_MULTICAST 
     42#       define IN_MULTICAST(a) IN_CLASSD(a) 
     43#   endif 
     44#else 
     45#   include <sys/socket.h> 
     46#endif 
     47 
    3848#undef DEBUG_BUFFER 
    3949 
    40 #include <announce.h> 
    41 #include <network.h> 
     50#include "announce.h" 
     51#include "network.h" 
    4252 
    4353#define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */ 
     
    5767static char * split( char *psz_in, char *psz_out1, char *psz_out2, char delim) 
    5868{ 
    59     unsigned int i_count = 0; /*pos in input string*/ 
    60     unsigned int i_pos1  = 0; /*pos in out2 string */ 
     69    unsigned int i_count = 0; /* pos in input string */ 
     70    unsigned int i_pos1  = 0; /* pos in out2 string */ 
    6171    unsigned int i_pos2  = 0; 
    62     char *psz_cur; /*store the pos of the first delim found */ 
    63  
    64     /*skip spaces at the beginning*/ 
    65     while(psz_in[i_count] == ' ' && i_count < strlen(psz_in)
     72    char *psz_cur; /* store the pos of the first delim found */ 
     73 
     74    /* Skip spaces at the beginning */ 
     75    while( psz_in[i_count] == ' '
    6676    { 
    6777        i_count++; 
    6878    } 
    69     if(i_count == strlen(psz_in)) 
     79 
     80    if( psz_in[i_count] == '\0' ) 
     81    { 
    7082        return NULL; 
    71  
    72     /*Look for delim*/ 
    73     while(psz_in[i_count] != delim && i_count < strlen(psz_in)) 
     83    } 
     84 
     85    /* Look for delim */ 
     86    while( psz_in[i_count] && psz_in[i_count] != delim ) 
    7487    { 
    7588        psz_out1[i_pos1] = psz_in[i_count]; 
     
    7891    } 
    7992    /* Mark the end of out1 */ 
    80     psz_out1[i_pos1] = 0; 
    81  
    82     if(i_count == strlen(psz_in)) 
     93    psz_out1[i_pos1] = '\0'; 
     94 
     95    if( psz_in[i_count] == '\0' ) 
     96    { 
    8397        return NULL; 
    84  
    85     /*store pos of the first delim*/ 
    86     psz_cur = &psz_in[i_count]; 
    87  
    88  
    89  
    90     /*skip all delim and all spaces*/ 
    91     while( (psz_in[i_count] == ' ' || 
    92             psz_in[i_count] == delim) 
    93            && i_count < strlen(psz_in)) 
     98    } 
     99 
     100    /* store pos of the first delim */ 
     101    psz_cur = psz_in + i_count; 
     102 
     103    /* skip all delim and all spaces */ 
     104    while( psz_in[i_count] == ' ' || psz_in[i_count] == delim ) 
    94105    { 
    95106        i_count++; 
    96107    } 
    97108 
    98     if(i_count == strlen(psz_in)) 
     109    if( psz_in[i_count] == '\0' ) 
     110    { 
    99111        return psz_cur; 
    100  
    101     /*Store the second string*/ 
    102     while(i_count < strlen(psz_in)) 
     112    } 
     113 
     114    /* Store the second string */ 
     115    while( psz_in[i_count] ) 
    103116    { 
    104117        psz_out2[i_pos2] = psz_in[i_count]; 
     
    106119        i_count++; 
    107120    } 
    108     psz_out2[i_pos2] = 0
     121    psz_out2[i_pos2] = '\0'
    109122 
    110123    return psz_cur; 
     
    114127 * sout_SAPNew: Creates a SAP Session 
    115128 *****************************************************************************/ 
    116 sap_session_t * sout_SAPNew ( sout_instance_t *p_sout, 
    117                 char * psz_url_arg, 
    118                 char * psz_name_arg, int ip_version, 
    119                 char * psz_v6_scope ) 
     129sap_session_t * sout_SAPNew ( sout_instance_t *p_sout, char * psz_url_arg, 
     130                              char * psz_name_arg, int ip_version, 
     131                              char * psz_v6_scope ) 
    120132{ 
    121     sap_session_t       *p_new; /* The SAP structure */ 
     133    sap_session_t       *p_sap; /* The SAP structure */ 
    122134    module_t            *p_network; /* Network module */ 
    123135    network_socket_t    socket_desc; /* Socket descriptor */ 
    124     char                psz_network[6]; /* IPv4 or IPv6 */ 
    125     char                *sap_ipv6_addr=NULL; /* IPv6 built address */ 
     136    char                *sap_ipv6_addr = NULL; /* IPv6 built address */ 
    126137    char                *psz_eol; /* Used to parse IPv6 URIs */ 
    127138    int                 i_port; /* Port in numerical format */ 
    128139 
    129140    /* Allocate the SAP structure */ 
    130     p_new = (sap_session_t *) malloc( sizeof ( sap_session_t ) ) ; 
    131     if ( !p_new
     141    p_sap = (sap_session_t *) malloc( sizeof ( sap_session_t ) ) ; 
     142    if ( !p_sap
    132143    { 
    133144        msg_Err( p_sout, "out of memory" ); 
     
    137148    /* Fill the information in the structure */ 
    138149    if( strstr( psz_url_arg, "[" ) ) 
    139     {      /* We have an IPv6 address. Do not use ':' as the port separator */ 
     150    { 
     151        /* We have an IPv6 address. Do not use ':' as the port separator */ 
    140152        psz_eol = strchr( psz_url_arg, ']' ); 
    141         if( !psz_eol ) /* No matching ] ! Aborting */ 
     153        if( !psz_eol ) 
    142154        { 
    143155            msg_Warn( p_sout, "no matching ], unable to parse URI"); 
    144156            return NULL; 
    145157        } 
     158 
    146159        if( !psz_eol++ ) 
    147160        { 
    148               sprintf( p_new->psz_url, "%s", psz_url_arg ); 
    149               sprintf( p_new->psz_port, "%s", DEFAULT_PORT ); 
     161            sprintf( p_sap->psz_url, "%s", psz_url_arg ); 
     162            sprintf( p_sap->psz_port, "%s", DEFAULT_PORT ); 
    150163        } 
    151164        else 
    152165        { 
    153166            *psz_eol = '\0'; 
    154             sprintf( p_new->psz_url, "%s", psz_url_arg ); 
     167            sprintf( p_sap->psz_url, "%s", psz_url_arg ); 
    155168            psz_eol++; 
    156169            if( psz_eol ) 
    157170            { 
    158                 sprintf( p_new->psz_port, "%s", psz_eol ); 
     171                sprintf( p_sap->psz_port, "%s", psz_eol ); 
    159172            } 
    160173        } 
     
    162175    else 
    163176    { 
    164         split( psz_url_arg, p_new->psz_url, p_new->psz_port, ':' ); 
     177        split( psz_url_arg, p_sap->psz_url, p_sap->psz_port, ':' ); 
    165178    } 
    166179 
    167180    /* Check if we have a port */ 
    168     if( !strlen( p_new->psz_port ) ) 
    169     { 
    170         sprintf( p_new->psz_port, "%s", DEFAULT_PORT ); 
    171     } 
    172  
    173     /* Make sure our port is valid and atoi it*/ 
    174     i_port = atoi( p_new->psz_port ); 
     181    if( !strlen( p_sap->psz_port ) ) 
     182    { 
     183        sprintf( p_sap->psz_port, "%s", DEFAULT_PORT ); 
     184    } 
     185 
     186    /* Make sure our port is valid and atoi it */ 
     187    i_port = atoi( p_sap->psz_port ); 
    175188 
    176189    if( !i_port ) 
    177190    { 
    178         sprintf( p_new->psz_port, "%s", DEFAULT_PORT ); 
     191        sprintf( p_sap->psz_port, "%s", DEFAULT_PORT ); 
    179192    } 
    180193    else 
    181194    { 
    182         sprintf( p_new->psz_port, "%i", i_port ); 
     195        sprintf( p_sap->psz_port, "%i", i_port ); 
    183196    } 
    184197 
    185198    /* The name that we send */ 
    186     sprintf( p_new->psz_name, "%s", psz_name_arg ); 
    187  
    188     p_new->i_ip_version = ip_version; 
     199    sprintf( p_sap->psz_name, "%s", psz_name_arg ); 
     200 
     201    p_sap->i_ip_version = ip_version; 
    189202 
    190203    /* Only "6" triggers IPv6. IPv4 is default */ 
     
    202215 
    203216        /* Call the network module */ 
    204         sprintf ( psz_network, "ipv4" ); 
    205217        p_sout->p_private = (void*) &socket_desc; 
    206         if( !( p_network = module_Need( p_sout, "network", psz_network ) ) ) 
     218        if( !( p_network = module_Need( p_sout, "network", "ipv4" ) ) ) 
    207219        { 
    208220             msg_Warn( p_sout, "failed to open a connection (udp)" ); 
     
    211223        module_Unneed( p_sout, p_network ); 
    212224 
    213         p_new->i_socket = socket_desc.i_handle; 
    214         if( p_new->i_socket < 0 ) 
     225        p_sap->i_socket = socket_desc.i_handle; 
     226        if( p_sap->i_socket < 0 ) 
    215227        { 
    216228            msg_Warn( p_sout, "unable to initialize SAP" ); 
     
    241253        socket_desc.i_handle        = 0; 
    242254 
    243         sprintf ( psz_network, "ipv6" ); 
    244  
    245255        /* Call the network module */ 
    246256        p_sout->p_private = (void *) &socket_desc; 
    247         if( !( p_network = module_Need( p_sout, "network", psz_network ) ) ) 
     257        if( !( p_network = module_Need( p_sout, "network", "ipv6" ) ) ) 
    248258        { 
    249259            msg_Warn( p_sout, "failed to open a connection (udp)" ); 
     
    252262        module_Unneed( p_sout, p_network ); 
    253263 
    254         p_new->i_socket = socket_desc.i_handle; 
    255         if( p_new->i_socket <= 0 ) 
     264        p_sap->i_socket = socket_desc.i_handle; 
     265        if( p_sap->i_socket <= 0 ) 
    256266        { 
    257267            msg_Warn( p_sout, "unable to initialize SAP" ); 
     
    268278    msg_Dbg( p_sout, "SAP initialization complete" ); 
    269279 
    270     return(p_new)
     280    return p_sap
    271281} 
    272282 
     
    274284 * sout_SAPDelete: Deletes a SAP Session 
    275285 *****************************************************************************/ 
    276 void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t * p_this
     286void sout_SAPDelete( sout_instance_t *p_sout, sap_session_t * p_sap
    277287{ 
    278     if( close( p_this->i_socket ) ) 
     288    int i_ret; 
     289 
     290#if defined( UNDER_CE ) 
     291    i_ret = CloseHandle( (HANDLE)p_sap->i_handle ); 
     292#elif defined( WIN32 ) 
     293    i_ret = closesocket( p_sap->i_handle ); 
     294#else 
     295    i_ret = close( p_sap->i_handle ); 
     296#endif 
     297 
     298    if( i_ret ) 
    279299    { 
    280300        msg_Err( p_sout, "unable to close SAP socket" ); 
    281301    } 
    282302 
    283     free( p_this ); 
     303    free( p_sap ); 
    284304} 
    285305 
     
    287307 * sout_SAPSend: Sends a SAP packet 
    288308 *****************************************************************************/ 
    289 void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_this
     309void sout_SAPSend( sout_instance_t *p_sout, sap_session_t * p_sap
    290310{ 
    291311    char psz_msg[1000];                     /* SDP content */ 
     
    299319 
    300320    /* We send a packet every 24 calls to the function */ 
    301     if( p_this->i_calls++ < 24 ) 
     321    if( p_sap->i_calls++ < 24 ) 
    302322    { 
    303323        return; 
     
    337357                      "c=IN IP4 %s/15\n" 
    338358                      "a=type:test\n", 
    339              p_this->psz_name, p_this->psz_port, p_this->psz_url ); 
     359             p_sap->psz_name, p_sap->psz_port, p_sap->psz_url ); 
    340360 
    341361    i_msg_size = strlen( psz_msg ); 
     
    355375    if( i_size < 1024 ) /* We mustn't send packets larger than 1024B */ 
    356376    { 
    357         if( p_this->i_ip_version == 6 ) 
    358         { 
    359             i_ret = send( p_this->i_socket, psz_send, i_size, 0 ); 
     377        if( p_sap->i_ip_version == 6 ) 
     378        { 
     379            i_ret = send( p_sap->i_socket, psz_send, i_size, 0 ); 
    360380        } 
    361381        else 
    362382        { 
    363             i_ret = send( p_this->i_socket, psz_send, i_size, 0 ); 
     383            i_ret = send( p_sap->i_socket, psz_send, i_size, 0 ); 
    364384        } 
    365385    } 
     
    368388    { 
    369389        msg_Warn( p_sout, "SAP send failed on socket %i (%s)", 
    370                           p_this->i_socket, strerror(errno) ); 
    371     } 
    372  
    373     p_this->i_calls = 0; 
     390                          p_sap->i_socket, strerror(errno) ); 
     391    } 
     392 
     393    p_sap->i_calls = 0; 
    374394 
    375395    /* Free what we allocated */