NsmClientSocket tstring
@@ -7,6 +7,8 @@ | ||
7 | 7 | #include <winsock2.h> |
8 | 8 | #include <ws2tcpip.h> |
9 | 9 | |
10 | +#include "../../Common/dummy.h" | |
11 | + | |
10 | 12 | #include "ClientSocketConsts.h" |
11 | 13 | #include "NsmClientSocket.h" |
12 | 14 |
@@ -21,7 +23,7 @@ | ||
21 | 23 | class AutoPtrAddrinfo |
22 | 24 | { |
23 | 25 | public: |
24 | - ADDRINFO *Info; | |
26 | + ADDRINFOT *Info; | |
25 | 27 | |
26 | 28 | AutoPtrAddrinfo() |
27 | 29 | :Info() |
@@ -29,7 +31,7 @@ | ||
29 | 31 | |
30 | 32 | ~AutoPtrAddrinfo() |
31 | 33 | { |
32 | - freeaddrinfo(Info); | |
34 | + FreeAddrInfo(Info); | |
33 | 35 | } |
34 | 36 | }; |
35 | 37 |
@@ -38,9 +40,9 @@ | ||
38 | 40 | NsmClientSocket::WinsockInit::WinsockInit() |
39 | 41 | { |
40 | 42 | WSADATA wsa; |
41 | - WSAStartup(WINSOCK_VERSION,&wsa); | |
43 | + WSAStartup(WINSOCK_VERSION, &wsa); | |
42 | 44 | |
43 | - WNDCLASSEX wc = {0}; | |
45 | + WNDCLASSEX wc = {}; | |
44 | 46 | wc.cbSize = sizeof(wc); |
45 | 47 | wc.lpszClassName = TEXT("socket"); |
46 | 48 | wc.lpfnWndProc = WndProc; |
@@ -57,11 +59,11 @@ | ||
57 | 59 | |
58 | 60 | // NsmClientSocket::FindByHandle ----------------------------------------------- |
59 | 61 | |
60 | - NsmClientSocket::FindByHandle::FindByHandle(const NsmClientSocket *const s) | |
62 | + NsmClientSocket::FindByHandle::FindByHandle(const NsmClientSocket *s) | |
61 | 63 | :x(s) |
62 | 64 | {} |
63 | 65 | |
64 | - bool NsmClientSocket::FindByHandle::operator ()(const NsmClientSocketListSub &v) const | |
66 | + bool NsmClientSocket::FindByHandle::operator()(const NsmClientSocketListSub &v) const | |
65 | 67 | { |
66 | 68 | return v.get() == x; |
67 | 69 | } |
@@ -68,7 +70,7 @@ | ||
68 | 70 | |
69 | 71 | // NsmClientSocket ------------------------------------------------------------- |
70 | 72 | |
71 | - NsmClientSocket::NsmClientSocket(const NsmClientSocketInitInfo& initInfo,bool useProxy,int proxyType) | |
73 | + NsmClientSocket::NsmClientSocket(const NsmClientSocketInitInfo &initInfo, bool useProxy, int proxyType) | |
72 | 74 | :InitInfo(initInfo) |
73 | 75 | ,Socket(INVALID_SOCKET) |
74 | 76 | ,Host() |
@@ -84,9 +86,9 @@ | ||
84 | 86 | |
85 | 87 | Handle = CreateWindow( |
86 | 88 | MAKEINTATOM(init.Atom), |
87 | - 0, | |
89 | + NULL, | |
88 | 90 | WS_OVERLAPPED, |
89 | - 0,0,0,0, | |
91 | + 0, 0, 0, 0, | |
90 | 92 | HWND_MESSAGE, |
91 | 93 | NULL, |
92 | 94 | NULL, |
@@ -97,13 +99,13 @@ | ||
97 | 99 | { |
98 | 100 | Close(); |
99 | 101 | |
100 | - PostMessage(Handle,WM_CLOSE,0,0); | |
102 | + PostMessage(Handle, WM_CLOSE, 0, 0); | |
101 | 103 | } |
102 | 104 | |
103 | - LRESULT CALLBACK NsmClientSocket::WndProc(const HWND hWnd,const UINT msg,const WPARAM wParam,const LPARAM lParam) | |
105 | + LRESULT CALLBACK NsmClientSocket::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) | |
104 | 106 | { |
105 | 107 | const NsmClientSocket *const window = (msg != WM_CREATE) ? |
106 | - reinterpret_cast<NsmClientSocket*>(GetWindowLongPtr(hWnd,GWLP_USERDATA)) | |
108 | + reinterpret_cast<NsmClientSocket*>(GetWindowLongPtr(hWnd, GWLP_USERDATA)) | |
107 | 109 | : static_cast<NsmClientSocket*>(reinterpret_cast<LPCREATESTRUCT>(lParam)->lpCreateParams); |
108 | 110 | |
109 | 111 | if(window) |
@@ -110,7 +112,7 @@ | ||
110 | 112 | switch(msg) |
111 | 113 | { |
112 | 114 | case WM_CREATE: |
113 | - SetWindowLongPtr(hWnd,GWLP_USERDATA,reinterpret_cast<LONG_PTR>(window)); | |
115 | + SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(window)); | |
114 | 116 | return FALSE; |
115 | 117 | case WM_WINSOCK: |
116 | 118 | { |
@@ -121,19 +123,19 @@ | ||
121 | 123 | { |
122 | 124 | case FD_READ: |
123 | 125 | if(WSAGETSELECTERROR(lParam)) |
124 | - window->DoError(SocketErrorType::Receive,WSAGetLastError()); | |
126 | + window->DoError(SocketErrorType::Receive, WSAGetLastError()); | |
125 | 127 | else |
126 | 128 | window->DoRead(); |
127 | 129 | return FALSE; |
128 | 130 | case FD_WRITE: |
129 | 131 | if(WSAGETSELECTERROR(lParam)) |
130 | - window->DoError(SocketErrorType::Send,WSAGetLastError()); | |
132 | + window->DoError(SocketErrorType::Send, WSAGetLastError()); | |
131 | 133 | else |
132 | 134 | window->DoWrite(); |
133 | 135 | return FALSE; |
134 | 136 | case FD_CLOSE: |
135 | 137 | if(WSAGETSELECTERROR(lParam)) |
136 | - window->DoError(SocketErrorType::Disconnect,WSAGetLastError()); | |
138 | + window->DoError(SocketErrorType::Disconnect, WSAGetLastError()); | |
137 | 139 | else |
138 | 140 | window->DoDisconnect(); |
139 | 141 | return FALSE; |
@@ -151,7 +153,7 @@ | ||
151 | 153 | { |
152 | 154 | if(InitInfo.lpOnConnect) |
153 | 155 | InitInfo.lpOnConnect( |
154 | - reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)),InitInfo.Data); | |
156 | + reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)), InitInfo.Data); | |
155 | 157 | } |
156 | 158 | |
157 | 159 | void NsmClientSocket::DoConnecting() const |
@@ -158,7 +160,7 @@ | ||
158 | 160 | { |
159 | 161 | if(InitInfo.lpOnConnecting) |
160 | 162 | InitInfo.lpOnConnecting( |
161 | - reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)),InitInfo.Data); | |
163 | + reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)), InitInfo.Data); | |
162 | 164 | } |
163 | 165 | |
164 | 166 | void NsmClientSocket::DoDisconnect() const |
@@ -169,12 +171,12 @@ | ||
169 | 171 | __try |
170 | 172 | { |
171 | 173 | InitInfo.lpOnDisconnect( |
172 | - reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)),InitInfo.Data); | |
174 | + reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)), InitInfo.Data); | |
173 | 175 | } |
174 | 176 | __except(EXCEPTION_EXECUTE_HANDLER){} |
175 | 177 | #else |
176 | 178 | InitInfo.lpOnDisconnect( |
177 | - reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)),InitInfo.Data); | |
179 | + reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)), InitInfo.Data); | |
178 | 180 | #endif |
179 | 181 | } |
180 | 182 | } |
@@ -183,7 +185,7 @@ | ||
183 | 185 | { |
184 | 186 | if(InitInfo.lpOnLookup) |
185 | 187 | InitInfo.lpOnLookup( |
186 | - reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)),InitInfo.Data); | |
188 | + reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)), InitInfo.Data); | |
187 | 189 | } |
188 | 190 | |
189 | 191 | void NsmClientSocket::DoRead() const |
@@ -190,7 +192,7 @@ | ||
190 | 192 | { |
191 | 193 | if(InitInfo.lpOnRead) |
192 | 194 | InitInfo.lpOnRead( |
193 | - reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)),InitInfo.Data); | |
195 | + reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)), InitInfo.Data); | |
194 | 196 | } |
195 | 197 | |
196 | 198 | void NsmClientSocket::DoWrite() const |
@@ -197,7 +199,7 @@ | ||
197 | 199 | { |
198 | 200 | if(InitInfo.lpOnWrite) |
199 | 201 | InitInfo.lpOnWrite( |
200 | - reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)),InitInfo.Data); | |
202 | + reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)), InitInfo.Data); | |
201 | 203 | } |
202 | 204 | |
203 | 205 | void NsmClientSocket::DoError(int errtype, int errcode) const |
@@ -204,7 +206,7 @@ | ||
204 | 206 | { |
205 | 207 | if(InitInfo.lpOnError) |
206 | 208 | InitInfo.lpOnError( |
207 | - reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)),InitInfo.Data,errtype,errcode); | |
209 | + reinterpret_cast<HNsmClientSocket>(const_cast<NsmClientSocket*>(this)), InitInfo.Data, errtype, errcode); | |
208 | 210 | } |
209 | 211 | |
210 | 212 | //---- |
@@ -225,7 +227,7 @@ | ||
225 | 227 | Host = Address; |
226 | 228 | } |
227 | 229 | |
228 | - std::string host; | |
230 | + tstring host; | |
229 | 231 | unsigned short port; |
230 | 232 | |
231 | 233 | if(UseProxy) |
@@ -248,7 +250,7 @@ | ||
248 | 250 | port = Port; |
249 | 251 | } |
250 | 252 | |
251 | - int err = DirectOpen(host,port); | |
253 | + int err = DirectOpen(host, port); | |
252 | 254 | if(err) |
253 | 255 | return err; |
254 | 256 |
@@ -281,45 +283,45 @@ | ||
281 | 283 | DoConnect(); |
282 | 284 | |
283 | 285 | // 非同期select |
284 | - WSAAsyncSelect(Socket,Handle,WM_WINSOCK,FD_READ | FD_WRITE | FD_CLOSE); | |
286 | + WSAAsyncSelect(Socket, Handle, WM_WINSOCK, FD_READ | FD_WRITE | FD_CLOSE); | |
285 | 287 | |
286 | 288 | return 0; |
287 | 289 | } |
288 | 290 | |
289 | - int NsmClientSocket::DirectOpen(const std::string &host,unsigned short port) | |
291 | + int NsmClientSocket::DirectOpen(const tstring &host, unsigned short port) | |
290 | 292 | { |
291 | 293 | // アドレスを変換する。 |
292 | - std::stringstream portStr; | |
294 | + tstringstream portStr; | |
293 | 295 | portStr << port; |
294 | 296 | |
295 | 297 | AutoPtrAddrinfo res; |
296 | 298 | |
297 | - ADDRINFO hints = {0}; | |
299 | + ADDRINFOT hints = {}; | |
298 | 300 | hints.ai_family = AF_INET; |
299 | 301 | hints.ai_socktype = SOCK_STREAM; |
300 | 302 | |
301 | 303 | DoLookup(); |
302 | 304 | |
303 | - int err = getaddrinfo(host.c_str(), portStr.str().c_str(), &hints, &res.Info); | |
305 | + int err = GetAddrInfo(host.c_str(), portStr.str().c_str(), &hints, &res.Info); | |
304 | 306 | if(err != 0) |
305 | 307 | return err; |
306 | 308 | |
307 | - Socket = socket(hints.ai_family,hints.ai_socktype,hints.ai_protocol); | |
309 | + Socket = socket(hints.ai_family, hints.ai_socktype, hints.ai_protocol); | |
308 | 310 | |
309 | 311 | // ソケットオープンに失敗 |
310 | 312 | if(Socket == INVALID_SOCKET) |
311 | 313 | return 2; |
312 | 314 | |
313 | - const ADDRINFO *it = res.Info; | |
314 | - for(; it != NULL; it = it->ai_next) | |
315 | + const ADDRINFOT *it = res.Info; | |
316 | + for(; it; it = it->ai_next) | |
315 | 317 | { |
316 | 318 | /* connect()が成功したらloopを抜けます */ |
317 | - if(connect(Socket,it->ai_addr,static_cast<int>(it->ai_addrlen)) != SOCKET_ERROR) | |
319 | + if(connect(Socket, it->ai_addr, static_cast<int>(it->ai_addrlen)) != SOCKET_ERROR) | |
318 | 320 | break; |
319 | 321 | } |
320 | 322 | |
321 | 323 | /* connectが全て失敗した場合 */ |
322 | - if(it == NULL) | |
324 | + if(!it) | |
323 | 325 | { |
324 | 326 | Close(); |
325 | 327 | return 3; |
@@ -344,17 +346,18 @@ | ||
344 | 346 | |
345 | 347 | AutoPtrAddrinfo res; |
346 | 348 | |
347 | - ADDRINFO hints = {0}; | |
349 | + ADDRINFOT hints = {}; | |
348 | 350 | hints.ai_family = AF_INET; |
349 | 351 | hints.ai_socktype = SOCK_STREAM; |
350 | 352 | |
351 | 353 | DoLookup(); |
352 | 354 | |
353 | - int err = getaddrinfo(Host.c_str(), NULL, &hints, &res.Info); | |
355 | + int err = GetAddrInfo(Host.c_str(), NULL, &hints, &res.Info); | |
356 | + | |
354 | 357 | if(err != 0) |
355 | 358 | return err; |
356 | 359 | |
357 | - Socks4Packet packet = {0}; | |
360 | + Socks4Packet packet = {}; | |
358 | 361 | packet.Version = 4; |
359 | 362 | packet.Command = 1; |
360 | 363 | packet.DstPort = htons(Port); |
@@ -363,7 +366,7 @@ | ||
363 | 366 | if(Send(reinterpret_cast<char*>(&packet), sizeof(packet)) == SOCKET_ERROR) |
364 | 367 | return WSAGetLastError(); |
365 | 368 | |
366 | - char user[1] = {0}; | |
369 | + char user[1] = {}; | |
367 | 370 | |
368 | 371 | if(Send(user, 1) == SOCKET_ERROR) |
369 | 372 | return WSAGetLastError(); |
@@ -380,7 +383,7 @@ | ||
380 | 383 | int NsmClientSocket::Socks5Open() const |
381 | 384 | { |
382 | 385 | { |
383 | - BYTE buff[3] = {0}; | |
386 | + BYTE buff[3] = {}; | |
384 | 387 | buff[0] = 5; |
385 | 388 | buff[1] = 1; |
386 | 389 | buff[2] = 0; |
@@ -400,17 +403,17 @@ | ||
400 | 403 | { |
401 | 404 | AutoPtrAddrinfo res; |
402 | 405 | |
403 | - ADDRINFO hints = {0}; | |
406 | + ADDRINFOT hints = {}; | |
404 | 407 | hints.ai_family = AF_INET; |
405 | 408 | hints.ai_socktype = SOCK_STREAM; |
406 | 409 | |
407 | 410 | DoLookup(); |
408 | 411 | |
409 | - int err = getaddrinfo(Host.c_str(), NULL, &hints, &res.Info); | |
412 | + int err = GetAddrInfo(Host.c_str(), NULL, &hints, &res.Info); | |
410 | 413 | if(err != 0) |
411 | 414 | return err; |
412 | 415 | |
413 | - BYTE buff[10] = {0}; | |
416 | + BYTE buff[10] = {}; | |
414 | 417 | buff[0] = 5; |
415 | 418 | buff[1] = 1; |
416 | 419 | buff[2] = 0; |
@@ -435,18 +438,18 @@ | ||
435 | 438 | |
436 | 439 | int NsmClientSocket::HttpOpen() const |
437 | 440 | { |
438 | - std::stringstream portStr; | |
441 | + tstringstream portStr; | |
439 | 442 | portStr << Port; |
440 | 443 | |
441 | - const std::string cmd = "CONNECT " + Host + ":" + portStr.str() + " HTTP/1.1\r\nHost: " | |
442 | - + Host + ":" + portStr.str() + "\r\nConnection: Keep-Alive\r\n" "\r\n"; | |
444 | + const std::string cmd = t2c(TEXT("CONNECT ") + Host + TEXT(":") + portStr.str() + TEXT(" HTTP/1.1\r\nHost: ") | |
445 | + + Host + TEXT(":") + portStr.str() + TEXT("\r\nConnection: Keep-Alive\r\n") TEXT("\r\n")); | |
443 | 446 | |
444 | 447 | if(Send(cmd.c_str(), cmd.size()) == SOCKET_ERROR) |
445 | 448 | return WSAGetLastError(); |
446 | 449 | |
447 | - char buff[1024] = {0}; | |
450 | + char buff[1024] = {}; | |
448 | 451 | |
449 | - if(Receive(buff, sizeof(buff)-1) == SOCKET_ERROR) | |
452 | + if(Receive(buff, sizeof(buff) - 1) == SOCKET_ERROR) | |
450 | 453 | return WSAGetLastError(); |
451 | 454 | |
452 | 455 | const std::string receive = buff; |
@@ -462,7 +465,7 @@ | ||
462 | 465 | |
463 | 466 | if(n != receive.npos && n < receive.size() - receiveMinLen) |
464 | 467 | { |
465 | - std::stringstream str(receive.substr(n+9,3)); | |
468 | + std::stringstream str(receive.substr(n + 9, 3)); | |
466 | 469 | int tmp; |
467 | 470 | str >> tmp; |
468 | 471 | if(!str.fail()) |
@@ -480,7 +483,7 @@ | ||
480 | 483 | if (Socket == INVALID_SOCKET) |
481 | 484 | return 0; |
482 | 485 | |
483 | - shutdown(Socket,SD_SEND); | |
486 | + shutdown(Socket, SD_SEND); | |
484 | 487 | |
485 | 488 | closesocket(Socket); |
486 | 489 |
@@ -491,14 +494,14 @@ | ||
491 | 494 | |
492 | 495 | //---- |
493 | 496 | |
494 | - int NsmClientSocket::Send(const char* buf,int len) const | |
497 | + int NsmClientSocket::Send(const char* buf, int len) const | |
495 | 498 | { |
496 | - return send(Socket,buf,len,0); | |
499 | + return send(Socket, buf, len, 0); | |
497 | 500 | } |
498 | 501 | |
499 | - int NsmClientSocket::Receive(char* buf,int len) const | |
502 | + int NsmClientSocket::Receive(char* buf, int len) const | |
500 | 503 | { |
501 | - return recv(Socket,buf,len,0); | |
504 | + return recv(Socket, buf, len, 0); | |
502 | 505 | } |
503 | 506 | |
504 | 507 | int NsmClientSocket::ReceiveLength() const |
@@ -75,7 +75,7 @@ | ||
75 | 75 | |
76 | 76 | // ソケットを生成 |
77 | 77 | NsmClientSocketListSub soc(new NsmClientSocket(initInfo, Config.UseProxy, Config.ProxyType)); |
78 | - soc->ProxyHost = t2c(Config.ProxyHost); | |
78 | + soc->ProxyHost = Config.ProxyHost; | |
79 | 79 | soc->ProxyPort = Config.ProxyPort; |
80 | 80 | |
81 | 81 | { |
@@ -223,10 +223,10 @@ | ||
223 | 223 | switch (sockInfo.nInfoKey) |
224 | 224 | { |
225 | 225 | case SocketInfoType::Host: |
226 | - handle->Host = Common::NsmInfoUtility::NsmInfoToStrA(sockInfo.lpInfo); | |
226 | + handle->Host = Common::NsmInfoUtility::NsmInfoToStr(sockInfo.lpInfo); | |
227 | 227 | return 1; |
228 | 228 | case SocketInfoType::Address: |
229 | - handle->Address = Common::NsmInfoUtility::NsmInfoToStrA(sockInfo.lpInfo); | |
229 | + handle->Address = Common::NsmInfoUtility::NsmInfoToStr(sockInfo.lpInfo); | |
230 | 230 | return 1; |
231 | 231 | case SocketInfoType::Port: |
232 | 232 | handle->Port = static_cast<unsigned short>(Common::NsmInfoUtility::NsmInfoToInt(sockInfo.lpInfo)); |
@@ -33,11 +33,11 @@ | ||
33 | 33 | SOCKET Socket; |
34 | 34 | HWND Handle; |
35 | 35 | |
36 | - static LRESULT CALLBACK WndProc(const HWND hWnd,const UINT msg,const WPARAM wParam,const LPARAM lParam); | |
36 | + static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); | |
37 | 37 | |
38 | 38 | //-- |
39 | 39 | |
40 | - int DirectOpen(const std::string &host,unsigned short port); | |
40 | + int DirectOpen(const tstring &host, unsigned short port); | |
41 | 41 | int Socks4Open() const; |
42 | 42 | int Socks5Open() const; |
43 | 43 | int HttpOpen() const; |
@@ -50,23 +50,23 @@ | ||
50 | 50 | void DoRead() const; |
51 | 51 | void DoWrite() const; |
52 | 52 | |
53 | - NsmClientSocket& operator=(const NsmClientSocket& other); | |
53 | + NsmClientSocket& operator=(const NsmClientSocket &); | |
54 | 54 | public: |
55 | 55 | struct FindByHandle |
56 | 56 | { |
57 | 57 | const NsmClientSocket *const x; |
58 | - explicit FindByHandle(const NsmClientSocket *const s); | |
59 | - bool operator()(const NsmClientSocketListSub& v)const; | |
58 | + explicit FindByHandle(const NsmClientSocket *s); | |
59 | + bool operator()(const NsmClientSocketListSub &v) const; | |
60 | 60 | private: |
61 | - FindByHandle& operator=(const FindByHandle& other); | |
61 | + FindByHandle& operator=(const FindByHandle &); | |
62 | 62 | }; |
63 | 63 | |
64 | 64 | // メンバ変数 |
65 | - std::string Host; | |
66 | - std::string Address; | |
65 | + tstring Host; | |
66 | + tstring Address; | |
67 | 67 | unsigned short Port; |
68 | - std::string ProxyHost; | |
69 | - std::string ProxyAddress; | |
68 | + tstring ProxyHost; | |
69 | + tstring ProxyAddress; | |
70 | 70 | unsigned short ProxyPort; |
71 | 71 | const bool UseProxy; |
72 | 72 | const int ProxyType; |
@@ -80,8 +80,8 @@ | ||
80 | 80 | |
81 | 81 | void DoError(int errtype, int errcode) const; |
82 | 82 | |
83 | - int Send(const char* buf,int len) const; | |
84 | - int Receive(char* buf,int len) const; | |
83 | + int Send(const char* buf, int len) const; | |
84 | + int Receive(char* buf, int len) const; | |
85 | 85 | int ReceiveLength() const; |
86 | 86 | }; |
87 | 87 |