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 9373 - (hide annotations) (download) (as text)
Wed Aug 18 15:45:39 2021 UTC (2 years, 6 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 35580 byte(s)
コンパイラの警告で簡単なものを修正

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

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