DDraceNetwork Documentation
Loading...
Searching...
No Matches
system.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3
4/*
5 Title: OS Abstraction
6*/
7
8#ifndef BASE_SYSTEM_H
9#define BASE_SYSTEM_H
10
11#include "dbg.h"
12#include "detect.h"
13#include "fs.h"
14#include "mem.h"
15#include "secure.h"
16#include "str.h"
17#include "time.h"
18#include "types.h"
19
20#include <chrono>
21#include <cinttypes>
22#include <cstdarg>
23#include <cstdint>
24#include <cstring>
25#include <ctime>
26#include <functional>
27#include <mutex>
28#include <optional>
29#include <string>
30
31#ifdef __MINGW32__
32#undef PRId64
33#undef PRIu64
34#undef PRIX64
35#define PRId64 "I64d"
36#define PRIu64 "I64u"
37#define PRIX64 "I64X"
38#define PRIzu "Iu"
39#else
40#define PRIzu "zu"
41#endif
42
43#ifdef CONF_FAMILY_UNIX
44#include <sys/un.h>
45#endif
46
47#ifdef CONF_PLATFORM_LINUX
48#include <netinet/in.h>
49#include <sys/socket.h>
50#endif
51
57
61enum
62{
81};
82
107
120IOHANDLE io_open(const char *filename, int flags);
121
133unsigned io_read(IOHANDLE io, void *buffer, unsigned size);
134
151bool io_read_all(IOHANDLE io, void **result, unsigned *result_len);
152
169char *io_read_all_str(IOHANDLE io);
170
181int io_skip(IOHANDLE io, int64_t size);
182
194int io_seek(IOHANDLE io, int64_t offset, ESeekOrigin origin);
195
205int64_t io_tell(IOHANDLE io);
206
216int64_t io_length(IOHANDLE io);
217
229unsigned io_write(IOHANDLE io, const void *buffer, unsigned size);
230
241
251int io_close(IOHANDLE io);
252
262int io_flush(IOHANDLE io);
263
273int io_sync(IOHANDLE io);
274
284int io_error(IOHANDLE io);
285
296
307
318
327
333typedef struct ASYNCIO ASYNCIO;
334
345
354void aio_lock(ASYNCIO *aio);
355
364void aio_unlock(ASYNCIO *aio);
365
375void aio_write(ASYNCIO *aio, const void *buffer, unsigned size);
376
384void aio_write_newline(ASYNCIO *aio);
385
396void aio_write_unlocked(ASYNCIO *aio, const void *buffer, unsigned size);
397
407
421int aio_error(ASYNCIO *aio);
422
430void aio_close(ASYNCIO *aio);
431
439void aio_wait(ASYNCIO *aio);
440
448void aio_free(ASYNCIO *aio);
449
455
456#if defined(CONF_FAMILY_WINDOWS)
457typedef void *SEMAPHORE;
458#elif defined(CONF_PLATFORM_MACOS)
459#include <semaphore.h>
460typedef sem_t *SEMAPHORE;
461#elif defined(CONF_FAMILY_UNIX)
462#include <semaphore.h>
463typedef sem_t SEMAPHORE;
464#else
465#error not implemented on this platform
466#endif
467
471void sphore_init(SEMAPHORE *sem);
475void sphore_wait(SEMAPHORE *sem);
479void sphore_signal(SEMAPHORE *sem);
483void sphore_destroy(SEMAPHORE *sem);
484
488
494
498extern const NETADDR NETADDR_ZEROED;
499
500#ifdef CONF_FAMILY_UNIX
504typedef int UNIXSOCKET;
508typedef struct sockaddr_un UNIXSOCKETADDR;
509#endif
510
518void net_init();
519
531int net_host_lookup(const char *hostname, NETADDR *addr, int types);
532
545int net_addr_comp(const NETADDR *a, const NETADDR *b);
546
559int net_addr_comp_noport(const NETADDR *a, const NETADDR *b);
560
573void net_addr_str(const NETADDR *addr, char *string, int max_length, bool add_port);
574
600int net_addr_from_url(NETADDR *addr, const char *string, char *host_buf, size_t host_buf_size);
601
611bool net_addr_is_local(const NETADDR *addr);
612
623int net_addr_from_str(NETADDR *addr, const char *string);
624
635
644
652int net_errno();
653
661std::string net_error_message();
662
670int net_would_block();
671
682int net_socket_read_wait(NETSOCKET sock, std::chrono::nanoseconds nanoseconds);
683
689
700int net_socket_type(NETSOCKET sock);
701
712
725int net_udp_send(NETSOCKET sock, const NETADDR *addr, const void *data, int size);
726
738int net_udp_recv(NETSOCKET sock, NETADDR *addr, unsigned char **data);
739
747void net_udp_close(NETSOCKET sock);
748
754
765
776int net_tcp_listen(NETSOCKET sock, int backlog);
777
789int net_tcp_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR *addr);
790
802int net_tcp_connect(NETSOCKET sock, const NETADDR *addr);
803
815
827int net_tcp_send(NETSOCKET sock, const void *data, int size);
828
841int net_tcp_recv(NETSOCKET sock, void *data, int maxsize);
842
850void net_tcp_close(NETSOCKET sock);
851
852#if defined(CONF_FAMILY_UNIX)
858
867
880int net_unix_send(UNIXSOCKET sock, UNIXSOCKETADDR *addr, void *data, int size);
881
890void net_unix_set_addr(UNIXSOCKETADDR *addr, const char *path);
891
899void net_unix_close(UNIXSOCKET sock);
900
901#endif
902
908
925[[gnu::format(printf, 3, 0)]] int str_format_v(char *buffer, int buffer_size, const char *format, va_list args);
926
943[[gnu::format(printf, 3, 4)]] int str_format(char *buffer, int buffer_size, const char *format, ...);
944
945#if !defined(CONF_DEBUG)
946int str_format_int(char *buffer, size_t buffer_size, int value);
947
948template<typename... Args>
949int str_format_opt(char *buffer, int buffer_size, const char *format, Args... args)
950{
951 static_assert(sizeof...(args) > 0, "Use str_copy instead of str_format without format arguments");
952 return str_format(buffer, buffer_size, format, args...);
953}
954
955template<>
956inline int str_format_opt(char *buffer, int buffer_size, const char *format, int val) // NOLINT(readability-inconsistent-declaration-parameter-name)
957{
958 if(strcmp(format, "%d") == 0)
959 {
960 return str_format_int(buffer, buffer_size, val);
961 }
962 else
963 {
964 return str_format(buffer, buffer_size, format, val);
965 }
966}
967
968#define str_format str_format_opt
969#endif
970
981int str_utf8_dist(const char *a, const char *b);
982
999int str_utf8_dist_buffer(const char *a, const char *b, int *buf, int buf_len);
1000
1019int str_utf32_dist_buffer(const int *a, int a_len, const int *b, int b_len, int *buf, int buf_len);
1020
1030void swap_endian(void *data, unsigned elem_size, unsigned num);
1031
1032void net_stats(NETSTATS *stats);
1033
1034int str_utf8_to_skeleton(const char *str, int *buf, int buf_len);
1035
1046int str_utf8_comp_confusable(const char *str1, const char *str2);
1047
1057int str_utf8_tolower_codepoint(int code);
1058
1071unsigned bytes_be_to_uint(const unsigned char *bytes);
1072
1084void uint_to_bytes_be(unsigned char *bytes, unsigned value);
1085
1091
1099int pid();
1100
1111void cmdline_fix(int *argc, const char ***argv);
1112
1121void cmdline_free(int argc, const char **argv);
1122
1123#if !defined(CONF_PLATFORM_ANDROID)
1144
1157PROCESS shell_execute(const char *file, EShellExecuteWindowState window_state, const char **arguments = nullptr, size_t num_arguments = 0);
1158
1168int kill_process(PROCESS process);
1169
1180bool is_process_alive(PROCESS process);
1181
1194int open_link(const char *link);
1195
1208int open_file(const char *path);
1209#endif // !defined(CONF_PLATFORM_ANDROID)
1210
1221bool os_version_str(char *version, size_t length);
1222
1237void os_locale_str(char *locale, size_t length);
1238
1242
1251void crashdump_init_if_available(const char *log_file_path);
1252
1260{
1262 const char **m_ppArgv;
1263
1264public:
1265 CCmdlineFix(int *pArgc, const char ***pppArgv)
1266 {
1267 cmdline_fix(pArgc, pppArgv);
1268 m_Argc = *pArgc;
1269 m_ppArgv = *pppArgv;
1270 }
1272 {
1274 }
1275 CCmdlineFix(const CCmdlineFix &) = delete;
1276};
1277
1278#endif
~CCmdlineFix()
Definition system.h:1271
int m_Argc
Definition system.h:1261
CCmdlineFix(const CCmdlineFix &)=delete
const char ** m_ppArgv
Definition system.h:1262
CCmdlineFix(int *pArgc, const char ***pppArgv)
Definition system.h:1265
void crashdump_init_if_available(const char *log_file_path)
Definition crashdump.cpp:48
void aio_write_newline_unlocked(ASYNCIO *aio)
Definition system.cpp:614
IOHANDLE io_current_exe()
Definition system.cpp:352
void aio_wait(ASYNCIO *aio)
Definition system.cpp:645
void aio_write_unlocked(ASYNCIO *aio, const void *buffer, unsigned size)
Definition system.cpp:557
int io_flush(IOHANDLE io)
Definition system.cpp:314
ESeekOrigin
Definition system.h:87
int64_t io_tell(IOHANDLE io)
Definition system.cpp:272
IOHANDLE io_stderr()
Definition system.cpp:347
IOHANDLE io_open(const char *filename, int flags)
Definition system.cpp:105
int io_close(IOHANDLE io)
Definition system.cpp:309
char * io_read_all_str(IOHANDLE io)
Definition system.cpp:227
void aio_write(ASYNCIO *aio, const void *buffer, unsigned size)
Definition system.cpp:607
void aio_write_newline(ASYNCIO *aio)
Definition system.cpp:623
int io_skip(IOHANDLE io, int64_t size)
Definition system.cpp:243
int aio_error(ASYNCIO *aio)
Definition system.cpp:630
void aio_free(ASYNCIO *aio)
Definition system.cpp:661
unsigned io_write(IOHANDLE io, const void *buffer, unsigned size)
Definition system.cpp:295
bool io_write_newline(IOHANDLE io)
Definition system.cpp:300
bool io_read_all(IOHANDLE io, void **result, unsigned *result_len)
Definition system.cpp:170
void aio_lock(ASYNCIO *aio)
Definition system.cpp:546
void aio_close(ASYNCIO *aio)
Definition system.cpp:636
int io_sync(IOHANDLE io)
Definition system.cpp:319
int io_seek(IOHANDLE io, int64_t offset, ESeekOrigin origin)
Definition system.cpp:248
IOHANDLE io_stdin()
Definition system.cpp:337
int io_error(IOHANDLE io)
Definition system.cpp:332
IOHANDLE io_stdout()
Definition system.cpp:342
ASYNCIO * aio_new(IOHANDLE io)
Definition system.cpp:489
void aio_unlock(ASYNCIO *aio)
Definition system.cpp:551
int64_t io_length(IOHANDLE io)
Definition system.cpp:281
unsigned io_read(IOHANDLE io, void *buffer, unsigned size)
Definition system.cpp:165
@ IOFLAG_APPEND
Definition system.h:80
@ IOFLAG_WRITE
Definition system.h:74
@ IOFLAG_READ
Definition system.h:68
@ IOSEEK_END
Definition system.h:105
@ IOSEEK_CUR
Definition system.h:99
@ IOSEEK_START
Definition system.h:93
int UNIXSOCKET
Definition system.h:504
int net_would_block()
Definition system.cpp:1960
int net_host_lookup(const char *hostname, NETADDR *addr, int types)
Definition system.cpp:1032
int net_addr_from_url(NETADDR *addr, const char *string, char *host_buf, size_t host_buf_size)
Definition system.cpp:1113
int net_set_non_blocking(NETSOCKET sock)
Definition system.cpp:1806
int net_socket_type(NETSOCKET sock)
Definition system.cpp:1375
const NETADDR NETADDR_ZEROED
Definition system.cpp:744
int net_errno()
Definition system.cpp:1940
std::string net_error_message()
Definition system.cpp:1949
int net_addr_comp_noport(const NETADDR *a, const NETADDR *b)
Definition system.cpp:827
void net_init()
Definition system.cpp:1969
int net_socket_read_wait(NETSOCKET sock, std::chrono::nanoseconds nanoseconds)
Definition system.cpp:2030
int net_addr_from_str(NETADDR *addr, const char *string)
Definition system.cpp:1176
int net_addr_comp(const NETADDR *a, const NETADDR *b)
Definition system.cpp:789
struct sockaddr_un UNIXSOCKETADDR
Definition system.h:508
struct NETSOCKET_INTERNAL * NETSOCKET
Definition types.h:42
bool net_addr_is_local(const NETADDR *addr)
Definition system.cpp:1162
void net_addr_str(const NETADDR *addr, char *string, int max_length, bool add_port)
Definition system.cpp:904
int net_tcp_connect_non_blocking(NETSOCKET sock, NETADDR bindaddr)
Definition system.cpp:1895
int net_tcp_listen(NETSOCKET sock, int backlog)
Definition system.cpp:1816
int net_tcp_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR *addr)
Definition system.cpp:1830
int net_tcp_connect(NETSOCKET sock, const NETADDR *addr)
Definition system.cpp:1872
NETSOCKET net_tcp_create(NETADDR bindaddr)
Definition system.cpp:1740
int net_tcp_send(NETSOCKET sock, const void *data, int size)
Definition system.cpp:1903
int net_tcp_recv(NETSOCKET sock, void *data, int maxsize)
Definition system.cpp:1919
void net_tcp_close(NETSOCKET sock)
Definition system.cpp:1935
NETSOCKET net_udp_create(NETADDR bindaddr)
Definition system.cpp:1380
void net_udp_close(NETSOCKET sock)
Definition system.cpp:1735
int net_udp_send(NETSOCKET sock, const NETADDR *addr, const void *data, int size)
Definition system.cpp:1490
int net_udp_recv(NETSOCKET sock, NETADDR *addr, unsigned char **data)
Definition system.cpp:1634
int net_unix_send(UNIXSOCKET sock, UNIXSOCKETADDR *addr, void *data, int size)
Definition system.cpp:1986
void net_unix_set_addr(UNIXSOCKETADDR *addr, const char *path)
Definition system.cpp:1991
UNIXSOCKET net_unix_create_unnamed()
Definition system.cpp:1981
void net_unix_close(UNIXSOCKET sock)
Definition system.cpp:1998
void sphore_signal(SEMAPHORE *sem)
Definition system.cpp:732
void sphore_destroy(SEMAPHORE *sem)
Definition system.cpp:736
void sphore_init(SEMAPHORE *sem)
Definition system.cpp:719
void sphore_wait(SEMAPHORE *sem)
Definition system.cpp:723
bool os_version_str(char *version, size_t length)
Definition system.cpp:2445
int kill_process(PROCESS process)
Definition system.cpp:2349
PROCESS shell_execute(const char *file, EShellExecuteWindowState window_state, const char **arguments=nullptr, size_t num_arguments=0)
Definition system.cpp:2289
bool is_process_alive(PROCESS process)
Definition system.cpp:2369
EShellExecuteWindowState
Definition system.h:1133
int open_link(const char *link)
Definition system.cpp:2382
void cmdline_free(int argc, const char **argv)
Definition system.cpp:2280
void os_locale_str(char *locale, size_t length)
Definition system.cpp:2524
void cmdline_fix(int *argc, const char ***argv)
Definition system.cpp:2242
int open_file(const char *path)
Definition system.cpp:2422
int pid()
Definition system.cpp:2233
pid_t PROCESS
Definition types.h:118
@ BACKGROUND
Definition system.h:1142
@ FOREGROUND
Definition system.h:1137
int str_utf8_tolower_codepoint(int code)
Definition tolower.cpp:3
int str_utf32_dist_buffer(const int *a, int a_len, const int *b, int b_len, int *buf, int buf_len)
Definition system.cpp:2156
int str_format_v(char *buffer, int buffer_size, const char *format, va_list args)
Definition system.cpp:2091
int str_utf8_comp_confusable(const char *str1, const char *str2)
Definition confusables.cpp:83
int str_utf8_dist_buffer(const char *a, const char *b, int *buf, int buf_len)
Definition system.cpp:2192
void * IOHANDLE
Definition logger.h:12
Definition system.cpp:366
unsigned char * buffer
Definition system.cpp:372
IOHANDLE io
Definition system.cpp:368
Definition types.h:72
Definition types.h:92
void swap_endian(void *data, unsigned elem_size, unsigned num)
Definition system.cpp:2004
int str_utf8_dist(const char *a, const char *b)
Definition system.cpp:2136
void uint_to_bytes_be(unsigned char *bytes, unsigned value)
Definition system.cpp:2225
int str_format_int(char *buffer, size_t buffer_size, int value)
Definition system.cpp:2104
int str_format_opt(char *buffer, int buffer_size, const char *format, Args... args)
Definition system.h:949
void net_stats(NETSTATS *stats)
Definition system.cpp:2212
int net_set_blocking(NETSOCKET sock)
Definition system.cpp:1811
sem_t SEMAPHORE
Definition system.h:463
#define str_format
Definition system.h:968
unsigned bytes_be_to_uint(const unsigned char *bytes)
Definition system.cpp:2220
int str_utf8_to_skeleton(const char *str, int *buf, int buf_len)
Definition confusables.cpp:66
float length(const vector2_base< T > &a)
Definition vmath.h:106