Commit | Line | Data |
---|---|---|
ef79bbde P |
1 | // |
2 | // DF Netplay Plugin | |
3 | // | |
4 | // Based on netSock 0.2 by linuzappz. | |
5 | // The Plugin is free source code. | |
6 | // | |
7 | ||
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <stdarg.h> | |
11 | #include <sys/types.h> | |
12 | #include <fcntl.h> | |
13 | #include <string.h> | |
14 | #include <sys/stat.h> | |
15 | #include <errno.h> | |
16 | #include <signal.h> | |
17 | #include <sys/wait.h> | |
18 | extern int errno; | |
19 | ||
20 | #include "dfnet.h" | |
21 | ||
22 | int ExecCfg(const char *arg, int f) { | |
23 | char cfg[512]; | |
24 | ||
25 | strcpy(cfg, "cfg/cfgDFNet"); | |
26 | strcat(cfg, " "); | |
27 | strcat(cfg, arg); | |
28 | ||
29 | if (f) { | |
30 | if (fork() == 0) { system(cfg); exit(0); } | |
31 | return 0; | |
32 | } | |
33 | ||
34 | return system(cfg); | |
35 | } | |
36 | ||
37 | void SysMessage(const char *fmt, ...) { | |
38 | va_list list; | |
39 | char msg[512]; | |
40 | char cmd[512]; | |
41 | ||
42 | va_start(list, fmt); | |
43 | vsprintf(msg, fmt, list); | |
44 | va_end(list); | |
45 | ||
46 | sprintf(cmd, "message %s\n", msg); | |
47 | ExecCfg(cmd, 1); | |
48 | } | |
49 | ||
50 | long sockInit() { | |
51 | conf.PlayerNum = 0; | |
52 | tm.tv_sec = 0; | |
53 | tm.tv_usec = 0; | |
54 | ||
55 | return 0; | |
56 | } | |
57 | ||
58 | long sockShutdown() { | |
59 | return 0; | |
60 | } | |
61 | ||
62 | long sockOpen() { | |
63 | if (ExecCfg("open", 0) == 0) return -1; | |
64 | ||
65 | LoadConf(); | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
70 | int sockPing() { | |
71 | char data[32]; | |
72 | struct timeval tv, tvn; | |
73 | ||
74 | memset(data, 0, sizeof(data)); | |
75 | ||
76 | gettimeofday(&tv, NULL); | |
77 | SEND(data, 32, PSE_NET_BLOCKING); | |
78 | RECV(data, 32, PSE_NET_BLOCKING); | |
79 | gettimeofday(&tvn, NULL); | |
80 | ||
81 | return (tvn.tv_sec - tv.tv_sec) * 1000 + | |
82 | (tvn.tv_usec - tv.tv_usec) / 1000; | |
83 | } | |
84 | ||
85 | void CALLBACK NETconfigure() { | |
86 | ExecCfg("configure", 1); | |
87 | } | |
88 | ||
89 | void CALLBACK NETabout() { | |
90 | ExecCfg("about", 1); | |
91 | } | |
92 | ||
93 | pid_t cfgpid = 0; | |
94 | ||
95 | void OnWaitDlg_Abort(int num) { | |
96 | WaitCancel = 1; | |
97 | cfgpid = 0; | |
98 | } | |
99 | ||
100 | void sockCreateWaitDlg() { | |
101 | signal(SIGUSR2, OnWaitDlg_Abort); | |
102 | if ((cfgpid = fork()) == 0) { | |
103 | execl("cfg/cfgDFNet", "cfgDFNet", "wait", NULL); | |
104 | exit(0); | |
105 | } | |
106 | usleep(100000); | |
107 | } | |
108 | ||
109 | void sockDlgUpdate() { | |
110 | usleep(100000); | |
111 | } | |
112 | ||
113 | void sockDestroyWaitDlg() { | |
114 | if (cfgpid > 0) { | |
115 | kill(cfgpid, SIGKILL); | |
116 | cfgpid = 0; | |
117 | } | |
118 | } | |
119 | ||
120 | long timeGetTime() { | |
121 | struct timeval tv; | |
122 | ||
123 | gettimeofday(&tv, NULL); | |
124 | return (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000; | |
125 | } |