Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9311 - (hide annotations) (download) (as text)
Fri Jun 18 16:05:10 2021 UTC (2 years, 8 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 35693 byte(s)
ANSI送信関数を削除

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

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