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 10474 - (hide annotations) (download) (as text)
Wed Jan 11 14:41:29 2023 UTC (13 months, 3 weeks ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 35579 byte(s)
TTGetLangStrW()のlngファイル引数をUnicodeへ変更

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

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