int http_get_file(int sockfd,char* path,int file_length,char* file_buf) { int i,j,count; char range[64]; int ret = 0; char* p = NULL; count = (file_length % MAX_RECV_SIZE) ? ( file_length / MAX_RECV_SIZE +1 ) : (file_length / MAX_RECV_SIZE); D(printf("File Size:%d Seg Count:%d\n",file_length,count)); for ( i = 0; i < count; ++i ) { D(printf("-------------Seg:%d\n",i)); if( (i == (count-1)) && ( file_length % MAX_RECV_SIZE ) ) sprintf(range,"%d-%d",i*MAX_RECV_SIZE,file_length-1); else sprintf(range,"%d-%d",i*MAX_RECV_SIZE,(i+1)*MAX_RECV_SIZE-1); head_to_get_file(path,range); D(printf("send : \n%s",buf_send)); socket_send(sockfd,buf_send,strlen(buf_send),0); memset(buf_recv,0,sizeof(buf_recv)); ret = http_recv(sockfd,buf_recv); if ( ret < 0 ) break; //disconnect if ( ret == 0 ) { } p = strstr(buf_recv,"\r\n\r\n"); if ( p == NULL ) { printf("ERROR: recv_buf not contain end flag!"); break; } else { memcpy(file_buf+j*MAX_RECV_SIZE,p+4,MAX_RECV_SIZE); j++; } } if( i == count ) return file_length; else return -1; }
static void *http_thread(void *ptr) { slimaudio_t *audio = (slimaudio_t *) ptr; #ifdef SLIMPROTO_DEBUG int last_state = 0; #endif #ifdef BSD_THREAD_LOCKING pthread_mutex_lock(&audio->http_mutex); #endif audio->http_state = STREAM_STOPPED; while (true) { #ifdef SLIMPROTO_DEBUG if (last_state == audio->http_state) { VDEBUGF("http_thread state %i\n", audio->http_state); } else { DEBUGF("http_thread state %i\n", audio->http_state); } last_state = audio->http_state; #endif switch (audio->http_state) { case STREAM_STOP: CLOSESOCKET(audio->streamfd); slimproto_dsco(audio->proto, DSCO_CLOSED); slimaudio_buffer_close(audio->decoder_buffer); audio->http_state = STREAM_STOPPED; pthread_cond_broadcast(&audio->http_cond); break; case STREAM_STOPPED: pthread_cond_wait(&audio->http_cond, &audio->http_mutex); break; case STREAM_PLAYING: pthread_mutex_unlock(&audio->http_mutex); http_recv(audio); pthread_mutex_lock(&audio->http_mutex); break; case STREAM_QUIT: return 0; } } pthread_mutex_unlock(&audio->http_mutex); }
static void *http_thread(void *ptr) { slimaudio_t *audio = (slimaudio_t *) ptr; #ifdef SLIMPROTO_DEBUG int last_state = 0; #endif #ifdef RENICE if ( renice ) if ( renice_thread (5) ) /* Lower thread priority to give precedence to the decoder */ fprintf(stderr, "http_thread: renice failed.\n"); #endif #ifdef BSD_THREAD_LOCKING pthread_mutex_lock(&audio->http_mutex); #endif audio->http_state = STREAM_STOPPED; while ( audio->http_state != STREAM_QUIT ) { #ifdef SLIMPROTO_DEBUG if (last_state == audio->http_state) { VDEBUGF("http_thread state %i\n", audio->http_state); } else { DEBUGF("http_thread state %i\n", audio->http_state); } last_state = audio->http_state; #endif switch (audio->http_state) { case STREAM_STOP: CLOSESOCKET(audio->streamfd); slimproto_dsco(audio->proto, DSCO_CLOSED); slimaudio_buffer_close(audio->decoder_buffer); audio->http_state = STREAM_STOPPED; pthread_cond_broadcast(&audio->http_cond); break; case STREAM_STOPPED: pthread_cond_wait(&audio->http_cond, &audio->http_mutex); break; case STREAM_PLAYING: pthread_mutex_unlock(&audio->http_mutex); http_recv(audio); pthread_mutex_lock(&audio->http_mutex); break; case STREAM_QUIT: break; } } pthread_mutex_unlock(&audio->http_mutex); return 0; }
void start_server(void) { int pid, c_dsc; struct sockaddr_in s_sin, c_sin; socklen_t s_len, c_len; http_in_t* req; s_len = sizeof(s_sin); c_len = sizeof(c_sin); s_sin.sin_family = AF_INET; s_sin.sin_port = htons(conf.port); s_sin.sin_addr.s_addr = htonl(INADDR_ANY); /* Creates socket */ if((s_dsc = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { log_critical("Failed to create socket: %s", strerror(errno)); remove_pid(); return; } /* Binds socket */ if(bind(s_dsc, (struct sockaddr*)&s_sin, s_len) < 0) { log_critical("Failed to bind: %s", strerror(errno)); remove_pid(); return; } /* Listens */ if(listen(s_dsc, SOMAXCONN) < 0) { log_critical("Failed to listen: %s", strerror(errno)); remove_pid(); return; } /* Infinite loop */ while(1) { if((c_dsc = accept(s_dsc, (struct sockaddr*)&c_sin, &c_len)) < 0) { log_error("Failed to accept: %s", strerror(errno)); continue; } pid = fork(); switch(pid) { case -1: /* Error */ log_error("Failed to fork: %s", strerror(errno)); break; case 0: /* Child process */ close(s_dsc); log_debug("Connection by %s:%d", inet_ntoa(c_sin.sin_addr), htons(c_sin.sin_port)); /* Receives HTTP request */ req = http_recv(c_dsc); /* Sends HTTP response */ http_send(c_dsc, req); close(c_dsc); exit(EXIT_SUCCESS); default: /* Father process */ close(c_dsc); } } }