| | |
| | | #include "socket_io.h" |
| | | #include "net_mod_socket_io.h" |
| | | #include "net_mod_socket.h" |
| | | #include "key_def.h" |
| | | |
| | | NetModServerSocket::NetModServerSocket(int port):max_buf(1024) |
| | | { |
| | | char portstr[32]; |
| | | static Logger * logger = LoggerFactory::getLogger(); |
| | | |
| | | //shmModSocket = new ShmModSocket; |
| | | sprintf(portstr, "%d", port); |
| | | listenfd = Open_listenfd(portstr); |
| | | init_pool(listenfd); |
| | | NetModServerSocket::NetModServerSocket(int _port): listenfd(0), port(_port), max_buf(1024), max_topic_buf(256), max_response_buf(1024) |
| | | { |
| | | |
| | | |
| | | buf = malloc(max_buf); |
| | | if(buf == NULL) { |
| | | err_exit(errno, "process_client malloc"); |
| | | err_exit(errno, "NetModServerSocket::NetModServerSocket malloc"); |
| | | } |
| | | |
| | | topic_buf = malloc(max_topic_buf); |
| | | if(topic_buf == NULL) { |
| | | err_exit(errno, "NetModServerSocket::NetModServerSocket malloc"); |
| | | } |
| | | |
| | | response_buf = (char *) malloc(max_response_buf); |
| | | if(response_buf == NULL) { |
| | | err_exit(errno, "NetModServerSocket::NetModServerSocket malloc"); |
| | | } |
| | | } |
| | | |
| | | |
| | | NetModServerSocket::~NetModServerSocket() { |
| | | Close(listenfd); |
| | | fee(buf); |
| | | if(listenfd != 0) { |
| | | Close(listenfd); |
| | | } |
| | | |
| | | if(buf != NULL) |
| | | free(buf); |
| | | if(topic_buf != NULL) |
| | | free(topic_buf); |
| | | if(response_buf != NULL) |
| | | free(response_buf); |
| | | } |
| | | |
| | | void NetModServerSocket::start() { |
| | | int NetModServerSocket::start() { |
| | | int connfd; |
| | | socklen_t clientlen; |
| | | struct sockaddr_storage clientaddr; |
| | | char portstr[32]; |
| | | if (Signal(SIGPIPE, SIG_IGN) == SIG_ERR) err_msg(errno, "signal"); |
| | | |
| | | //shmModSocket = new ShmModSocket; |
| | | sprintf(portstr, "%d", port); |
| | | listenfd = open_listenfd(portstr); |
| | | if(listenfd < 0) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModServerSocket::start . errno=%d", errno); |
| | | return -1; |
| | | } |
| | | init_pool(listenfd); |
| | | |
| | | while (1) |
| | | { |
| | | /* Wait for listening/connected descriptor(s) to become ready */ |
| | | pool.ready_set = pool.read_set; |
| | | pool.nready = select(pool.maxfd + 1, &pool.ready_set, NULL, NULL, NULL); |
| | | |
| | | // LoggerFactory::getLogger()->debug("select return \n"); |
| | | /* If listening descriptor ready, add new client to pool */ |
| | | if (FD_ISSET(listenfd, &pool.ready_set)) |
| | | { |
| | |
| | | /* Echo a text line from each ready connected descriptor */ |
| | | check_clients(); |
| | | } |
| | | return 0; |
| | | } |
| | | |
| | | void NetModServerSocket::init_pool(int listenfd) |
| | |
| | | { |
| | | int i; |
| | | pool.nready--; |
| | | for (i = 0; i < FD_SETSIZE; i++) /* Find an available slot */ |
| | | for (i = 0; i < FD_SETSIZE; i++) {/* Find an available slot */ |
| | | if (pool.clientfd[i] < 0) |
| | | { |
| | | /* 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 |
| | |
| | | pool.maxi = i; //line:conc:echoservers:endmaxi |
| | | break; |
| | | } |
| | | if (i == FD_SETSIZE) /* Couldn't find an empty slot */ |
| | | err_msg(errno, "add_client error: Too many clients"); |
| | | } |
| | | if (i == FD_SETSIZE) { |
| | | /* Couldn't find an empty slot */ |
| | | LoggerFactory::getLogger()->error(errno, "add_client error: Too many clients"); |
| | | |
| | | } |
| | | } |
| | | /* $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; |
| | | int ret; |
| | | char request_head_bs[NET_MODE_REQUEST_HEAD_LENGTH]; |
| | | void *recv_buf; |
| | | |
| | | int recv_size; |
| | | // char tmp[8196]; |
| | | int recv_size, response_buf_size; |
| | | struct timespec timeout; |
| | | |
| | | 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; |
| | | } |
| | | |
| | | request_head = NetModSocket::decode_request_head(request_head_bs); |
| | | |
| | | // printf("server received request from host = %s:%d, key = %d, timeout=%d,\n", |
| | | // request_head.host, request_head.port , request_head.key, request_head.timeout); |
| | | |
| | | 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( (buf = realloc(buf, request_head.content_length)) == NULL) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModServerSocket::process_client realloc buf"); |
| | | exit(1); |
| | | } else { |
| | | max_buf = request_head.content_length; |
| | | } |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | |
| | | if(request_head.mod == REQ_REP) { |
| | | shmModSocket.sendandrecv(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size); |
| | | 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); |
| | | |
| | | memcpy(response_head.host, request_head.host, NI_MAXHOST); |
| | | response_head.port = request_head.port; |
| | | response_head.key = request_head.key; |
| | | |
| | | |
| | | if(request_head.timeout > 0) { |
| | | timeout.tv_sec = request_head.timeout / 1000; |
| | | timeout.tv_nsec = (request_head.timeout - timeout.tv_sec * 1000) * 10e6; |
| | | |
| | | // printf(" timeout.tv_sec = %d, timeout.tv_nsec=%ld\n", timeout.tv_sec, timeout.tv_nsec ); |
| | | |
| | | ret = shmModSocket.sendandrecv_unsafe_timeout(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size, &timeout); |
| | | } |
| | | else if(request_head.timeout == 0) { |
| | | ret = shmModSocket.sendandrecv_unsafe_nowait(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size); |
| | | } |
| | | else if(request_head.timeout == -1) { |
| | | ret = shmModSocket.sendandrecv_unsafe(buf, request_head.content_length, request_head.key, &recv_buf, &recv_size); |
| | | } |
| | | |
| | | if( ret != 0) { |
| | | // 没有对应的key |
| | | response_head.code = ret; |
| | | response_head.content_length = 0; |
| | | if( rio_writen(connfd, NetModSocket::encode_response_head(response_head), NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH ) |
| | | return -1; |
| | | //Rio_writen(connfd, recv_buf, recv_size); |
| | | } else { |
| | | // 发送成功 |
| | | response_head.code = 0; |
| | | response_head.content_length = recv_size; |
| | | |
| | | response_buf_size = NET_MODE_RESPONSE_HEAD_LENGTH + recv_size; |
| | | if(max_response_buf < response_buf_size) { |
| | | if( (response_buf = (char *)realloc(response_buf, response_buf_size)) == NULL ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModServerSocket::process_client realloc response_buf"); |
| | | exit(1); |
| | | } else { |
| | | max_response_buf = response_buf_size; |
| | | } |
| | | |
| | | |
| | | } |
| | | memcpy(response_buf, NetModSocket::encode_response_head(response_head), NET_MODE_RESPONSE_HEAD_LENGTH); |
| | | memcpy(response_buf + NET_MODE_RESPONSE_HEAD_LENGTH, recv_buf, recv_size); |
| | | |
| | | if(rio_writen(connfd, response_buf, response_buf_size) != response_buf_size) { |
| | | return -1; |
| | | } |
| | | |
| | | } |
| | | return 0; |
| | | |
| | | |
| | | } else if(request_head.mod == BUS) { |
| | | if(request_head.topic_length > max_topic_buf) { |
| | | if( (topic_buf = realloc(topic_buf, request_head.topic_length)) == NULL ) { |
| | | LoggerFactory::getLogger()->error(errno, "NetModServerSocket::process_client realloc topic_buf"); |
| | | exit(1); |
| | | } else { |
| | | max_topic_buf = request_head.topic_length; |
| | | } |
| | | } |
| | | |
| | | if (rio_readn(connfd, topic_buf, request_head.topic_length) != request_head.topic_length ) { |
| | | return -1; |
| | | } |
| | | // LoggerFactory::getLogger()->debug("====server pub %s===\n", buf); |
| | | memcpy(response_head.host, request_head.host, NI_MAXHOST); |
| | | response_head.port = request_head.port; |
| | | // response_head.key = request_head.key; |
| | | |
| | | if(request_head.timeout > 0) { |
| | | timeout.tv_sec = request_head.timeout / 1000; |
| | | timeout.tv_nsec = (request_head.timeout - timeout.tv_sec * 1000) * 10e6; |
| | | ret = shmModSocket.pub_timeout((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, BUS_KEY, &timeout); |
| | | } |
| | | else if(request_head.timeout == 0) { |
| | | ret = shmModSocket.pub_nowait((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, BUS_KEY); |
| | | } |
| | | else if(request_head.timeout == -1) { |
| | | ret = shmModSocket.pub((char*)topic_buf, request_head.topic_length, buf, request_head.content_length, BUS_KEY); |
| | | } |
| | | |
| | | response_head.code = ret; |
| | | response_head.content_length = 0; |
| | | if( rio_writen(connfd, NetModSocket::encode_response_head(response_head), NET_MODE_RESPONSE_HEAD_LENGTH) != NET_MODE_RESPONSE_HEAD_LENGTH ) |
| | | return -1; |
| | | |
| | | |
| | | } |
| | | |
| | | return 0; |
| | |
| | | void NetModServerSocket::check_clients() |
| | | { |
| | | int i, connfd; |
| | | rio_t *rio; |
| | | |
| | | |
| | | for (i = 0; (i <= pool.maxi) && (pool.nready > 0); i++) |
| | | { |
| | | connfd = pool.clientfd[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) { |
| | | Close(connfd); //line:conc:echoservers:closeconnfd |
| | | FD_CLR(connfd, &pool.read_set); //line:conc:echoservers:beginremove |
| | | pool.clientfd[i] = -1; //line:conc:echoservers:endremove |
| | | if(process_client(connfd) != 0) { |
| | | |
| | | |
| | | Close(connfd); |
| | | FD_CLR(connfd, &pool.read_set); |
| | | pool.clientfd[i] = -1; |
| | | logger->debug("===server close client %d\n", connfd); |
| | | // printf("===server close client %d\n", connfd); |
| | | } |
| | | |
| | | } |