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 10609 - (hide annotations) (download) (as text)
Tue Feb 21 15:49:22 2023 UTC (12 months, 2 weeks ago) by zmatsuo
File MIME type: text/x-csrc
File size: 37359 byte(s)
iniファイルのキーを修正

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

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