~vcs-imports/gnome-media/main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/* GNet - Networking library
 * Copyright (C) 2000  David Helder
 * Copyright (C) 2000  Andrew Lanoix
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */

#include "gnet-private.h"
#include "gnet.h"


/**
 *  gnet_private_inetaddr_sockaddr_new:
 *  @sa: sockaddr struct to create InetAddr from.
 *
 *  Create an internet address from a sockaddr struct.  WARNING: This
 *  may go away or be hidden in a future version.
 *
 *  Returns: a new InetAddr, or NULL if there was a failure.
 *
 **/
GInetAddr*
#ifdef ENABLE_IPV6
gnet_private_inetaddr_sockaddr_new(const struct sockaddr_storage sa)
#else
gnet_private_inetaddr_sockaddr_new(const struct sockaddr sa)
#endif
{
  GInetAddr* ia = g_new0(GInetAddr, 1);

  ia->sa = sa;

  return ia;
}



/**
 *  gnet_private_inetaddr_get_sockaddr:
 *  @ia: Address to get the sockaddr of.
 *
 *  Get the sockaddr struct.  WARNING: This may go away or be hidden
 *  in a future version.
 *
 *  Returns: the sockaddr struct
 **/
#ifdef ENABLE_IPV6
struct sockaddr_storage
#else
struct sockaddr
#endif
gnet_private_inetaddr_get_sockaddr(const GInetAddr* ia)
{
  g_assert(ia != NULL);

  return ia->sa;
}



/* TODO: Need to port this to Solaris, Windows */

#if 0

/**
 *  gnet_udp_socket_get_MTU:
 *  @us: GUdpSocket to get MTU from.
 *
 *  Get the MTU for outgoing packets.
 *
 *  Returns: MTU; -1 if unknown.
 *
 **/
gint
gnet_udp_socket_get_MTU(GUdpSocket* us)
{
  struct ifreq ifr;

  /* FIX: Not everyone has ethernet, right? */
  strncpy (ifr.ifr_name, "eth0", sizeof (ifr.ifr_name));
  if (!ioctl(us->sockfd, SIOCGIFMTU, &ifr))
    return ifr.ifr_mtu;

  return -1;
}

#endif




/*

   Below is the Windows specific code and a general idea of how
   things were implemented.

   In the Windows Port, not much was needed to be done for the sync
   TCP, UDP and Mcast functions. The Async TCP functions needed to be
   rewritten.

   To accomplish this we use one GIOchannel to handle all the TCP
   async functions and their callbacks. This is different from the
   forking *nix version of Gnet.

   The async functions do all the 'parent' code and the functions then
   add the "state" information (containing the user and Gnet's
   bookkeeping data) into private global hashes.  Then they do the
   appropriate WinSock2 WSAAsync* function (the 'child'), which will
   returns a HANDLE.  This HANDLE is typecasted and returned to the
   user as their ID.  This number will also double as a key into the
   hash. Note that WinSock2 WSAAsyncGetXY task handles are
   unique. (sockfd is used in gnet_tcp_socket_new_async)

   When the WinSock async call is complete, it posts a message that
   will be pickup by gnet_MainCallBack(). The wParam in the MSG
   contains the original async task handle returned by the WinSock2
   async functions. The high 16 bits of lParm contain the error
   information. We use the macros defined in WinSock2.h to extract
   this. The 'message' parameter identifies which async callback we
   are dealing with.  Typcased names of these are #define-d in
   gnet.h. We then do any necessary work and then call the gnet async
   function's callback, removing the data record from the correct
   hash. Then these callbacks call the user's callback with the user's
   data.

   If the user calls an async cancel function, then we remove and
   destroy the data from the hash.  A WinSock 2 async call is
   destroyed by WSACancelAsyncRequest(HANDLE);.

	 UPDATE: As of March 2001, many of the problems in glib's iochannel
	 code were fixed so only a few gnet functions still use this method.

   I believe I have taken care of the multi-threaded issues by using
   mutexes.

*/

#ifdef GNET_WIN32

WNDCLASSEX gnetWndClass;
HWND  gnet_hWnd;
guint gnet_io_watch_ID;
GIOChannel *gnet_iochannel;

GHashTable *gnet_hash;
HANDLE gnet_Mutex;
HANDLE gnet_hostent_Mutex;


int
gnet_MainCallBack(GIOChannel *iochannel, GIOCondition condition, void *nodata)
{
  MSG msg;

  gpointer data;
  GInetAddrAsyncState *IAstate;
  GInetAddrReverseAsyncState *IARstate;

  int i;

  /*Take the msg off the message queue */
  i = PeekMessage (&msg, gnet_hWnd, 0, 0, PM_REMOVE);
  if (!i)
    return 1; /* you have a buggy version of glib that is calling this func when it shouldn't*/

  switch (msg.message)
    {
    case IA_NEW_MSG:
      {
	WaitForSingleObject(gnet_Mutex, INFINITE);
	data = g_hash_table_lookup(gnet_hash, (gpointer)msg.wParam);
	g_hash_table_remove(gnet_hash, (gpointer)msg.wParam);
	ReleaseMutex(gnet_Mutex);

	IAstate = (GInetAddrAsyncState*) data;
	IAstate->errorcode = WSAGETASYNCERROR(msg.lParam); /* NULL if OK */

	/* Now call the callback function */
	gnet_inetaddr_new_async_cb(NULL, G_IO_IN, (gpointer)IAstate);

	break;
      }
    case GET_NAME_MSG:
      {
	WaitForSingleObject(gnet_Mutex, INFINITE);
	data = g_hash_table_lookup(gnet_hash, (gpointer)msg.wParam);
	g_hash_table_remove(gnet_hash, (gpointer)msg.wParam);
	ReleaseMutex(gnet_Mutex);

	IARstate = (GInetAddrReverseAsyncState*) data;
	IARstate->errorcode = WSAGETASYNCERROR(msg.lParam); /* NULL if OK */

	/* Now call the callback function */
	gnet_inetaddr_get_name_async_cb(NULL, G_IO_IN, (gpointer)IARstate);
	break;
      }
    }

  return 1;
}

