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 9314 - (hide annotations) (download) (as text)
Sun Jun 20 01:07:54 2021 UTC (2 years, 9 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 35640 byte(s)
ComVar 構造体から CodePage メンバを削除

- Language, KanjiCode から文字コードを判定
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     };
359     TTMessageBoxW(
360     cv->HWin,
361     &info, (MB_TASKMODAL | MB_ICONEXCLAMATION),
362     ts->UILanguageFile);
363 maya 3857 }
364     InvalidHost = TRUE;
365     }
366     else {
367     TTXOpenTCP(); /* TTPLUG */
368     cv->Open = TRUE;
369     /* resolving address */
370     memset(&hints, 0, sizeof(hints));
371     hints.ai_family = ts->ProtocolFamily;
372     hints.ai_socktype = SOCK_STREAM;
373     hints.ai_protocol = IPPROTO_TCP;
374     _snprintf_s(pname, sizeof(pname), _TRUNCATE, "%d", ts->TCPPort);
375    
376 doda 4089 HAsync = PWSAAsyncGetAddrInfo(HW, WM_USER_GETHOST,
377 maya 3857 ts->HostName, pname, &hints, &cv->res0);
378     if (HAsync == 0)
379     InvalidHost = TRUE;
380     else {
381     cv->ComPort = 1; // set "getting host" flag
382     // (see CVTWindow::OnSysCommand())
383     do {
384     if (GetMessage(&Msg,0,0,0)) {
385     if ((Msg.hwnd==HW) &&
386     ((Msg.message == WM_SYSCOMMAND) &&
387     ((Msg.wParam & 0xfff0) == SC_CLOSE) ||
388     (Msg.message == WM_COMMAND) &&
389     (LOWORD(Msg.wParam) == ID_FILE_EXIT) ||
390     (Msg.message == WM_CLOSE))) { /* Exit when the user closes Tera Term */
391     PWSACancelAsyncRequest(HAsync);
392     CloseHandle(HAsync);
393     HAsync = 0;
394     cv->ComPort = 0; // clear "getting host" flag
395     PostMessage(HW,Msg.message,Msg.wParam,Msg.lParam);
396     return;
397     }
398     if (Msg.message != WM_USER_GETHOST) { /* Prosess messages */
399     TranslateMessage(&Msg);
400     DispatchMessage(&Msg);
401     }
402     }
403     else {
404     return;
405     }
406     } while (Msg.message!=WM_USER_GETHOST);
407     cv->ComPort = 0; // clear "getting host" flag
408     CloseHandle(HAsync);
409     HAsync = 0;
410     InvalidHost = WSAGETASYNCERROR(Msg.lParam) != 0;
411     }
412     } /* if (!LoadWinsock()) */
413    
414     if (InvalidHost) {
415     if (cv->NoMsg==0) {
416 zmatsuo 8616 static const TTMessageBoxInfoW info = {
417     "Tera Term",
418     "MSG_TT_ERROR", L"Tera Term: Error",
419     "MSG_INVALID_HOST_ERROR", L"Invalid host"
420     };
421     TTMessageBoxW(
422     cv->HWin, &info, MB_TASKMODAL | MB_ICONEXCLAMATION, ts->UILanguageFile);
423 maya 3857 }
424     goto BreakSC;
425     }
426     for (cv->res = cv->res0; cv->res; cv->res = cv->res->ai_next) {
427     cv->s = OpenSocket(cv);
428     if (cv->s == INVALID_SOCKET) {
429     CloseSocket(cv->s);
430     continue;
431     }
432     /* start asynchronous connect */
433     AsyncConnect(cv);
434     break; /* break for-loop immediately */
435     }
436     break;
437    
438     case IdSerial:
439     InitFileIO(IdSerial); /* TTPLUG */
440     TTXOpenFile(); /* TTPLUG */
441     _snprintf_s(P, sizeof(P), _TRUNCATE, "COM%d", ts->ComPort);
442     strncpy_s(ErrMsg, sizeof(ErrMsg),P, _TRUNCATE);
443     strncpy_s(P, sizeof(P),"\\\\.\\", _TRUNCATE);
444     strncat_s(P, sizeof(P),ErrMsg, _TRUNCATE);
445 doda 6893 cv->ComID = PCreateFile(P, GENERIC_READ | GENERIC_WRITE, 0, NULL,
446     OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
447 maya 3857 if (cv->ComID == INVALID_HANDLE_VALUE ) {
448 zmatsuo 8616 if (cv->NoMsg==0) {
449     DWORD err = GetLastError();
450     wchar_t *format;
451     wchar_t *PW = ToWcharA(&P[4]);
452     static const TTMessageBoxInfoW info = {
453     "Tera Term",
454     "MSG_TT_ERROR", L"Tera Term: Error",
455 zmatsuo 8953 NULL, NULL
456 zmatsuo 8616 };
457 maya 3857
458 zmatsuo 8616 switch (err) {
459     case ERROR_FILE_NOT_FOUND:
460     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR_NOTFOUND", L"Cannot open %s. Not found.", ts->UILanguageFile);
461     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW);
462     break;
463     case ERROR_ACCESS_DENIED:
464     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR_DENIED", L"Cannot open %s. Access denied.", ts->UILanguageFile);
465     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW);
466     break;
467     default:
468     format = TTGetLangStrW("Tera Term", "MSG_CANTOPEN_ERROR", L"Cannot open %s. (0x%08x)", ts->UILanguageFile);
469     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, format, PW, err);
470     break;
471     }
472     free(format);
473     free(PW);
474 doda 6893
475 zmatsuo 8616 TTMessageBoxW(
476     cv->HWin, &info, MB_TASKMODAL | MB_ICONEXCLAMATION, ts->UILanguageFile, ErrMsgW);
477 maya 3857 }
478     InvalidHost = TRUE;
479     }
480     else {
481     cv->Open = TRUE;
482     cv->ComPort = ts->ComPort;
483     CommResetSerial(ts, cv, ts->ClearComBuffOnOpen);
484     if (!ts->ClearComBuffOnOpen) {
485     cv->RRQ = TRUE;
486     }
487    
488     /* notify to VT window that Comm Port is open */
489     PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
490     InvalidHost = FALSE;
491    
492     SetCOMFlag(ts->ComPort);
493     }
494     break; /* end of "case IdSerial:" */
495    
496     case IdFile:
497     InitFileIO(IdFile); /* TTPLUG */
498     TTXOpenFile(); /* TTPLUG */
499 doda 6893 cv->ComID = PCreateFile(ts->HostName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
500 maya 3857 InvalidHost = (cv->ComID == INVALID_HANDLE_VALUE);
501     if (InvalidHost) {
502     if (cv->NoMsg==0) {
503 zmatsuo 8616 static const TTMessageBoxInfoW info = {
504     "Tera Term",
505     "MSG_TT_ERROR", L"Tera Term: Error",
506     "MSG_CANTOPEN_FILE_ERROR", L"Cannot open file"
507     };
508     TTMessageBoxW(
509     cv->HWin, &info, MB_TASKMODAL | MB_ICONEXCLAMATION, ts->UILanguageFile);
510 maya 3857 }
511     }
512     else {
513     cv->Open = TRUE;
514     PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
515     }
516     break;
517 yutakapon 4857
518     case IdNamedPipe:
519     InitFileIO(IdNamedPipe); /* TTPLUG */
520     TTXOpenFile(); /* TTPLUG */
521 doda 6435
522 yutakapon 4858 memset(P, 0, sizeof(P));
523 yutakapon 4857 strncpy_s(P, sizeof(P), ts->HostName, _TRUNCATE);
524 yutakapon 4858
525     // ���O�t���p�C�v�����������������`�F�b�N�����B
526 yutakapon 4860 if (CheckNamedPipeFormat(P, strlen(P)) < 0) {
527 zmatsuo 8616 static const TTMessageBoxInfoW info = {
528     "Tera Term",
529     "MSG_TT_ERROR", L"Tera Term: Error",
530 zmatsuo 8953 NULL, NULL
531 zmatsuo 8616 };
532 yutakapon 4860 InvalidHost = TRUE;
533    
534 zmatsuo 8616 _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE,
535     L"Invalid pipe name (%d)\n\n"
536     L"A valid pipe name has the form\n"
537     L"\"\\\\<ServerName>\\pipe\\<PipeName>\"",
538 yutakapon 4860 GetLastError());
539 zmatsuo 8616 TTMessageBoxW(cv->HWin, &info, MB_TASKMODAL | MB_ICONEXCLAMATION, ts->UILanguageFile, ErrMsgW);
540 yutakapon 4858 break;
541     }
542    
543 doda 6893 cv->ComID = PCreateFile(P, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
544     0, // �u���b�L���O���[�h������(FILE_FLAG_OVERLAPPED ���w��������)
545     NULL);
546 yutakapon 4857 if (cv->ComID == INVALID_HANDLE_VALUE ) {
547 zmatsuo 8616 if (cv->NoMsg==0) {
548     DWORD err = GetLastError();
549     wchar_t *format;
550     wchar_t* PW = ToWcharA(&P[4]);
551     static const TTMessageBoxInfoW info = {
552     "Tera Term",
553     "MSG_TT_ERROR", L"Tera Term: Error",
554 zmatsuo 8953 NULL, NULL
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 8616 TTMessageBoxW(
579     cv->HWin, &info, MB_TASKMODAL | MB_ICONEXCLAMATION, ts->UILanguageFile, ErrMsgW);
580 yutakapon 4857 }
581     InvalidHost = TRUE;
582     }
583     else {
584     cv->Open = TRUE;
585     PostMessage(cv->HWin, WM_USER_COMMOPEN, 0, 0);
586     InvalidHost = FALSE;
587     }
588     break; /* end of "case IdNamedPipe:" */
589    
590 maya 3857 } /* end of "switch" */
591    
592     BreakSC:
593     if (InvalidHost) {
594     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
595     if ( (ts->PortType==IdTCPIP) && cv->Open ) {
596     if ( cv->s!=INVALID_SOCKET ) {
597     Pclosesocket(cv->s);
598 yutakapon 3968 cv->s = INVALID_SOCKET; /* �\�P�b�g�����������t�����B(2010.8.6 yutaka) */
599 maya 3857 }
600     FreeWinsock();
601     }
602     return;
603     }
604     }
605    
606 yutakapon 4857 // ���O�t���p�C�v�p�X���b�h
607     void NamedPipeThread(void *arg)
608     {
609     PComVar cv = (PComVar)arg;
610     DWORD DErr;
611     HANDLE REnd;
612     char Temp[20];
613     char Buffer[1]; // 1byte
614     DWORD BytesRead, TotalBytesAvail, BytesLeftThisMessage;
615    
616     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
617     REnd = OpenEvent(EVENT_ALL_ACCESS,FALSE, Temp);
618     while (TRUE) {
619     BytesRead = 0;
620     // ���O�t���p�C�v���C�x���g���������������������d�l�������A�L���[�����g��
621     // �`���������������AReadFile() ���������������f�����B
622     if (PeekNamedPipe(cv->ComID, Buffer, sizeof(Buffer), &BytesRead, &TotalBytesAvail, &BytesLeftThisMessage)) {
623     if (! cv->Ready) {
624     _endthread();
625     }
626     if (BytesRead == 0) { // �����������A�����������B
627     Sleep(1);
628     continue;
629     }
630     if (! cv->RRQ) {
631     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_READ);
632     }
633     // ReadFile() ���I�������������B
634     WaitForSingleObject(REnd,INFINITE);
635     }
636     else {
637 doda 6435 DErr = GetLastError();
638 yutakapon 4962 // [VMware] this returns 109 (broken pipe) if a named pipe is removed.
639     // [Virtual Box] this returns 233 (pipe not connected) if a named pipe is removed.
640     if (! cv->Ready || ERROR_BROKEN_PIPE == DErr || ERROR_PIPE_NOT_CONNECTED == DErr) {
641 yutakapon 4863 PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
642 yutakapon 4857 _endthread();
643     }
644     }
645     }
646     }
647    
648 maya 3857 void CommThread(void *arg)
649     {
650     DWORD Evt;
651     PComVar cv = (PComVar)arg;
652     DWORD DErr;
653     HANDLE REnd;
654     char Temp[20];
655    
656     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
657     REnd = OpenEvent(EVENT_ALL_ACCESS,FALSE, Temp);
658     while (TRUE) {
659     if (WaitCommEvent(cv->ComID,&Evt,NULL)) {
660     if (! cv->Ready) {
661     _endthread();
662     }
663     if (! cv->RRQ) {
664     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_READ);
665     }
666     WaitForSingleObject(REnd,INFINITE);
667     }
668     else {
669     DErr = GetLastError(); // this returns 995 (operation aborted) if a USB com port is removed
670     if (! cv->Ready || ERROR_OPERATION_ABORTED == DErr) {
671     _endthread();
672     }
673     ClearCommError(cv->ComID,&DErr,NULL);
674     }
675     }
676     }
677    
678     void CommStart(PComVar cv, LONG lParam, PTTSet ts)
679     {
680     char ErrMsg[31];
681 zmatsuo 8616 wchar_t ErrMsgW[31];
682 maya 3857 char Temp[20];
683 zmatsuo 8616 wchar_t UIMsgW[MAX_UIMSG];
684 maya 3857
685     if (! cv->Open ) {
686     return;
687     }
688     if ( cv->Ready ) {
689     return;
690     }
691    
692     // �L�����Z���^�C�}�����������������B�������A�������_�� WM_TIMER �����������������\���������B
693     if (*cv->ConnetingTimeout > 0) {
694     KillTimer(cv->HWin, IdCancelConnectTimer);
695     }
696    
697     switch (cv->PortType) {
698     case IdTCPIP:
699     ErrMsg[0] = 0;
700     switch (HIWORD(lParam)) {
701     case WSAECONNREFUSED:
702 zmatsuo 8616 get_lang_msgW("MSG_COMM_REFUSE_ERROR", UIMsgW, _countof(UIMsgW), L"Connection refused", ts->UILanguageFile);
703     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, L"%s", UIMsgW);
704 maya 3857 break;
705     case WSAENETUNREACH:
706 zmatsuo 8616 get_lang_msgW("MSG_COMM_REACH_ERROR", UIMsgW, _countof(UIMsgW), L"Network cannot be reached", ts->UILanguageFile);
707     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, L"%s", UIMsgW);
708 maya 3857 break;
709     case WSAETIMEDOUT:
710 zmatsuo 8616 get_lang_msgW("MSG_COMM_CONNECT_ERROR", UIMsgW, _countof(UIMsgW), L"Connection timed out", ts->UILanguageFile);
711     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, L"%s", UIMsgW);
712 maya 3857 break;
713     default:
714 zmatsuo 8616 get_lang_msgW("MSG_COMM_TIMEOUT_ERROR", UIMsgW, _countof(UIMsgW), L"Cannot connect the host", ts->UILanguageFile);
715     _snwprintf_s(ErrMsgW, _countof(ErrMsgW), _TRUNCATE, L"%s", UIMsgW);
716 maya 3857 }
717     if (HIWORD(lParam)>0) {
718     /* connect() failed */
719     if (cv->res->ai_next != NULL) {
720     /* try to connect with other protocol */
721     CloseSocket(cv->s);
722     for (cv->res = cv->res->ai_next; cv->res; cv->res = cv->res->ai_next) {
723     cv->s = OpenSocket(cv);
724     if (cv->s == INVALID_SOCKET) {
725     CloseSocket(cv->s);
726     continue;
727     }
728     AsyncConnect(cv);
729     cv->Ready = FALSE;
730     cv->RetryWithOtherProtocol = TRUE; /* retry with other procotol */
731     return;
732     }
733     } else {
734     /* trying with all protocol family are failed */
735     if (cv->NoMsg==0)
736     {
737 zmatsuo 8616 static const TTMessageBoxInfoW info = {
738     "Tera Term",
739     "MSG_TT_ERROR", L"Tera Term: Error",
740     NULL, NULL
741     };
742     TTMessageBoxW(
743     cv->HWin,
744     &info, (MB_TASKMODAL | MB_ICONEXCLAMATION),
745     ts->UILanguageFile, ErrMsgW);
746 maya 3857 }
747     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
748     cv->RetryWithOtherProtocol = FALSE;
749     return;
750     }
751     }
752    
753     /* here is connection established */
754     cv->RetryWithOtherProtocol = FALSE;
755     PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMNOTIFY, FD_READ | FD_OOB | FD_CLOSE);
756     TCPIPClosed = FALSE;
757     break;
758    
759     case IdSerial:
760     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
761     ReadEnd = CreateEvent(NULL,FALSE,FALSE,Temp);
762     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", WRITENAME, cv->ComPort);
763     memset(&wol,0,sizeof(OVERLAPPED));
764     wol.hEvent = CreateEvent(NULL,TRUE,TRUE,Temp);
765     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READNAME, cv->ComPort);
766     memset(&rol,0,sizeof(OVERLAPPED));
767     rol.hEvent = CreateEvent(NULL,TRUE,FALSE,Temp);
768    
769     /* create the receiver thread */
770     if (_beginthread(CommThread,0,cv) == -1) {
771 zmatsuo 8616 static const TTMessageBoxInfoW info = {
772     "Tera Term",
773     "MSG_TT_ERROR", L"Tera Term: Error",
774     "MSG_TT_ERROR", L"Can't create thread"
775     };
776     TTMessageBoxW(cv->HWin, &info, MB_TASKMODAL | MB_ICONEXCLAMATION, ts->UILanguageFile);
777 maya 3857 }
778     break;
779    
780     case IdFile:
781     cv->RRQ = TRUE;
782     break;
783 yutakapon 4857
784     case IdNamedPipe:
785     cv->ComPort = 0;
786     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READENDNAME, cv->ComPort);
787     ReadEnd = CreateEvent(NULL,FALSE,FALSE,Temp);
788     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", WRITENAME, cv->ComPort);
789     memset(&wol,0,sizeof(OVERLAPPED));
790     wol.hEvent = CreateEvent(NULL,TRUE,TRUE,Temp);
791     _snprintf_s(Temp, sizeof(Temp), _TRUNCATE, "%s%d", READNAME, cv->ComPort);
792     memset(&rol,0,sizeof(OVERLAPPED));
793     rol.hEvent = CreateEvent(NULL,TRUE,FALSE,Temp);
794    
795     /* create the receiver thread */
796     if (_beginthread(NamedPipeThread,0,cv) == -1) {
797 zmatsuo 8616 static const TTMessageBoxInfoW info = {
798     "Tera Term",
799     "MSG_TT_ERROR", L"Tera Term: Error",
800     "MSG_TT_ERROR", L"Can't create thread"
801     };
802     TTMessageBoxW(cv->HWin, &info, MB_TASKMODAL | MB_ICONEXCLAMATION, ts->UILanguageFile);
803 yutakapon 4857 }
804     break;
805 maya 3857 }
806     cv->Ready = TRUE;
807 doda 6947 cv->ConnectedTime = GetTickCount();
808 maya 3857 }
809    
810     BOOL CommCanClose(PComVar cv)
811     { // check if data remains in buffer
812     if (! cv->Open) {
813     return TRUE;
814     }
815     if (cv->InBuffCount>0) {
816     return FALSE;
817     }
818 zmatsuo 8906 if (FLogIsOpend() && FLogGetCount() > 0) {
819 maya 3857 return FALSE;
820     }
821 zmatsuo 8860 if (DDELog && DDEGetCount() > 0) {
822     return FALSE;
823     }
824 maya 3857 return TRUE;
825     }
826    
827     void CommClose(PComVar cv)
828     {
829     if ( ! cv->Open ) {
830     return;
831     }
832     cv->Open = FALSE;
833    
834     /* disable event message posting & flush buffer */
835     cv->RRQ = FALSE;
836     cv->Ready = FALSE;
837     cv->InPtr = 0;
838     cv->InBuffCount = 0;
839     cv->OutPtr = 0;
840     cv->OutBuffCount = 0;
841     cv->LineModeBuffCount = 0;
842     cv->FlushLen = 0;
843     cv->Flush = FALSE;
844    
845     /* close port & release resources */
846     switch (cv->PortType) {
847     case IdTCPIP:
848     if (HAsync!=0) {
849     PWSACancelAsyncRequest(HAsync);
850     }
851     HAsync = 0;
852 doda 4165 Pfreeaddrinfo(cv->res0);
853 maya 3857 if ( cv->s!=INVALID_SOCKET ) {
854     Pclosesocket(cv->s);
855     }
856     cv->s = INVALID_SOCKET;
857     TTXCloseTCP(); /* TTPLUG */
858     FreeWinsock();
859     break;
860     case IdSerial:
861     if ( cv->ComID != INVALID_HANDLE_VALUE ) {
862     CloseHandle(ReadEnd);
863     CloseHandle(wol.hEvent);
864     CloseHandle(rol.hEvent);
865     PurgeComm(cv->ComID, PURGE_TXABORT | PURGE_RXABORT |
866     PURGE_TXCLEAR | PURGE_RXCLEAR);
867     EscapeCommFunction(cv->ComID,CLRDTR);
868     SetCommMask(cv->ComID,0);
869     PCloseFile(cv->ComID);
870     ClearCOMFlag(cv->ComPort);
871     }
872     TTXCloseFile(); /* TTPLUG */
873     break;
874     case IdFile:
875     if (cv->ComID != INVALID_HANDLE_VALUE) {
876     PCloseFile(cv->ComID);
877     }
878     TTXCloseFile(); /* TTPLUG */
879     break;
880 yutakapon 4857
881     case IdNamedPipe:
882     if ( cv->ComID != INVALID_HANDLE_VALUE ) {
883     CloseHandle(ReadEnd);
884     CloseHandle(wol.hEvent);
885     CloseHandle(rol.hEvent);
886     PCloseFile(cv->ComID);
887     }
888     TTXCloseFile(); /* TTPLUG */
889     break;
890 maya 3857 }
891     cv->ComID = INVALID_HANDLE_VALUE;
892     cv->PortType = 0;
893     }
894    
895     void CommProcRRQ(PComVar cv)
896     {
897     if ( ! cv->Ready ) {
898     return;
899     }
900     /* disable receive request */
901     switch (cv->PortType) {
902     case IdTCPIP:
903     if (! TCPIPClosed) {
904     PWSAAsyncSelect(cv->s,cv->HWin,WM_USER_COMMNOTIFY, FD_OOB | FD_CLOSE);
905     }
906     break;
907     case IdSerial:
908     break;
909     }
910     cv->RRQ = TRUE;
911     CommReceive(cv);
912     }
913    
914     void CommReceive(PComVar cv)
915     {
916     DWORD C;
917     DWORD DErr;
918    
919     if (! cv->Ready || ! cv->RRQ ||
920     (cv->InBuffCount>=InBuffSize)) {
921     return;
922     }
923    
924     /* Compact buffer */
925     if ((cv->InBuffCount>0) && (cv->InPtr>0)) {
926     memmove(cv->InBuff,&(cv->InBuff[cv->InPtr]),cv->InBuffCount);
927     cv->InPtr = 0;
928     }
929    
930     if (cv->InBuffCount<InBuffSize) {
931     switch (cv->PortType) {
932     case IdTCPIP:
933     C = Precv(cv->s, &(cv->InBuff[cv->InBuffCount]),
934     InBuffSize-cv->InBuffCount, 0);
935     if (C == SOCKET_ERROR) {
936     C = 0;
937     PWSAGetLastError();
938     }
939     cv->InBuffCount = cv->InBuffCount + C;
940     break;
941     case IdSerial:
942     do {
943     ClearCommError(cv->ComID,&DErr,NULL);
944     if (! PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
945     InBuffSize-cv->InBuffCount,&C,&rol)) {
946     if (GetLastError() == ERROR_IO_PENDING) {
947     if (WaitForSingleObject(rol.hEvent, 1000) != WAIT_OBJECT_0) {
948     C = 0;
949     }
950     else {
951     GetOverlappedResult(cv->ComID,&rol,&C,FALSE);
952     }
953     }
954     else {
955     C = 0;
956     }
957     }
958     cv->InBuffCount = cv->InBuffCount + C;
959     } while ((C!=0) && (cv->InBuffCount<InBuffSize));
960     ClearCommError(cv->ComID,&DErr,NULL);
961     break;
962     case IdFile:
963     if (PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
964     InBuffSize-cv->InBuffCount,&C,NULL)) {
965     if (C == 0) {
966     DErr = ERROR_HANDLE_EOF;
967     }
968     else {
969     cv->InBuffCount = cv->InBuffCount + C;
970     }
971     }
972     else {
973     DErr = GetLastError();
974     }
975     break;
976 yutakapon 4857
977     case IdNamedPipe:
978     // �L���[����������1�o�C�g�������f�[�^�������������������m�F���������������A
979     // ReadFile() ���u���b�N�������������������A�������������B
980     if (PReadFile(cv->ComID,&(cv->InBuff[cv->InBuffCount]),
981     InBuffSize-cv->InBuffCount,&C,NULL)) {
982     if (C == 0) {
983     DErr = ERROR_HANDLE_EOF;
984     }
985     else {
986     cv->InBuffCount = cv->InBuffCount + C;
987     }
988     }
989     else {
990     DErr = GetLastError();
991     }
992    
993     // 1�o�C�g�������������A�C�x���g���N�����A�X���b�h�����J�������B
994     if (cv->InBuffCount > 0) {
995     cv->RRQ = FALSE;
996     SetEvent(ReadEnd);
997     }
998     break;
999 maya 3857 }
1000     }
1001    
1002     if (cv->InBuffCount==0) {
1003     switch (cv->PortType) {
1004     case IdTCPIP:
1005     if (! TCPIPClosed) {
1006     PWSAAsyncSelect(cv->s,cv->HWin, WM_USER_COMMNOTIFY,
1007     FD_READ | FD_OOB | FD_CLOSE);
1008     }
1009     break;
1010     case IdSerial:
1011     cv->RRQ = FALSE;
1012     SetEvent(ReadEnd);
1013     return;
1014     case IdFile:
1015     if (DErr != ERROR_IO_PENDING) {
1016     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
1017     cv->RRQ = FALSE;
1018     }
1019     else {
1020     cv->RRQ = TRUE;
1021     }
1022     return;
1023 yutakapon 4857 case IdNamedPipe:
1024     // TODO: �������A���������������������B
1025     if (DErr != ERROR_IO_PENDING) {
1026     PostMessage(cv->HWin, WM_USER_COMMNOTIFY, 0, FD_CLOSE);
1027     cv->RRQ = FALSE;
1028     }
1029     else {
1030     cv->RRQ = TRUE;
1031     }
1032     SetEvent(ReadEnd);
1033     return;
1034 maya 3857 }
1035     cv->RRQ = FALSE;
1036     }
1037     }
1038    
1039     void CommSend(PComVar cv)
1040     {
1041     int delay;
1042     COMSTAT Stat;
1043     BYTE LineEnd;
1044     int C, D, Max;
1045     DWORD DErr;
1046    
1047     if ((! cv->Open) || (! cv->Ready)) {
1048 doda 6435 cv->OutBuffCount = 0;
1049 maya 3857 return;
1050     }
1051    
1052     if ((cv->OutBuffCount == 0) || (! cv->CanSend)) {
1053     return;
1054     }
1055    
1056     /* Max num of bytes to be written */
1057     switch (cv->PortType) {
1058     case IdTCPIP:
1059     if (TCPIPClosed) {
1060     cv->OutBuffCount = 0;
1061     }
1062     Max = cv->OutBuffCount;
1063     break;
1064     case IdSerial:
1065     ClearCommError(cv->ComID,&DErr,&Stat);
1066     Max = OutBuffSize - Stat.cbOutQue;
1067     break;
1068     case IdFile:
1069     Max = cv->OutBuffCount;
1070     break;
1071 yutakapon 4857 case IdNamedPipe:
1072     Max = cv->OutBuffCount;
1073     break;
1074 maya 3857 }
1075    
1076     if ( Max<=0 ) {
1077     return;
1078     }
1079     if ( Max > cv->OutBuffCount ) {
1080     Max = cv->OutBuffCount;
1081     }
1082    
1083     if (cv->PortType == IdTCPIP && cv->TelFlag) {
1084     cv->LastSendTime = time(NULL);
1085     }
1086    
1087     C = Max;
1088     delay = 0;
1089    
1090     if ( cv->DelayFlag && (cv->PortType==IdSerial) ) {
1091     if ( cv->DelayPerLine > 0 ) {
1092     if ( cv->CRSend==IdCR ) {
1093     LineEnd = 0x0d;
1094     }
1095 maya 6479 else { // CRLF or LF
1096 maya 3857 LineEnd = 0x0a;
1097     }
1098     C = 1;
1099     if ( cv->DelayPerChar==0 ) {
1100     while ((C<Max) && (cv->OutBuff[cv->OutPtr+C-1]!=LineEnd)) {
1101     C++;
1102     }
1103     }
1104     if ( cv->OutBuff[cv->OutPtr+C-1]==LineEnd ) {
1105     delay = cv->DelayPerLine;
1106     }
1107     else {
1108     delay = cv->DelayPerChar;
1109     }
1110     }
1111     else if ( cv->DelayPerChar > 0 ) {
1112     C = 1;
1113     delay = cv->DelayPerChar;
1114     }
1115     }
1116    
1117     /* Write to comm driver/Winsock */
1118     switch (cv->PortType) {
1119     case IdTCPIP:
1120     D = Psend(cv->s, &(cv->OutBuff[cv->OutPtr]), C, 0);
1121     if ( D==SOCKET_ERROR ) { /* if error occurs */
1122     PWSAGetLastError(); /* Clear error */
1123     D = 0;
1124     }
1125     break;
1126    
1127     case IdSerial:
1128     if (! PWriteFile(cv->ComID,&(cv->OutBuff[cv->OutPtr]),C,(LPDWORD)&D,&wol)) {
1129     if (GetLastError() == ERROR_IO_PENDING) {
1130     if (WaitForSingleObject(wol.hEvent,1000) != WAIT_OBJECT_0) {
1131     D = C; /* Time out, ignore data */
1132     }
1133     else {
1134     GetOverlappedResult(cv->ComID,&wol,(LPDWORD)&D,FALSE);
1135     }
1136     }
1137     else { /* I/O error */
1138     D = C; /* ignore error */
1139     }
1140     }
1141     ClearCommError(cv->ComID,&DErr,&Stat);
1142     break;
1143    
1144     case IdFile:
1145     if (! PWriteFile(cv->ComID, &(cv->OutBuff[cv->OutPtr]), C, (LPDWORD)&D, NULL)) {
1146 doda 8281 if (! (GetLastError() == ERROR_IO_PENDING)) {
1147 maya 3857 D = C; /* ignore data */
1148     }
1149     }
1150     break;
1151 yutakapon 4857
1152     case IdNamedPipe:
1153     if (! PWriteFile(cv->ComID, &(cv->OutBuff[cv->OutPtr]), C, (LPDWORD)&D, NULL)) {
1154 yutakapon 4863 // ERROR_IO_PENDING ���O���G���[���������A�p�C�v���N���[�Y�������������������������A
1155     // ���M�����������������B
1156     if (! (GetLastError() == ERROR_IO_PENDING)) {
1157 yutakapon 4857 D = C; /* ignore data */
1158     }
1159     }
1160     break;
1161 maya 3857 }
1162    
1163     cv->OutBuffCount = cv->OutBuffCount - D;
1164     if ( cv->OutBuffCount==0 ) {
1165     cv->OutPtr = 0;
1166     }
1167     else {
1168     cv->OutPtr = cv->OutPtr + D;
1169     }
1170    
1171     if ( (C==D) && (delay>0) ) {
1172     cv->CanSend = FALSE;
1173     SetTimer(cv->HWin, IdDelayTimer, delay, NULL);
1174     }
1175     }
1176    
1177 maya 5694 void CommSendBreak(PComVar cv, int msec)
1178 maya 3857 /* for only serial ports */
1179     {
1180     MSG DummyMsg;
1181    
1182     if ( ! cv->Ready ) {
1183     return;
1184     }
1185    
1186     switch (cv->PortType) {
1187     case IdSerial:
1188     /* Set com port into a break state */
1189     SetCommBreak(cv->ComID);
1190    
1191     /* pause for 1 sec */
1192 maya 5694 if (SetTimer(cv->HWin, IdBreakTimer, msec, NULL) != 0) {
1193 maya 3857 GetMessage(&DummyMsg,cv->HWin,WM_TIMER,WM_TIMER);
1194     }
1195    
1196     /* Set com port into the nonbreak state */
1197     ClearCommBreak(cv->ComID);
1198     break;
1199     }
1200     }
1201    
1202     void CommLock(PTTSet ts, PComVar cv, BOOL Lock)
1203     {
1204     BYTE b;
1205     DWORD Func;
1206    
1207     if (! cv->Ready) {
1208     return;
1209     }
1210     if ((cv->PortType==IdTCPIP) ||
1211     (cv->PortType==IdSerial) &&
1212 zmatsuo 8860 (!(ts->Flow == IdFlowHard || ts->Flow == IdFlowHardDsrDtr))
1213     ) {
1214 maya 3857 if (Lock) {
1215     b = XOFF;
1216     }
1217     else {
1218     b = XON;
1219     }
1220     CommBinaryOut(cv,&b,1);
1221     }
1222     else if ((cv->PortType==IdSerial) &&
1223 yutakapon 8051 (ts->Flow == IdFlowHard || ts->Flow == IdFlowHardDsrDtr)) {
1224     // �n�[�h�E�F�A�t���[���������������g���@�\�R�[�h�������������B
1225 maya 3857 if (Lock) {
1226     Func = CLRRTS;
1227 yutakapon 8051 if (ts->Flow == IdFlowHardDsrDtr)
1228     Func = CLRDTR;
1229 maya 3857 }
1230     else {
1231     Func = SETRTS;
1232 yutakapon 8051 if (ts->Flow == IdFlowHardDsrDtr)
1233     Func = SETDTR;
1234 maya 3857 }
1235     EscapeCommFunction(cv->ComID,Func);
1236     }
1237     }
1238    
1239     BOOL PrnOpen(PCHAR DevName)
1240     {
1241 maya 4283 char Temp[MAXPATHLEN], *c;
1242 maya 3857 DCB dcb;
1243     DWORD DErr;
1244     COMMTIMEOUTS ctmo;
1245    
1246     strncpy_s(Temp, sizeof(Temp),DevName, _TRUNCATE);
1247 maya 4283 c = Temp;
1248     while (*c != '\0' && *c != ':') {
1249     c++;
1250     }
1251     *c = '\0';
1252 maya 3857 LPTFlag = (Temp[0]=='L') ||
1253     (Temp[0]=='l');
1254 maya 4284
1255 yutakapon 6286 if (IsWindowsNTKernel()) {
1256 maya 4284 // �l�b�g���[�N���L���}�b�v�������f�o�C�X�������������A�������������������������� (2011.01.25 maya)
1257     // http://logmett.com/forum/viewtopic.php?f=2&t=1383
1258     // http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx#5
1259     PrnID = CreateFile(Temp,GENERIC_WRITE | FILE_READ_ATTRIBUTES,
1260     FILE_SHARE_READ,NULL,CREATE_ALWAYS,
1261     0,NULL);
1262     }
1263     else {
1264     // 9x �������L���R�[�h���������������������]������������
1265     PrnID = CreateFile(Temp,GENERIC_WRITE,
1266     0,NULL,OPEN_EXISTING,
1267     0,NULL);
1268     }
1269    
1270 maya 3857 if (PrnID == INVALID_HANDLE_VALUE) {
1271     return FALSE;
1272     }
1273    
1274     if (GetCommState(PrnID,&dcb)) {
1275     BuildCommDCB(DevName,&dcb);
1276     SetCommState(PrnID,&dcb);
1277     }
1278     ClearCommError(PrnID,&DErr,NULL);
1279     if (! LPTFlag) {
1280     SetupComm(PrnID,0,CommOutQueSize);
1281     }
1282     /* flush output buffer */
1283     PurgeComm(PrnID, PURGE_TXABORT | PURGE_TXCLEAR);
1284     memset(&ctmo,0,sizeof(ctmo));
1285     ctmo.WriteTotalTimeoutConstant = 1000;
1286     SetCommTimeouts(PrnID,&ctmo);
1287     if (! LPTFlag) {
1288     EscapeCommFunction(PrnID,SETDTR);
1289     }
1290     return TRUE;
1291     }
1292    
1293     int PrnWrite(PCHAR b, int c)
1294     {
1295     int d;
1296     DWORD DErr;
1297     COMSTAT Stat;
1298    
1299     if (PrnID == INVALID_HANDLE_VALUE ) {
1300     return c;
1301     }
1302    
1303     ClearCommError(PrnID,&DErr,&Stat);
1304     if (! LPTFlag &&
1305     (OutBuffSize - (int)Stat.cbOutQue < c)) {
1306     c = OutBuffSize - Stat.cbOutQue;
1307     }
1308     if (c<=0) {
1309     return 0;
1310     }
1311     if (! WriteFile(PrnID,b,c,(LPDWORD)&d,NULL)) {
1312     d = 0;
1313     }
1314     ClearCommError(PrnID,&DErr,NULL);
1315     return d;
1316     }
1317    
1318     void PrnCancel()
1319     {
1320     PurgeComm(PrnID, PURGE_TXABORT | PURGE_TXCLEAR);
1321     PrnClose();
1322     }
1323    
1324     void PrnClose()
1325     {
1326     if (PrnID != INVALID_HANDLE_VALUE) {
1327     if (!LPTFlag) {
1328     EscapeCommFunction(PrnID,CLRDTR);
1329     }
1330     CloseHandle(PrnID);
1331     }
1332     PrnID = INVALID_HANDLE_VALUE;
1333     }

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