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 6806 - (hide annotations) (download) (as text)
Thu Jun 15 00:37:01 2017 UTC (6 years, 8 months ago) by doda
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 33889 byte(s)
TeraTerm Project としてのライセンス表記を追加

とりあえず Tera Term 本体分。
TeraTerm Project としての copyright 表記の年部分はコミットログを確認して書いたつもりだけど、ミスってたらすみません。

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

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