/* Not used but required*/
LRESULT CALLBACK
GnetWndProc(HWND hwnd,        /* handle to window */
	    UINT uMsg,        /* message identifier */
	    WPARAM wParam,    /* first message parameter */
	    LPARAM lParam)    /* second message parameter */
{

    switch (uMsg)
    {
        case WM_CREATE:
            /* Initialize the window. */
            return 0;

        case WM_PAINT:
            /* Paint the window's client area. */
            return 0;

        case WM_SIZE:
            /* Set the size and position of the window. */
            return 0;

        case WM_DESTROY:
            /* Clean up window-specific data objects. */
            return 0;

        /*
          Process other messages.
        */

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    return 0;
}


BOOL WINAPI
DllMain(HINSTANCE hinstDLL,  /* handle to DLL module */
	DWORD fdwReason,     /* reason for calling functionm */
	LPVOID lpvReserved   /* reserved */)
{

  switch(fdwReason)
    {
    case DLL_PROCESS_ATTACH:
      /* The DLL is being mapped into process's address space */
      /* Do any required initialization on a per application basis, return FALSE if failed */
      {
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD( 2, 0 );

	err = WSAStartup( wVersionRequested, &wsaData );
	if ( err != 0 )
	  {
				/* Tell the user that we could not find a usable */
				/* WinSock DLL.                                  */
	    return FALSE;
	  }

	/* Confirm that the WinSock DLL supports 2.0.*/
	/* Note that if the DLL supports versions greater    */
	/* than 2.0 in addition to 2.0, it will still return */
	/* 2.0 in wVersion since that is the version we      */
	/* requested.                                        */

	if ( LOBYTE( wsaData.wVersion ) != 2 ||
	     HIBYTE( wsaData.wVersion ) != 0 ) {
	  /* Tell the user that we could not find a usable */
	  /* WinSock DLL.                                  */
	  WSACleanup();
	  return FALSE;
	}

	/* The WinSock DLL is acceptable. Proceed. */

	/* Setup and register a windows class that we use for our GIOchannel */
	gnetWndClass.cbSize = sizeof(WNDCLASSEX);
	gnetWndClass.style = CS_SAVEBITS; /* doesn't matter, need something? */
	gnetWndClass.lpfnWndProc = (WNDPROC) GnetWndProc;
	gnetWndClass.cbClsExtra = 0;
	gnetWndClass.cbWndExtra = 0;
	gnetWndClass.hInstance = hinstDLL;
	gnetWndClass.hIcon = NULL;
	gnetWndClass.hCursor = NULL;
	gnetWndClass.hbrBackground = NULL;
	gnetWndClass.lpszMenuName = NULL;
	gnetWndClass.lpszClassName = "Gnet";
	gnetWndClass.hIconSm = NULL;

	if (!RegisterClassEx(&gnetWndClass))
	  {
	    return FALSE;
	  }

	gnet_hWnd  = CreateWindowEx
	  (
	   0,
	   "Gnet",
	   "none",
	   WS_OVERLAPPEDWINDOW,
	   CW_USEDEFAULT,
	   CW_USEDEFAULT,
	   CW_USEDEFAULT,
	   CW_USEDEFAULT,
	   (HWND) NULL,
	   (HMENU) NULL,
	   hinstDLL,
	   (LPVOID) NULL);

	if (!gnet_hWnd)
	  {
	    return FALSE;
	  }

	gnet_iochannel = g_io_channel_win32_new_messages((unsigned int)gnet_hWnd);

	/* Add a watch */
	gnet_io_watch_ID = g_io_add_watch(gnet_iochannel,
					  (GIOCondition)(G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL),
					  gnet_MainCallBack,
					  NULL);

	gnet_hash = g_hash_table_new(NULL, NULL);

	gnet_Mutex = CreateMutex(
				 NULL,                       /* no security attributes */
				 FALSE,                      /* initially not owned */
				 "gnet_Mutex");  /* name of mutex */

	if (gnet_Mutex == NULL)
	  {
	    return FALSE;
	  }

	gnet_hostent_Mutex = CreateMutex(
					 NULL,                       /* no security attributes */
					 FALSE,                      /* initially not owned */
					 "gnet_hostent_Mutex");  /* name of mutex */

	if (gnet_hostent_Mutex == NULL)
	  {
	    return FALSE;
	  }
	break;
      }
    case DLL_THREAD_ATTACH:
      /* A thread is created. Do any required initialization on a per thread basis*/
      {
	/*Nothing needs to be done. */
	break;
      }
    case DLL_THREAD_DETACH:
      /* Thread exits with  cleanup */
      {
	/*Nothing needs to be done. */
	break;
      }
    case DLL_PROCESS_DETACH:
      /* The DLL unmapped from process's address space. Do necessary cleanup */
      {
	g_source_remove(gnet_io_watch_ID);
	g_free(gnet_iochannel);
	DestroyWindow(gnet_hWnd);

	/*CleanUp WinSock 2 */
	WSACleanup();

	break;
      }
    }

  return TRUE;
}

#endif