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 8860 - (hide annotations) (download) (as text)
Sat Jul 25 16:00:24 2020 UTC (3 years, 8 months ago) by zmatsuo
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 36278 byte(s)
マクロ(ttmacro)用送信バッファの分離

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

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