Develop and Download Open Source Software

Browse Subversion Repository

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 9313 - (hide annotations) (download) (as text)
Sun Jun 20 01:07:40 2021 UTC (2 years, 8 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 35672 byte(s)
locale設定を削除

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

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