void process_new_connection(datasocket_t s) { set_recv_timeout(s,10*1000); set_send_timeout(s,10*1000); add_client(s); ++count; printf("%d\n",count); }
int after_pass_data(int ret, struct author *author, mbuf_type *mbuf) { struct epoll_event ev = {0}; int fd; if (ret == 0) return 0; if (mbuf == NULL) return -1; if (ret < 0) //tcp needed. { //printf("tcp needed\n"); ret = ret + 1; ret = -ret; if ((mbuf->tcpfd > 0) && ((mbuf->qtimes % (MAX_TRY_TIMES / 3)) == 0)) //retry tcp { ev.data.fd = mbuf->tcpfd; mbuf->tcpfd = 0; author->tcpinuse--; epoll_ctl(author->bdepfd, EPOLL_CTL_DEL, ev.data.fd, &ev); close(ev.data.fd); } if (mbuf->tcpfd > 0) //processing... return 0; //restart.. if (author->tcpinuse > (LIST_SPACE / 10)) fd = -1; //too many tcps else { mbuf->tcpnums++; fd = socket(AF_INET, SOCK_STREAM, 0); } if (fd > 0) { author->tcpinuse++; mbuf->tcpfd = fd; mbuf->socktype = TCP; ev.data.fd = fd; ev.events = EPOLLOUT; //wait for ready to write author->eptcpfds[fd].ret = ret; memcpy(author->eptcpfds[fd].domain, mbuf->td, mbuf->dlen); set_non_block(fd); set_recv_timeout(fd, 0, 500); //half second epoll_ctl(author->bdepfd, EPOLL_CTL_ADD, fd, &ev); query_from_auth_tcp(author, mbuf); return 0; } else ret++; //fix ret value, for deleting afterword } if (ret > 0) //delete qoutinfo { ret = ret - 1; //minus the 1 p_a_d added release_qoutinfo(author, mbuf, ret); } return 0; }
static int TcpConnect(char *pStrIp, unsigned short u16Port, unsigned int u32Timeout) { int sock; struct sockaddr_in dest_addr; struct timeval timeout; sock = socket(AF_INET, SOCK_STREAM, 0); if(sock == -1) { LOGERR("socket error\n"); return -1; } timeout.tv_sec = u32Timeout / 1000000; timeout.tv_usec = u32Timeout % 1000000; //设置套接字选项,接收和发送超时时间1s if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0) { LOGERR("setsockopt error\n"); close(sock); return -1; } if (set_recv_timeout(sock, 20) == -1) { close(sock); return -1; } dest_addr.sin_family = AF_INET; dest_addr.sin_port = htons(u16Port); dest_addr.sin_addr.s_addr = inet_addr(pStrIp); memset(&dest_addr.sin_zero, 0, sizeof(dest_addr.sin_zero)); //连接服务器 if(connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr_in)) < 0) { close(sock); return -1; } return sock; }