////////////////////////////////////////////////////////////////////////// /// /// 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; } } }