[Mplayerxp-cvslog] SF.net SVN: mplayerxp:[516] mplayerxp
Brought to you by:
olov
From: <nic...@us...> - 2012-12-07 14:29:51
|
Revision: 516 http://mplayerxp.svn.sourceforge.net/mplayerxp/?rev=516&view=rev Author: nickols_k Date: 2012-12-07 14:29:41 +0000 (Fri, 07 Dec 2012) Log Message: ----------- use class Tcp and class Udp for networking instead of flat fd=open() access Modified Paths: -------------- mplayerxp/libmpstream/Makefile mplayerxp/libmpstream/asf_mmst_streaming.cpp mplayerxp/libmpstream/asf_streaming.cpp mplayerxp/libmpstream/asf_streaming.h mplayerxp/libmpstream/cache2.cpp mplayerxp/libmpstream/cddb.cpp mplayerxp/libmpstream/librtsp/rtsp.cpp mplayerxp/libmpstream/librtsp/rtsp.h mplayerxp/libmpstream/librtsp/rtsp_session.cpp mplayerxp/libmpstream/librtsp/rtsp_session.h mplayerxp/libmpstream/network.cpp mplayerxp/libmpstream/network.h mplayerxp/libmpstream/pnm.cpp mplayerxp/libmpstream/pnm.h mplayerxp/libmpstream/rtp.cpp mplayerxp/libmpstream/rtp.h mplayerxp/libmpstream/s_ftp.cpp mplayerxp/libmpstream/s_network.cpp mplayerxp/libmpstream/s_rtsp.cpp mplayerxp/libmpstream/s_udp.cpp mplayerxp/libmpstream/stream.cpp mplayerxp/libmpstream/stream.h mplayerxp/libmpstream/tcp.cpp mplayerxp/libmpstream/tcp.h mplayerxp/libmpstream/udp.cpp mplayerxp/libmpstream/udp.h mplayerxp/libmpsub/vobsub.cpp Modified: mplayerxp/libmpstream/Makefile =================================================================== --- mplayerxp/libmpstream/Makefile 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/Makefile 2012-12-07 14:29:41 UTC (rev 516) @@ -7,7 +7,7 @@ DO_ALL = @ for i in $(SUBDIRS); do $(MAKE) -C $$i all || exit; done CXXSRCS= s_file.cpp s_lavc.cpp s_null.cpp s_tv.cpp -CXXSRCS+= mrl.cpp stream.cpp url.cpp cache2.cpp +CXXSRCS+= mrl.cpp stream.cpp url.cpp cache2.cpp ifeq ($(HAVE_LIBCDIO_CDDA),yes) CXXSRCS += s_cdd.cpp CXXSRCS += cdda.cpp Modified: mplayerxp/libmpstream/asf_mmst_streaming.cpp =================================================================== --- mplayerxp/libmpstream/asf_mmst_streaming.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/asf_mmst_streaming.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -61,7 +61,7 @@ static int num_stream_ids; static int stream_ids[MAX_STREAMS]; -static int get_data (int s,unsigned char *buf, size_t count); +static int get_data (Tcp& s,unsigned char *buf, size_t count); static void put_32 (command_t *cmd, uint32_t value) { @@ -88,7 +88,7 @@ return ret; } -static void send_command (int s, int command, uint32_t switches, +static void send_command (Tcp& tcp, int command, uint32_t switches, uint32_t extra, int length, unsigned char *data) { @@ -117,7 +117,7 @@ if (length & 7) memset(&cmd.buf[48 + length], 0, 8 - (length & 7)); - if (send (s, cmd.buf, len8*8+48, 0) != (len8*8+48)) { + if (tcp.write (cmd.buf, len8*8+48) != (len8*8+48)) { MSG_ERR ("write error\n"); } } @@ -157,7 +157,7 @@ #endif } -static void get_answer (int s) +static void get_answer (Tcp& tcp) { unsigned char data[BUF_SIZE]; int command = 0x1b; @@ -165,7 +165,7 @@ while (command == 0x1b) { int len; - len = recv (s, data, BUF_SIZE, 0) ; + len = tcp.read(data, BUF_SIZE); if (!len) { MSG_ERR ("\nalert! eof\n"); return; @@ -174,18 +174,18 @@ command = get_32 (data, 36) & 0xFFFF; if (command == 0x1b) - send_command (s, 0x1b, 0, 0, 0, data); + send_command (tcp, 0x1b, 0, 0, 0, data); } } -static int get_data (int s,unsigned char *buf, size_t count) +static int get_data (Tcp& tcp,unsigned char *buf, size_t count) { ssize_t len; size_t total = 0; while (total < count) { - len = recv (s, &buf[total], count-total, 0); + len = tcp.read(&buf[total], count-total); if (len<=0) { MSG_ERR ("read error:"); @@ -205,7 +205,7 @@ } -static int get_header (int s, uint8_t *header, networking_t *networking) +static int get_header (Tcp& tcp, uint8_t *header, networking_t *networking) { unsigned char pre_header[8]; int header_len; @@ -213,7 +213,7 @@ header_len = 0; while (1) { - if (!get_data (s, pre_header, 8)) { + if (!get_data (tcp, pre_header, 8)) { MSG_ERR ("pre-header read failed\n"); return 0; } @@ -230,7 +230,7 @@ return 0; } - if (!get_data (s, &header[header_len], packet_len)) { + if (!get_data (tcp, &header[header_len], packet_len)) { MSG_ERR("header data read failed\n"); return 0; } @@ -256,7 +256,7 @@ int command; unsigned char data[BUF_SIZE]; - if (!get_data (s, (unsigned char*)&packet_len, 4)) { + if (!get_data (tcp, (unsigned char*)&packet_len, 4)) { MSG_ERR ("packet_len read failed\n"); return 0; } @@ -270,7 +270,7 @@ return 0; } - if (!get_data (s, data, packet_len)) { + if (!get_data (tcp, data, packet_len)) { MSG_ERR ("command data read failed\n"); return 0; } @@ -280,7 +280,7 @@ // printf ("command: %02x\n", command); if (command == 0x1b) - send_command (s, 0x1b, 0, 0, 0, data); + send_command (tcp, 0x1b, 0, 0, 0, data); } @@ -363,11 +363,11 @@ } -static int get_media_packet (int s, int padding, networking_t *stream_ctrl) { +static int get_media_packet (Tcp& tcp, int padding, networking_t *stream_ctrl) { unsigned char pre_header[8]; unsigned char data[BUF_SIZE]; - if (!get_data (s, pre_header, 8)) { + if (!get_data (tcp, pre_header, 8)) { MSG_ERR ("pre-header read failed\n"); return 0; } @@ -389,7 +389,7 @@ return 0; } - if (!get_data (s, data, packet_len)) { + if (!get_data (tcp, data, packet_len)) { MSG_ERR ("media data read failed\n"); return 0; } @@ -401,7 +401,7 @@ int32_t packet_len; int command; - if (!get_data (s, (unsigned char*)&packet_len, 4)) { + if (!get_data (tcp, (unsigned char*)&packet_len, 4)) { MSG_ERR ("packet_len read failed\n"); return 0; } @@ -413,7 +413,7 @@ return 0; } - if (!get_data (s, data, packet_len)) { + if (!get_data (tcp, data, packet_len)) { MSG_ERR ("command data read failed\n"); return 0; } @@ -430,7 +430,7 @@ // printf ("\ncommand packet detected, len=%d cmd=0x%X\n", packet_len, command); if (command == 0x1b) - send_command (s, 0x1b, 0, 0, 0, data); + send_command (tcp, 0x1b, 0, 0, 0, data); else if (command == 0x1e) { MSG_OK ("everything done. Thank you for downloading a media file containing proprietary and patentend technology.\n"); return 0; @@ -454,13 +454,13 @@ static int packet_length1; -static int asf_mmst_networking_read( int fd, char *buffer, int size, networking_t *stream_ctrl ) +static int asf_mmst_networking_read(Tcp& tcp, char *buffer, int size, networking_t *stream_ctrl ) { int len; while( stream_ctrl->buffer_size==0 ) { // buffer is empty - fill it! - int ret = get_media_packet( fd, packet_length1, stream_ctrl); + int ret = get_media_packet(tcp, packet_length1, stream_ctrl); if( ret<0 ) { MSG_ERR("get_media_packet error : %s\n",strerror(errno)); return -1; @@ -482,16 +482,15 @@ } -static int asf_mmst_networking_seek( int fd, off_t pos, networking_t *networking ) +static int asf_mmst_networking_seek(Tcp& tcp, off_t pos, networking_t *networking ) { - return -1; - // Shut up gcc warning - fd++; - pos++; - networking=NULL; + UNUSED(tcp); + UNUSED(pos); + UNUSED(networking); + return -1; } -int asf_mmst_networking_start(net_fd_t* fd, networking_t *networking) +int asf_mmst_networking_start(Tcp& tcp, networking_t *networking) { char str[1024]; unsigned char data[BUF_SIZE]; @@ -500,12 +499,8 @@ int len, i, packet_length; char *path, *unescpath; URL_t *url1 = networking->url; - net_fd_t s = *fd; - if( s>0 ) { - closesocket( *fd ); - *fd = -1; - } + tcp.close(); /* parse url */ path = strchr(url1->file,'/') + 1; @@ -524,10 +519,10 @@ if( url1->port==0 ) { url1->port=1755; } - s = tcp_connect2Server(networking->libinput, url1->hostname, url1->port, 0); - if( s<0 ) { + tcp.open(networking->libinput, url1->hostname, url1->port, Tcp::IP4); + if( !tcp.established()) { delete path; - return s; + return -1; } MSG_INFO ("connected\n"); @@ -551,9 +546,9 @@ snprintf (str, 1023, "\034\003NSPlayer/7.0.0.1956; {33715801-BAB3-9D85-24E9-03B90328270A}; Host: %s", url1->hostname); string_utf16 (data, str, strlen(str)); // send_command(s, commandno ....) - send_command (s, 1, 0, 0x0004000b, strlen(str) * 2+2, data); + send_command (tcp, 1, 0, 0x0004000b, strlen(str) * 2+2, data); - len = recv (s, data, BUF_SIZE, 0) ; + len = tcp.read (data, BUF_SIZE); /*This sends details of the local machine IP address to a Funnel system at the server. * Also, the TCP or UDP transport selection is sent. @@ -565,19 +560,19 @@ string_utf16 (&data[8], "\002\000\\\\192.168.0.1\\TCP\\1037", 24); memset (data, 0, 8); - send_command (s, 2, 0, 0, 24*2+10, data); + send_command (tcp, 2, 0, 0, 24*2+10, data); - len = recv (s, data, BUF_SIZE, 0) ; + len = tcp.read(data, BUF_SIZE); /* This command sends file path (at server) and file name request to the server. * 0x5 */ string_utf16 (&data[8], path, strlen(path)); memset (data, 0, 8); - send_command (s, 5, 0, 0, strlen(path)*2+10, data); + send_command (tcp, 5, 0, 0, strlen(path)*2+10, data); delete path; - get_answer (s); + get_answer (tcp); /* The ASF header chunk request. Includes ?session' variable for pre header value. * After this command is sent, @@ -587,15 +582,15 @@ memset (data, 0, 40); data[32] = 2; - send_command (s, 0x15, 1, 0, 40, data); + send_command (tcp, 0x15, 1, 0, 40, data); num_stream_ids = 0; /* get_headers(s, asf_header); */ - asf_header_len = get_header (s, asf_header, networking); + asf_header_len = get_header (tcp, asf_header, networking); // printf("---------------------------------- asf_header %d\n",asf_header); if (asf_header_len==0) { //error reading header - closesocket(s); + tcp.close(); return -1; } packet_length = interp_header (asf_header, asf_header_len); @@ -617,7 +612,7 @@ data[2] = 0xFF; data[3] = 0xFF; data[4] = mp_conf.audio_id; - send_command(s, 0x33, num_stream_ids, 0xFFFF | mp_conf.audio_id << 16, 8, data); + send_command(tcp, 0x33, num_stream_ids, 0xFFFF | mp_conf.audio_id << 16, 8, data); } else { for (i=1; i<num_stream_ids; i++) { data [ (i-1) * 6 + 2 ] = 0xFF; @@ -626,10 +621,10 @@ data [ (i-1) * 6 + 5 ] = 0x00; } - send_command (s, 0x33, num_stream_ids, 0xFFFF | stream_ids[0] << 16, (num_stream_ids-1)*6+2 , data); + send_command (tcp, 0x33, num_stream_ids, 0xFFFF | stream_ids[0] << 16, (num_stream_ids-1)*6+2 , data); } - get_answer (s); + get_answer (tcp); /* Start sending file from packet xx. * This command is also used for resume downloads or requesting a lost packet. @@ -644,9 +639,8 @@ data[20] = 0x04; - send_command (s, 0x07, 1, 0xFFFF | stream_ids[0] << 16, 24, data); + send_command (tcp, 0x07, 1, 0xFFFF | stream_ids[0] << 16, 24, data); - *fd = s; networking->networking_read = asf_mmst_networking_read; networking->networking_seek = asf_mmst_networking_seek; networking->buffering = 1; Modified: mplayerxp/libmpstream/asf_streaming.cpp =================================================================== --- mplayerxp/libmpstream/asf_streaming.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/asf_streaming.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -28,10 +28,9 @@ #include "stream_msg.h" #if defined( ARCH_X86 ) || defined(ARCH_X86_64) -#define ASF_LOAD_GUID_PREFIX(guid) (*(uint32_t *)(guid)) +static inline uint32_t ASF_LOAD_GUID_PREFIX(uint8_t* guid) { return *(uint32_t*)guid; } #else -#define ASF_LOAD_GUID_PREFIX(guid) \ - ((guid)[3] << 24 | (guid)[2] << 16 | (guid)[1] << 8 | (guid)[0]) +static inline uint32_t ASF_LOAD_GUID_PREFIX(uint8_t* guid) { return bswap_32(*(uint32_t*)guid); } #endif // ASF networking support several network protocol. @@ -52,7 +51,7 @@ // WMP sequence is MMSU then MMST and then HTTP. // In MPlayer case since HTTP support is more reliable, // we are doing HTTP first then we try MMST if HTTP fail. -static int asf_http_networking_start(net_fd_t* fd, networking_t *networking ); +static int asf_http_networking_start(Tcp& fd, networking_t *networking ); /* ASF networking support several network protocol. @@ -74,10 +73,10 @@ In MPlayer case since HTTP support is more reliable, we are doing HTTP first then we try MMST if HTTP fail. */ -int asf_networking_start(net_fd_t* fd, networking_t *networking) { +int asf_networking_start(Tcp& tcp, networking_t *networking) { char *proto = networking->url->protocol; - *fd = -1; int port = networking->url->port; + int rc; // Is protocol even valid mms,mmsu,mmst,http,http_proxy? if (!(!strncasecmp(proto, "mmst", 4) || !strncasecmp(proto, "mmsu", 4) || @@ -91,30 +90,30 @@ if (!strncasecmp(proto, "mmsu", 4) || !strncasecmp(proto, "mms", 3)) { MSG_V("Trying ASF/UDP...\n"); //fd = asf_mmsu_networking_start( stream ); - if( *fd>-1 ) return 0; //mmsu support is not implemented yet - using this code + //mmsu support is not implemented yet - using this code MSG_V(" ===> ASF/UDP failed\n"); - if( *fd==-2 ) return -1; + return -1; } //Is protocol mms or mmst? if (!strncasecmp(proto, "mmst", 4) || !strncasecmp(proto, "mms", 3)) { MSG_V("Trying ASF/TCP...\n"); - *fd = asf_mmst_networking_start(fd,networking); + rc = asf_mmst_networking_start(tcp,networking); networking->url->port = port; - if( *fd>-1 ) return 0; + if( rc>-1 ) return 0; MSG_V(" ===> ASF/TCP failed\n"); - if( *fd==-2 ) return -1; + if( rc==-2 ) return -1; } //Is protocol http, http_proxy, or mms? if (!strncasecmp(proto, "http_proxy", 10) || !strncasecmp(proto, "http", 4) || !strncasecmp(proto, "mms", 3)) { MSG_V("Trying ASF/HTTP...\n"); - *fd = asf_http_networking_start(fd,networking); + rc = asf_http_networking_start(tcp,networking); networking->url->port = port; - if( *fd>-1 ) return 0; + if( rc>-1 ) return 0; MSG_V(" ===> ASF/HTTP failed\n"); - if( *fd==-2 ) return -1; + if( rc==-2 ) return -1; } //everything failed return -1; @@ -186,7 +185,7 @@ } static int -asf_networking_parse_header(int fd, networking_t* networking) { +asf_networking_parse_header(Tcp& tcp, networking_t* networking) { ASF_header_t asfh; ASF_stream_chunck_t chunk; asf_http_networking_t* asf_ctrl = (asf_http_networking_t*) networking->data; @@ -206,7 +205,7 @@ // So we need to retrieve all the chunk before starting to parse the header. do { for( r=0; r < (int)sizeof(ASF_stream_chunck_t) ; ) { - i = nop_networking_read(fd,((char*)&chunk)+r,sizeof(ASF_stream_chunck_t) - r,networking); + i = nop_networking_read(tcp,((char*)&chunk)+r,sizeof(ASF_stream_chunck_t) - r,networking); if(i <= 0) return -1; r += i; } @@ -239,7 +238,7 @@ buffer_size += size; for(r = 0; r < size;) { - i = nop_networking_read(fd,buffer+r,size-r,networking); + i = nop_networking_read(tcp,buffer+r,size-r,networking); if(i < 0) { MSG_ERR("Error while reading network stream\n"); return -1; @@ -438,7 +437,7 @@ } static int -asf_http_networking_read( int fd, char *buffer, int size, networking_t *networking ) { +asf_http_networking_read( Tcp& tcp, char *buffer, int size, networking_t *networking ) { static ASF_stream_chunck_t chunk; int read,chunk_size = 0; static int rest = 0, drop_chunk = 0, waiting = 0; @@ -448,7 +447,7 @@ if (rest == 0 && waiting == 0) { read = 0; while(read < (int)sizeof(ASF_stream_chunck_t)){ - int r = nop_networking_read( fd, ((char*)&chunk) + read, + int r = nop_networking_read( tcp, ((char*)&chunk) + read, sizeof(ASF_stream_chunck_t)-read, networking ); if(r <= 0){ @@ -490,7 +489,7 @@ chunk_size = size; } while(read < chunk_size) { - int got = nop_networking_read( fd,buffer+read,chunk_size-read,networking ); + int got = nop_networking_read( tcp,buffer+read,chunk_size-read,networking ); if(got <= 0) { if(got < 0) MSG_ERR("Error while reading chunk\n"); @@ -514,12 +513,11 @@ } static int -asf_http_networking_seek( int fd, off_t pos, networking_t *networking ) { - return -1; - // to shut up gcc warning - fd++; - pos++; - networking=NULL; +asf_http_networking_seek( Tcp& tcp, off_t pos, networking_t *networking ) { + UNUSED(tcp); + UNUSED(pos); + UNUSED(networking); + return -1; } static int @@ -743,7 +741,7 @@ return 0; } -static int asf_http_networking_start(net_fd_t* fd, networking_t *networking) { +static int asf_http_networking_start(Tcp& tcp, networking_t *networking) { HTTP_header_t *http_hdr=NULL; URL_t *url = networking->url; asf_http_networking_t *asf_http_ctrl; @@ -765,20 +763,20 @@ do { done = 1; - if( *fd>0 ) closesocket( *fd ); + tcp.close(); if( !strcasecmp( url->protocol, "http_proxy" ) ) { if( url->port==0 ) url->port = 8080; } else { if( url->port==0 ) url->port = 80; } - *fd = tcp_connect2Server(networking->libinput, url->hostname, url->port, 0); - if( *fd<0 ) return 0; + tcp.open(networking->libinput, url->hostname, url->port, Tcp::IP4); + if( !tcp.established()) return 0; http_hdr = asf_http_request( networking ); MSG_DBG2("Request [%s]\n", http_hdr->buffer ); for(i=0; i < (int)http_hdr->buffer_size ; ) { - int r = send( *fd, http_hdr->buffer+i, http_hdr->buffer_size-i, 0); + int r = tcp.write((uint8_t*)(http_hdr->buffer+i), http_hdr->buffer_size-i); if(r <0) { MSG_ERR("Socket write error : %s\n",strerror(errno)); return -1; @@ -788,7 +786,7 @@ http_free( http_hdr ); http_hdr = http_new_header(); do { - i = recv( *fd, buffer, BUFFER_SIZE, 0); + i = tcp.read((uint8_t*)buffer, BUFFER_SIZE); if( i<=0 ) { perror("read"); http_free( http_hdr ); @@ -819,7 +817,7 @@ if( asf_http_ctrl->request==1 ) { if( asf_http_ctrl->networking_type!=ASF_PlainText_e ) { // First request, we only got the ASF header. - ret = asf_networking_parse_header(*fd,networking); + ret = asf_networking_parse_header(tcp,networking); if(ret < 0) return -1; if(asf_http_ctrl->n_audio == 0 && asf_http_ctrl->n_video == 0) { MSG_ERR("No stream found\n"); @@ -850,14 +848,13 @@ case ASF_Unknown_e: default: MSG_ERR("Unknown ASF networking type\n"); - closesocket(*fd); + tcp.close(); http_free( http_hdr ); return -1; } // Check if we got a redirect. } while(!done); - fd = fd; if( asf_http_ctrl->networking_type==ASF_PlainText_e || asf_http_ctrl->networking_type==ASF_Redirector_e ) { networking->networking_read = nop_networking_read; networking->networking_seek = nop_networking_seek; Modified: mplayerxp/libmpstream/asf_streaming.h =================================================================== --- mplayerxp/libmpstream/asf_streaming.h 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/asf_streaming.h 2012-12-07 14:29:41 UTC (rev 516) @@ -2,8 +2,10 @@ #define __ASF_STEAMING_H_INCLUDED 1 #include "stream.h" -typedef int net_fd_t; -extern int asf_networking_start(net_fd_t* fd, networking_t *networking); -extern int asf_mmst_networking_start(net_fd_t* fd, networking_t *networking); +namespace mpxp { + class Tcp; +} +extern int asf_networking_start(Tcp& fd, networking_t *networking); +extern int asf_mmst_networking_start(Tcp& fd, networking_t *networking); #endif Modified: mplayerxp/libmpstream/cache2.cpp =================================================================== --- mplayerxp/libmpstream/cache2.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/cache2.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -271,7 +271,7 @@ int ss=stream->sector_size()>1?stream->sector_size():STREAM_BUFFER_SIZE; cache_vars_t* c; - if (!(stream->type()&STREAMTYPE_SEEKABLE) && stream->fd < 0) { + if (!(stream->type()&STREAMTYPE_SEEKABLE)) { // The stream has no 'fd' behind it, so is non-cacheable MSG_WARN("\rThis stream is non-cacheable\n"); return 1; Modified: mplayerxp/libmpstream/cddb.cpp =================================================================== --- mplayerxp/libmpstream/cddb.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/cddb.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -44,6 +44,7 @@ #include "libmpconf/cfgparser.h" #include "cdd.h" +#include "tcp.h" #include "version.h" #include "network.h" #include "stream_msg.h" @@ -142,7 +143,8 @@ int __FASTCALL__ cddb_http_request(const char *command, int (*reply_parser)(HTTP_header_t*,cddb_data_t*), cddb_data_t *cddb_data) { char request[4096]; - int fd, ret = 0; + int ret = 0; + Tcp* tcp; URL_t *url; HTTP_header_t *http_hdr; @@ -157,15 +159,16 @@ return -1; } - fd = http_send_request(cddb_data->libinput,url,0); - if( fd<0 ) { + tcp = http_send_request(cddb_data->libinput,url,0); + if( !tcp ) { MSG_ERR("failed to send the http request\n"); return -1; } - http_hdr = http_read_response( fd ); + http_hdr = http_read_response( *tcp ); if( http_hdr==NULL ) { MSG_ERR("Failed to read the http response\n"); + delete tcp; return -1; } @@ -185,6 +188,7 @@ http_free( http_hdr ); url_free( url ); + delete tcp; return ret; } Modified: mplayerxp/libmpstream/librtsp/rtsp.cpp =================================================================== --- mplayerxp/libmpstream/librtsp/rtsp.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/librtsp/rtsp.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -54,6 +54,7 @@ #include <sys/types.h> #include <inttypes.h> +#include "tcp.h" #include "rtsp.h" #include "rtsp_session.h" #include "osdep/timer.h" @@ -69,7 +70,7 @@ struct rtsp_s { - int s; + Tcp* tcp; char *host; int port; @@ -117,14 +118,14 @@ * network utilities */ -static int write_stream(int s, const char *buf, int len) { +static int write_stream(Tcp& tcp, const char *buf, int len) { int total, timeout; total = 0; timeout = 30; while (total < len){ int n; - n = send (s, &buf[total], len - total, 0); + n = tcp.write((uint8_t*)(&buf[total]), len - total); if (n > 0) total += n; @@ -143,7 +144,7 @@ return total; } -static ssize_t read_stream(int fd, any_t*buf, size_t count) { +static ssize_t read_stream(Tcp& tcp, any_t*buf, size_t count) { ssize_t ret, total; @@ -151,30 +152,13 @@ while (total < count) { - ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0); + ret=tcp.read(((uint8_t*)buf)+total, count-total); if (ret<0) { - if(errno == EAGAIN) { - fd_set rset; - struct timeval timeout; + if(errno == EAGAIN) if(!tcp.has_data(0)) return -1; + continue; + } else total += ret; - FD_ZERO (&rset); - FD_SET (fd, &rset); - - timeout.tv_sec = 30; - timeout.tv_usec = 0; - - if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) { - return -1; - } - continue; - } - - MSG_ERR("rtsp: read error.\n"); - return ret; - } else - total += ret; - /* end of stream */ if (!ret) break; } @@ -193,9 +177,9 @@ char *buffer = new char [BUF_SIZE]; char *string = NULL; - read_stream(s->s, buffer, 1); + read_stream(*s->tcp, buffer, 1); while (n<BUF_SIZE) { - read_stream(s->s, &(buffer[n]), 1); + read_stream(*s->tcp, &(buffer[n]), 1); if ((buffer[n-1]==0x0d)&&(buffer[n]==0x0a)) break; n++; } @@ -233,7 +217,7 @@ buf[len]=0x0d; buf[len+1]=0x0a; - write_stream(s->s, buf, len+2); + write_stream(*s->tcp, buf, len+2); #ifdef LOG MSG_INFO(" done.\n"); @@ -522,7 +506,7 @@ int i,seq; if (size>=4) { - i=read_stream(s->s, buffer, 4); + i=read_stream(*s->tcp, buffer, 4); if (i<4) return i; if (((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_')) || ((buffer[0]=='O')&&(buffer[1]=='P')&&(buffer[2]=='T')&&(buffer[3]=='I'))) // OPTIONS @@ -554,14 +538,14 @@ rtsp_put(s, rest); delete rest; rtsp_put(s, ""); - i=read_stream(s->s, buffer, size); + i=read_stream(*s->tcp, buffer, size); } else { - i=read_stream(s->s, buffer+4, size-4); + i=read_stream(*s->tcp, buffer+4, size-4); i+=4; } } else - i=read_stream(s->s, buffer, size); + i=read_stream(*s->tcp, buffer, size); #ifdef LOG MSG_INFO("librtsp: << %d of %d bytes\n", i, size); #endif @@ -574,7 +558,7 @@ */ //rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) { -rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) { +rtsp_t *rtsp_connect(Tcp& tcp, char* mrl, char *path, char *host, int port, char *user_agent) { rtsp_t *s=new rtsp_t; int i; @@ -604,9 +588,9 @@ path++; if ((s->param = strchr(s->path, '?')) != NULL) s->param++; - s->s = fd; + s->tcp = &tcp; - if (s->s < 0) { + if (!tcp.established()) { MSG_ERR("rtsp: failed to connect to '%s'\n", s->host); rtsp_close(s); return NULL; @@ -640,7 +624,7 @@ { if (s->server_state == RTSP_PLAYING) rtsp_request_teardown (s, NULL); - closesocket (s->s); + s->tcp->close(); } if (s->path) delete s->path; Modified: mplayerxp/libmpstream/librtsp/rtsp.h =================================================================== --- mplayerxp/libmpstream/librtsp/rtsp.h 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/librtsp/rtsp.h 2012-12-07 14:29:41 UTC (rev 516) @@ -47,7 +47,7 @@ typedef struct rtsp_s rtsp_t; -rtsp_t* rtsp_connect (int fd, char *mrl, char *path, char *host, int port, char *user_agent); +rtsp_t* rtsp_connect (Tcp& tcp, char *mrl, char *path, char *host, int port, char *user_agent); int rtsp_request_options(rtsp_t *s, const char *what); int rtsp_request_describe(rtsp_t *s, const char *what); Modified: mplayerxp/libmpstream/librtsp/rtsp_session.cpp =================================================================== --- mplayerxp/libmpstream/librtsp/rtsp_session.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/librtsp/rtsp_session.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -47,6 +47,7 @@ #include <string.h> #include <inttypes.h> +#include "tcp.h" #include "url.h" #include "rtp.h" #include "rtsp.h" @@ -77,7 +78,7 @@ }; //rtsp_session_t *rtsp_session_start(char *mrl) { -rtsp_session_t *rtsp_session_start(int fd, char **mrl, char *path, char *host, +rtsp_session_t *rtsp_session_start(Tcp& tcp, char **mrl, char *path, char *host, int port, int *redir, uint32_t bandwidth, char *user, char *pass) { rtsp_session_t *rtsp_session = NULL; @@ -94,7 +95,7 @@ *redir = 0; /* connect to server */ - rtsp_session->s=rtsp_connect(fd,*mrl,path,host,port,NULL); + rtsp_session->s=rtsp_connect(tcp,*mrl,path,host,port,NULL); if (!rtsp_session->s) { MSG_ERR("rtsp_session: failed to connect to server %s\n", path); @@ -266,8 +267,9 @@ else if (self->rtp_session) { int l = 0; + Tcp tcp(self->rtp_session->rtp_socket); - l = read_rtp_from_server (self->rtp_session->rtp_socket, data, len); + l = read_rtp_from_server (tcp, data, len); /* send RTSP and RTCP keepalive */ rtcp_send_rr (self->s, self->rtp_session); Modified: mplayerxp/libmpstream/librtsp/rtsp_session.h =================================================================== --- mplayerxp/libmpstream/librtsp/rtsp_session.h 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/librtsp/rtsp_session.h 2012-12-07 14:29:41 UTC (rev 516) @@ -33,7 +33,7 @@ typedef struct rtsp_session_s rtsp_session_t; -rtsp_session_t *rtsp_session_start(int fd, char **mrl, char *path, char *host, +rtsp_session_t *rtsp_session_start(Tcp& tcp, char **mrl, char *path, char *host, int port, int *redir, uint32_t bandwidth, char *user, char *pass); int rtsp_session_read(rtsp_session_t *session, char *data, int len); Modified: mplayerxp/libmpstream/network.cpp =================================================================== --- mplayerxp/libmpstream/network.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/network.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -166,11 +166,11 @@ return url_out; } -net_fd_t http_send_request(libinput_t* libinput, URL_t *url, off_t pos ) { +Tcp* http_send_request(libinput_t* libinput, URL_t *url, off_t pos ) { HTTP_header_t *http_hdr; URL_t *server_url; char str[256]; - net_fd_t fd=-1; + Tcp* tcp=NULL; int ret; int proxy = 0; // Boolean @@ -215,19 +215,17 @@ if( proxy ) { if( url->port==0 ) url->port = 8080; // Default port for the proxy server - fd = tcp_connect2Server(libinput, url->hostname, url->port, 0); + tcp = new Tcp(libinput, url->hostname, url->port, Tcp::IP4); url_free( server_url ); server_url = NULL; } else { if( server_url->port==0 ) server_url->port = 80; // Default port for the web server - fd = tcp_connect2Server(libinput, server_url->hostname, server_url->port, 0); + tcp = new Tcp(libinput, server_url->hostname, server_url->port, Tcp::IP4); } - if( fd<0 ) { - goto err_out; - } + if(!tcp->established()) goto err_out; MSG_DBG2("Request: [%s]\n", http_hdr->buffer ); - ret = send( fd, http_hdr->buffer, http_hdr->buffer_size, 0); + ret = tcp->write((uint8_t*)(http_hdr->buffer), http_hdr->buffer_size); if( ret!=(int)http_hdr->buffer_size ) { MSG_ERR("Error while sending HTTP request: didn't sent all the request\n"); goto err_out; @@ -235,17 +233,16 @@ http_free( http_hdr ); - return fd; + return tcp; err_out: - if (fd > 0) closesocket(fd); + if (tcp) delete tcp; http_free(http_hdr); if (proxy && server_url) url_free(server_url); - return -1; + return NULL; } -HTTP_header_t * -http_read_response( int fd ) { +HTTP_header_t* http_read_response( Tcp& tcp ) { HTTP_header_t *http_hdr; char response[BUFFER_SIZE]; int i; @@ -256,7 +253,7 @@ } do { - i = recv( fd, response, BUFFER_SIZE, 0); + i = tcp.read((uint8_t*)response, BUFFER_SIZE); if( i<0 ) { MSG_ERR("Read failed\n"); http_free( http_hdr ); @@ -324,46 +321,48 @@ return 0; } -off_t http_seek(net_fd_t* fd, networking_t *networking, off_t pos ) { - HTTP_header_t *http_hdr = NULL; +off_t http_seek(Tcp& tcp, networking_t *networking, off_t pos ) { + HTTP_header_t *http_hdr = NULL; + Tcp* other_tcp; - if( *fd>0 ) closesocket(*fd); // need to reconnect to seek in http-stream - *fd = http_send_request(networking->libinput, networking->url, pos ); - if( *fd<0 ) return 0; + tcp.close(); + other_tcp = http_send_request(networking->libinput, networking->url, pos ); + if(! other_tcp->established() ) return 0; + tcp=*other_tcp; + delete other_tcp; - http_hdr = http_read_response( *fd ); + http_hdr = http_read_response(tcp); - if( http_hdr==NULL ) return 0; + if( http_hdr==NULL ) return 0; - switch( http_hdr->status_code ) { - case 200: - case 206: // OK - MSG_V("Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); - MSG_V("Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); - if( http_hdr->body_size>0 ) { - if( networking_bufferize( networking, http_hdr->body, http_hdr->body_size )<0 ) { - http_free( http_hdr ); - return 0; - } - } - break; - default: - MSG_ERR("Server return %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); - close( *fd ); - *fd = -1; - } + switch( http_hdr->status_code ) { + case 200: + case 206: // OK + MSG_V("Content-Type: [%s]\n", http_get_field(http_hdr, "Content-Type") ); + MSG_V("Content-Length: [%s]\n", http_get_field(http_hdr, "Content-Length") ); + if( http_hdr->body_size>0 ) { + if( networking_bufferize( networking, http_hdr->body, http_hdr->body_size )<0 ) { + http_free( http_hdr ); + return 0; + } + } + break; + default: + MSG_ERR("Server return %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); + tcp.close(); + } - if( http_hdr ) { - http_free( http_hdr ); - networking->data = NULL; - } + if( http_hdr ) { + http_free( http_hdr ); + networking->data = NULL; + } - return pos; + return pos; } // By using the protocol, the extension of the file or the content-type // we might be able to guess the networking type. -int autodetectProtocol(networking_t *networking, net_fd_t *fd_out) { +int autodetectProtocol(networking_t *networking, Tcp& tcp) { HTTP_header_t *http_hdr=NULL; unsigned int i; int fd=-1; @@ -377,7 +376,6 @@ URL_t *url = networking->url; do { - *fd_out = -1; next_url = NULL; extension = NULL; content_type = NULL; @@ -401,12 +399,11 @@ #endif // HTTP based protocol if( !strcasecmp(url->protocol, "http") || !strcasecmp(url->protocol, "http_proxy") ) { - fd = http_send_request(networking->libinput, url, 0 ); - if( fd<0 ) goto err_out; + tcp = *http_send_request(networking->libinput, url, 0 ); + if(!tcp.established()) goto err_out; - http_hdr = http_read_response( fd ); + http_hdr = http_read_response(tcp); if( http_hdr==NULL ) goto err_out; - *fd_out=fd; if( mp_conf.verbose ) http_debug_hdr( http_hdr ); networking->data = (any_t*)http_hdr; @@ -525,7 +522,7 @@ } int -nop_networking_read( int fd, char *buffer, int size, networking_t *stream_ctrl ) { +nop_networking_read(Tcp& tcp, char *buffer, int size, networking_t *stream_ctrl ) { int len=0; //printf("nop_networking_read\n"); if( stream_ctrl->buffer_size!=0 ) { @@ -546,7 +543,7 @@ } if( len<size ) { int ret; - ret = read( fd, buffer+len, size-len ); + ret = tcp.read((uint8_t*)(buffer+len), size-len); if( ret<0 ) { MSG_ERR("nop_networking_read error : %s\n",strerror(errno)); } @@ -557,21 +554,24 @@ } int -nop_networking_seek( int fd, off_t pos, networking_t *stream_ctrl ) { +nop_networking_seek(Tcp& tcp, off_t pos, networking_t *n ) { + UNUSED(tcp); + UNUSED(pos); + UNUSED(n); return -1; } int -nop_networking_start(net_fd_t *fd,networking_t* networking ) { +nop_networking_start(Tcp& tcp,networking_t* networking ) { HTTP_header_t *http_hdr = NULL; char *next_url=NULL; URL_t *rd_url=NULL; int ret; - if( *fd<0 ) { - *fd = http_send_request(networking->libinput, networking->url,0); - if( *fd<0 ) return -1; - http_hdr = http_read_response( *fd ); + if( !tcp.established() ) { + tcp = *http_send_request(networking->libinput, networking->url,0); + if( !tcp.established() ) return -1; + http_hdr = http_read_response(tcp); if( http_hdr==NULL ) return -1; switch( http_hdr->status_code ) { @@ -597,11 +597,10 @@ if (next_url != NULL && rd_url != NULL) { MSG_STATUS("Redirected: Using this url instead %s\n",next_url); networking->url=check4proxies(rd_url); - ret=nop_networking_start(fd,networking); //recursively get networking started + ret=nop_networking_start(tcp,networking); //recursively get networking started } else { MSG_ERR("Redirection failed\n"); - closesocket( *fd ); - *fd = -1; + tcp.close(); } return ret; break; @@ -611,8 +610,7 @@ case 500: //Server Error default: MSG_ERR("Server return %d: %s\n", http_hdr->status_code, http_hdr->reason_phrase ); - closesocket( *fd ); - *fd = -1; + tcp.close(); return -1; break; } @@ -653,20 +651,20 @@ } int -pnm_networking_read( int fd, char *buffer, int size, networking_t *stream_ctrl ) { +pnm_networking_read(Tcp& tcp, char *buffer, int size, networking_t *stream_ctrl ) { + UNUSED(tcp); return pnm_read(reinterpret_cast<pnm_t*>(stream_ctrl->data), buffer, size); } -int pnm_networking_start(net_fd_t *fd,networking_t *networking ) { +int pnm_networking_start(Tcp& tcp,networking_t *networking ) { pnm_t *pnm; - *fd = tcp_connect2Server(networking->libinput, networking->url->hostname, - networking->url->port ? networking->url->port : 7070, 0); - MSG_V("PNM:// fd=%d\n",fd); - if(*fd<0) return -1; + tcp.open(networking->libinput, networking->url->hostname, + networking->url->port ? networking->url->port : 7070); + if(!tcp.established()) return -1; - pnm = pnm_connect(fd,networking->url->file); + pnm = pnm_connect(tcp,networking->url->file); if(!pnm) return -2; networking->data=pnm; @@ -737,16 +735,18 @@ #ifndef STREAMING_LIVE_DOT_COM static int -rtp_networking_read( int fd, char *buffer, int size, networking_t *networking ) { - return read_rtp_from_server( fd, buffer, size ); +rtp_networking_read(Tcp& tcp, char *buffer, int size, networking_t *networking ) { + UNUSED(networking); + return read_rtp_from_server(tcp, buffer, size ); } static int -rtp_networking_start( net_fd_t* fd,networking_t* networking, int raw_udp ) { +rtp_networking_start(Tcp& tcp,networking_t* networking, int raw_udp ) { - if( *fd<0 ) { - *fd = udp_open_socket( (networking->url) ); - if( *fd<0 ) return -1; + if( !tcp.established() ) { + Udp* udp(new(zeromem) Udp(networking->url)); + tcp = udp->socket(); + if( !tcp.established()) return -1; } if(raw_udp) @@ -762,12 +762,12 @@ } #endif -int networking_start(net_fd_t* fd,networking_t* networking, URL_t *url) { +int networking_start(Tcp& tcp,networking_t* networking, URL_t *url) { int ret; networking->url = check4proxies( url ); - ret = autodetectProtocol( networking, fd); + ret = autodetectProtocol( networking, tcp); if( ret<0 ) return -1; ret = -1; @@ -777,15 +777,11 @@ // For RTP streams, we usually don't know the stream type until we open it. if( !strcasecmp( networking->url->protocol, "rtp")) { - if(*fd >= 0) { - if(closesocket(*fd) < 0) - MSG_ERR("networking_start : Closing socket %d failed %s\n",*fd,strerror(errno)); - } - *fd = -1; - ret = rtp_networking_start(fd, networking, 0); + if(tcp.established()) tcp.close(); + ret = rtp_networking_start(tcp, networking, 0); } else if( !strcasecmp( networking->url->protocol, "pnm")) { - *fd = -1; - ret = pnm_networking_start(fd, networking); + tcp.close(); + ret = pnm_networking_start(tcp, networking); if (ret == -1) { MSG_INFO("Can't connect with pnm, retrying with http.\n"); return -1; @@ -794,10 +790,10 @@ #ifdef HAVE_RTSP_SESSION_H else if( !strcasecmp( networking->url->protocol, "rtsp")) { *fd = -1; - if ((ret = realrtsp_networking_start( fd, networking )) < 0) { + if ((ret = realrtsp_networking_start( tcp, networking )) < 0) { MSG_INFO("Not a Realmedia rtsp url. Trying standard rtsp protocol.\n"); #ifdef STREAMING_LIVE_DOT_COM - ret = rtsp_networking_start( stream ); + ret = rtsp_networking_start( tcp, networking ); if( ret<0 ) MSG_ERR("rtsp_networking_start failed\n"); return ret; #else @@ -808,8 +804,8 @@ } #endif else if(!strcasecmp( networking->url->protocol, "udp")) { - *fd = -1; - ret = rtp_networking_start(fd, networking, 1); + tcp.close(); + ret = rtp_networking_start(tcp, networking, 1); if(ret<0) { MSG_ERR("rtp_networking_start(udp) failed\n"); return -1; @@ -820,16 +816,15 @@ // ASF raw stream is encapsulated. // It can also be a playlist (redirector) // so we need to pass demuxer_type too - ret = asf_networking_start(fd,networking); + ret = asf_networking_start(tcp,networking); if( ret<0 ) { //sometimes a file is just on a webserver and it is not streamed. //try loading them default method as last resort for http protocol if ( !strcasecmp(networking->url->protocol, "http") ) { MSG_STATUS("Trying default networking for http protocol\n "); //reset stream - close(*fd); - *fd=-1; - ret=nop_networking_start(fd,networking); + tcp.close(); + ret=nop_networking_start(tcp,networking); } if (ret<0) { MSG_ERR("asf_networking_start failed\n"); Modified: mplayerxp/libmpstream/network.h =================================================================== --- mplayerxp/libmpstream/network.h 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/network.h 2012-12-07 14:29:41 UTC (rev 516) @@ -28,8 +28,8 @@ namespace mpxp { struct stream_t; struct libinput_t; + class Tcp; } -typedef int net_fd_t; enum networking_status { networking_stopped_e, networking_playing_e @@ -44,24 +44,24 @@ unsigned int buffer_size; unsigned int buffer_pos; unsigned int bandwidth; // The downstream available - int (*networking_read)( net_fd_t fd, char *buffer, int buffer_size, networking_t *stream_ctrl ); - int (*networking_seek)( net_fd_t fd, off_t pos, networking_t *stream_ctrl ); + int (*networking_read)( Tcp& fd, char *buffer, int buffer_size, networking_t *stream_ctrl ); + int (*networking_seek)( Tcp& fd, off_t pos, networking_t *stream_ctrl ); any_t*data; libinput_t* libinput; /**< provides possibility to inperrupt network streams */ }; extern void fixup_network_stream_cache(networking_t *s); -extern int networking_start(net_fd_t* fd,networking_t *n, URL_t *url); +extern int networking_start(Tcp& fd,networking_t *n, URL_t *url); extern int networking_bufferize(networking_t *networking,unsigned char *buffer, int size); extern networking_t *new_networking(libinput_t* libinput); extern void free_networking( networking_t *networking ); extern URL_t* check4proxies( URL_t *url ); -int nop_networking_read( net_fd_t fd, char *buffer, int size, networking_t *stream_ctrl ); -int nop_networking_seek( net_fd_t fd, off_t pos, networking_t *stream_ctrl ); +int nop_networking_read(Tcp& fd, char *buffer, int size, networking_t *stream_ctrl ); +int nop_networking_seek(Tcp& fd, off_t pos, networking_t *stream_ctrl ); -int http_send_request(libinput_t* libinput,URL_t *url, off_t pos); -HTTP_header_t *http_read_response(net_fd_t fd); +Tcp* http_send_request(libinput_t* libinput,URL_t *url, off_t pos); +HTTP_header_t *http_read_response(Tcp& fd); int http_authenticate(HTTP_header_t *http_hdr, URL_t *url, int *auth_retry); Modified: mplayerxp/libmpstream/pnm.cpp =================================================================== --- mplayerxp/libmpstream/pnm.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/pnm.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -47,6 +47,7 @@ #include <winsock2.h> #endif +#include "tcp.h" #include "pnm.h" #define FOURCC_TAG( ch0, ch1, ch2, ch3 ) \ @@ -72,7 +73,7 @@ #define HEADER_SIZE 4096 struct pnm_s { - net_fd_t s; + Tcp* tcp; char* path; char buffer[BUF_SIZE]; /* scratch buffer */ /* receive buffer */ @@ -94,11 +95,11 @@ * utility macros */ -#define BE_16(x) be2me_16(*((uint16_t *)x)) -#define BE_32(x) be2me_16(*((uint32_t *)x)) +static inline uint16_t BE_16(uint8_t* x) { return be2me_16(*((uint16_t *)x)); } +static inline uint32_t BE_32(uint8_t* x) { return be2me_32(*((uint32_t *)x)); } /* D means direct (no pointer) */ -#define BE_16D(x) BE_16(x) +static inline uint16_t BE_16D(uint8_t* x) { return BE_16(x); } /* sizes */ #define PREAMBLE_SIZE 8 @@ -194,14 +195,14 @@ static void hexdump (char *buf, int length); -static int rm_write(int s, const char *buf, int len) { +static int rm_write(Tcp& tcp, const char *buf, int len) { int total, timeout; total = 0; timeout = 30; while (total < len){ int n; - n = send (s, &buf[total], len - total, 0); + n = tcp.write ((const uint8_t*)&buf[total], len - total); if (n > 0) total += n; @@ -220,7 +221,7 @@ return total; } -static ssize_t rm_read(int fd, any_t*buf, size_t count) { +static ssize_t rm_read(Tcp& tcp, any_t*buf, size_t count) { ssize_t ret, total; @@ -228,21 +229,8 @@ while (total < count) { - fd_set rset; - struct timeval timeout; + ret=tcp.read (((uint8_t*)buf)+total, count-total); - FD_ZERO (&rset); - FD_SET (fd, &rset); - - timeout.tv_sec = 3; - timeout.tv_usec = 0; - - if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) { - return -1; - } - - ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0); - if (ret<=0) { printf ("input_pnm: read error.\n"); return ret; @@ -306,16 +294,16 @@ return -1; /* get first PREAMBLE_SIZE bytes and ignore checksum */ - rm_read (p->s, data, CHECKSUM_SIZE); + rm_read (*p->tcp, data, CHECKSUM_SIZE); if (data[0] == 0x72) - rm_read (p->s, data, PREAMBLE_SIZE); + rm_read (*p->tcp, data, PREAMBLE_SIZE); else - rm_read (p->s, data+CHECKSUM_SIZE, PREAMBLE_SIZE-CHECKSUM_SIZE); + rm_read (*p->tcp, data+CHECKSUM_SIZE, PREAMBLE_SIZE-CHECKSUM_SIZE); max -= PREAMBLE_SIZE; - *chunk_type = BE_32(data); - chunk_size = BE_32(data+4); + *chunk_type = BE_32((uint8_t*)data); + chunk_size = BE_32((uint8_t*)(data+4)); switch (*chunk_type) { case PNA_TAG: @@ -323,7 +311,7 @@ ptr=data+PREAMBLE_SIZE; if (max < 1) return -1; - rm_read (p->s, ptr++, 1); + rm_read (*p->tcp, ptr++, 1); max -= 1; while(1) { @@ -331,19 +319,19 @@ if (max < 2) return -1; - rm_read (p->s, ptr, 2); + rm_read (*p->tcp, ptr, 2); max -= 2; if (*ptr == 'X') /* checking for server message */ { printf("input_pnm: got a message from server:\n"); if (max < 1) return -1; - rm_read (p->s, ptr+2, 1); + rm_read (*p->tcp, ptr+2, 1); max = -1; - n=BE_16(ptr+1); + n=BE_16((uint8_t*)(ptr+1)); if (max < n) return -1; - rm_read (p->s, ptr+3, n); + rm_read (*p->tcp, ptr+3, n); max -= n; ptr[3+n]=0; printf("%s\n",ptr+3); @@ -365,14 +353,14 @@ n=ptr[1]; if (max < n) return -1; - rm_read (p->s, ptr+2, n); + rm_read (*p->tcp, ptr+2, n); max -= n; ptr+=(n+2); } /* the checksum of the next chunk is ignored here */ if (max < 1) return -1; - rm_read (p->s, ptr+2, 1); + rm_read (*p->tcp, ptr+2, 1); ptr+=3; chunk_size=ptr-data; break; @@ -384,12 +372,12 @@ if (chunk_size > max || chunk_size < PREAMBLE_SIZE) { printf("error: max chunk size exeeded (max was 0x%04x)\n", max); #ifdef LOG - n=rm_read (p->s, &data[PREAMBLE_SIZE], 0x100 - PREAMBLE_SIZE); + n=rm_read (*p->tcp, &data[PREAMBLE_SIZE], 0x100 - PREAMBLE_SIZE); hexdump(data,n+PREAMBLE_SIZE); #endif return -1; } - rm_read (p->s, &data[PREAMBLE_SIZE], chunk_size-PREAMBLE_SIZE); + rm_read (*p->tcp, &data[PREAMBLE_SIZE], chunk_size-PREAMBLE_SIZE); break; default: *chunk_type = 0; @@ -457,7 +445,7 @@ /* client id string */ p->buffer[c]=PNA_CLIENT_STRING; - i16=BE_16D((strlen(client_string)-1)); /* dont know why do we have -1 here */ + i16=BE_16D((uint8_t*)((strlen(client_string)-1))); /* dont know why do we have -1 here */ memcpy(&p->buffer[c+1],&i16,2); memcpy(&p->buffer[c+3],client_string,strlen(client_string)+1); c=c+3+strlen(client_string)+1; @@ -465,7 +453,7 @@ /* file path */ p->buffer[c]=0; p->buffer[c+1]=PNA_PATH_REQUEST; - i16=BE_16D(strlen(p->path)); + i16=BE_16D((uint8_t*)(strlen(p->path))); memcpy(&p->buffer[c+2],&i16,2); memcpy(&p->buffer[c+4],p->path,strlen(p->path)); c=c+4+strlen(p->path); @@ -474,7 +462,7 @@ p->buffer[c]='y'; p->buffer[c+1]='B'; - rm_write(p->s,p->buffer,c+2); + rm_write(*p->tcp,p->buffer,c+2); } /* @@ -491,7 +479,7 @@ memcpy(&p->buffer[3], response, size); - rm_write (p->s, p->buffer, size+3); + rm_write (*p->tcp, p->buffer, size+3); } @@ -554,7 +542,7 @@ /* read challenge */ memcpy (p->buffer, ptr, PREAMBLE_SIZE); - rm_read (p->s, &p->buffer[PREAMBLE_SIZE], 64); + rm_read (*p->tcp, &p->buffer[PREAMBLE_SIZE], 64); /* now write a data header */ memcpy(ptr, pnm_data_header, PNM_DATA_HEADER_SIZE); @@ -650,7 +638,7 @@ /* realplayer seems to do that every 43th package */ if ((p->packet%43) == 42) { - rm_write(p->s,&keepalive,1); + rm_write(*p->tcp,&keepalive,1); } /* data chunks begin with: 'Z' <o> <o> <i1> 'Z' <i2> @@ -659,13 +647,13 @@ * <i2> is a 8 bit index which counts from 0x10 to somewhere */ - n = rm_read (p->s, p->buffer, 8); + n = rm_read (*p->tcp, p->buffer, 8); if (n<8) return 0; /* skip 8 bytes if 0x62 is read */ if (p->buffer[0] == 0x62) { - n = rm_read (p->s, p->buffer, 8); + n = rm_read (*p->tcp, p->buffer, 8); if (n<8) return 0; #ifdef LOG printf("input_pnm: had to seek 8 bytes on 0x62\n"); @@ -675,9 +663,9 @@ /* a server message */ if (p->buffer[0] == 'X') { - int size=BE_16(&p->buffer[1]); + int size=BE_16((uint8_t*)(&p->buffer[1])); - rm_read (p->s, &p->buffer[8], size-5); + rm_read (*p->tcp, &p->buffer[8], size-5); p->buffer[size+3]=0; printf("input_pnm: got message from server while reading stream:\n%s\n", &p->buffer[3]); return 0; @@ -698,7 +686,7 @@ for (i=1; i<8; i++) { p->buffer[i-1]=p->buffer[i]; } - rm_read (p->s, &p->buffer[7], 1); + rm_read (*p->tcp, &p->buffer[7], 1); n++; } @@ -715,8 +703,8 @@ } /* check offsets */ - fof1=BE_16(&p->buffer[1]); - fof2=BE_16(&p->buffer[3]); + fof1=BE_16((uint8_t*)(&p->buffer[1])); + fof2=BE_16((uint8_t*)(&p->buffer[3])); if (fof1 != fof2) { printf("input_pnm: frame offsets are different: 0x%04x 0x%04x\n",fof1,fof2); @@ -724,10 +712,10 @@ } /* get first index */ - p->seq_current[0]=BE_16(&p->buffer[5]); + p->seq_current[0]=BE_16((uint8_t*)(&p->buffer[5])); /* now read the rest of stream chunk */ - n = rm_read (p->s, &p->recv[5], fof1-5); + n = rm_read (*p->tcp, &p->recv[5], fof1-5); if (n<(fof1-5)) return 0; /* get second index */ @@ -747,7 +735,7 @@ p->recv[0]=0; /* object version */ p->recv[1]=0; - fof2=BE_16(&fof2); + fof2=BE_16((uint8_t*)(&fof2)); memcpy(&p->recv[2], &fof2, 2); /*p->recv[2]=(fof2>>8)%0xff;*/ /* length */ /*p->recv[3]=(fof2)%0xff;*/ @@ -765,13 +753,13 @@ } // pnm_t *pnm_connect(const char *mrl) { -pnm_t *pnm_connect(int* fd,const char *path) { +pnm_t *pnm_connect(Tcp& tcp,const char *path) { pnm_t *p=new pnm_t; int need_response=0; p->path=mp_strdup(path); - p->s=*fd; + p->tcp=&tcp; pnm_send_request(p,pnm_available_bandwidths[10]); if (!pnm_get_headers(p, &need_response)) { @@ -836,8 +824,9 @@ void pnm_close(pnm_t *p) { - if (p->s >= 0) closesocket(p->s); + if (p->tcp->established()) p->tcp->close(); delete p->path; + delete p->tcp; delete p; } Modified: mplayerxp/libmpstream/pnm.h =================================================================== --- mplayerxp/libmpstream/pnm.h 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/pnm.h 2012-12-07 14:29:41 UTC (rev 516) @@ -31,9 +31,11 @@ /*#include "xine_internal.h" */ typedef struct pnm_s pnm_t; -typedef int net_fd_t; +namespace mpxp { + class Tcp; +} -pnm_t* pnm_connect (net_fd_t* fd,const char *url); +pnm_t* pnm_connect (Tcp&,const char *url); int pnm_read (pnm_t *self, char *data, int len); void pnm_close (pnm_t *self); Modified: mplayerxp/libmpstream/rtp.cpp =================================================================== --- mplayerxp/libmpstream/rtp.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/rtp.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -31,6 +31,7 @@ /* MPEG-2 TS RTP stack */ #define DEBUG 1 +#include "tcp.h" #include "rtp.h" #include "stream_msg.h" @@ -67,7 +68,7 @@ }; static struct rtpbuffer rtpbuf; -static int getrtp2(int fd, struct rtpheader *rh, char** data, int* lengthData); +static int getrtp2(Tcp& fd, struct rtpheader *rh, char** data, int* lengthData); // RTP Reordering functions // Algorithm works as follows: @@ -90,7 +91,7 @@ } // Write in a cache the rtp packet in right rtp sequence order -static int rtp_cache(int fd, char *buffer, int length) +static int rtp_cache(Tcp& tcp, char *buffer, int length) { struct rtpheader rh; int newseq; @@ -98,7 +99,7 @@ unsigned short seq; static int is_first = 1; - getrtp2(fd, &rh, &data, &length); + getrtp2(tcp, &rh, &data, &length); if(!length) return 0; seq = rh.b.sequence; @@ -159,7 +160,7 @@ // Get next packet in cache // Look in cache to get first packet in sequence -static int rtp_get_next(int fd, char *buffer, int length) +static int rtp_get_next(Tcp& tcp, char *buffer, int length) { int i; unsigned short nextseq; @@ -168,7 +169,7 @@ for (i=0; i < MAXRTPPACKETSIN -3; i++) { if (rtpbuf.len[rtpbuf.first] != 0) break; - length = rtp_cache(fd, buffer, length) ; + length = rtp_cache(tcp, buffer, length) ; // returns on first packet in sequence if (length > 0) { @@ -202,7 +203,7 @@ // Read next rtp packet using cache -int read_rtp_from_server(net_fd_t fd, char *buffer, int length) { +int read_rtp_from_server(Tcp& tcp, char *buffer, int length) { // Following test is ASSERT (i.e. uneuseful if code is correct) if(buffer==NULL || length<STREAM_BUFFER_SIZE) { MSG_ERR("RTP buffer invalid; no data return from network\n"); @@ -210,20 +211,20 @@ } // loop just to skip empty packets - while ((length = rtp_get_next(fd, buffer, length)) == 0) { + while ((length = rtp_get_next(tcp, buffer, length)) == 0) { MSG_ERR("Got empty packet from RTP cache!?\n"); } return(length); } -static int getrtp2(int fd, struct rtpheader *rh, char** data, int* lengthData) { +static int getrtp2(Tcp& tcp, struct rtpheader *rh, char** data, int* lengthData) { static char buf[1600]; unsigned int intP; char* charP = (char*) &intP; int headerSize; int lengthPacket; - lengthPacket=recv(fd,buf,1590,0); + lengthPacket=tcp.read((uint8_t*)(buf),1590); if (lengthPacket<0) MSG_ERR("rtp: socket read error\n"); else if (lengthPacket<12) Modified: mplayerxp/libmpstream/rtp.h =================================================================== --- mplayerxp/libmpstream/rtp.h 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/rtp.h 2012-12-07 14:29:41 UTC (rev 516) @@ -9,6 +9,6 @@ #define RTP_H #include "network.h" -int read_rtp_from_server(net_fd_t fd, char *buffer, int length); +int read_rtp_from_server(Tcp& fd, char *buffer, int length); #endif Modified: mplayerxp/libmpstream/s_ftp.cpp =================================================================== --- mplayerxp/libmpstream/s_ftp.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/s_ftp.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -40,7 +40,6 @@ virtual off_t size() const; virtual off_t sector_size() const; private: - static int fd_can_read(int fd,int timeout); int readline(char *buf,int max); int readresp(char* rsp); int OpenPort(); @@ -56,17 +55,16 @@ libinput_t* libinput; char* cput,*cget; - net_fd_t handle; + Tcp tcp; int cavail,cleft; char* buf; off_t spos; off_t file_len; }; -Ftp_Stream_Interface::Ftp_Stream_Interface() {} +Ftp_Stream_Interface::Ftp_Stream_Interface():tcp(-1) {} Ftp_Stream_Interface::~Ftp_Stream_Interface() { SendCmd("QUIT",NULL); - if(handle) ::closesocket(handle); if(buf) delete buf; } @@ -76,20 +74,6 @@ #define TELNET_IP 244 /* interrupt process--permanently */ #define TELNET_SYNCH 242 /* for telfunc calls */ -// Check if there is something to read on a fd. This avoid hanging -// forever if the network stop responding. -int Ftp_Stream_Interface::fd_can_read(int fd,int timeout) { - fd_set fds; - struct timeval tv; - - FD_ZERO(&fds); - FD_SET(fd,&fds); - tv.tv_sec = timeout; - tv.tv_usec = 0; - - return (::select(fd+1, &fds, NULL, NULL, &tv) > 0); -} - /* * read a line of text * @@ -135,12 +119,12 @@ if (retval == 0) retval = -1; break; } - if(!fd_can_read(handle, 15)) { + if(!tcp.has_data(15)) { MSG_ERR("[ftp] read timed out\n"); retval = -1; break; } - if ((x = recv(handle,cput,cleft,0)) == -1) { + if ((x = tcp.read((uint8_t*)cput,cleft)) == -1) { MSG_ERR("[ftp] read error: %s\n",strerror(errno)); retval = -1; break; @@ -195,7 +179,7 @@ if(hascrlf && l == 2) MSG_V("\n"); else MSG_V("[ftp] > %s",cmd); while(l > 0) { - int s = ::send(handle,cmd,l,0); + int s = tcp.write((const uint8_t*)cmd,l); if(s <= 0) { MSG_ERR("[ftp] write error: %s\n",strerror(errno)); return 0; @@ -226,10 +210,10 @@ } sscanf(par+1,"%u,%u,%u,%u,%u,%u",&num[0],&num[1],&num[2],&num[3],&num[4],&num[5]); snprintf(str,127,"%d.%d.%d.%d",num[0],num[1],num[2],num[3]); - fd = tcp_connect2Server(libinput,str,(num[4]<<8)+num[5],0); + tcp.open(libinput,str,(num[4]<<8)+num[5]); if(fd < 0) MSG_ERR("[ftp] failed to create data connection\n"); - return fd; + return 1; } int Ftp_Stream_Interface::OpenData(size_t newpos) { @@ -237,9 +221,9 @@ char str[256],rsp_txt[256]; // Open a new connection - handle = OpenPort(); + OpenPort(); - if(handle < 0) return 0; + if(tcp.established()) return 0; if(newpos > 0) { snprintf(str,255,"REST %"PRId64, (int64_t)newpos); @@ -266,12 +250,12 @@ if(!OpenData(spos)) return -1; - if(!fd_can_read(handle, 15)) { + if(!tcp.has_data(15)) { MSG_ERR("[ftp] read timed out\n"); return -1; } MSG_V("ftp read: %u bytes\n",sp->len); - r = ::recv(handle,sp->buf,sp->len,0); + r = tcp.read((uint8_t*)sp->buf,sp->len); spos+=r; return (r <= 0) ? -1 : r; } @@ -285,25 +269,21 @@ if(spos > file_len) return 0; // Check to see if the server did not already terminate the transfer - if(fd_can_read(handle, 0)) { + if(tcp.has_data(0)) { if(readresp(rsp_txt) != 2) MSG_WARN("[ftp] Warning the server didn't finished the transfer correctly: %s\n",rsp_txt); - ::closesocket(handle); - handle = -1; + tcp.close(); } // Close current download - if(handle >= 0) { + if(tcp.established()) { static const char pre_cmd[]={TELNET_IAC,TELNET_IP,TELNET_IAC,TELNET_SYNCH}; //int fl; - // First close the fd - ::closesocket(handle); - handle = -1; // Send send the telnet sequence needed to make the server react // send only first byte as OOB due to OOB braindamage in many unices - ::send(handle,pre_cmd,1,MSG_OOB); - ::send(handle,pre_cmd+1,sizeof(pre_cmd)-1,0); + tcp.write((uint8_t*)pre_cmd,1,MSG_OOB); + tcp.write((uint8_t*)(pre_cmd+1),sizeof(pre_cmd)-1); // Get the 426 Transfer aborted // Or the 226 Transfer complete @@ -315,6 +295,8 @@ // Send the ABOR command // Ignore the return code as sometimes it fail with "nothing to abort" SendCmd("ABOR",rsp_txt); + // close the fd + tcp.close(); } if(OpenData(newpos)) spos=newpos; return spos; @@ -327,10 +309,7 @@ void Ftp_Stream_Interface::close() { - if(handle > 0) { - ::closesocket(handle); - handle = 0; - } + if(tcp.established()) tcp.close(); } MPXP_Rc Ftp_Stream_Interface::open(libinput_t*_libinput,const char *_filename,unsigned flags) @@ -360,9 +339,9 @@ MSG_V("FTP: Opening ~%s :%s @%s :%i %s\n",user,pass,host,port,filename); // Open the control connection - handle = tcp_connect2Server(libinput,host,port,1); + tcp.open(libinput,host,port); - if(handle < 0) { + if(!tcp.established()) { url_free(url); return MPXP_False; } Modified: mplayerxp/libmpstream/s_network.cpp =================================================================== --- mplayerxp/libmpstream/s_network.cpp 2012-12-06 16:46:40 UTC (rev 515) +++ mplayerxp/libmpstream/s_network.cpp 2012-12-07 14:29:41 UTC (rev 516) @@ -17,6 +17,7 @@ #include "stream_msg.h" #include "url.h" +#include "tcp.h" #include "network.h" namespace mpxp { @@ -37,11 +38,11 @@ private: URL_t* url; off_t spos; - net_fd_t fd; + Tcp tcp; networking_t* networking; }; -Network_Stream_Interface::Network_Stream_Interface() {} +Network_Stream_Interface::Network_Stream_Interface():tcp(-1) {} Network_Stream_Interface::~Network_Stream_Interface() { if(url) { url_free(url); @@ -55,7 +56,7 @@ url = url_new(filename); if(url) { networking=new_networking(libinput); - if(networki... [truncated message content] |