diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 552cc6c50..fb576bfe0 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -4,6 +4,7 @@ D2X-Rebirth Changelog -------- SConstruct: Fixing Windows build which was not linking against SDL main/net_udp.c: Fixed Compiler-warning on Windows +main/net_ipx.c, main/net_udp.c: Implemented Winsock functions for UDP which I totally forgot about; Now when leaving game in any way, close sockets and Winsock stuff 20100701 -------- diff --git a/main/net_ipx.c b/main/net_ipx.c index 6de40e3b8..1c9e51c3d 100644 --- a/main/net_ipx.c +++ b/main/net_ipx.c @@ -230,7 +230,6 @@ void ipxdrv_close() // -5 if error with getting internetwork address int ipxdrv_init( int socket_number ) { - static int cleanup = 0; #ifdef _WIN32 WORD wVersionRequested; WSADATA wsaData; @@ -267,10 +266,6 @@ int ipxdrv_init( int socket_number ) ipxdrv_installed = 1; - if (!cleanup) - atexit(ipxdrv_close); - cleanup = 1; - return 0; } @@ -3224,6 +3219,9 @@ int net_ipx_setup_game() i = newmenu_do1( NULL, NULL, optnum, m, (int (*)( newmenu *, d_event *, void * ))net_ipx_game_param_handler, &opt, 1 ); + if (i < 0) + ipxdrv_close(); + return i >= 0; } @@ -3907,7 +3905,7 @@ int net_ipx_join_poll( newmenu *menu, d_event *event, void *menu_text ) case EVENT_WINDOW_CLOSE: d_free(menu_text); d_free(menus); - + ipxdrv_close(); if (!Game_wind) Network_status = NETSTAT_MENU; // they cancelled break; @@ -4067,6 +4065,7 @@ net_ipx_level_sync(void) { Players[Player_num].connected = CONNECT_DISCONNECTED; net_ipx_send_endlevel_packet(); + ipxdrv_close(); if (Game_wind) window_close(Game_wind); show_menus(); @@ -4392,6 +4391,7 @@ void net_ipx_leave_game() write_player_file(); net_ipx_flush(); + ipxdrv_close(); } void net_ipx_flush() diff --git a/main/net_udp.c b/main/net_udp.c index 9a50b40fd..2cc234569 100644 --- a/main/net_udp.c +++ b/main/net_udp.c @@ -58,6 +58,7 @@ // Prototypes void net_udp_init(); +void net_udp_close(); void net_udp_request_game_info(struct _sockaddr game_addr, int lite); void net_udp_listen(); int net_udp_show_game_info(); @@ -476,6 +477,7 @@ static int manual_join_game_handler(newmenu *menu, d_event *event, direct_join * case EVENT_WINDOW_CLOSE: d_free(dj); + net_udp_close(); break; default: @@ -589,7 +591,7 @@ int net_udp_list_join_poll( newmenu *menu, d_event *event, void *menu_text ) case EVENT_WINDOW_CLOSE: d_free(menu_text); d_free(menus); - + net_udp_close(); if (!Game_wind) Network_status = NETSTAT_MENU; // they cancelled break; @@ -786,6 +788,17 @@ void net_udp_init() int t; int save_pnum = Player_num; +#ifdef _WIN32 +{ + WORD wVersionRequested; + WSADATA wsaData; + wVersionRequested = MAKEWORD(2, 0); + WSACleanUp(); + if (WSAStartup( wVersionRequested, &wsaData)) + nm_messagebox( TXT_ERROR, 1, TXT_OK, "Cannot init Winsock!"); // no break here... game will fail at socket creation anyways... +} +#endif + if( UDP_Socket[0] != -1 ) udp_close_socket(0); if( UDP_Socket[1] != -1 ) @@ -823,6 +836,18 @@ void net_udp_init() Netgame.PacketsPerSec=10; } +void net_udp_close() +{ +#ifdef _WIN32 + WSACleanUp(); +#endif + + if( UDP_Socket[0] != -1 ) + udp_close_socket(0); + if( UDP_Socket[1] != -1 ) + udp_close_socket(1); +} + int net_udp_how_many_connected() { int num=0,i; @@ -999,6 +1024,7 @@ net_udp_disconnect_player(int playernum) multi_quit_game = 1; game_leave_menus(); multi_reset_stuff(); + net_udp_close(); } } @@ -3205,6 +3231,9 @@ int net_udp_setup_game() i = newmenu_do1( NULL, NULL, optnum, m, (int (*)( newmenu *, d_event *, void * ))net_udp_game_param_handler, &opt, 1 ); + if (i < 0) + net_udp_close(); + return i >= 0; } @@ -3790,6 +3819,7 @@ net_udp_level_sync(void) { Players[Player_num].connected = CONNECT_DISCONNECTED; net_udp_send_endlevel_packet(); + net_udp_close(); if (Game_wind) window_close(Game_wind); show_menus(); @@ -3905,11 +3935,7 @@ void net_udp_leave_game() write_player_file(); net_udp_flush(); - - if( UDP_Socket[0] != -1 ) - udp_close_socket(0); - if( UDP_Socket[1] != -1 ) - udp_close_socket(1); + net_udp_close(); } void net_udp_flush()