Sat, 17 Aug 2013 23:27:20 +0530
Renamed prpl.[ch] to protocol.[ch]
| 14792 | 1 | /* GLIB - Library of useful routines for C programming |
| 2 | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald | |
| 3 | * | |
| 4 | * giowin32.c: IO Channels for Win32. | |
| 5 | * Copyright 1998 Owen Taylor and Tor Lillqvist | |
| 6 | * Copyright 1999-2000 Tor Lillqvist and Craig Setera | |
| 7 | * Copyright 2001-2003 Andrew Lanoix | |
| 8 | * | |
| 9 | * This library is free software; you can redistribute it and/or | |
| 10 | * modify it under the terms of the GNU Lesser General Public | |
| 11 | * License as published by the Free Software Foundation; either | |
| 12 | * version 2 of the License, or (at your option) any later version. | |
| 13 | * | |
| 14 | * This library is distributed in the hope that it will be useful, | |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 17 | * Lesser General Public License for more details. | |
| 18 | * | |
| 19 | * You should have received a copy of the GNU Lesser General Public | |
| 20 | * License along with this library; if not, write to the | |
|
19859
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
16413
diff
changeset
|
21 | * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
71d37b57eff2
The FSF changed its address a while ago; our files were out of date.
John Bailey <rekkanoryo@rekkanoryo.org>
parents:
16413
diff
changeset
|
22 | * Boston, MA 02111-1301, USA. |
| 14792 | 23 | */ |
| 24 | ||
| 25 | /* | |
| 26 | * Modified by the GLib Team and others 1997-2000. See the AUTHORS | |
| 27 | * file for a list of people on the GLib Team. See the ChangeLog | |
| 28 | * files for a list of changes. These files are distributed with | |
| 29 | * GLib at ftp://ftp.gtk.org/pub/gtk/. | |
| 30 | */ | |
| 31 | ||
| 32 | /* Define this to get (very) verbose logging of all channels */ | |
| 33 | /* #define G_IO_WIN32_DEBUG */ | |
| 34 | ||
| 16413 | 35 | /* #include "config.h" */ |
| 14792 | 36 | |
| 37 | #include <glib.h> | |
| 38 | ||
| 39 | #include <stdlib.h> | |
|
22869
3fc7a5fc49eb
Include winsock2.h before windows.h is included to avoid winsock.h being
Daniel Atallah <datallah@pidgin.im>
parents:
19859
diff
changeset
|
40 | #include <winsock2.h> |
| 14792 | 41 | #include <windows.h> |
| 42 | #include <fcntl.h> | |
| 43 | #include <io.h> | |
| 44 | #include <process.h> | |
| 45 | #include <errno.h> | |
| 46 | #include <sys/stat.h> | |
| 47 | ||
| 48 | #include <glib/gstdio.h> | |
| 49 | ||
| 50 | typedef struct _GIOWin32Channel GIOWin32Channel; | |
| 51 | typedef struct _GIOWin32Watch GIOWin32Watch; | |
| 52 | ||
| 53 | #define BUFFER_SIZE 4096 | |
| 54 | ||
|
24231
6907b62f25fe
Warnings cleanup from Lee Roach. Fixes #6242.
Daniel Atallah <datallah@pidgin.im>
parents:
22869
diff
changeset
|
55 | GIOChannel *wpurple_g_io_channel_win32_new_socket (int socket); |
|
6907b62f25fe
Warnings cleanup from Lee Roach. Fixes #6242.
Daniel Atallah <datallah@pidgin.im>
parents:
22869
diff
changeset
|
56 | |
| 14792 | 57 | typedef enum { |
| 58 | G_IO_WIN32_WINDOWS_MESSAGES, /* Windows messages */ | |
| 59 | G_IO_WIN32_FILE_DESC, /* Unix-like file descriptors from | |
| 60 | * _open() or _pipe(). Read with read(). | |
| 61 | * Have to create separate thread to read. | |
| 62 | */ | |
| 63 | G_IO_WIN32_SOCKET /* Sockets. A separate thread is blocked | |
| 64 | * in select() most of the time. | |
| 65 | */ | |
| 66 | } GIOWin32ChannelType; | |
| 67 | ||
| 68 | struct _GIOWin32Channel { | |
| 69 | GIOChannel channel; | |
| 70 | gint fd; /* Either a Unix-like file handle as provided | |
| 71 | * by the Microsoft C runtime, or a SOCKET | |
| 72 | * as provided by WinSock. | |
| 73 | */ | |
| 74 | GIOWin32ChannelType type; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
75 | |
| 14792 | 76 | gboolean debug; |
| 77 | ||
| 78 | CRITICAL_SECTION mutex; | |
| 79 | ||
| 80 | /* This is used by G_IO_WIN32_WINDOWS_MESSAGES channels */ | |
| 81 | HWND hwnd; /* handle of window, or NULL */ | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
82 | |
| 14792 | 83 | /* Following fields are used by both fd and socket channels. */ |
| 84 | gboolean running; /* Is reader thread running. FALSE if | |
| 85 | * EOF has been reached. | |
| 86 | */ | |
| 87 | gboolean needs_close; /* If the channel has been closed while | |
| 88 | * the reader thread was still running. | |
| 89 | */ | |
| 90 | guint thread_id; /* If non-NULL has a reader thread, or has | |
| 91 | * had.*/ | |
| 92 | HANDLE data_avail_event; | |
| 93 | ||
| 94 | gushort revents; | |
| 95 | ||
| 96 | /* Following fields used by fd channels for input */ | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
97 | |
| 14792 | 98 | /* Data is kept in a circular buffer. To be able to distinguish between |
| 99 | * empty and full buffer, we cannot fill it completely, but have to | |
| 100 | * leave a one character gap. | |
| 101 | * | |
| 102 | * Data available is between indexes rdp and wrp-1 (modulo BUFFER_SIZE). | |
| 103 | * | |
| 104 | * Empty: wrp == rdp | |
| 105 | * Full: (wrp + 1) % BUFFER_SIZE == rdp | |
| 106 | * Partial: otherwise | |
| 107 | */ | |
| 108 | guchar *buffer; /* (Circular) buffer */ | |
| 109 | gint wrp, rdp; /* Buffer indices for writing and reading */ | |
| 110 | HANDLE space_avail_event; | |
| 111 | ||
| 112 | /* Following fields used by socket channels */ | |
| 113 | GSList *watches; | |
| 114 | HANDLE data_avail_noticed_event; | |
| 115 | gint reset_send; /* socket used to send data so select_thread() can reset/re-loop */ | |
| 116 | gint reset_recv; /* socket used to recv data so select_thread() can reset/re-loop */ | |
| 117 | }; | |
| 118 | ||
| 119 | #define LOCK(mutex) EnterCriticalSection (&mutex) | |
| 120 | #define UNLOCK(mutex) LeaveCriticalSection (&mutex) | |
| 121 | ||
| 122 | struct _GIOWin32Watch { | |
| 123 | GSource source; | |
| 124 | GPollFD pollfd; | |
| 125 | GIOChannel *channel; | |
| 126 | GIOCondition condition; | |
| 127 | }; | |
| 128 | ||
| 129 | static void | |
| 130 | g_win32_print_gioflags (GIOFlags flags) | |
| 131 | { | |
| 132 | char *bar = ""; | |
| 133 | ||
| 134 | if (flags & G_IO_FLAG_APPEND) | |
| 135 | bar = "|", g_print ("APPEND"); | |
| 136 | if (flags & G_IO_FLAG_NONBLOCK) | |
| 137 | g_print ("%sNONBLOCK", bar), bar = "|"; | |
| 138 | if (flags & G_IO_FLAG_IS_READABLE) | |
| 139 | g_print ("%sREADABLE", bar), bar = "|"; | |
| 140 | if (flags & G_IO_FLAG_IS_WRITEABLE) | |
| 141 | g_print ("%sWRITEABLE", bar), bar = "|"; | |
| 142 | if (flags & G_IO_FLAG_IS_SEEKABLE) | |
| 143 | g_print ("%sSEEKABLE", bar), bar = "|"; | |
| 144 | } | |
| 145 | ||
| 146 | static gboolean | |
| 147 | g_io_win32_get_debug_flag (void) | |
| 148 | { | |
| 149 | #ifdef G_IO_WIN32_DEBUG | |
| 150 | return TRUE; | |
| 151 | #else | |
| 152 | if (getenv ("G_IO_WIN32_DEBUG") != NULL) | |
| 153 | return TRUE; | |
| 154 | else | |
| 155 | return FALSE; | |
| 156 | #endif | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
157 | } |
| 14792 | 158 | |
| 159 | static void | |
| 160 | g_io_channel_win32_init (GIOWin32Channel *channel) | |
| 161 | { | |
| 162 | channel->debug = g_io_win32_get_debug_flag (); | |
| 163 | channel->buffer = NULL; | |
| 164 | channel->running = FALSE; | |
| 165 | channel->needs_close = FALSE; | |
| 166 | channel->thread_id = 0; | |
| 167 | channel->data_avail_event = NULL; | |
| 168 | channel->revents = 0; | |
| 169 | channel->space_avail_event = NULL; | |
| 170 | channel->reset_send = INVALID_SOCKET; | |
| 171 | channel->reset_recv = INVALID_SOCKET; | |
| 172 | channel->data_avail_noticed_event = NULL; | |
| 173 | channel->watches = NULL; | |
| 174 | InitializeCriticalSection (&channel->mutex); | |
| 175 | } | |
| 176 | ||
| 177 | static void | |
| 178 | create_events (GIOWin32Channel *channel) | |
| 179 | { | |
| 180 | SECURITY_ATTRIBUTES sec_attrs; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
181 | |
| 14792 | 182 | sec_attrs.nLength = sizeof (SECURITY_ATTRIBUTES); |
| 183 | sec_attrs.lpSecurityDescriptor = NULL; | |
| 184 | sec_attrs.bInheritHandle = FALSE; | |
| 185 | ||
| 186 | /* The data available event is manual reset, the space available event | |
| 187 | * is automatic reset. | |
| 188 | */ | |
| 189 | if (!(channel->data_avail_event = CreateEvent (&sec_attrs, TRUE, FALSE, NULL)) | |
| 190 | || !(channel->space_avail_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL)) | |
| 191 | || !(channel->data_avail_noticed_event = CreateEvent (&sec_attrs, FALSE, FALSE, NULL))) | |
| 192 | { | |
| 193 | gchar *emsg = g_win32_error_message (GetLastError ()); | |
| 194 | g_error ("Error creating event: %s", emsg); | |
| 195 | g_free (emsg); | |
| 196 | } | |
| 197 | } | |
| 198 | ||
| 199 | static void | |
| 200 | create_thread (GIOWin32Channel *channel, | |
| 201 | GIOCondition condition, | |
| 202 | unsigned (__stdcall *thread) (void *parameter)) | |
| 203 | { | |
| 204 | HANDLE thread_handle; | |
| 205 | ||
| 206 | thread_handle = (HANDLE) _beginthreadex (NULL, 0, thread, channel, 0, | |
| 207 | &channel->thread_id); | |
| 208 | if (thread_handle == 0) | |
| 209 | g_warning (G_STRLOC ": Error creating reader thread: %s", | |
| 210 | g_strerror (errno)); | |
| 211 | else if (!CloseHandle (thread_handle)) | |
| 212 | g_warning (G_STRLOC ": Error closing thread handle: %s\n", | |
| 213 | g_win32_error_message (GetLastError ())); | |
| 214 | ||
| 215 | WaitForSingleObject (channel->space_avail_event, INFINITE); | |
| 216 | } | |
| 217 | ||
| 218 | static void | |
| 219 | init_reset_sockets (GIOWin32Channel *channel) | |
| 220 | { | |
| 221 | struct sockaddr_in local, local2, server; | |
| 222 | int len; | |
| 223 | ||
| 224 | channel->reset_send = (gint) socket (AF_INET, SOCK_DGRAM, 0); | |
| 225 | if (channel->reset_send == INVALID_SOCKET) | |
| 226 | { | |
| 227 | g_warning (G_STRLOC ": Error creating reset_send socket: %s\n", | |
| 228 | g_win32_error_message (WSAGetLastError ())); | |
| 229 | } | |
| 230 | ||
| 231 | local.sin_family = AF_INET; | |
| 232 | local.sin_port = 0; | |
| 233 | local.sin_addr.s_addr = htonl (INADDR_LOOPBACK); | |
| 234 | ||
| 235 | if (bind (channel->reset_send, (struct sockaddr *)&local, sizeof (local)) == SOCKET_ERROR) | |
| 236 | { | |
| 237 | g_warning (G_STRLOC ": Error binding to reset_send socket: %s\n", | |
| 238 | g_win32_error_message (WSAGetLastError ())); | |
| 239 | } | |
| 240 | ||
| 241 | local2.sin_family = AF_INET; | |
| 242 | local2.sin_port = 0; | |
| 243 | local2.sin_addr.s_addr = htonl (INADDR_LOOPBACK); | |
| 244 | ||
| 245 | channel->reset_recv = (gint) socket (AF_INET, SOCK_DGRAM, 0); | |
| 246 | if (channel->reset_recv == INVALID_SOCKET) | |
| 247 | { | |
| 248 | g_warning (G_STRLOC ": Error creating reset_recv socket: %s\n", | |
| 249 | g_win32_error_message (WSAGetLastError ())); | |
| 250 | } | |
| 251 | ||
| 252 | if (bind (channel->reset_recv, (struct sockaddr *)&local2, sizeof (local)) == SOCKET_ERROR) | |
| 253 | { | |
| 254 | g_warning (G_STRLOC ": Error binding to reset_recv socket: %s\n", | |
| 255 | g_win32_error_message (WSAGetLastError ())); | |
| 256 | } | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
257 | |
| 14792 | 258 | len = sizeof (local2); |
| 259 | if (getsockname (channel->reset_recv, (struct sockaddr *)&local2, &len) == SOCKET_ERROR) | |
| 260 | { | |
| 261 | g_warning (G_STRLOC ": Error getsockname with reset_recv socket: %s\n", | |
| 262 | g_win32_error_message (WSAGetLastError ())); | |
| 263 | } | |
| 264 | ||
| 265 | memset (&server, 0, sizeof (server)); | |
| 266 | server.sin_addr.s_addr = htonl (INADDR_LOOPBACK); | |
| 267 | server.sin_family = AF_INET; | |
| 268 | server.sin_port = local2.sin_port; | |
| 269 | ||
| 270 | if (connect (channel->reset_send, (struct sockaddr *)&server, sizeof (server)) == SOCKET_ERROR) | |
| 271 | { | |
| 272 | g_warning (G_STRLOC ": connect to reset_recv socket: %s\n", | |
| 273 | g_win32_error_message (WSAGetLastError ())); | |
| 274 | } | |
| 275 | ||
| 276 | } | |
| 277 | ||
| 278 | static unsigned __stdcall | |
| 279 | select_thread (void *parameter) | |
| 280 | { | |
| 281 | GIOWin32Channel *channel = parameter; | |
| 282 | fd_set read_fds, write_fds, except_fds; | |
| 283 | GSList *tmp; | |
| 284 | int n; | |
| 285 | char buffer[8]; | |
| 286 | ||
| 287 | g_io_channel_ref ((GIOChannel *)channel); | |
| 288 | ||
| 289 | if (channel->debug) | |
| 290 | g_print ("select_thread %#x: start fd:%d data_avail:%#x data_avail_noticed:%#x\n", | |
| 291 | channel->thread_id, | |
| 292 | channel->fd, | |
| 293 | (guint) channel->data_avail_event, | |
| 294 | (guint) channel->data_avail_noticed_event); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
295 | |
| 14792 | 296 | channel->rdp = channel->wrp = 0; |
| 297 | channel->running = TRUE; | |
| 298 | ||
| 299 | SetEvent (channel->space_avail_event); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
300 | |
| 14792 | 301 | while (channel->running) |
| 302 | { | |
| 303 | FD_ZERO (&read_fds); | |
| 304 | FD_ZERO (&write_fds); | |
| 305 | FD_ZERO (&except_fds); | |
| 306 | FD_SET (channel->reset_recv, &read_fds); | |
| 307 | ||
| 308 | LOCK (channel->mutex); | |
| 309 | tmp = channel->watches; | |
| 310 | while (tmp) | |
| 311 | { | |
| 312 | GIOWin32Watch *watch = (GIOWin32Watch *)tmp->data; | |
| 313 | ||
| 314 | if (watch->condition & (G_IO_IN | G_IO_HUP)) | |
| 315 | FD_SET (channel->fd, &read_fds); | |
| 316 | if (watch->condition & G_IO_OUT) | |
| 317 | FD_SET (channel->fd, &write_fds); | |
| 318 | if (watch->condition & G_IO_ERR) | |
| 319 | FD_SET (channel->fd, &except_fds); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
320 | |
| 14792 | 321 | tmp = tmp->next; |
| 322 | } | |
| 323 | UNLOCK (channel->mutex); | |
| 324 | ||
| 325 | if (channel->debug) | |
| 326 | g_print ("select_thread %#x: calling select() for%s%s%s\n", | |
| 327 | channel->thread_id, | |
| 328 | (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""), | |
| 329 | (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""), | |
| 330 | (FD_ISSET (channel->fd, &except_fds) ? " ERR" : "")); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
331 | |
| 14792 | 332 | n = select (1, &read_fds, &write_fds, &except_fds, NULL); |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
333 | |
| 14792 | 334 | LOCK (channel->mutex); |
| 335 | if (channel->needs_close) | |
| 336 | { | |
| 337 | UNLOCK (channel->mutex); | |
| 338 | break; | |
| 339 | } | |
| 340 | UNLOCK (channel->mutex); | |
| 341 | ||
| 342 | if (n == SOCKET_ERROR) | |
| 343 | { | |
| 344 | if (channel->debug) | |
| 345 | g_print ("select_thread %#x: select returned SOCKET_ERROR\n", | |
| 346 | channel->thread_id); | |
| 347 | break; | |
| 348 | } | |
| 349 | ||
| 350 | if (FD_ISSET (channel->reset_recv, &read_fds)) | |
| 351 | { | |
| 352 | if (channel->debug) | |
| 353 | g_print ("select_thread %#x: re-looping\n", | |
| 354 | channel->thread_id); | |
| 355 | recv (channel->reset_recv, (char *)&buffer, (int) sizeof (buffer), 0); | |
| 356 | continue; | |
| 357 | } | |
| 358 | ||
| 359 | if (channel->debug) | |
| 360 | g_print ("select_thread %#x: got%s%s%s\n", | |
| 361 | channel->thread_id, | |
| 362 | (FD_ISSET (channel->fd, &read_fds) ? " IN" : ""), | |
| 363 | (FD_ISSET (channel->fd, &write_fds) ? " OUT" : ""), | |
| 364 | (FD_ISSET (channel->fd, &except_fds) ? " ERR" : "")); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
365 | |
| 14792 | 366 | if (FD_ISSET (channel->fd, &read_fds)) |
| 367 | channel->revents |= G_IO_IN; | |
| 368 | if (FD_ISSET (channel->fd, &write_fds)) | |
| 369 | channel->revents |= G_IO_OUT; | |
| 370 | if (FD_ISSET (channel->fd, &except_fds)) | |
| 371 | channel->revents |= G_IO_ERR; | |
| 372 | ||
| 373 | if (channel->debug) | |
| 374 | g_print ("select_thread %#x: resetting data_avail_noticed, setting data_avail\n", | |
| 375 | channel->thread_id); | |
| 376 | ||
| 377 | LOCK (channel->mutex); | |
| 378 | ResetEvent (channel->data_avail_noticed_event); | |
| 379 | SetEvent (channel->data_avail_event); | |
| 380 | if (channel->needs_close) | |
| 381 | { | |
| 382 | UNLOCK (channel->mutex); | |
| 383 | break; | |
| 384 | } | |
| 385 | UNLOCK (channel->mutex); | |
| 386 | ||
| 387 | if (channel->debug) | |
| 388 | g_print ("select_thread %#x: waiting for data_avail_noticed\n", | |
| 389 | channel->thread_id); | |
| 390 | ||
| 391 | WaitForSingleObject (channel->data_avail_noticed_event, INFINITE); | |
| 392 | if (channel->debug) | |
| 393 | g_print ("select_thread %#x: got data_avail_noticed\n", | |
| 394 | channel->thread_id); | |
| 395 | } | |
| 396 | ||
| 397 | LOCK (channel->mutex); | |
| 398 | channel->running = FALSE; | |
| 399 | if (channel->debug) | |
| 400 | g_print ("select_thread %#x: got error, setting data_avail\n", | |
| 401 | channel->thread_id); | |
| 402 | SetEvent (channel->data_avail_event); | |
|
16042
3c83dae9def2
Avoid trying to unlock the mutex after it has been destroyed.
Daniel Atallah <datallah@pidgin.im>
parents:
15884
diff
changeset
|
403 | UNLOCK (channel->mutex); |
| 14792 | 404 | g_io_channel_unref ((GIOChannel *)channel); |
| 405 | ||
| 406 | /* No need to call _endthreadex(), the actual thread starter routine | |
| 407 | * in MSVCRT (see crt/src/threadex.c:_threadstartex) calls | |
| 408 | * _endthreadex() for us. | |
| 409 | */ | |
| 410 | ||
| 411 | return 0; | |
| 412 | } | |
| 413 | ||
| 414 | static gboolean | |
| 415 | g_io_win32_prepare (GSource *source, | |
| 416 | gint *timeout) | |
| 417 | { | |
| 418 | GIOWin32Watch *watch = (GIOWin32Watch *)source; | |
| 419 | GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel); | |
| 420 | GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
421 | |
| 14792 | 422 | *timeout = -1; |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
423 | |
| 14792 | 424 | if (channel->debug) |
| 425 | g_print ("g_io_win32_prepare: for thread %#x buffer_condition:%#x\n" | |
| 426 | " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n", | |
| 427 | channel->thread_id, buffer_condition, | |
| 428 | watch->pollfd.events, watch->pollfd.revents, channel->revents); | |
| 429 | ||
| 430 | if (channel->type == G_IO_WIN32_FILE_DESC) | |
| 431 | { | |
| 432 | LOCK (channel->mutex); | |
| 433 | if (channel->running && channel->wrp == channel->rdp) | |
| 434 | { | |
| 435 | if (channel->debug) | |
| 436 | g_print ("g_io_win32_prepare: for thread %#x, setting channel->revents = 0\n", | |
| 437 | channel->thread_id); | |
| 438 | channel->revents = 0; | |
| 439 | } | |
| 440 | UNLOCK (channel->mutex); | |
| 441 | } | |
| 442 | else if (channel->type == G_IO_WIN32_SOCKET) | |
| 443 | { | |
| 444 | LOCK (channel->mutex); | |
| 445 | channel->revents = 0; | |
| 446 | if (channel->debug) | |
| 447 | g_print ("g_io_win32_prepare: for thread %#x, setting data_avail_noticed\n", | |
| 448 | channel->thread_id); | |
| 449 | SetEvent (channel->data_avail_noticed_event); | |
| 450 | if (channel->debug) | |
| 451 | g_print ("g_io_win32_prepare: thread %#x, there.\n", | |
| 452 | channel->thread_id); | |
| 453 | UNLOCK (channel->mutex); | |
| 454 | } | |
| 455 | ||
| 456 | return ((watch->condition & buffer_condition) == watch->condition); | |
| 457 | } | |
| 458 | ||
| 459 | static gboolean | |
| 460 | g_io_win32_check (GSource *source) | |
| 461 | { | |
| 462 | MSG msg; | |
| 463 | GIOWin32Watch *watch = (GIOWin32Watch *)source; | |
| 464 | GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel; | |
| 465 | GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel); | |
| 466 | ||
| 467 | if (channel->debug) | |
| 468 | g_print ("g_io_win32_check: for thread %#x buffer_condition:%#x\n" | |
| 469 | " watch->pollfd.events:%#x watch->pollfd.revents:%#x channel->revents:%#x\n", | |
| 470 | channel->thread_id, buffer_condition, | |
| 471 | watch->pollfd.events, watch->pollfd.revents, channel->revents); | |
| 472 | ||
| 473 | if (channel->type != G_IO_WIN32_WINDOWS_MESSAGES) | |
| 474 | { | |
| 475 | watch->pollfd.revents = (watch->pollfd.events & channel->revents); | |
| 476 | } | |
| 477 | else | |
| 478 | { | |
| 479 | return (PeekMessage (&msg, channel->hwnd, 0, 0, PM_NOREMOVE)); | |
| 480 | } | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
481 | |
| 14792 | 482 | if (channel->type == G_IO_WIN32_SOCKET) |
| 483 | { | |
| 484 | LOCK (channel->mutex); | |
| 485 | if (channel->debug) | |
| 486 | g_print ("g_io_win32_check: thread %#x, resetting data_avail\n", | |
| 487 | channel->thread_id); | |
| 488 | ResetEvent (channel->data_avail_event); | |
| 489 | if (channel->debug) | |
| 490 | g_print ("g_io_win32_check: thread %#x, there.\n", | |
| 491 | channel->thread_id); | |
| 492 | UNLOCK (channel->mutex); | |
| 493 | } | |
| 494 | ||
| 495 | return ((watch->pollfd.revents | buffer_condition) & watch->condition); | |
| 496 | } | |
| 497 | ||
| 498 | static gboolean | |
| 499 | g_io_win32_dispatch (GSource *source, | |
| 500 | GSourceFunc callback, | |
| 501 | gpointer user_data) | |
| 502 | { | |
| 503 | GIOFunc func = (GIOFunc)callback; | |
| 504 | GIOWin32Watch *watch = (GIOWin32Watch *)source; | |
| 505 | GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
506 | |
| 14792 | 507 | if (!func) |
| 508 | { | |
| 509 | g_warning (G_STRLOC ": GIOWin32Watch dispatched without callback\n" | |
| 510 | "You must call g_source_connect()."); | |
| 511 | return FALSE; | |
| 512 | } | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
513 | |
| 14792 | 514 | return (*func) (watch->channel, |
| 515 | (watch->pollfd.revents | buffer_condition) & watch->condition, | |
| 516 | user_data); | |
| 517 | } | |
| 518 | ||
| 519 | static void | |
| 520 | g_io_win32_finalize (GSource *source) | |
| 521 | { | |
| 522 | GIOWin32Watch *watch = (GIOWin32Watch *)source; | |
| 523 | GIOWin32Channel *channel = (GIOWin32Channel *)watch->channel; | |
| 524 | char send_buffer[] = "f"; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
525 | |
| 14792 | 526 | LOCK (channel->mutex); |
| 527 | if (channel->debug) | |
| 528 | g_print ("g_io_win32_finalize: channel with thread %#x\n", | |
| 529 | channel->thread_id); | |
| 530 | ||
| 531 | channel->watches = g_slist_remove (channel->watches, watch); | |
| 532 | ||
| 533 | SetEvent (channel->data_avail_noticed_event); | |
| 534 | if (channel->type == G_IO_WIN32_SOCKET) | |
| 535 | { | |
| 536 | /* Tell select_thread() to exit */ | |
| 537 | channel->needs_close = 1; | |
| 538 | /* Wake up select_thread() from its blocking select() */ | |
| 539 | send (channel->reset_send, send_buffer, sizeof (send_buffer), 0); | |
| 540 | } | |
| 541 | ||
| 542 | UNLOCK (channel->mutex); | |
| 543 | g_io_channel_unref (watch->channel); | |
| 544 | } | |
| 545 | ||
|
22869
3fc7a5fc49eb
Include winsock2.h before windows.h is included to avoid winsock.h being
Daniel Atallah <datallah@pidgin.im>
parents:
19859
diff
changeset
|
546 | static GSourceFuncs wp_g_io_watch_funcs = { |
| 14792 | 547 | g_io_win32_prepare, |
| 548 | g_io_win32_check, | |
| 549 | g_io_win32_dispatch, | |
| 550 | g_io_win32_finalize, | |
| 551 | NULL, NULL | |
| 552 | }; | |
| 553 | ||
| 554 | static GSource * | |
| 555 | g_io_win32_create_watch (GIOChannel *channel, | |
| 556 | GIOCondition condition, | |
| 557 | unsigned (__stdcall *thread) (void *parameter)) | |
| 558 | { | |
| 559 | GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel; | |
| 560 | GIOWin32Watch *watch; | |
| 561 | GSource *source; | |
| 562 | char send_buffer[] = "c"; | |
| 563 | ||
|
22869
3fc7a5fc49eb
Include winsock2.h before windows.h is included to avoid winsock.h being
Daniel Atallah <datallah@pidgin.im>
parents:
19859
diff
changeset
|
564 | source = g_source_new (&wp_g_io_watch_funcs, sizeof (GIOWin32Watch)); |
| 14792 | 565 | watch = (GIOWin32Watch *)source; |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
566 | |
| 14792 | 567 | watch->channel = channel; |
| 568 | g_io_channel_ref (channel); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
569 | |
| 14792 | 570 | watch->condition = condition; |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
571 | |
| 14792 | 572 | if (win32_channel->data_avail_event == NULL) |
| 573 | create_events (win32_channel); | |
| 574 | ||
| 575 | watch->pollfd.fd = (gint) win32_channel->data_avail_event; | |
| 576 | watch->pollfd.events = condition; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
577 | |
| 14792 | 578 | if (win32_channel->debug) |
| 579 | g_print ("g_io_win32_create_watch: fd:%d condition:%#x handle:%#x\n", | |
| 580 | win32_channel->fd, condition, watch->pollfd.fd); | |
| 581 | ||
| 582 | LOCK (win32_channel->mutex); | |
| 583 | win32_channel->watches = g_slist_append (win32_channel->watches, watch); | |
| 584 | ||
| 585 | if (win32_channel->thread_id == 0) | |
| 586 | create_thread (win32_channel, condition, thread); | |
| 587 | else | |
| 588 | send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0); | |
| 589 | ||
| 590 | g_source_add_poll (source, &watch->pollfd); | |
| 591 | UNLOCK (win32_channel->mutex); | |
| 592 | ||
| 593 | return source; | |
| 594 | } | |
| 595 | ||
| 596 | static void | |
| 597 | g_io_win32_free (GIOChannel *channel) | |
| 598 | { | |
| 599 | GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
600 | |
| 14792 | 601 | if (win32_channel->debug) |
| 602 | g_print ("thread %#x: freeing channel, fd: %d\n", | |
| 603 | win32_channel->thread_id, | |
| 604 | win32_channel->fd); | |
| 605 | ||
| 606 | if (win32_channel->reset_send && win32_channel->reset_send != INVALID_SOCKET) | |
| 607 | closesocket (win32_channel->reset_send); | |
| 608 | if (win32_channel->reset_recv && win32_channel->reset_recv != INVALID_SOCKET) | |
| 609 | closesocket (win32_channel->reset_recv); | |
| 610 | if (win32_channel->data_avail_event) | |
| 611 | CloseHandle (win32_channel->data_avail_event); | |
| 612 | if (win32_channel->space_avail_event) | |
| 613 | CloseHandle (win32_channel->space_avail_event); | |
| 614 | if (win32_channel->data_avail_noticed_event) | |
| 615 | CloseHandle (win32_channel->data_avail_noticed_event); | |
| 616 | DeleteCriticalSection (&win32_channel->mutex); | |
| 617 | ||
| 618 | g_free (win32_channel->buffer); | |
| 619 | g_slist_free (win32_channel->watches); | |
| 620 | g_free (win32_channel); | |
| 621 | } | |
| 622 | ||
| 623 | static GIOStatus | |
| 624 | g_io_win32_sock_read (GIOChannel *channel, | |
| 625 | gchar *buf, | |
| 626 | gsize count, | |
| 627 | gsize *bytes_read, | |
| 628 | GError **err) | |
| 629 | { | |
| 630 | GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel; | |
| 631 | gint result; | |
| 632 | GIOChannelError error = G_IO_STATUS_NORMAL; | |
| 633 | GIOStatus internal_status = G_IO_STATUS_NORMAL; | |
| 634 | char send_buffer[] = "sr"; | |
| 635 | ||
| 636 | if (win32_channel->debug) | |
| 637 | g_print ("g_io_win32_sock_read: sockfd:%d count:%d\n", | |
| 638 | win32_channel->fd, count); | |
| 639 | #ifdef WE_NEED_TO_HANDLE_WSAEINTR | |
| 640 | repeat: | |
| 641 | #endif | |
| 642 | result = recv (win32_channel->fd, buf, count, 0); | |
| 643 | ||
| 644 | if (win32_channel->debug) | |
| 645 | g_print ("g_io_win32_sock_read: recv:%d\n", result); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
646 | |
| 14792 | 647 | if (result == SOCKET_ERROR) |
| 648 | { | |
| 649 | *bytes_read = 0; | |
| 650 | ||
| 651 | switch (WSAGetLastError ()) | |
| 652 | { | |
| 653 | case WSAEINVAL: | |
| 654 | error = G_IO_CHANNEL_ERROR_INVAL; | |
| 655 | break; | |
| 656 | case WSAEWOULDBLOCK: | |
| 657 | return G_IO_STATUS_AGAIN; | |
| 658 | #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */ | |
| 659 | case WSAEINTR: | |
| 660 | goto repeat; | |
| 661 | #endif | |
| 662 | default: | |
| 663 | error = G_IO_CHANNEL_ERROR_FAILED; | |
| 664 | break; | |
| 665 | } | |
| 666 | g_set_error (err, G_IO_CHANNEL_ERROR, error, "Socket read error"); | |
| 667 | internal_status = G_IO_STATUS_ERROR; | |
| 668 | /* FIXME get all errors, better error messages */ | |
| 669 | } | |
| 670 | else | |
| 671 | { | |
| 672 | *bytes_read = result; | |
| 673 | if (result == 0) | |
| 674 | internal_status = G_IO_STATUS_EOF; | |
| 675 | } | |
| 676 | ||
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
677 | if ((internal_status == G_IO_STATUS_EOF) || |
| 14792 | 678 | (internal_status == G_IO_STATUS_ERROR)) |
| 679 | { | |
| 680 | LOCK (win32_channel->mutex); | |
| 681 | SetEvent (win32_channel->data_avail_noticed_event); | |
| 682 | win32_channel->needs_close = 1; | |
| 683 | send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0); | |
| 684 | UNLOCK (win32_channel->mutex); | |
| 685 | } | |
| 686 | return internal_status; | |
| 687 | } | |
| 688 | ||
| 689 | static GIOStatus | |
| 690 | g_io_win32_sock_write (GIOChannel *channel, | |
| 691 | const gchar *buf, | |
| 692 | gsize count, | |
| 693 | gsize *bytes_written, | |
| 694 | GError **err) | |
| 695 | { | |
| 696 | GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel; | |
| 697 | gint result; | |
| 698 | GIOChannelError error = G_IO_STATUS_NORMAL; | |
| 699 | char send_buffer[] = "sw"; | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
700 | |
| 14792 | 701 | if (win32_channel->debug) |
| 702 | g_print ("g_io_win32_sock_write: sockfd:%d count:%d\n", | |
| 703 | win32_channel->fd, count); | |
| 704 | #ifdef WE_NEED_TO_HANDLE_WSAEINTR | |
| 705 | repeat: | |
| 706 | #endif | |
| 707 | result = send (win32_channel->fd, buf, count, 0); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
708 | |
| 14792 | 709 | if (win32_channel->debug) |
| 710 | g_print ("g_io_win32_sock_write: send:%d\n", result); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
711 | |
| 14792 | 712 | if (result == SOCKET_ERROR) |
| 713 | { | |
| 714 | *bytes_written = 0; | |
| 715 | ||
| 716 | switch (WSAGetLastError ()) | |
| 717 | { | |
| 718 | case WSAEINVAL: | |
| 719 | error = G_IO_CHANNEL_ERROR_INVAL; | |
| 720 | break; | |
| 721 | case WSAEWOULDBLOCK: | |
| 722 | return G_IO_STATUS_AGAIN; | |
| 723 | #ifdef WE_NEED_TO_HANDLE_WSAEINTR /* not anymore with wsock2 ? */ | |
| 724 | case WSAEINTR: | |
| 725 | goto repeat; | |
| 726 | #endif | |
| 727 | default: | |
| 728 | error = G_IO_CHANNEL_ERROR_FAILED; | |
| 729 | break; | |
| 730 | } | |
| 731 | g_set_error (err, G_IO_CHANNEL_ERROR, error, "Socket write error"); | |
| 732 | LOCK (win32_channel->mutex); | |
| 733 | SetEvent (win32_channel->data_avail_noticed_event); | |
| 734 | win32_channel->needs_close = 1; | |
| 735 | send (win32_channel->reset_send, send_buffer, sizeof (send_buffer), 0); | |
| 736 | UNLOCK (win32_channel->mutex); | |
| 737 | return G_IO_STATUS_ERROR; | |
| 738 | /* FIXME get all errors, better error messages */ | |
| 739 | } | |
| 740 | else | |
| 741 | { | |
| 742 | *bytes_written = result; | |
| 743 | ||
| 744 | return G_IO_STATUS_NORMAL; | |
| 745 | } | |
| 746 | } | |
| 747 | ||
| 748 | static GIOStatus | |
| 749 | g_io_win32_sock_close (GIOChannel *channel, | |
| 750 | GError **err) | |
| 751 | { | |
| 752 | GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel; | |
| 753 | ||
| 754 | LOCK (win32_channel->mutex); | |
| 755 | if (win32_channel->running) | |
| 756 | { | |
| 757 | if (win32_channel->debug) | |
| 758 | g_print ("thread %#x: running, marking for later close\n", | |
| 759 | win32_channel->thread_id); | |
| 760 | win32_channel->running = FALSE; | |
| 761 | win32_channel->needs_close = TRUE; | |
| 762 | SetEvent(win32_channel->data_avail_noticed_event); | |
| 763 | } | |
| 764 | if (win32_channel->fd != -1) | |
| 765 | { | |
| 766 | if (win32_channel->debug) | |
| 767 | g_print ("thread %#x: closing socket %d\n", | |
| 768 | win32_channel->thread_id, | |
| 769 | win32_channel->fd); | |
|
31294
73607ab89c6f
Remove trailing whitespace
Richard Laager <rlaager@pidgin.im>
parents:
24231
diff
changeset
|
770 | |
| 14792 | 771 | closesocket (win32_channel->fd); |
| 772 | win32_channel->fd = -1; | |
| 773 | } | |
| 774 | UNLOCK (win32_channel->mutex); | |
| 775 | ||
| 776 | /* FIXME error detection? */ | |
| 777 | ||
| 778 | return G_IO_STATUS_NORMAL; | |
| 779 | } | |
| 780 | ||
| 781 | static GSource * | |
| 782 | g_io_win32_sock_create_watch (GIOChannel *channel, | |
| 783 | GIOCondition condition) | |
| 784 | { | |
| 785 | return g_io_win32_create_watch (channel, condition, select_thread); | |
| 786 | } | |
| 787 | ||
| 788 | static GIOStatus | |
| 789 | g_io_win32_set_flags (GIOChannel *channel, | |
| 790 | GIOFlags flags, | |
| 791 | GError **err) | |
| 792 | { | |
| 793 | GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel; | |
| 794 | ||
| 795 | if (win32_channel->debug) | |
| 796 | { | |
| 797 | g_print ("g_io_win32_set_flags: "); | |
| 798 | g_win32_print_gioflags (flags); | |
| 799 | g_print ("\n"); | |
| 800 | } | |
| 801 | ||
| 802 | g_warning ("g_io_win32_set_flags () not implemented.\n"); | |
| 803 | ||
| 804 | return G_IO_STATUS_NORMAL; | |
| 805 | } | |
| 806 | ||
| 807 | static GIOFlags | |
| 808 | g_io_win32_sock_get_flags (GIOChannel *channel) | |
| 809 | { | |
| 810 | /* XXX Could do something here. */ | |
| 811 | return 0; | |
| 812 | } | |
| 813 | ||
| 814 | static GIOFuncs win32_channel_sock_funcs = { | |
| 815 | g_io_win32_sock_read, | |
| 816 | g_io_win32_sock_write, | |
| 817 | NULL, | |
| 818 | g_io_win32_sock_close, | |
| 819 | g_io_win32_sock_create_watch, | |
| 820 | g_io_win32_free, | |
| 821 | g_io_win32_set_flags, | |
| 822 | g_io_win32_sock_get_flags, | |
| 823 | }; | |
| 824 | ||
| 825 | GIOChannel * | |
| 15884 | 826 | wpurple_g_io_channel_win32_new_socket (int socket) |
| 14792 | 827 | { |
| 828 | GIOWin32Channel *win32_channel = g_new (GIOWin32Channel, 1); | |
| 829 | GIOChannel *channel = (GIOChannel *)win32_channel; | |
| 830 | ||
| 831 | g_io_channel_init (channel); | |
| 832 | g_io_channel_win32_init (win32_channel); | |
| 833 | init_reset_sockets (win32_channel); | |
| 834 | if (win32_channel->debug) | |
| 835 | g_print ("g_io_channel_win32_new_socket: sockfd:%d\n", socket); | |
| 836 | channel->funcs = &win32_channel_sock_funcs; | |
| 837 | win32_channel->type = G_IO_WIN32_SOCKET; | |
| 838 | win32_channel->fd = socket; | |
| 839 | ||
| 840 | /* XXX: check this */ | |
| 841 | channel->is_readable = TRUE; | |
| 842 | channel->is_writeable = TRUE; | |
| 843 | ||
| 844 | channel->is_seekable = FALSE; | |
| 845 | ||
| 846 | return channel; | |
| 847 | } | |
| 848 | ||
| 849 | #if 0 | |
| 850 | void | |
| 851 | g_io_channel_win32_set_debug (GIOChannel *channel, | |
| 852 | gboolean flag) | |
| 853 | { | |
| 854 | GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel; | |
| 855 | ||
| 856 | win32_channel->debug = flag; | |
| 857 | } | |
| 858 | #endif | |
| 859 |