| 1 | /* |
| 2 | * Copyright (C) 1999,2000 Ross Combs ([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 "tick.h" |
| 20 | |
| 21 | #include <cerrno> |
| 22 | #include <cstddef> |
| 23 | #include <cstring> |
| 24 | |
| 25 | #include "compat/gettimeofday.h" |
| 26 | #include "common/eventlog.h" |
| 27 | |
| 28 | #include "common/setup_after.h" |
| 29 | |
| 30 | |
| 31 | namespace pvpgn |
| 32 | { |
| 33 | |
| 34 | namespace bnetd |
| 35 | { |
| 36 | |
| 37 | /* |
| 38 | * This routine returns the number of miliseconds that have passed since one second |
| 39 | * before it is first called. This is used for timing fields in some packets. |
| 40 | */ |
| 41 | extern unsigned int get_ticks(void) |
| 42 | { |
| 43 | static int first = 1; |
| 44 | static long beginsec; |
| 45 | struct timeval tv; |
| 46 | |
| 47 | if (gettimeofday(&tv, NULL) < 0) |
| 48 | { |
| 49 | eventlog(eventlog_level_error, __FUNCTION__, "could not get std::time (gettimeofday: {})", std::strerror(errno)); |
| 50 | return 0; |
| 51 | } |
| 52 | if (first) |
| 53 | { |
| 54 | beginsec = tv.tv_sec - 1; |
| 55 | first = 0; |
| 56 | } |
| 57 | |
| 58 | return (unsigned int)((tv.tv_sec - beginsec) * 1000 + tv.tv_usec / 1000); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | } |