Debian Codegraph @

terps/tads/tads3/win32/osnetwin.cpp

raw symbols SCIP
1 /* Copyright (c) 2010 by Michael J. Roberts. All Rights Reserved. */
2 /*
3 Name
4 osnetwin.cpp - TADS networking and threading package - windows version
5 Function
6
7 Notes
8
9 Modified
10 04/10/10 MJRoberts - Creation
11 */
12
13
14 #include "t3std.h"
15 #include "os.h"
16 #include "osifcnet.h"
17 #include "vmnet.h"
18 #include "vmisaac.h"
19 #include "vmfile.h"
20
21
22 /* ------------------------------------------------------------------------ */
23 /*
24 * Globals
25 */
26
27 /* master thread list */
28 TadsThreadList *G_thread_list = 0;
29
30
31
32 /* ------------------------------------------------------------------------ */
33 /*
34 * Package initialization and termination
35 */
36
37 /* have we initialized the WinSock layer yet? */
38 static int winsock_inited = FALSE;
39
40 /*
41 * package initialization
42 */
43 void os_net_init(TadsNetConfig *)
44 {
45 /* if we haven't initialized winsock yet, do so now */
46 if (!winsock_inited)
47 {
48 /* initialize the winsock library */
49 WSADATA wsa_data;
50 if (WSAStartup(MAKEWORD(2, 2), &wsa_data))
51 winsock_inited = TRUE;
52 }
53
54 /* create the master thread list */
55 if (G_thread_list == 0)
56 G_thread_list = new TadsThreadList();
57 }
58
59 /*
60 * package cleanup
61 */
62 void os_net_cleanup()
63 {
64 /* if we have a master thread list, wait for threads to exit */
65 if (G_thread_list != 0)
66 {
67 /* wait for threads to exit */
68 G_thread_list->wait_all(500);
69
70 /* done with the thread list */
71 G_thread_list->release_ref();
72 G_thread_list = 0;
73 }
74
75 /* close winsock if we initialized it */
76 if (winsock_inited)
77 {
78 /* shut down WinSock */
79 WSACleanup();
80
81 /* flag it */
82 winsock_inited = FALSE;
83 }
84 }
85
86
87 /* ------------------------------------------------------------------------ */
88 /*
89 * Local host information
90 */
91
92 int os_get_hostname(char *buf, size_t buflen)
93 {
94 return !gethostname(buf, buflen);
95 }
96
97 int os_get_local_ip(char *buf, size_t buflen, const char *host)
98 {
99 /*
100 * Get the local host information. Note that 'host' can be null, which
101 * specifies that we should get the default IP address; it so happens
102 * that gethostbyname() will get the default host info if 'host' is
103 * null, so this does just what we want.
104 */
105 struct hostent *local_host = gethostbyname(host);
106 lib_strcpy(buf, buflen,
107 inet_ntoa(*(struct in_addr *)*local_host->h_addr_list));
108
109 /* success */
110 return TRUE;
111 }
112
113 /* ------------------------------------------------------------------------ *
114 /*
115 * Waitable objects
116 */
117 int OS_Waitable::multi_wait(int cnt, OS_Waitable **objs,
118 unsigned long timeout)
119 {
120 /* set up an array of event handles */
121 HANDLE *handles = new HANDLE[cnt + 1];
122
123 /* copy the handles */
124 for (int i = 0 ; i < cnt ; ++i)
125 handles[i] = objs[i]->h;
126
127 /* figure the ending time */
128 DWORD t_end = GetTickCount() + timeout;
129
130 /* loop until we get a signaled event handle or error */
131 int ret;
132 for (;;)
133 {
134 /* figure the current timeout as the delta to the ending time */
135 if (timeout != OS_FOREVER)
136 {
137 /* if the timeout has already expired, stop now */
138 DWORD t_now = GetTickCount();
139 if (t_now > t_end)
140 {
141 ret = OSWAIT_TIMEOUT;
142 break;
143 }
144
145 /* figure the interval remaining */
146 timeout = t_end - t_now;
147 }
148
149 /* do the wait */
150 DWORD result = MsgWaitForMultipleObjects(
151 cnt, handles, FALSE, timeout, QS_ALLEVENTS);
152
153 /* interpret the result */
154 if (result == WAIT_OBJECT_0 + cnt)
155 {
156 /*
157 * we have messages in the message queue - process them, then
158 * continue looping to wait for one of the caller's events
159 */
160 process_messages();
161 }
162 else if (result >= WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + cnt)
163 {
164 /* one of our handles is in the signaled state */
165 ret = OSWAIT_EVENT + (result - WAIT_OBJECT_0);
166 break;
167 }
168 else if (result == WAIT_TIMEOUT)
169 {
170 /* timed out */
171 ret = OSWAIT_TIMEOUT;
172 break;
173 }
174 else
175 {
176 /* anything else is an error */
177 ret = OSWAIT_ERROR;
178 break;
179 }
180 }
181
182 /* done with the handle array */
183 delete [] handles;
184
185 /* return the result */
186 return ret;
187 }
188
189
190 /* ------------------------------------------------------------------------ */
191 /*
192 * Threads
193 */
194
195 OS_Thread::OS_Thread()
196 {
197 /* add myself to the master thread list */
198 if (G_thread_list != 0)
199 G_thread_list->add(this);
200
201 /* we haven't failed to launch yet */
202 failed = FALSE;
203 }
204
205 OS_Thread::~OS_Thread()
206 {
207 /* clean up the master thread list for our deletion */
208 if (G_thread_list != 0)
209 G_thread_list->clean();
210 }