| 1 | /* |
| 2 | * Copyright (C) 2000,2001 Onlyer ([email protected]) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #include "common/setup_before.h" |
| 19 | #include "setup.h" |
| 20 | #include "bnetd.h" |
| 21 | |
| 22 | #include <ctime> |
| 23 | |
| 24 | #include "common/eventlog.h" |
| 25 | #include "prefs.h" |
| 26 | #include "s2s.h" |
| 27 | #include "handle_bnetd.h" |
| 28 | #include "common/setup_after.h" |
| 29 | |
| 30 | namespace pvpgn |
| 31 | { |
| 32 | |
| 33 | namespace d2cs |
| 34 | { |
| 35 | |
| 36 | static t_connection * bnetd_connection = NULL; |
| 37 | |
| 38 | extern int bnetd_init(void) |
| 39 | { |
| 40 | return bnetd_check(); |
| 41 | } |
| 42 | |
| 43 | extern int bnetd_check(void) |
| 44 | { |
| 45 | static std::time_t prev_connecting_checktime = 0; |
| 46 | |
| 47 | if (bnetd_connection) { |
| 48 | if (d2cs_conn_get_state(bnetd_connection) == conn_state_connecting) { |
| 49 | if (std::time(nullptr) - prev_connecting_checktime > prefs_get_s2s_timeout()) { |
| 50 | eventlog(eventlog_level_warn, __FUNCTION__, "connection to bnetd s2s timeout"); |
| 51 | d2cs_conn_set_state(bnetd_connection, conn_state_destroy); |
| 52 | return -1; |
| 53 | } |
| 54 | } |
| 55 | return 0; |
| 56 | } |
| 57 | if (!(bnetd_connection = s2s_create(prefs_get_bnetdaddr(), BNETD_SERV_PORT, conn_class_bnetd))) { |
| 58 | return -1; |
| 59 | } |
| 60 | if (d2cs_conn_get_state(bnetd_connection) == conn_state_init) { |
| 61 | handle_bnetd_init(bnetd_connection); |
| 62 | } |
| 63 | else { |
| 64 | prev_connecting_checktime = std::time(nullptr); |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | extern t_connection * bnetd_conn(void) |
| 71 | { |
| 72 | return bnetd_connection; |
| 73 | } |
| 74 | |
| 75 | extern int bnetd_set_connection(t_connection * c) |
| 76 | { |
| 77 | bnetd_connection = c; |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | extern int bnetd_destroy(t_connection * c) |
| 82 | { |
| 83 | if (bnetd_connection != c) { |
| 84 | eventlog(eventlog_level_error, __FUNCTION__, "bnetd connection do not match"); |
| 85 | return -1; |
| 86 | } |
| 87 | bnetd_connection = NULL; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | } |