Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /branches/ttcomtester/teraterm/teraterm/WSAAsyncGetAddrInfo.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 7930 - (hide annotations) (download) (as text)
Fri Aug 9 13:36:46 2019 UTC (4 years, 8 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/WSAAsyncGetAddrInfo.c
File MIME type: text/x-csrc
File size: 4476 byte(s)
不要なHANDLEキャストを削除

- コンパイラの型チェックを積極的に利用するのが目的
- 出力されるコードは特に変化ない
1 maya 3227 /*
2 zmatsuo 7479 * Copyright (C) 2010-2019 TeraTerm Project
3 doda 6806 * All rights reserved.
4     *
5 doda 6841 * Redistribution and use in source and binary forms, with or without
6     * modification, are permitted provided that the following conditions
7     * are met:
8 doda 6806 *
9 doda 6841 * 1. Redistributions of source code must retain the above copyright
10     * notice, this list of conditions and the following disclaimer.
11     * 2. Redistributions in binary form must reproduce the above copyright
12     * notice, this list of conditions and the following disclaimer in the
13     * documentation and/or other materials provided with the distribution.
14     * 3. The name of the author may not be used to endorse or promote products
15     * derived from this software without specific prior written permission.
16 doda 6806 *
17 doda 6841 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20     * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 doda 6806 */
28     /*
29 maya 3227 * WSAAsyncGetAddrInfo.c -- asynchronous version of getaddrinfo
30     * Copyright(C) 2000-2003 Jun-ya Kato <kato@win6.jp>
31     */
32     #include <winsock2.h>
33     #include <ws2tcpip.h>
34 zmatsuo 7479 #include <wspiapi.h>
35 maya 3227 #include <windows.h>
36     #include <process.h>
37 zmatsuo 7930 #include <crtdbg.h>
38 zmatsuo 7479 #include "WSAAsyncGetAddrInfo.h"
39 doda 4165 #include "ttwsk.h"
40 maya 3227
41 zmatsuo 7930 struct getaddrinfo_args {
42     HWND hWnd;
43     unsigned int wMsg;
44     char *hostname;
45     char *portname;
46     struct addrinfo hints;
47     struct addrinfo **res;
48     HANDLE *lpHandle;
49     };
50    
51     #ifdef _DEBUG
52     #define malloc(l) _malloc_dbg((l), _NORMAL_BLOCK, __FILE__, __LINE__)
53     #define free(p) _free_dbg((p), _NORMAL_BLOCK)
54     #define _strdup(s) _strdup_dbg((s), _NORMAL_BLOCK, __FILE__, __LINE__)
55     #endif
56    
57 doda 6801 static unsigned __stdcall getaddrinfo_thread(void * p);
58 maya 3227
59 doda 6801 HANDLE PASCAL WSAAsyncGetAddrInfo(HWND hWnd, unsigned int wMsg,
60     const char *hostname,
61     const char *portname,
62     struct addrinfo *hints,
63     struct addrinfo **res)
64 maya 3227 {
65     HANDLE thread;
66     unsigned tid;
67 doda 6801 struct getaddrinfo_args * ga;
68 maya 3227
69     /*
70     * allocate structure to pass args to sub-thread dynamically
71     * WSAAsyncGetAddrInfo() is reentrant
72     */
73 doda 6801 if ((ga = (struct getaddrinfo_args *)malloc(sizeof(struct getaddrinfo_args))) == NULL)
74 maya 3227 return NULL;
75    
76     /* packing arguments struct addrinfo_args */
77     ga->hWnd = hWnd;
78     ga->wMsg = wMsg;
79 yutakapon 5062 ga->hostname = _strdup(hostname); // �|�C���^�����n�����A�X���b�h�����s���������B(2012.11.7 yutaka)
80     ga->portname = _strdup(portname);
81 yutakapon 6344 ga->hints = *hints; // �|�C���^�����n�����A�X���b�h�����s���������B(2016.3.11 yutaka)
82 maya 3227 ga->res = res;
83    
84 doda 6801 ga->lpHandle = (HANDLE *)malloc(sizeof(HANDLE));
85 yutakapon 5062 if (ga->lpHandle == NULL) {
86     free(ga->hostname);
87     free(ga->portname);
88 maya 3227 return NULL;
89 yutakapon 5062 }
90 maya 3227
91     /* create sub-thread running getaddrinfo() */
92     thread = (HANDLE)_beginthreadex(NULL, 0, getaddrinfo_thread, ga, CREATE_SUSPENDED, &tid);
93 zmatsuo 7930 *ga->lpHandle = thread;
94 maya 3227 ResumeThread(thread);
95    
96     /* return thread handle */
97     if (thread == 0) {
98     free(ga->lpHandle);
99 yutakapon 5062 free(ga->hostname);
100     free(ga->portname);
101 maya 3227 free(ga);
102     return NULL;
103     } else
104 zmatsuo 7930 return thread;
105 maya 3227 }
106    
107 doda 6801 static unsigned __stdcall getaddrinfo_thread(void * p)
108 maya 3227 {
109     int gai;
110     HWND hWnd;
111     unsigned int wMsg;
112 doda 6801 const char *hostname;
113     const char *portname;
114     struct addrinfo *hints;
115     struct addrinfo **res;
116     struct getaddrinfo_args *ga;
117 maya 3227
118     /* unpacking arguments */
119 doda 6801 ga = (struct getaddrinfo_args *)p;
120 maya 3227 hWnd = ga->hWnd;
121     wMsg = ga->wMsg;
122     hostname = ga->hostname;
123     portname = ga->portname;
124 yutakapon 6344 hints = &ga->hints;
125 maya 3227 res = ga->res;
126    
127     /* call getaddrinfo */
128 doda 4165 // gai = Pgetaddrinfo(hostname, portname, hints, res);
129 maya 3227 gai = getaddrinfo(hostname, portname, hints, res);
130    
131     /* send value of gai as message to window hWnd */
132     PostMessage(hWnd, wMsg, (WPARAM)*ga->lpHandle, MAKELPARAM(0, gai));
133    
134     free(ga->lpHandle);
135 yutakapon 5062 free(ga->hostname);
136     free(ga->portname);
137 maya 3227 free(p);
138    
139     return 0;
140     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26