Develop and Download Open Source Software

Browse Subversion Repository

Contents of /branches/ttcomtester/teraterm/teraterm/commlib.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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