void on_event_read(myconn_t* c) { while (sizeof(c->readbuff) - c->readbuff_len > 0) { ssize_t len = read(c->fd, c->readbuff + c->readbuff_len, sizeof(c->readbuff) - c->readbuff_len); if (len < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { break; } else if (errno == EINTR) { continue; /* or should i check thread cancel point? */ } else { /* most errors detected here */ struct epoll_event e = { .events = EPOLLERR, .data.ptr = c, }; handle_events(&e); return; } } else if (len == 0) { /* most hups detected here */ struct epoll_event e = { .events = EPOLLHUP, .data.ptr = c, }; handle_events(&e); return; } else { c->readbuff_len += len; c->last_active = time(NULL); } } if (deal_data(c->readbuff, &c->readbuff_len, c->writebuff, &c->writebuff_len)) { poll_connection(epollfd, EPOLLIN | EPOLLOUT | EPOLLET, c); } }
int main(int argc,char **argv) { int pipefd[2]; pid_t pid; /*pipe*/ if (pipe(pipefd) < 0) { perror("pipe"); exit(1); } /*fork*/ pid = fork(); if (pid<0) { perror("fork()"); exit(1); } if (pid==0) { close(pipefd[1]); dup2(pipefd[0], 0); if (pipefd[0] > 0) { close(pipefd[0]); } execl(client_conf.player, "mpg123","-",NULL); exit(0); } close(pipefd[0]); /*父进程接收网络上的包,交给子进程去播放*/ deal_data(pipefd[1]); wait(NULL); exit(0); }
void on_event_hup(myconn_t* c) { if (c->readbuff_len) { /* deal with last words */ c->readbuff[c->readbuff_len++] = '$'; if (deal_data(c->readbuff, &c->readbuff_len, c->writebuff, &c->writebuff_len)) { poll_connection(epollfd, /* EPOLLIN | */EPOLLOUT | EPOLLET, c); /* no read EOF */ } } else { on_event_hup_dft(c); } }
int main(void) { int ret; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2 delay_init(162);//延时初始化 tim3_init(162);//时钟初始化 sram_init();//SRAM初始化 ads1271_init(0);//ad初始化 参数为0 高速模式 led_init();//LED初始化 read_device_config();//读取flash中设备配置 current_source_init();//恒流源初始化 ethernet_init();//网络初始化 tcp_sever();//建立tcp服务器 while(!is_con());//等待连接 led_link(1);//开启连接灯 while(1) { //INT处理部分 ret = deal_int(); if((ret == NET_ERR) || (ret == NET_DISCONNECT)) sys_restart();//重启 //PRE处理部分 ret = deal_pre(); if((ret == NET_ERR) || (ret == NET_DISCONNECT)) sys_restart();//重启 //DIV处理 ret = deal_div(); if((ret == NET_ERR) || (ret == NET_DISCONNECT)) sys_restart();//重启 //STA处理 ret = deal_sta(); if((ret == NET_ERR) || (ret == NET_DISCONNECT)) sys_restart();//重启 //数据传输 while(1) { ret = deal_data(); if((ret == NET_ERR) || (ret == NET_DISCONNECT)) sys_restart();//重启 } } }