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 9069 - (hide annotations) (download) (as text)
Sun Dec 20 12:09:44 2020 UTC (3 years, 2 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 36009 byte(s)
ファイル送信ポーズフラグを filesys.cpp へ移動

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

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