| 1 |
/*
|
| 2 |
* WSAAsyncGetAddrInfo.c -- asynchronous version of getaddrinfo
|
| 3 |
* Copyright(C) 2000-2003 Jun-ya Kato <kato@win6.jp>
|
| 4 |
*/
|
| 5 |
#include <winsock2.h>
|
| 6 |
#include <ws2tcpip.h>
|
| 7 |
#include <windows.h>
|
| 8 |
#include <process.h>
|
| 9 |
#include "WSAASyncGetAddrInfo.h"
|
| 10 |
|
| 11 |
static unsigned __stdcall getaddrinfo_thread(void FAR * p);
|
| 12 |
|
| 13 |
HANDLE WSAAsyncGetAddrInfo(HWND hWnd, unsigned int wMsg,
|
| 14 |
const char FAR * hostname,
|
| 15 |
const char FAR * portname,
|
| 16 |
struct addrinfo FAR * hints,
|
| 17 |
struct addrinfo FAR * FAR * res)
|
| 18 |
{
|
| 19 |
HANDLE thread;
|
| 20 |
unsigned tid;
|
| 21 |
struct getaddrinfo_args FAR * ga;
|
| 22 |
|
| 23 |
/*
|
| 24 |
* allocate structure to pass args to sub-thread dynamically
|
| 25 |
* WSAAsyncGetAddrInfo() is reentrant
|
| 26 |
*/
|
| 27 |
if ((ga = (struct getaddrinfo_args FAR *)malloc(sizeof(struct getaddrinfo_args))) == NULL)
|
| 28 |
return NULL;
|
| 29 |
|
| 30 |
/* packing arguments struct addrinfo_args */
|
| 31 |
ga->hWnd = hWnd;
|
| 32 |
ga->wMsg = wMsg;
|
| 33 |
ga->hostname = hostname;
|
| 34 |
ga->portname = portname;
|
| 35 |
ga->hints = hints;
|
| 36 |
ga->res = res;
|
| 37 |
|
| 38 |
ga->lpHandle = (HANDLE FAR *)malloc(sizeof(HANDLE));
|
| 39 |
if (ga->lpHandle == NULL)
|
| 40 |
return NULL;
|
| 41 |
|
| 42 |
/* create sub-thread running getaddrinfo() */
|
| 43 |
thread = (HANDLE)_beginthreadex(NULL, 0, getaddrinfo_thread, ga, CREATE_SUSPENDED, &tid);
|
| 44 |
*ga->lpHandle = (HANDLE)thread;
|
| 45 |
ResumeThread(thread);
|
| 46 |
|
| 47 |
/* return thread handle */
|
| 48 |
if (thread == 0) {
|
| 49 |
free(ga->lpHandle);
|
| 50 |
free(ga);
|
| 51 |
return NULL;
|
| 52 |
} else
|
| 53 |
return (HANDLE)thread;
|
| 54 |
}
|
| 55 |
|
| 56 |
static unsigned __stdcall getaddrinfo_thread(void FAR * p)
|
| 57 |
{
|
| 58 |
int gai;
|
| 59 |
HWND hWnd;
|
| 60 |
unsigned int wMsg;
|
| 61 |
const char FAR * hostname;
|
| 62 |
const char FAR * portname;
|
| 63 |
struct addrinfo FAR * hints;
|
| 64 |
struct addrinfo FAR * FAR * res;
|
| 65 |
struct getaddrinfo_args FAR * ga;
|
| 66 |
|
| 67 |
/* unpacking arguments */
|
| 68 |
ga = (struct getaddrinfo_args FAR *)p;
|
| 69 |
hWnd = ga->hWnd;
|
| 70 |
wMsg = ga->wMsg;
|
| 71 |
hostname = ga->hostname;
|
| 72 |
portname = ga->portname;
|
| 73 |
hints = ga->hints;
|
| 74 |
res = ga->res;
|
| 75 |
|
| 76 |
/* call getaddrinfo */
|
| 77 |
gai = getaddrinfo(hostname, portname, hints, res);
|
| 78 |
|
| 79 |
/* send value of gai as message to window hWnd */
|
| 80 |
PostMessage(hWnd, wMsg, (WPARAM)*ga->lpHandle, MAKELPARAM(0, gai));
|
| 81 |
|
| 82 |
free(ga->lpHandle);
|
| 83 |
free(p);
|
| 84 |
|
| 85 |
return 0;
|
| 86 |
}
|