| | |
| | | #include "net_mod_socket_io.h" |
| | | #include "net_mod_socket.h" |
| | | |
| | | NetModServerSocket::NetModServerSocket(int port):max_buf(1024) |
| | | NetModServerSocket::NetModServerSocket(int port):max_buf(1024), max_topic_buf(256) |
| | | { |
| | | char portstr[32]; |
| | | |
| | |
| | | if(buf == NULL) { |
| | | err_exit(errno, "process_client malloc"); |
| | | } |
| | | |
| | | topic_buf = malloc(max_topic_buf); |
| | | if(topic_buf == NULL) { |
| | | err_exit(errno, "process_client malloc"); |
| | | } |
| | | } |
| | | |
| | | |
| | | NetModServerSocket::~NetModServerSocket() { |
| | | Close(listenfd); |
| | | fee(buf); |
| | | free(buf); |
| | | free(topic_buf); |
| | | } |
| | | |
| | | void NetModServerSocket::start() { |
| | |
| | | { |
| | | /* Add connected descriptor to the pool */ |
| | | pool.clientfd[i] = connfd; //line:conc:echoservers:beginaddclient |
| | | Rio_readinitb(&pool.clientrio[i], connfd); //line:conc:echoservers:endaddclient |
| | | // Rio_readinitb(&pool.clientrio[i], connfd); //line:conc:echoservers:endaddclient |
| | | |
| | | /* Add the descriptor to descriptor set */ |
| | | FD_SET(connfd, &pool.read_set); //line:conc:echoservers:addconnfd |
| | |
| | | /* $end add_client */ |
| | | |
| | | |
| | | int NetModServerSocket::process_client(rio_t *rio, int connfd) { |
| | | int n; |
| | | int NetModServerSocket::process_client(int connfd) { |
| | | net_mod_request_head_t request_head; |
| | | net_mod_response_head_t response_head; |
| | | char request_head_bs[NET_MODE_REQUEST_HEAD_LENGTH]; |
| | |
| | | |
| | | int recv_size; |
| | | |
| | | if(buf == NULL) { |
| | | buf = malloc(max_buf); |
| | | if(buf == NULL) { |
| | | err_exit(errno, "process_client malloc"); |
| | | } |
| | | } |
| | | // if(buf == NULL) { |
| | | // buf = malloc(max_buf); |
| | | // if(buf == NULL) { |
| | | // err_exit(errno, "process_client malloc"); |
| | | // } |
| | | // } |
| | | |
| | | |
| | | if (rio_readnb(rio, request_head_bs, NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) |
| | | if (rio_readn(connfd, request_head_bs, NET_MODE_REQUEST_HEAD_LENGTH) != NET_MODE_REQUEST_HEAD_LENGTH) |
| | | { |
| | | return -1; |
| | | } |
| | |
| | | buf = realloc(buf, request_head.content_length); |
| | | max_buf = request_head.content_length; |
| | | if(buf == NULL) { |
| | | err_exit(errno, "process_client realloc"); |
| | | LoggerFactory::getLogger()->error(errno, "process_client realloc"); |
| | | exit(1); |
| | | } |
| | | } |
| | | |
| | | if ((n = rio_readnb(rio, buf, request_head.content_length)) != request_head.content_length ) { |
| | | if (rio_readn(connfd, buf, request_head.content_length) != request_head.content_length ) { |
| | | return -1; |
| | | } |
| | | |
| | |
| | | response_head.content_length = recv_size; |
| | | Rio_writen(connfd, NetModSocket::encode_response_head(response_head), NET_MODE_RESPONSE_HEAD_LENGTH); |
| | | Rio_writen(connfd, recv_buf, recv_size); |
| | | } else if(request_head.mod == BUS) { |
| | | if(request_head.topic_length > max_topic_buf) { |
| | | topic_buf = realloc(topic_buf, request_head.topic_length); |
| | | max_topic_buf = request_head.topic_length; |
| | | if(topic_buf == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "process_client realloc"); |
| | | exit(1); |
| | | } |
| | | } |
| | | |
| | | if (rio_readn(connfd, topic_buf, request_head.topic_length) != request_head.topic_length ) { |
| | | return -1; |
| | | } |
| | | LoggerFactory::getLogger()->debug("====server pub %s===\n", buf); |
| | | shmModSocket.pub((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, request_head.key); |
| | | } |
| | | |
| | | return 0; |
| | |
| | | void NetModServerSocket::check_clients() |
| | | { |
| | | int i, connfd; |
| | | rio_t *rio; |
| | | //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(process_client(rio, connfd) != 0) { |
| | | if(process_client(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 |
| | | FD_CLR(connfd, &pool.read_set); |
| | | pool.clientfd[i] = -1; |
| | | } |
| | | |
| | | } |