⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 async.c

📁 ReactOS是一些高手根据Windows XP的内核编写出的类XP。内核实现机理和API函数调用几乎相同。甚至可以兼容XP的程序。喜欢研究系统内核的人可以看一看。
💻 C
📖 第 1 页 / 共 2 页
字号:
static HANDLE16 __ws_async_handle = 0xdead;/* Generic async query struct. we use symbolic names for the different queries * for readability. */typedef struct _async_query {	HWND16		hWnd;	UINT16		uMsg;	LPCSTR		ptr1;#define host_name	ptr1#define host_addr	ptr1#define serv_name	ptr1#define proto_name	ptr1	LPCSTR		ptr2;#define serv_proto	ptr2	int		int1;#define host_len	int1#define proto_number	int1#define serv_port	int1	int		int2;#define host_type	int2	SEGPTR		sbuf;	INT16		sbuflen;	HANDLE16	async_handle;	int		flags;	int		qt;    char xbuf[1];} async_query;/**************************************************************************** * The async query function. * * It is either called as a thread startup routine or directly. It has * to free the passed arg from the process heap and PostMessageA the async * result or the error code. * * FIXME: *	- errorhandling not verified. */static DWORD WINAPI _async_queryfun(LPVOID arg) {	async_query	*aq = (async_query*)arg;	int		size = 0;	WORD		fail = 0;	char		*targetptr = (HB_WIN32(aq)?(char*)aq->sbuf:0/*(char*)MapSL(aq->sbuf)*/);	switch (aq->flags & AQ_GETMASK) {	case AQ_GETHOST: {			struct hostent *he;			char *copy_hostent = targetptr;                        char buf[100];                        if( !(aq->host_name)) {                            aq->host_name = buf;                            if( gethostname( buf, 100) == -1) {                                fail = WSAENOBUFS; /* appropriate ? */                                break;                            }                        }			he = (aq->flags & AQ_NAME) ?				gethostbyname(aq->host_name):				gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type);                        if (!he) fail = WSAGetLastError();			if (he) {				size = WS_copy_he(copy_hostent,(char*)aq->sbuf,aq->sbuflen,he,aq->flags);				if (size < 0) {					fail = WSAENOBUFS;					size = -size;				}			}		}		break;	case AQ_GETPROTO: {#if defined(HAVE_GETPROTOBYNAME) && defined(HAVE_GETPROTOBYNUMBER)			struct protoent *pe;			char *copy_protoent = targetptr;			pe = (aq->flags & AQ_NAME)?				getprotobyname(aq->proto_name) :				getprotobynumber(aq->proto_number);			if (pe) {				size = WS_copy_pe(copy_protoent,(char*)aq->sbuf,aq->sbuflen,pe,aq->flags);				if (size < 0) {					fail = WSAENOBUFS;					size = -size;				}			} else {                            if (aq->flags & AQ_NAME)                                MESSAGE("protocol %s not found; You might want to add "                                        "this to /etc/protocols\n", debugstr_a(aq->proto_name) );                            else                                MESSAGE("protocol number %d not found; You might want to add "                                        "this to /etc/protocols\n", aq->proto_number );                            fail = WSANO_DATA;			}#else                        fail = WSANO_DATA;#endif		}		break;	case AQ_GETSERV: {			struct servent	*se;			char *copy_servent = targetptr;			se = (aq->flags & AQ_NAME)?				getservbyname(aq->serv_name,aq->serv_proto) :#ifdef HAVE_GETSERVBYPORT				getservbyport(aq->serv_port,aq->serv_proto);#else                                NULL;#endif			if (se) {				size = WS_copy_se(copy_servent,(char*)aq->sbuf,aq->sbuflen,se,aq->flags);				if (size < 0) {					fail = WSAENOBUFS;					size = -size;				}			} else {                            if (aq->flags & AQ_NAME)                                MESSAGE("service %s protocol %s not found; You might want to add "                                        "this to /etc/services\n", debugstr_a(aq->serv_name) ,                                        aq->serv_proto ? debugstr_a(aq->serv_proto ):"*");                            else                                MESSAGE("service on port %d protocol %s not found; You might want to add "                                        "this to /etc/services\n", aq->serv_port,                                        aq->serv_proto ? debugstr_a(aq->serv_proto ):"*");                            fail = WSANO_DATA;			}		}		break;	}	PostMessageA(HWND_32(aq->hWnd),aq->uMsg,(WPARAM) aq->async_handle,size|(fail<<16));	HeapFree(GetProcessHeap(),0,arg);	return 0;}/**************************************************************************** * The main async help function. * * It either starts a thread or just calls the function directly for platforms * with no thread support. This relies on the fact that PostMessage() does * not actually call the windowproc before the function returns. */static HANDLE16	__WSAsyncDBQuery(	HWND hWnd, UINT uMsg,INT int1,LPCSTR ptr1, INT int2, LPCSTR ptr2,	void *sbuf, INT sbuflen, UINT flags){        async_query*	aq;	char*		pto;	LPCSTR		pfm;	int		xbuflen = 0;	/* allocate buffer to copy protocol- and service name to */	/* note: this is done in the calling thread so we can return */	/* a decent error code if the Alloc fails */	switch (flags & AQ_MASKPTR1) {	case 0:							break;	case AQ_COPYPTR1:	xbuflen += int1;		break;	case AQ_DUPLOWPTR1:	xbuflen += strlen(ptr1) + 1;	break;	}	switch (flags & AQ_MASKPTR2) {	case 0:							break;	case AQ_COPYPTR2:	xbuflen += int2;		break;	case AQ_DUPLOWPTR2:	xbuflen += strlen(ptr2) + 1;	break;	}	if(!(aq = HeapAlloc(GetProcessHeap(),0,sizeof(async_query) + xbuflen))) {	        SetLastError(WSAEWOULDBLOCK); /* insufficient resources */		return 0;	}	pto = aq->xbuf;	if (ptr1) switch (flags & AQ_MASKPTR1) {	case 0:											break;	case AQ_COPYPTR1:   memcpy(pto, ptr1, int1); ptr1 = pto; pto += int1; 			break;	case AQ_DUPLOWPTR1: pfm = ptr1; ptr1 = pto; do *pto++ = tolower(*pfm); while (*pfm++);	break;	}	if (ptr2) switch (flags & AQ_MASKPTR2) {	case 0:											break;	case AQ_COPYPTR2:   memcpy(pto, ptr2, int2); ptr2 = pto; pto += int2;			break;	case AQ_DUPLOWPTR2: pfm = ptr2; ptr2 = pto; do *pto++ = tolower(*pfm); while (*pfm++);	break;	}	aq->hWnd	= HWND_16(hWnd);	aq->uMsg	= uMsg;	aq->int1	= int1;	aq->ptr1	= ptr1;	aq->int2	= int2;	aq->ptr2	= ptr2;	/* avoid async_handle = 0 */	aq->async_handle = (++__ws_async_handle ? __ws_async_handle : ++__ws_async_handle);	aq->flags	= flags;	aq->sbuf	= (SEGPTR)sbuf;	aq->sbuflen	= sbuflen;#if 1	if (CreateThread(NULL,0,_async_queryfun,aq,0,NULL) == INVALID_HANDLE_VALUE)#endif		_async_queryfun(aq);	return __ws_async_handle;}/*********************************************************************** *       WSAAsyncGetHostByAddr	(WINSOCK.102) */HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,                               INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen){	TRACE("hwnd %04x, msg %04x, addr %08x[%i]\n",	       hWnd, uMsg, (unsigned)addr , len );	return __WSAsyncDBQuery(HWND_32(hWnd),uMsg,len,addr,type,NULL,				(void*)sbuf,buflen,				AQ_NUMBER|AQ_COPYPTR1|AQ_WIN16|AQ_GETHOST);}/*********************************************************************** *       WSAAsyncGetHostByAddr        (WS2_32.102) */HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,                               INT len, INT type, LPSTR sbuf, INT buflen){	TRACE("hwnd %p, msg %04x, addr %08x[%i]\n",	       hWnd, uMsg, (unsigned)addr , len );	return WSA_H32( __WSAsyncDBQuery(hWnd,uMsg,len,addr,type,NULL,sbuf,buflen,				AQ_NUMBER|AQ_COPYPTR1|AQ_WIN32|AQ_GETHOST));}/*********************************************************************** *       WSAAsyncGetHostByName	(WINSOCK.103) */HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,                                      SEGPTR sbuf, INT16 buflen){	TRACE("hwnd %04x, msg %04x, host %s, buffer %i\n",	      hWnd, uMsg, (name)?name:"<null>", (int)buflen );	return __WSAsyncDBQuery(HWND_32(hWnd),uMsg,0,name,0,NULL,				(void*)sbuf,buflen,				AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN16|AQ_GETHOST);}/*********************************************************************** *       WSAAsyncGetHostByName	(WS2_32.103) */HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,					LPSTR sbuf, INT buflen){	TRACE("hwnd %p, msg %08x, host %s, buffer %i\n",	       hWnd, uMsg, (name)?name:"<null>", (int)buflen );	return WSA_H32( __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,				AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN32|AQ_GETHOST));}/*********************************************************************** *       WSAAsyncGetProtoByName	(WINSOCK.105) */HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,                                         SEGPTR sbuf, INT16 buflen){	TRACE("hwnd %04x, msg %08x, protocol %s\n",	       hWnd, uMsg, (name)?name:"<null>" );	return __WSAsyncDBQuery(HWND_32(hWnd),uMsg,0,name,0,NULL,				(void*)sbuf,buflen,				AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN16|AQ_GETPROTO);}/*********************************************************************** *       WSAAsyncGetProtoByName       (WS2_32.105) */HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,                                         LPSTR sbuf, INT buflen){	TRACE("hwnd %p, msg %08x, protocol %s\n",	       hWnd, uMsg, (name)?name:"<null>" );	return WSA_H32( __WSAsyncDBQuery(hWnd,uMsg,0,name,0,NULL,sbuf,buflen,				AQ_NAME|AQ_DUPLOWPTR1|AQ_WIN32|AQ_GETPROTO));}/*********************************************************************** *       WSAAsyncGetProtoByNumber	(WINSOCK.104) */HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,                                           SEGPTR sbuf, INT16 buflen){	TRACE("hwnd %04x, msg %04x, num %i\n", hWnd, uMsg, number );	return __WSAsyncDBQuery(HWND_32(hWnd),uMsg,number,NULL,0,NULL,				(void*)sbuf,buflen,				AQ_GETPROTO|AQ_NUMBER|AQ_WIN16);}/*********************************************************************** *       WSAAsyncGetProtoByNumber     (WS2_32.104) */HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,                                           LPSTR sbuf, INT buflen){	TRACE("hwnd %p, msg %04x, num %i\n", hWnd, uMsg, number );	return WSA_H32( __WSAsyncDBQuery(hWnd,uMsg,number,NULL,0,NULL,sbuf,buflen,				AQ_GETPROTO|AQ_NUMBER|AQ_WIN32));}/*********************************************************************** *       WSAAsyncGetServByName	(WINSOCK.107) */HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,                                        LPCSTR proto, SEGPTR sbuf, INT16 buflen){	TRACE("hwnd %04x, msg %04x, name %s, proto %s\n",	       hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>");	return __WSAsyncDBQuery(HWND_32(hWnd),uMsg,0,name,0,proto,				(void*)sbuf,buflen,				AQ_GETSERV|AQ_NAME|AQ_DUPLOWPTR1|AQ_DUPLOWPTR2|AQ_WIN16);}/*********************************************************************** *       WSAAsyncGetServByName        (WS2_32.107) */HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,                                        LPCSTR proto, LPSTR sbuf, INT buflen){	TRACE("hwnd %p, msg %04x, name %s, proto %s\n",	       hWnd, uMsg, (name)?name:"<null>", (proto)?proto:"<null>");	return WSA_H32( __WSAsyncDBQuery(hWnd,uMsg,0,name,0,proto,sbuf,buflen,				AQ_GETSERV|AQ_NAME|AQ_DUPLOWPTR1|AQ_DUPLOWPTR2|AQ_WIN32));}/*********************************************************************** *       WSAAsyncGetServByPort	(WINSOCK.106) */HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,                                        LPCSTR proto, SEGPTR sbuf, INT16 buflen){	TRACE("hwnd %04x, msg %04x, port %i, proto %s\n",	       hWnd, uMsg, port, (proto)?proto:"<null>" );	return __WSAsyncDBQuery(HWND_32(hWnd),uMsg,port,NULL,0,proto,				(void*)sbuf,buflen,				AQ_GETSERV|AQ_NUMBER|AQ_DUPLOWPTR2|AQ_WIN16);}/*********************************************************************** *       WSAAsyncGetServByPort        (WS2_32.106) */HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,                                        LPCSTR proto, LPSTR sbuf, INT buflen){	TRACE("hwnd %p, msg %04x, port %i, proto %s\n",	       hWnd, uMsg, port, (proto)?proto:"<null>" );	return WSA_H32( __WSAsyncDBQuery(hWnd,uMsg,port,NULL,0,proto,sbuf,buflen,				AQ_GETSERV|AQ_NUMBER|AQ_DUPLOWPTR2|AQ_WIN32));}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -