static void proxy_conn_free(proxy_conn_t* proxy) { net_conn_close(&proxy->conn.base); if(NULL != proxy->write_ch) { chan_dispose(proxy->write_ch); } if(NULL != proxy->die_ch) { chan_dispose(proxy->die_ch); } free(proxy); }
////////////////////////////////////////////////////////////////////////// /// /// net_process /// @author xuliang<*****@*****.**> /// @date 2010-09-27 ////////////////////////////////////////////////////////////////////////// void net_process(void* arg) { int i; int addr_size = 0; int res = 0; int client_sock = -1; struct timeval tv; struct sockaddr_in client_addr; static int m_max_i = 0; //! max client connection number static fd_set m_readfds; //! set of read socket handles int m_max_sock = -1; NET_INFO* door = (NET_INFO*)arg; NET_CONN_INFO *conn = door->conn; NET_SERV_INFO *serv = door->serv; // while (1) { FD_ZERO(&m_readfds); if (serv->fd > 0) { FD_SET(serv->fd, &m_readfds); m_max_sock = serv->fd; } for ( i = 0; i < MAX_TCP_CONN; i++) { if (conn[i].client_conn < 0) { continue; } FD_SET(conn[i].client_conn, &m_readfds); m_max_sock = m_max_sock > conn[i].client_conn ? m_max_sock : conn[i].client_conn; } tv.tv_sec = 0; tv.tv_usec = 10000; //res = select(m_max_sock + 1, &m_readfds, NULL, NULL, &tv); res = select(m_max_sock + 1, &m_readfds, NULL, NULL, NULL); if (res == 0) { return; } else if (res < 0) { if (errno == EINTR) { return; } perror("server_process: select"); //Close all client connections for (i = 0; i < MAX_TCP_CONN; i++) { if (conn[i].client_conn > 0) { net_conn_close(&conn[i]); } } return; } else if (res > 0) { if (FD_ISSET(serv->fd, &m_readfds)) { addr_size = sizeof(client_addr); client_sock = accept(serv->fd, (struct sockaddr *) & client_addr, &addr_size); sys_print(FUNC, INFO, "++++++++++++++++++++新连接进入 %s.++++++++++++++++++++\n", inet_ntoa(client_addr.sin_addr)); if (client_sock > 0) { for (i = 0; i < MAX_TCP_CONN; i++) { if (conn[i].client_conn < 0) { conn[i].conn_idx = i; conn[i].client_conn = client_sock; conn[i].idle = 0; strcpy(conn[i].clientip, inet_ntoa(client_addr.sin_addr)); if (i > m_max_i) { m_max_i = i; } break; } } if (i == MAX_TCP_CONN) { close(client_sock); client_sock = -1; } } if (client_sock > m_max_sock) { m_max_sock = client_sock; } } for (i = 0; i <= m_max_i; i++) { if (conn[i].client_conn < 0) { continue; } if (FD_ISSET(conn[i].client_conn, &m_readfds)) { conn[i].idle = 0; net_svr_proc(&conn[i]); } } } } }
////////////////////////////////////////////////////////////////////////// /// /// net_conn_recv /// @author xuliang<*****@*****.**> /// @date 2010-09-27 ////////////////////////////////////////////////////////////////////////// int net_conn_recv(NET_CONN_INFO *conn_info, void *net_data, DWORD len) { BYTE *p_net_data = NULL; int recv_cnt = 0; int total_length = 0; fd_set readfds; int res = -1; struct timeval tv; UQWORD ms_cnt1 = 0LLU; UQWORD ms_cnt2 = 0LLU; UQWORD ms_cnt3 = 0LLU; if (conn_info->client_conn <= 0) { return FAILURE; } p_net_data = net_data; total_length = len; ms_cnt1 = system_mscount_get(); while (1) { FD_ZERO(&readfds); FD_SET(conn_info->client_conn, &readfds); tv.tv_sec = 0; tv.tv_usec = 1000; res = select(conn_info->client_conn + 1, &readfds, NULL, NULL, &tv); if (res < 0) { /* * note by xuw 2007/03/02: for signal interrupt, do not close the socket, only continue. */ if (errno == EINTR) { continue; } perror("net_conn_recv:select"); net_conn_close(conn_info); return FAILURE; } else if (res > 0) { if (FD_ISSET(conn_info->client_conn, &readfds)) { recv_cnt = recv(conn_info->client_conn, p_net_data, total_length, MSG_DONTWAIT | MSG_NOSIGNAL); ms_cnt1 = system_mscount_get(); } } else { ms_cnt2 = system_mscount_get(); //If the return value of select() is still Zero after 500ms, Close the socket if (ms_cnt2 > ms_cnt1 && ms_cnt2 - ms_cnt1 >= TIMEOUT_CNT) { net_conn_close(conn_info); sys_print(FUNC, ERROR, "客户端超过 %llu毫秒未响应, 必须关闭此次连接\n", ms_cnt2 - ms_cnt1); writeLog(LOG_ERROR, "客户端超过 %llu毫秒未响应, 必须关闭此次连接\n", ms_cnt2 - ms_cnt1); return FAILURE; } continue; } if (recv_cnt == -1) { perror("net_conn_recv:recv"); if (errno == ECONNRESET) { net_conn_close(conn_info); } return FAILURE; } if (recv_cnt == 0) { net_conn_close(conn_info); return FAILURE; } if (total_length - recv_cnt == 0) { return SUCCESS; } else { ms_cnt3 = system_mscount_get(); //If the return value of select() is still Zero after 500ms, Close the socket if (ms_cnt3 > ms_cnt1 && ms_cnt3 - ms_cnt1 >= TIMEOUT_CNT) { //We must close this socket and return FAILURE when the client is in problem. //Otherwise system will stop to capture picture and video net_conn_close(conn_info); sys_print(FUNC, ERROR, "客户端发送错误,必须关闭此次连接\n"); return FAILURE; } p_net_data += recv_cnt; total_length -= recv_cnt; } } }
int net_conn_send_pkt(NET_CONN_INFO *conn_info, void *net_data, int len) { BYTE *ptr = NULL; int send_cnt = 0; int total_length = 0; fd_set writefds; struct timeval tv; int res = -1; UQWORD ms_cnt1 = 0LLU; UQWORD ms_cnt2 = 0LLU; UQWORD ms_cnt3 = 0LLU; if (conn_info->client_conn <= 0){ return FAILURE; } conn_info->idle = 0; ms_cnt1 = system_mscount_get(); ptr = (BYTE*)net_data; total_length = len; while (1){ FD_ZERO(&writefds); FD_SET(conn_info->client_conn, &writefds); tv.tv_sec = 0; tv.tv_usec = 10000; res =-1; res = select(conn_info->client_conn + 1, NULL, &writefds, NULL, &tv); if (res < 0){ if (errno == EINTR) continue; perror("net_conn_send:select"); net_conn_close(conn_info); return FAILURE; } else if (res > 0) { if (FD_ISSET(conn_info->client_conn, &writefds)) { send_cnt = send(conn_info->client_conn, ptr, total_length, MSG_DONTWAIT | MSG_NOSIGNAL); //ms_cnt1 = system_mscount_get(); } } else { ms_cnt2 = system_mscount_get(); //If the return value of select() is still Zero after 500ms, Close the socket if (ms_cnt2 > ms_cnt1 && ms_cnt2 - ms_cnt1 >= TIMEOUT_CNT) { net_conn_close(conn_info); printf("MS_CNT1: %llu, MS_CNT2: %llu.\n", ms_cnt1, ms_cnt2); printf("net_conn_send: Client has no response in %llu ms, we must Close this socket.\n", ms_cnt2 - ms_cnt1); return FAILURE; } continue; } if (send_cnt == -1){ perror("net_conn_send:send"); net_conn_close(conn_info); return FAILURE; } if (send_cnt == 0){ net_conn_close(conn_info); return FAILURE; } if (total_length - send_cnt == 0){ return SUCCESS; } else { ms_cnt3 = system_mscount_get(); //If the return value of select() is still Zero after 500ms, Close the socket if (ms_cnt3 > ms_cnt1 && ms_cnt3 - ms_cnt1 >= TIMEOUT_CNT){ //We must close this socket and return FAILURE when the client is in problem. //Otherwise system will stop to capture picture and video net_conn_close(conn_info); printf("net_conn_send: Client has something wrong, we must Close this socket.\n"); return FAILURE; } ptr += send_cnt; total_length -= send_cnt; } } }