diff options
Diffstat (limited to 'source/netinf.c')
-rw-r--r-- | source/netinf.c | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/source/netinf.c b/source/netinf.c new file mode 100644 index 0000000..4e4ec6f --- /dev/null +++ b/source/netinf.c | |||
@@ -0,0 +1,161 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2008 Tantric | ||
3 | * Copyright (C) 2012 Pedro Aguiar | ||
4 | * Copyright (C) 2017 hatkirby | ||
5 | * | ||
6 | * Originally part of the WiiTweet project, which was distributed under the | ||
7 | * GPL. | ||
8 | */ | ||
9 | #include "netinf.h" | ||
10 | #include <gccore.h> | ||
11 | #include <network.h> | ||
12 | #include <ogc/lwp_watchdog.h> | ||
13 | #include <sys/errno.h> | ||
14 | #include <unistd.h> | ||
15 | #include <string.h> | ||
16 | #include <stdio.h> | ||
17 | |||
18 | static bool networkInit = false; | ||
19 | static lwp_t networkThread = LWP_THREAD_NULL; | ||
20 | static u8 netStack[32768] ATTRIBUTE_ALIGN(32); | ||
21 | static char wiiIP[16] = {0}; | ||
22 | static bool netHalt = false; | ||
23 | |||
24 | static void* interface(void* arg) | ||
25 | { | ||
26 | static bool prevInit = false; | ||
27 | |||
28 | while (!netHalt) | ||
29 | { | ||
30 | int retry = 5; | ||
31 | int result = -1; | ||
32 | |||
33 | while ((retry > 0) && (!netHalt)) | ||
34 | { | ||
35 | if (prevInit) | ||
36 | { | ||
37 | net_deinit(); | ||
38 | |||
39 | for (int i=0; (i<400) && !netHalt; i++) | ||
40 | { | ||
41 | if (net_get_status() != -EBUSY) | ||
42 | { | ||
43 | usleep(2000); | ||
44 | net_wc24cleanup(); | ||
45 | prevInit = false; | ||
46 | usleep(20000); | ||
47 | |||
48 | break; | ||
49 | } | ||
50 | |||
51 | usleep(20000); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | usleep(2000); | ||
56 | |||
57 | if (net_init_async(NULL, NULL)) | ||
58 | { | ||
59 | sleep(1); | ||
60 | |||
61 | retry--; | ||
62 | |||
63 | continue; | ||
64 | } | ||
65 | |||
66 | result = net_get_status(); | ||
67 | for (int wait = 400; (wait > 0) && (result == -EBUSY) && !netHalt; wait++) | ||
68 | { | ||
69 | usleep(20000); | ||
70 | |||
71 | result = net_get_status(); | ||
72 | } | ||
73 | |||
74 | if (result == 0) | ||
75 | { | ||
76 | break; | ||
77 | } | ||
78 | |||
79 | retry--; | ||
80 | |||
81 | usleep(2000); | ||
82 | } | ||
83 | |||
84 | if (result == 0) | ||
85 | { | ||
86 | struct in_addr hostip; | ||
87 | hostip.s_addr = net_gethostip(); | ||
88 | |||
89 | if (hostip.s_addr) | ||
90 | { | ||
91 | strcpy(wiiIP, inet_ntoa(hostip)); | ||
92 | networkInit = true; | ||
93 | prevInit = true; | ||
94 | } | ||
95 | } | ||
96 | |||
97 | if (!netHalt) | ||
98 | { | ||
99 | LWP_SuspendThread(networkThread); | ||
100 | } | ||
101 | } | ||
102 | |||
103 | return NULL; | ||
104 | } | ||
105 | |||
106 | static void startNetworkThread() | ||
107 | { | ||
108 | netHalt = false; | ||
109 | |||
110 | if (networkThread == LWP_THREAD_NULL) | ||
111 | { | ||
112 | LWP_CreateThread(&networkThread, interface, NULL, netStack, 8192, 40); | ||
113 | } else { | ||
114 | LWP_ResumeThread(networkThread); | ||
115 | } | ||
116 | } | ||
117 | |||
118 | static void stopNetworkThread() | ||
119 | { | ||
120 | if ((networkThread == LWP_THREAD_NULL) | ||
121 | || (!LWP_ThreadIsSuspended(networkThread))) | ||
122 | { | ||
123 | return; | ||
124 | } | ||
125 | |||
126 | netHalt = true; | ||
127 | |||
128 | LWP_ResumeThread(networkThread); | ||
129 | LWP_JoinThread(networkThread, NULL); | ||
130 | |||
131 | networkThread = LWP_THREAD_NULL; | ||
132 | } | ||
133 | |||
134 | bool initializeNetwork() | ||
135 | { | ||
136 | stopNetworkThread(); | ||
137 | |||
138 | if (networkInit && (net_gethostip() > 0)) | ||
139 | { | ||
140 | return true; | ||
141 | } | ||
142 | |||
143 | networkInit = false; | ||
144 | |||
145 | printf("Initializing network...\n"); | ||
146 | |||
147 | u64 start = gettime(); | ||
148 | startNetworkThread(); | ||
149 | |||
150 | while (!LWP_ThreadIsSuspended(networkThread)) | ||
151 | { | ||
152 | usleep(50 * 1000); | ||
153 | |||
154 | if (diff_sec(start, gettime()) > 10) | ||
155 | { | ||
156 | break; | ||
157 | } | ||
158 | } | ||
159 | |||
160 | return networkInit; | ||
161 | } | ||