| 2456 | } |
| 2457 | |
| 2458 | int nw_DoReceiveCallbacks(void) { |
| 2459 | SOCKADDR_IN ip_addr; // UDP/TCP socket structure |
| 2460 | socklen_t read_len, from_len; |
| 2461 | network_address from_addr; |
| 2462 | |
| 2463 | uint8_t packet_data[1500]; |
| 2464 | |
| 2465 | nw_ReliableResend(); |
| 2466 | |
| 2467 | while (TCP_active) { |
| 2468 | // check if there is any data on the socket to be read. The amount of data that can be |
| 2469 | // atomically read is stored in len. |
| 2470 | |
| 2471 | #if 0 |
| 2472 | fd_set rfds; |
| 2473 | FD_ZERO(&rfds); |
| 2474 | FD_SET( TCP_socket, &rfds ); |
| 2475 | timeout.tv_sec = 0; |
| 2476 | timeout.tv_usec = 0; |
| 2477 | |
| 2478 | timeval timeout; |
| 2479 | if ( select( TCP_socket+1, &rfds, NULL, NULL, &timeout) == SOCKET_ERROR) |
| 2480 | { |
| 2481 | mprintf(0, "Error %d doing a socket select on IP read\n", WSAGetLastError()); |
| 2482 | break; |
| 2483 | } |
| 2484 | |
| 2485 | // if the read file descriptor is not set, then bail! |
| 2486 | if ( !FD_ISSET(TCP_socket, &rfds ) ) |
| 2487 | break; |
| 2488 | #endif |
| 2489 | // get data off the socket and process |
| 2490 | from_len = sizeof(SOCKADDR_IN); |
| 2491 | read_len = recvfrom(TCP_socket, (char *)packet_data, 1500, 0, (SOCKADDR *)&ip_addr, &from_len); |
| 2492 | |
| 2493 | if (read_len == SOCKET_ERROR) { |
| 2494 | int x = WSAGetLastError(); |
| 2495 | if (x != WSAEWOULDBLOCK) { |
| 2496 | mprintf(0, "Read error on IP socket. Winsock error %d \n", x); |
| 2497 | } |
| 2498 | break; |
| 2499 | } |
| 2500 | memset(&from_addr, 0x00, sizeof(network_address)); |
| 2501 | from_addr.connection_type = NP_TCP; |
| 2502 | from_addr.port = ntohs(ip_addr.sin_port); |
| 2503 | |
| 2504 | #ifdef WIN32 |
| 2505 | memcpy(from_addr.address, &ip_addr.sin_addr.S_un.S_addr, 4); |
| 2506 | #else |
| 2507 | memcpy(from_addr.address, &ip_addr.sin_addr.s_addr, 4); |
| 2508 | #endif |
| 2509 | uint8_t packet_id = (packet_data[0] & 0x0f); |
| 2510 | if (Netcallbacks[packet_id]) { |
| 2511 | // mprintf(0,"Calling network callback for id %d.\n",packet_id); |
| 2512 | int rlen = read_len - 1; |
| 2513 | if (packet_id == NWT_UNRELIABLE) { |
| 2514 | NetStatistics.udp_total_packets_rec++; |
| 2515 | NetStatistics.udp_total_bytes_rec += rlen; |
no test coverage detected