static void server_loop(int sd) { int newsd; char ipstr[STRSIZE]; struct sockaddr_in raddr; socklen_t raddr_len; raddr_len = sizeof(raddr); while(1) { newsd = accept(sd,(void *)&raddr,&raddr_len); if(newsd < 0) { if(errno == EINTR || errno == EAGAIN) continue; perror("accept()"); exit(1); } inet_ntop(AF_INET,&raddr.sin_addr,ipstr,STRSIZE); printf("Client:%s:%d\n",ipstr,ntohs(raddr.sin_port)); server_job(newsd); close(newsd); } }
static int add_1_server(void) { pid_t pid; int i; if(idle_count + busy_count >= MAXCLIENT) return -1; for(i = 0 ; i < MAXCLIENT; i++) { if(serverpool[i].pid == -1) break; } pid = fork(); if(pid < 0) { perror("fork()"); exit(1); } if(pid == 0) // child { server_job(i); exit(0); } else { //idle_count ++; //serverpool[i].state = STATE_IDLE; serverpool[i].pid = pid; } return 0; }
static void server_job(snd_pcm_direct_t *dmix) { int ret, sck, i; int max = 128, current = 0; struct pollfd pfds[max + 1]; server_job_dmix = dmix; /* don't allow to be killed */ signal(SIGHUP, server_job_signal); signal(SIGQUIT, server_job_signal); signal(SIGTERM, server_job_signal); signal(SIGKILL, server_job_signal); /* close all files to free resources */ i = sysconf(_SC_OPEN_MAX); #ifdef SERVER_JOB_DEBUG while (--i >= 3) { #else while (--i >= 0) { #endif if (i != dmix->server_fd && i != dmix->hw_fd) close(i); } /* detach from parent */ setsid(); pfds[0].fd = dmix->server_fd; pfds[0].events = POLLIN | POLLERR | POLLHUP; server_printf("DIRECT SERVER STARTED\n"); while (1) { ret = poll(pfds, current + 1, 500); server_printf("DIRECT SERVER: poll ret = %i, revents[0] = 0x%x, errno = %i\n", ret, pfds[0].revents, errno); if (ret < 0) { if (errno == EINTR) continue; /* some error */ break; } if (ret == 0 || (pfds[0].revents & (POLLERR | POLLHUP))) { /* timeout or error? */ struct shmid_ds buf; snd_pcm_direct_semaphore_down(dmix, DIRECT_IPC_SEM_CLIENT); if (shmctl(dmix->shmid, IPC_STAT, &buf) < 0) { _snd_pcm_direct_shm_discard(dmix); snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT); continue; } server_printf("DIRECT SERVER: nattch = %i\n", (int)buf.shm_nattch); if (buf.shm_nattch == 1) /* server is the last user, exit */ break; snd_pcm_direct_semaphore_up(dmix, DIRECT_IPC_SEM_CLIENT); continue; } if (pfds[0].revents & POLLIN) { ret--; sck = accept(dmix->server_fd, 0, 0); if (sck >= 0) { server_printf("DIRECT SERVER: new connection %i\n", sck); if (current == max) { close(sck); } else { unsigned char buf = 'A'; pfds[current+1].fd = sck; pfds[current+1].events = POLLIN | POLLERR | POLLHUP; _snd_send_fd(sck, &buf, 1, dmix->hw_fd); server_printf("DIRECT SERVER: fd sent ok\n"); current++; } } } for (i = 0; i < current && ret > 0; i++) { struct pollfd *pfd = &pfds[i+1]; unsigned char cmd; server_printf("client %i revents = 0x%x\n", pfd->fd, pfd->revents); if (pfd->revents & (POLLERR | POLLHUP)) { ret--; close(pfd->fd); pfd->fd = -1; continue; } if (!(pfd->revents & POLLIN)) continue; ret--; if (read(pfd->fd, &cmd, 1) == 1) cmd = 0 /*process command */; } for (i = 0; i < current; i++) { if (pfds[i+1].fd < 0) { if (i + 1 != max) memcpy(&pfds[i+1], &pfds[i+2], sizeof(struct pollfd) * (max - i - 1)); current--; } } } server_cleanup(dmix); server_printf("DIRECT SERVER EXIT\n"); #ifdef SERVER_JOB_DEBUG close(0); close(1); close(2); #endif _exit(EXIT_SUCCESS); } int snd_pcm_direct_server_create(snd_pcm_direct_t *dmix) { int ret; dmix->server_fd = -1; ret = get_tmp_name(dmix->shmptr->socket_name, sizeof(dmix->shmptr->socket_name)); if (ret < 0) return ret; ret = make_local_socket(dmix->shmptr->socket_name, 1, dmix->ipc_perm, dmix->ipc_gid); if (ret < 0) return ret; dmix->server_fd = ret; ret = listen(dmix->server_fd, 4); if (ret < 0) { close(dmix->server_fd); return ret; } ret = fork(); if (ret < 0) { close(dmix->server_fd); return ret; } else if (ret == 0) { ret = fork(); if (ret == 0) server_job(dmix); _exit(EXIT_SUCCESS); } else { waitpid(ret, NULL, 0); } dmix->server_pid = ret; dmix->server = 1; return 0; }