void webif_data(uint8_t id, eth_frame_t *frame, uint16_t len) { ip_packet_t *ip = (void*)(frame->data); tcp_packet_t *tcp = (void*)(ip->data); char *req = (void*)tcp_get_data(tcp); char *buf = (void*)(tcp->data), *buf_ptr = buf; char *url, *p, *params, *name, *value; if(!len) return; if( (memcmp_P(req, PSTR("GET "), 4) == 0) && ((p = strchr(req + 4, ' ')) != 0) ) { url = req + 4; *p = 0; if((params = strchr(url, '?'))) *(params++) = 0; if(strcmp_P(url, PSTR("/")) == 0) { if(params==NULL) { send_Uart_str(" No params!"); send_Uart(13); }else { send_Uart_str(" With params!"); send_Uart(13); } while(params) { if((p = strchr(params, '&'))) *(p++) = 0; name = params; if((value = strchr(name, '='))) *(value++) = 0; if( (strcmp_P(name, PSTR("led")) == 0 ) && value ) { if(strcmp_P(value, PSTR("on")) == 0) led_on() else if(strcmp_P(value, PSTR("off")) == 0) led_off() } else if( (strcmp_P(name, PSTR("lang")) == 0) && value ) { if(strcmp_P(value, PSTR("en")) == 0) lang_ru = 0; else if(strcmp_P(value, PSTR("ru")) == 0) lang_ru = 1; } params = p; } fill_buf_p(&buf_ptr, webif_200_header); fill_buf_p(&buf_ptr, PSTR("<pre>")); if(!lang_ru) { fill_buf_p(&buf_ptr, PSTR("<p align='right'>[<b>EN</b> | " "<a href='/?lang=ru'>RU</a>]</p>")); } else { fill_buf_p(&buf_ptr, PSTR("<p align='right'>[<a href='/?lang=en'>EN</a> | " "<b>RU</b>]</p>")); } if((!led_state)&&(!lang_ru)) fill_buf_p(&buf_ptr, PSTR("Led is OFF. Turn <a href='/?led=on'>on</a>.")); else if(led_state &&(!lang_ru)) fill_buf_p(&buf_ptr, PSTR("Led is ON. Turn <a href='/?led=off'>off</a>.")); else if((!led_state)&&(lang_ru)) fill_buf_p(&buf_ptr, PSTR("Светодиод выключен. <a href='/?led=on'>Включить</a>.")); else if(led_state &&(lang_ru)) fill_buf_p(&buf_ptr, PSTR("Светодиод включен. <a href='/?led=off'>Выключить</a>.")); fill_buf_p(&buf_ptr, PSTR("</pre>")); }
void * msg_recv(void *args) { msg_recv_args * input = (msg_recv_args *)args; int i, n; int listenfd, nfds, connfd, sockfd, totfds; socklen_t clilen; char cliip[24]; // char *buf; struct sockaddr_in servaddr, cliaddr; MSG_DATA *new_msg; //socket listenfd = socket(AF_INET, SOCK_STREAM, 0); //setnonblock if(set_nonblock(listenfd)==-1) { perror("error in set_nonblock"); return NULL; } //buf = (char *)malloc(MSG_SIZE); MEMSET(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(input->port); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //bind bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)); //listen listen(listenfd, LISTENQ); //printf("Listening %d\n", SERV_PORT); epfd = epoll_create(EPOLL_SIZE); if (epfd == -1) { perror("epoll_create"); } struct epoll_event ev0; ev0.data.fd = listenfd; ev0.events= EPOLLIN; if(epoll_ctl(epfd, EPOLL_CTL_ADD, listenfd, &ev0) == -1) { perror("epoll_ctl"); } totfds = 1; while(TRUE) { //printf("waiting...\n"); nfds = epoll_wait(epfd, events, totfds, -1); if(nfds <= 0) { continue; //perror("epoll_wait"); } for(i=0; i< nfds; i++) { //New connection if(events[i].data.fd == listenfd) { clilen = sizeof(cliaddr); connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen); if(connfd < 0) { perror("error in accept new connection"); continue; } if (totfds >= EPOLL_SIZE) { close(connfd); continue; } if(set_nonblock(connfd)==-1) { perror("error in set_nonblock"); continue; } inet_ntop(AF_INET, &cliaddr.sin_addr, cliip, sizeof(cliip)); printf("New connection %s %d\n", cliip, ntohs(cliaddr.sin_port)); struct epoll_event ev; ev.data.fd = connfd; ev.events = EPOLLIN; epoll_ctl(epfd, EPOLL_CTL_ADD, connfd, &ev); totfds++; } else if(events[i].events & EPOLLIN) { if((sockfd = events[i].data.fd)<0) { continue; } // MEMSET(buf, MSG_SIZE); new_msg = msg_mem_alloc(); // MEMCPY(new_msg->data, buf, n); n = tcp_get_data(sockfd, new_msg->data, MSG_SIZE); if (n < 0) { tcp_get_err_output(n); switch (n) { case MT_READDISCONNECT: totfds--; close(sockfd); break; case MT_READQUIT: totfds--; close(sockfd); break; default: break; } goto finish; } Assert(n > RPC_MAGIC_MAX_LEN); #if 0 if((n = read(sockfd, buf, MSG_SIZE))==-1) { if(errno==ECONNRESET) close(sockfd); else perror("error in read"); } else if( n==0 ) { close(sockfd); printf("client close connect!\n"); } else { printf("read %d->[%s]\n", n, buf); if (!strncasecmp(RPC_RBD_MAGIC, buf, STRLEN(RPC_RBD_MAGIC))) { int block_left = sizeof(REBALANCE_DATA); char * buffer = malloc(block_left); MEMCPY(buffer, buf, n); block_left -= n; n = tcp_get_data(sockfd, buffer + n, block_left); if (n != block_left) { switch (n) { case MT_READDISCONNECT: close(sockfd); break; case MT_READQUIT: close(sockfd); printf("client close connect!\n"); break; case MT_READERROR: perror("error in read"); break; default: Assert(0); break; } } new_msg = msg_mem_alloc(); MEMCPY(new_msg->data, buf, STRLEN(RPC_RBD_MAGIC)); new_msg->fd = sockfd; new_msg->n_size = sizeof(REBALANCE_DATA); new_msg->block_buffer = buffer; new_msg->next = NULL; } else #endif if (n > MSG_SIZE) { /* TODO : for the big data transferring. */ Assert(0); } else { Assert(new_msg); new_msg->fd = sockfd; new_msg->n_size = n; new_msg->block_buffer = NULL; new_msg->next = NULL; } pthread_mutex_lock(&mutex); if (msg_list_head == NULL) { msg_list_head = new_msg; msg_list_tail = new_msg; } else { msg_list_tail->next = new_msg; msg_list_tail = new_msg; } msg_list_len++; pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); #if 0 } #endif finish: if(msg_list_len > MAX_MSG_LIST) { fprintf(stderr, "big error, msg list length exceeds the max len!\n"); } } else if(events[i].events & EPOLLOUT) { MSG_DATA * resp_msg = (MSG_DATA *)events[i].data.ptr; events[i].data.ptr = NULL; sockfd = resp_msg->fd; //msg_write(sockfd, resp_msg->data, resp_msg->n_size); tcp_put_data(sockfd, resp_msg->data, resp_msg->n_size); // printf("write %d->[%s] -- socketfd = %d \n", resp_msg->n_size, resp_msg->data, sockfd); if(resp_msg->block_buffer) { free(resp_msg->block_buffer); } msg_mem_free(resp_msg); struct epoll_event ev; ev.data.fd = sockfd; ev.events = EPOLLIN; epoll_ctl(epfd, EPOLL_CTL_MOD, sockfd, &ev); } } } // free(buf); }