| | |
| | | |
| | | #include "socket_io.h" |
| | | #include "net_mod_socket_io.h" |
| | | #include "net_mod_socket.h" |
| | | |
| | | NetModServerSocket::NetModServerSocket(int port, ShmModSocket * modsocket): shm_mod_socket(modsocket) |
| | | NetModServerSocket::NetModServerSocket(int port) |
| | | { |
| | | char portstr[32]; |
| | | |
| | | //shmModSocket = new ShmModSocket; |
| | | sprintf(portstr, "%d", port); |
| | | listenfd = Open_listenfd(portstr); |
| | | init_pool(listenfd); |
| | |
| | | /* $end add_client */ |
| | | |
| | | |
| | | int NetModServerSocket::process_client(rio_t *rio, int connfd) { |
| | | int n; |
| | | net_mod_request_head_t request_head; |
| | | net_mod_response_head_t response_head; |
| | | void *buf, *recv_buf; |
| | | int recv_size; |
| | | |
| | | size_t max_buf = 8096; |
| | | |
| | | buf = malloc(max_buf); |
| | | if(buf == NULL) { |
| | | err_exit(errno, "process_client malloc"); |
| | | } |
| | | if ((n = rio_readnb(rio, &request_head, sizeof(net_mod_request_head_t))) != sizeof(net_mod_request_head_t)) |
| | | { |
| | | free(buf); |
| | | return -1; |
| | | } |
| | | |
| | | if(request_head.content_length > max_buf) { |
| | | buf = realloc(buf, request_head.content_length); |
| | | max_buf = request_head.content_length; |
| | | if(buf == NULL) { |
| | | err_exit(errno, "process_client realloc"); |
| | | } |
| | | } |
| | | |
| | | if ((n = rio_readnb(rio, buf, request_head.content_length)) != request_head.content_length ) { |
| | | free(buf); |
| | | return -1; |
| | | } |
| | | |
| | | shmModSocket.sendandrecv(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size); |
| | | response_head.content_length = recv_size; |
| | | Rio_writen(connfd, &response_head, sizeof(response_head)); |
| | | Rio_writen(connfd, recv_buf, recv_size); |
| | | free(buf); |
| | | return 0; |
| | | |
| | | } |
| | | |
| | | /* $begin check_clients */ |
| | | void NetModServerSocket::check_clients() |
| | | { |
| | | int i, connfd, n; |
| | | char buf[MAXLINE]; |
| | | rio_t rio; |
| | | int i, connfd; |
| | | rio_t *rio; |
| | | |
| | | |
| | | for (i = 0; (i <= pool.maxi) && (pool.nready > 0); i++) |
| | | { |
| | | connfd = pool.clientfd[i]; |
| | | rio = pool.clientrio[i]; |
| | | rio = &pool.clientrio[i]; |
| | | |
| | | /* If the descriptor is ready, echo a text line from it */ |
| | | if ((connfd > 0) && (FD_ISSET(connfd, &pool.ready_set))) |
| | | { |
| | | pool.nready--; |
| | | if ((n = rio_readpkgb(&rio, buf, MAXLINE)) > 0) |
| | | { |
| | | |
| | | Rio_writen(connfd, buf, n); |
| | | Rio_writen(connfd, PKG_SEP, strlen(PKG_SEP)); |
| | | // shm_mod_socket->sendto(buf, n, msg->key); |
| | | // net_mod_msg_t *msg = (net_mod_msg_t*)buf; |
| | | // if(msg.mod == PUB_SUB) { |
| | | // shm_mod_socket->pub(msg->topic, msg->topic_size, msg->content, msg->content_size, msg->key); |
| | | // } else { |
| | | // shm_mod_socket->sendto(msg->buf, msg->size, msg->key); |
| | | // } |
| | | } |
| | | |
| | | /* EOF detected, remove descriptor from pool */ |
| | | else |
| | | { |
| | | if(process_client(rio, connfd) != 0) { |
| | | Close(connfd); //line:conc:echoservers:closeconnfd |
| | | FD_CLR(connfd, &pool.read_set); //line:conc:echoservers:beginremove |
| | | pool.clientfd[i] = -1; //line:conc:echoservers:endremove |
| | | } |
| | | |
| | | } |
| | | } |
| | | } |