Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/teraterm/teraterm/commlib.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9342 - (hide annotations) (download) (as text)
Sun Aug 1 15:32:15 2021 UTC (2 years, 7 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 35618 byte(s)
TTMessageBoxW() を TTMessageBoxA() へ改名

- 引数 uType を TTMessageBoxInfoW へ移動
- 未使用マクロ get_lang_msgT() を削除
1 doda 6806 /*
2     * Copyright (C) 1994-1998 T. Teranishi
3 nmaya 9048 * (C) 2005- TeraTerm Project
4 doda 6806 * All rights reserved.
5     *
6 doda 6841 * Redistribution and use in source and binary forms, with or without
7     * modification, are permitted provided that the following conditions
8     * are met:
9 doda 6806 *
10 doda 6841 * 1. Redistributions of source code must retain the above copyright
11     * notice, this list of conditions and the following disclaimer.
12     * 2. Redistributions in binary form must reproduce the above copyright
13     * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the distribution.
15     * 3. The name of the author may not be used to endorse or promote products
16     * derived from this software without specific prior written permission.
17 doda 6806 *
18 doda 6841 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21     * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 doda 6806 */
29 maya 3857 /* IPv6 modification is Copyright (C) 2000, 2001 Jun-ya KATO <kato@win6.jp> */
30    
31     /* TERATERM.EXE, Communication routines */
32     #include "teraterm.h"
33     #include "tttypes.h"
34     #include "tt_res.h"
35     #include <process.h>
36    
37     #include "ttcommon.h"
38     #include "ttwsk.h"
39     #include "ttlib.h"
40     #include "ttfileio.h"
41     #include "ttplug.h" /* TTPLUG */
42 zmatsuo 8860 #include "ttdde.h"
43 maya 3857
44     #include "commlib.h"
45     #include <winsock2.h>
46     #include <ws2tcpip.h>
47     #include <stdio.h> /* for _snprintf() */
48     #include <time.h>
49    
50 zmatsuo 8906 #include "filesys_log.h"
51 zmatsuo 8616 #include "ttlib.h"
52     #include "codeconv.h"
53    
54 maya 3857 static SOCKET OpenSocket(PComVar);
55     static void AsyncConnect(PComVar);
56     static int CloseSocket(SOCKET);
57    
58     /* create socket */
59     static SOCKET OpenSocket(PComVar cv)
60     {
61     cv->s = cv->res->ai_family;
62     cv->s = Psocket(cv->res->ai_family, cv->res->ai_socktype, cv->res->ai_protocol);
63     return cv->s;
64     }
65    
66     /* connect with asynchronous mode */
67     static void AsyncConnect(PComVar cv)
68     {
69     int Err;
70     BOOL BBuf;
71     BBuf = TRUE;
72     /* set synchronous mode */
73     PWSAAsyncSelect(cv->s,cv->HWin,0,0);
74 doda 6801 Psetsockopt(cv->s,(int)SOL_SOCKET,SO_OOBINLINE,(char *)&BBuf,sizeof(BBuf));
75 maya 3857 /* set asynchronous mode */
76     PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMOPEN, FD_CONNECT);
77    
78     // �z�X�g���������������������������A�����I���\�P�b�g���N���[�Y�����A
79     // �����������L�����Z���������B�l��0�������������������B
80     // (2007.1.11 yutaka)
81     if (*cv->ConnetingTimeout > 0) {
82     SetTimer(cv->HWin, IdCancelConnectTimer, *cv->ConnetingTimeout * 1000, NULL);
83     }
84    
85     /* WM_USER_COMMOPEN occurs, CommOpen is called, then CommStart is called */
86     Err = Pconnect(cv->s, cv->res->ai_addr, cv->res->ai_addrlen);
87     if (Err != 0) {
88     Err = PWSAGetLastError();
89     if (Err == WSAEWOULDBLOCK) {
90     /* Do nothing */
91     } else if (Err!=0 ) {
92     PostMessage(cv->HWin, WM_USER_COMMOPEN,0,
93     MAKELONG(FD_CONNECT,Err));
94     }
95     }
96     }
97    
98     /* close socket */
99     static int CloseSocket(SOCKET s)
100     {
101     return Pclosesocket(s);
102     }
103    
104     #define CommInQueSize 8192
105     #define CommOutQueSize 2048
106     #define CommXonLim 2048
107     #define CommXoffLim 2048
108    
109     #define READENDNAME "ReadEnd"
110     #define WRITENAME "Write"
111     #define READNAME "Read"
112     #define PRNWRITENAME "PrnWrite"
113    
114     static HANDLE ReadEnd;
115     static OVERLAPPED wol, rol;
116    
117     // Winsock async operation handle
118     static HANDLE HAsync=0;
119    
120     BOOL TCPIPClosed = TRUE;
121    
122     /* Printer port handle for
123     direct pass-thru printing */
124     static HANDLE PrnID = INVALID_HANDLE_VALUE;
125     static BOOL LPTFlag;
126    
127     // Initialize ComVar.
128     // This routine is called only once
129     // by the initialization procedure of Tera Term.
130     void CommInit(PComVar cv)
131     {
132     cv->Open = FALSE;
133     cv->Ready = FALSE;
134    
135     /* message flag */
136     cv->NoMsg = 0;
137 doda 6662
138 doda 6788 cv->isSSH = 0;
139     cv->TitleRemote[0] = '\0';
140    
141 doda 6662 cv->NotifyIcon = NULL;
142 doda 6947
143     cv->ConnectedTime = 0;
144 maya 3857 }
145    
146     /* reset a serial port which is already open */
147     void CommResetSerial(PTTSet ts, PComVar cv, BOOL ClearBuff)
148     {
149     DCB dcb;
150     DWORD DErr;
151     COMMTIMEOUTS ctmo;
152    
153     if (! cv->Open ||
154     (cv->PortType != IdSerial)) {
155     return;
156     }
157    
158     ClearCommError(cv->ComID,&DErr,NULL);
159     SetupComm(cv->ComID,CommInQueSize,CommOutQueSize);
160     /* flush input and output buffers */
161     if (ClearBuff) {
162     PurgeComm(cv->ComID, PURGE_TXABORT | PURGE_RXABORT |
163     PURGE_TXCLEAR | PURGE_RXCLEAR);
164     }
165    
166     memset(&ctmo,0,sizeof(ctmo));
167     ctmo.ReadIntervalTimeout = MAXDWORD;
168     ctmo.WriteTotalTimeoutConstant = 500;
169     SetCommTimeouts(cv->ComID,&ctmo);
170     cv->InBuffCount = 0;
171     cv->InPtr = 0;
172     cv->OutBuffCount = 0;
173     cv->OutPtr = 0;
174    
175     cv->DelayPerChar = ts->DelayPerChar;
176     cv->DelayPerLine = ts->DelayPerLine;
177    
178     memset(&dcb,0,sizeof(DCB));
179     dcb.DCBlength = sizeof(DCB);
180 maya 3874 dcb.BaudRate = ts->Baud;
181 maya 3857 dcb.fBinary = TRUE;
182     switch (ts->Parity) {
183 doda 4849 case IdParityNone:
184     dcb.Parity = NOPARITY;
185     break;
186     case IdParityOdd:
187     dcb.fParity = TRUE;
188     dcb.Parity = ODDPARITY;
189     break;
190 maya 3857 case IdParityEven:
191     dcb.fParity = TRUE;
192     dcb.Parity = EVENPARITY;
193     break;
194 doda 4849 case IdParityMark:
195 maya 3857 dcb.fParity = TRUE;
196 doda 4849 dcb.Parity = MARKPARITY;
197 maya 3857 break;
198 doda 4849 case IdParitySpace:
199     dcb.fParity = TRUE;
200     dcb.Parity = SPACEPARITY;
201 maya 3857 break;
202     }
203    
204     dcb.fDtrControl = DTR_CONTROL_ENABLE;
205     dcb.fRtsControl = RTS_CONTROL_ENABLE;
206     switch (ts->Flow) {
207     case IdFlowX:
208     dcb.fOutX = TRUE;
209     dcb.fInX = TRUE;
210     dcb.XonLim = CommXonLim;
211     dcb.XoffLim = CommXoffLim;
212     dcb.XonChar = XON;
213     dcb.XoffChar = XOFF;
214     break;
215 yutakapon 8051 case IdFlowHard: // RTS/CTS
216 maya 3857 dcb.fOutxCtsFlow = TRUE;
217     dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
218     break;
219 yutakapon 8051 case IdFlowHardDsrDtr: // DSR/DTR
220     dcb.fOutxDsrFlow = TRUE;
221     dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
222     break;
223 maya 3857 }
224    
225     switch (ts->DataBit) {
226     case IdDataBit7:
227     dcb.ByteSize = 7;
228     break;
229     case IdDataBit8:
230     dcb.ByteSize = 8;
231     break;
232     }
233     switch (ts->StopBit) {
234     case IdStopBit1:
235     dcb.StopBits = ONESTOPBIT;
236     break;
237     case IdStopBit2:
238     dcb.StopBits = TWOSTOPBITS;
239     break;
240     }
241    
242     SetCommState(cv->ComID, &dcb);
243    
244     /* enable receive request */
245     SetCommMask(cv->ComID,0);
246     SetCommMask(cv->ComID,EV_RXCHAR);
247     }
248    
249 yutakapon 4860 // ���O�t���p�C�v�����������������`�F�b�N�����B
250     // \\ServerName\pipe\PipeName
251     //
252     // return 0: ������
253     // -1: �s��
254     // (2012.3.10 yutaka)
255     int CheckNamedPipeFormat(char *p, int size)
256     {
257     int ret = -1;
258     char *s;
259    
260     if (size <= 8)
261     goto error;
262    
263     if (p[0] == '\\' && p[1] == '\\') {
264     s = strchr(&p[2], '\\');
265     if (s && _strnicmp(s+1, "pipe\\", 5) == 0) {
266     ret = 0;
267     }
268     }
269    
270     error:
271     return (ret);
272     }
273    
274 maya 3857 void CommOpen(HWND HW, PTTSet ts, PComVar cv)
275     {
276 zmatsuo 8616 char ErrMsg[21 + 256];
277     wchar_t ErrMsgW[21 + 256];
278 yutakapon 4857 char P[50+256];
279 maya 3857
280     MSG Msg;
281     ADDRINFO hints;
282     char pname[NI_MAXSERV];
283    
284     BOOL InvalidHost;
285    
286 yutakapon 4860 // �z�X�g�������O�t���p�C�v�����������������B
287     if (ts->PortType == IdTCPIP) {
288     if (CheckNamedPipeFormat(ts->HostName, strlen(ts->HostName)) == 0) {
289     ts->PortType = IdNamedPipe;
290     }
291     }
292    
293 maya 3857 /* initialize ComVar */
294     cv->InBuffCount = 0;
295     cv->InPtr = 0;
296     cv->OutBuffCount = 0;
297     cv->OutPtr = 0;
298     cv->HWin = HW;
299     cv->Ready = FALSE;
300     cv->Open = FALSE;
301     cv->PortType = ts->PortType;
302     cv->ComPort = 0;
303     cv->RetryCount = 0;
304     cv->RetryWithOtherProtocol = TRUE;
305     cv->s = INVALID_SOCKET;
306     cv->ComID = INVALID_HANDLE_VALUE;
307     cv->CanSend = TRUE;
308     cv->RRQ = FALSE;
309     cv->SendCode = IdASCII;
310     cv->EchoCode = IdASCII;
311     cv->Language = ts->Language;
312     cv->CRSend = ts->CRSend;
313     cv->KanjiCodeEcho = ts->KanjiCode;
314     cv->JIS7KatakanaEcho = ts->JIS7Katakana;
315     cv->KanjiCodeSend = ts->KanjiCodeSend;
316     cv->JIS7KatakanaSend = ts->JIS7KatakanaSend;
317     cv->KanjiIn = ts->KanjiIn;
318     cv->KanjiOut = ts->KanjiOut;
319     cv->RussHost = ts->RussHost;
320     cv->DelayFlag = TRUE;
321     cv->DelayPerChar = ts->DelayPerChar;
322     cv->DelayPerLine = ts->DelayPerLine;
323     cv->TelBinRecv = FALSE;
324     cv->TelBinSend = FALSE;
325     cv->TelFlag = FALSE;
326     cv->TelMode = FALSE;
327     cv->IACFlag = FALSE;
328     cv->TelCRFlag = FALSE;
329     cv->TelCRSend = FALSE;
330     cv->TelCRSendEcho = FALSE;
331     cv->TelAutoDetect = ts->TelAutoDetect; /* TTPLUG */
332     cv->ConnetingTimeout = &ts->ConnectingTimeout;
333     cv->LastSendTime = time(NULL);
334     cv->LineModeBuffCount = 0;
335     cv->Flush = FALSE;
336     cv->FlushLen = 0;
337     cv->TelLineMode = FALSE;
338 doda 6947 cv->ConnectedTime = 0;
339 maya 3857
340     if ((ts->PortType!=IdSerial) && (strlen(ts->HostName)==0))
341     {
342     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
343     return;
344     }
345    
346     switch (ts->PortType) {
347     case IdTCPIP:
348     cv->TelFlag = (ts->Telnet > 0);
349     if (ts->EnableLineMode) {
350     cv->TelLineMode = TRUE;
351     }
352     if (! LoadWinsock()) {
353     if (cv->NoMsg==0) {
354 zmatsuo 8616 static const TTMessageBoxInfoW info = {
355     "Tera Term",
356     "MSG_TT_ERROR", L"Tera Term: Error",
357     "MSG_WINSOCK_ERROR", L"Cannot use winsock",
358 zmatsuo 9342 MB_TASKMODAL | MB_ICONEXCLAMATION,
359 zmatsuo 8616 };
360 zmatsuo 9342 TTMessageBoxA(cv->HWin, &info, ts->UILanguageFile);
361 maya 3857 }
362     InvalidHost = TRUE;
363     }
364     else {
365     TTXOpenTCP(); /* TTPLUG */
366     cv->Open = TRUE;
367     /* resolving address */
368     memset(&hints, 0, sizeof(hints));
369     hints.ai_family = ts->ProtocolFamily;
370     hints.ai_socktype = SOCK_STREAM;
371     hints.ai_protocol = IPPROTO_TCP;
372     _snprintf_s(pname, sizeof(pname), _TRUNCATE, "%d", ts->TCPPort);
373    
374 doda 4089 HAsync = PWSAAsyncGetAddrInfo(HW, WM_USER_GETHOST,
375 maya 3857 ts->HostName, pname, &hints, &cv->res0);
376     if (HAsync == 0)
377     InvalidHost = TRUE;
378     else {
379     cv->ComPort = 1; // set "getting host" flag
380     // (see CVTWindow::OnSysCommand())
381     do {
382     if (GetMessage(&Msg,0,0,0)) {
383     if ((Msg.hwnd==HW) &&
384     ((Msg.message == WM_SYSCOMMAND) &&
385     ((Msg.wParam & 0xfff0) == SC_CLOSE) ||
386     (Msg.message == WM_COMMAND) &&
387     (LOWORD(Msg.wParam) == ID_FILE_EXIT) ||
388     (Msg.message == WM_CLOSE))) { /* Exit when the user closes Tera Term */
389     PWSACancelAsyncRequest(HAsync);
390     CloseHandle(HAsync);
391     HAsync = 0;
392     cv->ComPort = 0; // clear "getting host" flag
393     PostMessage(HW,Msg.message,Msg.wParam,Msg.lParam);
394     return;
395     }
396     if (Msg.message != WM_USER_GETHOST) { /* Prosess messages */
397     TranslateMessage(&Msg);
398     DispatchMessage(&Msg);
399     }
400     }
401     else {
402     return;
403     }
404     } while (Msg.message!=WM_USER_GETHOST);
405     cv->ComPort = 0; // clear "getting host" flag
406     CloseHandle(HAsync);
407     HAsync = 0;
408     InvalidHost = WSAGETASYNCERROR(Msg.lParam) != 0;
409     }
410     } /* if (!LoadWinsock()) */
411    
412     if (InvalidHost) {
413     if (cv->NoMsg==0) {
414 zmatsuo 8616 static const TTMessageBoxInfoW info = {
415     "Tera Term",
416     "MSG_TT_ERROR", L"Tera Term: Error",
417 zmatsuo 9342 "MSG_INVALID_HOST_ERROR", L"Invalid host",
418     MB_TASKMODAL | MB_ICONEXCLAMATION
419 zmatsuo 8616 };
420 zmatsuo 9342 TTMessageBoxA(cv->HWin, &info, ts->UILanguageFile);
421 maya 3857 }
422     goto BreakSC;
423     }
424     for (cv->res = cv->res0; cv->res; cv->res = cv->res->ai_next) {
425     cv->s = OpenSocket(cv);
426     if (cv->s == INVALID_SOCKET) {
427     CloseSocket(cv->s);
428     continue;
429     }
430     /* start asynchronous connect */
431     AsyncConnect(cv);
432     break; /* break for-loop immediately */
433     }
434     break;
435    
436     case IdSerial:
437     InitFileIO(IdSerial); /* TTPLUG */
438     TTXOpenFile(); /* TTPLUG */
439     _snprintf_s(P, sizeof(P), _TRUNCATE, "COM%d", ts->ComPort);
440     strncpy_s(ErrMsg, sizeof(ErrMsg),P, _TRUNCATE);
441     strncpy_s(P, sizeof(P),"\\\\.\\", _TRUNCATE);
442     strncat_s(P, sizeof(P),ErrMsg, _TRUNCATE);
443 doda 6893 cv->ComID = PCreateFile(P, GENERIC_READ | GENERIC_WRITE, 0, NULL,
444     OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
445 maya 3857 if (cv->ComID == INVALID_HANDLE_VALUE ) {
446 zmatsuo 8616 if (cv->NoMsg==0) {
447     DWORD err = GetLastError();
448     wchar_t *format;
449     wchar_t *PW = ToWcharA(&P[4]);
450     static const TTMessageBoxInfoW info = {
451     "Tera Term",
452     "MSG_TT_ERROR", L"Tera Term: Error",
453 zmatsuo 9342 NULL, NULL,
454     MB_TASKMODAL | MB_ICONEXCLAMATION
455 zmatsuo 8616 };
456 maya 3857
457 zmatsuo 8616 switch (err) {
458     case ERROR_FILE_NOT_FOUND:
459     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR_NOTFOUND", L"Cannot open %s. Not found.", ts->UILanguageFile);
460     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW);
461     break;
462     case ERROR_ACCESS_DENIED:
463     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR_DENIED", L"Cannot open %s. Access denied.", ts->UILanguageFile);
464     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW);
465     break;
466     default:
467     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR", L"Cannot open %s. (0x%08x)", ts->UILanguageFile);
468     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW, err);
469     break;
470     }
471     free(format);
472     free(PW);
473 doda 6893
474 zmatsuo 9342 TTMessageBoxA(cv->HWin, &info, ts->UILanguageFile, ErrMsgW);
475 maya 3857 }
476     InvalidHost = TRUE;
477     }
478     else {
479     cv->Open = TRUE;
480     cv->ComPort = ts->ComPort;
481     CommResetSerial(ts, cv, ts->ClearComBuffOnOpen);
482     if (!ts->ClearComBuffOnOpen) {
483     cv->RRQ = TRUE;
484     }
485    
486     /* notify to VT window that Comm Port is open */
487     PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
488     InvalidHost = FALSE;
489    
490     SetCOMFlag(ts->ComPort);
491     }
492     break; /* end of "case IdSerial:" */
493    
494     case IdFile:
495     InitFileIO(IdFile); /* TTPLUG */
496     TTXOpenFile(); /* TTPLUG */
497 doda 6893 cv->ComID = PCreateFile(ts->HostName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
498 maya 3857 InvalidHost = (cv->ComID == INVALID_HANDLE_VALUE);
499     if (InvalidHost) {
500     if (cv->NoMsg==0) {
501 zmatsuo 8616 static const TTMessageBoxInfoW info = {
502     "Tera Term",
503     "MSG_TT_ERROR", L"Tera Term: Error",
504 zmatsuo 9342 "MSG_CANTOPEN_FILE_ERROR", L"Cannot open file",
505     MB_TASKMODAL | MB_ICONEXCLAMATION
506 zmatsuo 8616 };
507 zmatsuo 9342 TTMessageBoxA(cv->HWin, &info, ts->UILanguageFile);
508 maya 3857 }
509     }
510     else {
511     cv->Open = TRUE;
512     PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
513     }
514     break;
515 yutakapon 4857
516     case IdNamedPipe:
517     InitFileIO(IdNamedPipe); /* TTPLUG */
518     TTXOpenFile(); /* TTPLUG */
519 doda 6435
520 yutakapon 4858 memset(P, 0, sizeof(P));
521 yutakapon 4857 strncpy_s(P, sizeof(P), ts->HostName, _TRUNCATE);
522 yutakapon 4858
523     // ���O�t���p�C�v�����������������`�F�b�N�����B
524 yutakapon 4860 if (CheckNamedPipeFormat(P, strlen(P)) < 0) {
525 zmatsuo 8616 static const TTMessageBoxInfoW info = {
526     "Tera Term",
527     "MSG_TT_ERROR", L"Tera Term: Error",
528 zmatsuo 9342 NULL, NULL,
529     MB_TASKMODAL | MB_ICONEXCLAMATION
530 zmatsuo 8616 };
531 yutakapon 4860 InvalidHost = TRUE;
532    
533 zmatsuo 8616 _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE,
534     L"Invalid pipe name (%d)\n\n"
535     L"A valid pipe name has the form\n"
536     L"\"\\\\<ServerName>\\pipe\\<PipeName>\"",
537 yutakapon 4860 GetLastError());
538 zmatsuo 9342 TTMessageBoxA(cv->HWin, &info, ts->UILanguageFile, ErrMsgW);
539 yutakapon 4858 break;
540     }
541    
542 doda 6893 cv->ComID = PCreateFile(P, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
543     0, // �u���b�L���O���[�h������(FILE_FLAG_OVERLAPPED ���w��������)
544     NULL);
545 yutakapon 4857 if (cv->ComID == INVALID_HANDLE_VALUE ) {
546 zmatsuo 8616 if (cv->NoMsg==0) {
547     DWORD err = GetLastError();
548     wchar_t *format;
549     wchar_t* PW = ToWcharA(&P[4]);
550     static const TTMessageBoxInfoW info = {
551     "Tera Term",
552     "MSG_TT_ERROR", L"Tera Term: Error",
553 zmatsuo 9342 NULL, NULL,
554     MB_TASKMODAL | MB_ICONEXCLAMATION
555 zmatsuo 8616 };
556 yutakapon 4857
557 zmatsuo 8616 switch (err) {
558     case ERROR_FILE_NOT_FOUND:
559     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR_NOTFOUND", L"Cannot open %s. Not found.", ts->UILanguageFile);
560     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW);
561     break;
562     case ERROR_ACCESS_DENIED:
563     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR_DENIED", L"Cannot open %s. Access denied.", ts->UILanguageFile);
564     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW);
565     break;
566     case ERROR_PIPE_BUSY:
567     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR_PIPEBUSY", L"Cannot open %s. Pipe is busy.", ts->UILanguageFile);
568     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW);
569     break;
570     default:
571     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR", L"Cannot open %s. (0x%08x)", ts->UILanguageFile);
572     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW, err);
573     break;
574     }
575     free(format);
576     free(PW);
577 doda 6893
578 zmatsuo 9342 TTMessageBoxA(cv->HWin, &info, ts->UILanguageFile, ErrMsgW);
579 yutakapon 4857 }
580     InvalidHost = TRUE;
581     }
582     else {
583     cv->Open = TRUE;
584     PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
585     InvalidHost = FALSE;
586     }
587     break; /* end of "case IdNamedPipe:" */
588    
589 maya 3857 } /* end of "switch" */
590    
591     BreakSC:
592     if (InvalidHost) {
593     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
594     if ( (ts->PortType==IdTCPIP) && cv->Open ) {
595     if ( cv->s!=INVALID_SOCKET ) {
596     Pclosesocket(cv->s);
597 yutakapon 3968 cv->s = INVALID_SOCKET; /* �\�P�b�g�����������t�����B(2010.8.6 yutaka) */
598 maya 3857 }
599     FreeWinsock();
600     }
601     return;
602     }
603     }
604    
605 yutakapon 4857 // ���O�t���p�C�v�p�X���b�h
606     void NamedPipeThread(void *arg)
607     {
608     PComVar cv = (PComVar)arg;
609     DWORD DErr;
610     HANDLE REnd;
611     char Temp[20];
612     char Buffer[1]; // 1byte
613     DWORD BytesRead, TotalBytesAvail, BytesLeftThisMessage;
614    
615     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
616     REnd = OpenEvent(EVENT_ALL_ACCESS,FALSE, Temp);
617     while (TRUE) {
618     BytesRead = 0;
619     // ���O�t���p�C�v���C�x���g���������������������d�l�������A�L���[�����g��
620     // �`���������������AReadFile() ���������������f�����B
621     if (PeekNamedPipe(cv->ComID, Buffer, sizeof(Buffer), &BytesRead, &TotalBytesAvail, &BytesLeftThisMessage)) {
622     if (! cv->Ready) {
623     _endthread();
624     }
625     if (BytesRead == 0) { // �����������A�����������B
626     Sleep(1);
627     continue;
628     }
629     if (! cv->RRQ) {
630     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_READ);
631     }
632     // ReadFile() ���I�������������B
633     WaitForSingleObject(REnd,INFINITE);
634     }
635     else {
636 doda 6435 DErr = GetLastError();
637 yutakapon 4962 // [VMware] this returns 109 (broken pipe) if a named pipe is removed.
638     // [Virtual Box] this returns 233 (pipe not connected) if a named pipe is removed.
639     if (! cv->Ready || ERROR_BROKEN_PIPE == DErr || ERROR_PIPE_NOT_CONNECTED == DErr) {
640 yutakapon 4863 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
641 yutakapon 4857 _endthread();
642     }
643     }
644     }
645     }
646    
647 maya 3857 void CommThread(void *arg)
648     {
649     DWORD Evt;
650     PComVar cv = (PComVar)arg;
651     DWORD DErr;
652     HANDLE REnd;
653     char Temp[20];
654    
655     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
656     REnd = OpenEvent(EVENT_ALL_ACCESS,FALSE, Temp);
657     while (TRUE) {
658     if (WaitCommEvent(cv->ComID,&Evt,NULL)) {
659     if (! cv->Ready) {
660     _endthread();
661     }
662     if (! cv->RRQ) {
663     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_READ);
664     }
665     WaitForSingleObject(REnd,INFINITE);
666     }
667     else {
668     DErr = GetLastError(); // this returns 995 (operation aborted) if a USB com port is removed
669     if (! cv->Ready || ERROR_OPERATION_ABORTED == DErr) {
670     _endthread();
671     }
672     ClearCommError(cv->ComID,&DErr,NULL);
673     }
674     }
675     }
676    
677     void CommStart(PComVar cv, LONG lParam, PTTSet ts)
678     {
679     char ErrMsg[31];
680 zmatsuo 8616 wchar_t ErrMsgW[31];
681 maya 3857 char Temp[20];
682 zmatsuo 8616 wchar_t UIMsgW[MAX_UIMSG];
683 maya 3857
684     if (! cv->Open ) {
685     return;
686     }
687     if ( cv->Ready ) {
688     return;
689     }
690    
691     // �L�����Z���^�C�}�����������������B�������A�������_�� WM_TIMER �����������������\���������B
692     if (*cv->ConnetingTimeout > 0) {
693     KillTimer(cv->HWin, IdCancelConnectTimer);
694     }
695    
696     switch (cv->PortType) {
697     case IdTCPIP:
698     ErrMsg[0] = 0;
699     switch (HIWORD(lParam)) {
700     case WSAECONNREFUSED:
701 zmatsuo 8616 get_lang_msgW("MSG_COMM_REFUSE_ERROR", UIMsgW, _countof(UIMsgW), L"Connection refused", ts->UILanguageFile);
702     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, L"%s", UIMsgW);
703 maya 3857 break;
704     case WSAENETUNREACH:
705 zmatsuo 8616 get_lang_msgW("MSG_COMM_REACH_ERROR", UIMsgW, _countof(UIMsgW), L"Network cannot be reached", ts->UILanguageFile);
706     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, L"%s", UIMsgW);
707 maya 3857 break;
708     case WSAETIMEDOUT:
709 zmatsuo 8616 get_lang_msgW("MSG_COMM_CONNECT_ERROR", UIMsgW, _countof(UIMsgW), L"Connection timed out", ts->UILanguageFile);
710     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, L"%s", UIMsgW);
711 maya 3857 break;
712     default:
713 zmatsuo 8616 get_lang_msgW("MSG_COMM_TIMEOUT_ERROR", UIMsgW, _countof(UIMsgW), L"Cannot connect the host", ts->UILanguageFile);
714     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, L"%s", UIMsgW);
715 maya 3857 }
716     if (HIWORD(lParam)>0) {
717     /* connect() failed */
718     if (cv->res->ai_next != NULL) {
719     /* try to connect with other protocol */
720     CloseSocket(cv->s);
721     for (cv->res = cv->res->ai_next; cv->res; cv->res = cv->res->ai_next) {
722     cv->s = OpenSocket(cv);
723     if (cv->s == INVALID_SOCKET) {
724     CloseSocket(cv->s);
725     continue;
726     }
727     AsyncConnect(cv);
728     cv->Ready = FALSE;
729     cv->RetryWithOtherProtocol = TRUE; /* retry with other procotol */
730     return;
731     }
732     } else {
733     /* trying with all protocol family are failed */
734     if (cv->NoMsg==0)
735     {
736 zmatsuo 8616 static const TTMessageBoxInfoW info = {
737     "Tera Term",
738     "MSG_TT_ERROR", L"Tera Term: Error",
739 zmatsuo 9342 NULL, NULL,
740     MB_TASKMODAL | MB_ICONEXCLAMATION
741 zmatsuo 8616 };
742 zmatsuo 9342 TTMessageBoxA(cv->HWin, &info, ts->UILanguageFile, ErrMsgW);
743 maya 3857 }
744     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
745     cv->RetryWithOtherProtocol = FALSE;
746     return;
747     }
748     }
749    
750     /* here is connection established */
751     cv->RetryWithOtherProtocol = FALSE;
752     PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMNOTIFY, FD_READ | FD_OOB | FD_CLOSE);
753     TCPIPClosed = FALSE;
754     break;
755    
756     case IdSerial:
757     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
758     ReadEnd = CreateEvent(NULL,FALSE,FALSE,Temp);
759     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", WRITENAME, cv->ComPort);
760     memset(&wol,0,sizeof(OVERLAPPED));
761     wol.hEvent = CreateEvent(NULL,TRUE,TRUE,Temp);
762     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READNAME, cv->ComPort);
763     memset(&rol,0,sizeof(OVERLAPPED));
764     rol.hEvent = CreateEvent(NULL,TRUE,FALSE,Temp);
765    
766     /* create the receiver thread */
767     if (_beginthread(CommThread,0,cv) == -1) {
768 zmatsuo 8616 static const TTMessageBoxInfoW info = {
769     "Tera Term",
770     "MSG_TT_ERROR", L"Tera Term: Error",
771 zmatsuo 9342 "MSG_TT_ERROR", L"Can't create thread",
772     MB_TASKMODAL | MB_ICONEXCLAMATION
773 zmatsuo 8616 };
774 zmatsuo 9342 TTMessageBoxA(cv->HWin, &info, ts->UILanguageFile);
775 maya 3857 }
776     break;
777    
778     case IdFile:
779     cv->RRQ = TRUE;
780     break;
781 yutakapon 4857
782     case IdNamedPipe:
783     cv->ComPort = 0;
784     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
785     ReadEnd = CreateEvent(NULL,FALSE,FALSE,Temp);
786     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", WRITENAME, cv->ComPort);
787     memset(&wol,0,sizeof(OVERLAPPED));
788     wol.hEvent = CreateEvent(NULL,TRUE,TRUE,Temp);
789     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READNAME, cv->ComPort);
790     memset(&rol,0,sizeof(OVERLAPPED));
791     rol.hEvent = CreateEvent(NULL,TRUE,FALSE,Temp);
792    
793     /* create the receiver thread */
794     if (_beginthread(NamedPipeThread,0,cv) == -1) {
795 zmatsuo 8616 static const TTMessageBoxInfoW info = {
796     "Tera Term",
797     "MSG_TT_ERROR", L"Tera Term: Error",
798 zmatsuo 9342 "MSG_TT_ERROR", L"Can't create thread",
799     MB_TASKMODAL | MB_ICONEXCLAMATION
800 zmatsuo 8616 };
801 zmatsuo 9342 TTMessageBoxA(cv->HWin, &info, ts->UILanguageFile);
802 yutakapon 4857 }
803     break;
804 maya 3857 }
805     cv->Ready = TRUE;
806 doda 6947 cv->ConnectedTime = GetTickCount();
807 maya 3857 }
808    
809     BOOL CommCanClose(PComVar cv)
810     { // check if data remains in buffer
811     if (! cv->Open) {
812     return TRUE;
813     }
814     if (cv->InBuffCount>0) {
815     return FALSE;
816     }
817 zmatsuo 8906 if (FLogIsOpend() && FLogGetCount() > 0) {
818 maya 3857 return FALSE;
819     }
820 zmatsuo 8860 if (DDELog && DDEGetCount() > 0) {
821     return FALSE;
822     }
823 maya 3857 return TRUE;
824     }
825    
826     void CommClose(PComVar cv)
827     {
828     if ( ! cv->Open ) {
829     return;
830     }
831     cv->Open = FALSE;
832    
833     /* disable event message posting & flush buffer */
834     cv->RRQ = FALSE;
835     cv->Ready = FALSE;
836     cv->InPtr = 0;
837     cv->InBuffCount = 0;
838     cv->OutPtr = 0;
839     cv->OutBuffCount = 0;
840     cv->LineModeBuffCount = 0;
841     cv->FlushLen = 0;
842     cv->Flush = FALSE;
843    
844     /* close port & release resources */
845     switch (cv->PortType) {
846     case IdTCPIP:
847     if (HAsync!=0) {
848     PWSACancelAsyncRequest(HAsync);
849     }
850     HAsync = 0;
851 doda 4165 Pfreeaddrinfo(cv->res0);
852 maya 3857 if ( cv->s!=INVALID_SOCKET ) {
853     Pclosesocket(cv->s);
854     }
855     cv->s = INVALID_SOCKET;
856     TTXCloseTCP(); /* TTPLUG */
857     FreeWinsock();
858     break;
859     case IdSerial:
860     if ( cv->ComID != INVALID_HANDLE_VALUE ) {
861     CloseHandle(ReadEnd);
862     CloseHandle(wol.hEvent);
863     CloseHandle(rol.hEvent);
864     PurgeComm(cv->ComID, PURGE_TXABORT | PURGE_RXABORT |
865     PURGE_TXCLEAR | PURGE_RXCLEAR);
866     EscapeCommFunction(cv->ComID,CLRDTR);
867     SetCommMask(cv->ComID,0);
868     PCloseFile(cv->ComID);
869     ClearCOMFlag(cv->ComPort);
870     }
871     TTXCloseFile(); /* TTPLUG */
872     break;
873     case IdFile:
874     if (cv->ComID != INVALID_HANDLE_VALUE) {
875     PCloseFile(cv->ComID);
876     }
877     TTXCloseFile(); /* TTPLUG */
878     break;
879 yutakapon 4857
880     case IdNamedPipe:
881     if ( cv->ComID != INVALID_HANDLE_VALUE ) {
882     CloseHandle(ReadEnd);
883     CloseHandle(wol.hEvent);
884     CloseHandle(rol.hEvent);
885     PCloseFile(cv->ComID);
886     }
887     TTXCloseFile(); /* TTPLUG */
888     break;
889 maya 3857 }
890     cv->ComID = INVALID_HANDLE_VALUE;
891     cv->PortType = 0;
892     }
893    
894     void CommProcRRQ(PComVar cv)
895     {
896     if ( ! cv->Ready ) {
897     return;
898     }
899     /* disable receive request */
900     switch (cv->PortType) {
901     case IdTCPIP:
902     if (! TCPIPClosed) {
903     PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMNOTIFY, FD_OOB | FD_CLOSE);
904     }
905     break;
906     case IdSerial:
907     break;
908     }
909     cv->RRQ = TRUE;
910     CommReceive(cv);
911     }
912    
913     void CommReceive(PComVar cv)
914     {
915     DWORD C;
916     DWORD DErr;
917    
918     if (! cv->Ready || ! cv->RRQ ||
919     (cv->InBuffCount>=InBuffSize)) {
920     return;
921     }
922    
923     /* Compact buffer */
924     if ((cv->InBuffCount>0) && (cv->InPtr>0)) {
925     memmove(cv->InBuff,&(cv->InBuff[cv->InPtr]),cv->InBuffCount);
926     cv->InPtr = 0;
927     }
928    
929     if (cv->InBuffCount<InBuffSize) {
930     switch (cv->PortType) {
931     case IdTCPIP:
932     C = Precv(cv->s, &(cv->InBuff[cv->InBuffCount]),
933     InBuffSize-cv->InBuffCount, 0);
934     if (C == SOCKET_ERROR) {
935     C = 0;
936     PWSAGetLastError();
937     }
938     cv->InBuffCount = cv->InBuffCount + C;
939     break;
940     case IdSerial:
941     do {
942     ClearCommError(cv->ComID,&DErr,NULL);
943     if (! PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
944     InBuffSize-cv->InBuffCount,&C,&rol)) {
945     if (GetLastError() == ERROR_IO_PENDING) {
946     if (WaitForSingleObject(rol.hEvent, 1000) != WAIT_OBJECT_0) {
947     C = 0;
948     }
949     else {
950     GetOverlappedResult(cv->ComID,&rol,&C,FALSE);
951     }
952     }
953     else {
954     C = 0;
955     }
956     }
957     cv->InBuffCount = cv->InBuffCount + C;
958     } while ((C!=0) && (cv->InBuffCount<InBuffSize));
959     ClearCommError(cv->ComID,&DErr,NULL);
960     break;
961     case IdFile:
962     if (PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
963     InBuffSize-cv->InBuffCount,&C,NULL)) {
964     if (C == 0) {
965     DErr = ERROR_HANDLE_EOF;
966     }
967     else {
968     cv->InBuffCount = cv->InBuffCount + C;
969     }
970     }
971     else {
972     DErr = GetLastError();
973     }
974     break;
975 yutakapon 4857
976     case IdNamedPipe:
977     // �L���[����������1�o�C�g�������f�[�^�������������������m�F���������������A
978     // ReadFile() ���u���b�N�������������������A�������������B
979     if (PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
980     InBuffSize-cv->InBuffCount,&C,NULL)) {
981     if (C == 0) {
982     DErr = ERROR_HANDLE_EOF;
983     }
984     else {
985     cv->InBuffCount = cv->InBuffCount + C;
986     }
987     }
988     else {
989     DErr = GetLastError();
990     }
991    
992     // 1�o�C�g�������������A�C�x���g���N�����A�X���b�h�����J�������B
993     if (cv->InBuffCount > 0) {
994     cv->RRQ = FALSE;
995     SetEvent(ReadEnd);
996     }
997     break;
998 maya 3857 }
999     }
1000    
1001     if (cv->InBuffCount==0) {
1002     switch (cv->PortType) {
1003     case IdTCPIP:
1004     if (! TCPIPClosed) {
1005     PWSAAsyncSelect(cv->s,cv->HWin, WM_USER_COMMNOTIFY,
1006     FD_READ | FD_OOB | FD_CLOSE);
1007     }
1008     break;
1009     case IdSerial:
1010     cv->RRQ = FALSE;
1011     SetEvent(ReadEnd);
1012     return;
1013     case IdFile:
1014     if (DErr != ERROR_IO_PENDING) {
1015     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
1016     cv->RRQ = FALSE;
1017     }
1018     else {
1019     cv->RRQ = TRUE;
1020     }
1021     return;
1022 yutakapon 4857 case IdNamedPipe:
1023     // TODO: �������A���������������������B
1024     if (DErr != ERROR_IO_PENDING) {
1025     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
1026     cv->RRQ = FALSE;
1027     }
1028     else {
1029     cv->RRQ = TRUE;
1030     }
1031     SetEvent(ReadEnd);
1032     return;
1033 maya 3857 }
1034     cv->RRQ = FALSE;
1035     }
1036     }
1037    
1038     void CommSend(PComVar cv)
1039     {
1040     int delay;
1041     COMSTAT Stat;
1042     BYTE LineEnd;
1043     int C, D, Max;
1044     DWORD DErr;
1045    
1046     if ((! cv->Open) || (! cv->Ready)) {
1047 doda 6435 cv->OutBuffCount = 0;
1048 maya 3857 return;
1049     }
1050    
1051     if ((cv->OutBuffCount == 0) || (! cv->CanSend)) {
1052     return;
1053     }
1054    
1055     /* Max num of bytes to be written */
1056     switch (cv->PortType) {
1057     case IdTCPIP:
1058     if (TCPIPClosed) {
1059     cv->OutBuffCount = 0;
1060     }
1061     Max = cv->OutBuffCount;
1062     break;
1063     case IdSerial:
1064     ClearCommError(cv->ComID,&DErr,&Stat);
1065     Max = OutBuffSize - Stat.cbOutQue;
1066     break;
1067     case IdFile:
1068     Max = cv->OutBuffCount;
1069     break;
1070 yutakapon 4857 case IdNamedPipe:
1071     Max = cv->OutBuffCount;
1072     break;
1073 maya 3857 }
1074    
1075     if ( Max<=0 ) {
1076     return;
1077     }
1078     if ( Max > cv->OutBuffCount ) {
1079     Max = cv->OutBuffCount;
1080     }
1081    
1082     if (cv->PortType == IdTCPIP && cv->TelFlag) {
1083     cv->LastSendTime = time(NULL);
1084     }
1085    
1086     C = Max;
1087     delay = 0;
1088    
1089     if ( cv->DelayFlag && (cv->PortType==IdSerial) ) {
1090     if ( cv->DelayPerLine > 0 ) {
1091     if ( cv->CRSend==IdCR ) {
1092     LineEnd = 0x0d;
1093     }
1094 maya 6479 else { // CRLF or LF
1095 maya 3857 LineEnd = 0x0a;
1096     }
1097     C = 1;
1098     if ( cv->DelayPerChar==0 ) {
1099     while ((C<Max) && (cv->OutBuff[cv->OutPtr+C-1]!=LineEnd)) {
1100     C++;
1101     }
1102     }
1103     if ( cv->OutBuff[cv->OutPtr+C-1]==LineEnd ) {
1104     delay = cv->DelayPerLine;
1105     }
1106     else {
1107     delay = cv->DelayPerChar;
1108     }
1109     }
1110     else if ( cv->DelayPerChar > 0 ) {
1111     C = 1;
1112     delay = cv->DelayPerChar;
1113     }
1114     }
1115    
1116     /* Write to comm driver/Winsock */
1117     switch (cv->PortType) {
1118     case IdTCPIP:
1119     D = Psend(cv->s, &(cv->OutBuff[cv->OutPtr]), C, 0);
1120     if ( D==SOCKET_ERROR ) { /* if error occurs */
1121     PWSAGetLastError(); /* Clear error */
1122     D = 0;
1123     }
1124     break;
1125    
1126     case IdSerial:
1127     if (! PWriteFile(cv->ComID,&(cv->OutBuff[cv->OutPtr]),C,(LPDWORD)&D,&wol)) {
1128     if (GetLastError() == ERROR_IO_PENDING) {
1129     if (WaitForSingleObject(wol.hEvent,1000) != WAIT_OBJECT_0) {
1130     D = C; /* Time out, ignore data */
1131     }
1132     else {
1133     GetOverlappedResult(cv->ComID,&wol,(LPDWORD)&D,FALSE);
1134     }
1135     }
1136     else { /* I/O error */
1137     D = C; /* ignore error */
1138     }
1139     }
1140     ClearCommError(cv->ComID,&DErr,&Stat);
1141     break;
1142    
1143     case IdFile:
1144     if (! PWriteFile(cv->ComID, &(cv->OutBuff[cv->OutPtr]), C, (LPDWORD)&D, NULL)) {
1145 doda 8281 if (! (GetLastError() == ERROR_IO_PENDING)) {
1146 maya 3857 D = C; /* ignore data */
1147     }
1148     }
1149     break;
1150 yutakapon 4857
1151     case IdNamedPipe:
1152     if (! PWriteFile(cv->ComID, &(cv->OutBuff[cv->OutPtr]), C, (LPDWORD)&D, NULL)) {
1153 yutakapon 4863 // ERROR_IO_PENDING ���O���G���[���������A�p�C�v���N���[�Y�������������������������A
1154     // ���M�����������������B
1155     if (! (GetLastError() == ERROR_IO_PENDING)) {
1156 yutakapon 4857 D = C; /* ignore data */
1157     }
1158     }
1159     break;
1160 maya 3857 }
1161    
1162     cv->OutBuffCount = cv->OutBuffCount - D;
1163     if ( cv->OutBuffCount==0 ) {
1164     cv->OutPtr = 0;
1165     }
1166     else {
1167     cv->OutPtr = cv->OutPtr + D;
1168     }
1169    
1170     if ( (C==D) && (delay>0) ) {
1171     cv->CanSend = FALSE;
1172     SetTimer(cv->HWin, IdDelayTimer, delay, NULL);
1173     }
1174     }
1175    
1176 maya 5694 void CommSendBreak(PComVar cv, int msec)
1177 maya 3857 /* for only serial ports */
1178     {
1179     MSG DummyMsg;
1180    
1181     if ( ! cv->Ready ) {
1182     return;
1183     }
1184    
1185     switch (cv->PortType) {
1186     case IdSerial:
1187     /* Set com port into a break state */
1188     SetCommBreak(cv->ComID);
1189    
1190     /* pause for 1 sec */
1191 maya 5694 if (SetTimer(cv->HWin, IdBreakTimer, msec, NULL) != 0) {
1192 maya 3857 GetMessage(&DummyMsg,cv->HWin,WM_TIMER,WM_TIMER);
1193     }
1194    
1195     /* Set com port into the nonbreak state */
1196     ClearCommBreak(cv->ComID);
1197     break;
1198     }
1199     }
1200    
1201     void CommLock(PTTSet ts, PComVar cv, BOOL Lock)
1202     {
1203     BYTE b;
1204     DWORD Func;
1205    
1206     if (! cv->Ready) {
1207     return;
1208     }
1209     if ((cv->PortType==IdTCPIP) ||
1210     (cv->PortType==IdSerial) &&
1211 zmatsuo 8860 (!(ts->Flow == IdFlowHard || ts->Flow == IdFlowHardDsrDtr))
1212     ) {
1213 maya 3857 if (Lock) {
1214     b = XOFF;
1215     }
1216     else {
1217     b = XON;
1218     }
1219     CommBinaryOut(cv,&b,1);
1220     }
1221     else if ((cv->PortType==IdSerial) &&
1222 yutakapon 8051 (ts->Flow == IdFlowHard || ts->Flow == IdFlowHardDsrDtr)) {
1223     // �n�[�h�E�F�A�t���[���������������g���@�\�R�[�h�������������B
1224 maya 3857 if (Lock) {
1225     Func = CLRRTS;
1226 yutakapon 8051 if (ts->Flow == IdFlowHardDsrDtr)
1227     Func = CLRDTR;
1228 maya 3857 }
1229     else {
1230     Func = SETRTS;
1231 yutakapon 8051 if (ts->Flow == IdFlowHardDsrDtr)
1232     Func = SETDTR;
1233 maya 3857 }
1234     EscapeCommFunction(cv->ComID,Func);
1235     }
1236     }
1237    
1238     BOOL PrnOpen(PCHAR DevName)
1239     {
1240 maya 4283 char Temp[MAXPATHLEN], *c;
1241 maya 3857 DCB dcb;
1242     DWORD DErr;
1243     COMMTIMEOUTS ctmo;
1244    
1245     strncpy_s(Temp, sizeof(Temp),DevName, _TRUNCATE);
1246 maya 4283 c = Temp;
1247     while (*c != '\0' && *c != ':') {
1248     c++;
1249     }
1250     *c = '\0';
1251 maya 3857 LPTFlag = (Temp[0]=='L') ||
1252     (Temp[0]=='l');
1253 maya 4284
1254 yutakapon 6286 if (IsWindowsNTKernel()) {
1255 maya 4284 // �l�b�g���[�N���L���}�b�v�������f�o�C�X�������������A�������������������������� (2011.01.25 maya)
1256     // http://logmett.com/forum/viewtopic.php?f=2&t=1383
1257     // http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx#5
1258     PrnID = CreateFile(Temp,GENERIC_WRITE | FILE_READ_ATTRIBUTES,
1259     FILE_SHARE_READ,NULL,CREATE_ALWAYS,
1260     0,NULL);
1261     }
1262     else {
1263     // 9x �������L���R�[�h���������������������]������������
1264     PrnID = CreateFile(Temp,GENERIC_WRITE,
1265     0,NULL,OPEN_EXISTING,
1266     0,NULL);
1267     }
1268    
1269 maya 3857 if (PrnID == INVALID_HANDLE_VALUE) {
1270     return FALSE;
1271     }
1272    
1273     if (GetCommState(PrnID,&dcb)) {
1274     BuildCommDCB(DevName,&dcb);
1275     SetCommState(PrnID,&dcb);
1276     }
1277     ClearCommError(PrnID,&DErr,NULL);
1278     if (! LPTFlag) {
1279     SetupComm(PrnID,0,CommOutQueSize);
1280     }
1281     /* flush output buffer */
1282     PurgeComm(PrnID, PURGE_TXABORT | PURGE_TXCLEAR);
1283     memset(&ctmo,0,sizeof(ctmo));
1284     ctmo.WriteTotalTimeoutConstant = 1000;
1285     SetCommTimeouts(PrnID,&ctmo);
1286     if (! LPTFlag) {
1287     EscapeCommFunction(PrnID,SETDTR);
1288     }
1289     return TRUE;
1290     }
1291    
1292     int PrnWrite(PCHAR b, int c)
1293     {
1294     int d;
1295     DWORD DErr;
1296     COMSTAT Stat;
1297    
1298     if (PrnID == INVALID_HANDLE_VALUE ) {
1299     return c;
1300     }
1301    
1302     ClearCommError(PrnID,&DErr,&Stat);
1303     if (! LPTFlag &&
1304     (OutBuffSize - (int)Stat.cbOutQue < c)) {
1305     c = OutBuffSize - Stat.cbOutQue;
1306     }
1307     if (c<=0) {
1308     return 0;
1309     }
1310     if (! WriteFile(PrnID,b,c,(LPDWORD)&d,NULL)) {
1311     d = 0;
1312     }
1313     ClearCommError(PrnID,&DErr,NULL);
1314     return d;
1315     }
1316    
1317     void PrnCancel()
1318     {
1319     PurgeComm(PrnID, PURGE_TXABORT | PURGE_TXCLEAR);
1320     PrnClose();
1321     }
1322    
1323     void PrnClose()
1324     {
1325     if (PrnID != INVALID_HANDLE_VALUE) {
1326     if (!LPTFlag) {
1327     EscapeCommFunction(PrnID,CLRDTR);
1328     }
1329     CloseHandle(PrnID);
1330     }
1331     PrnID = INVALID_HANDLE_VALUE;
1332     }

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