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 6947 - (show annotations) (download) (as text)
Tue Oct 17 09:38:24 2017 UTC (6 years, 5 months ago) by doda
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 35400 byte(s)
ログのタイムスタンプの種別として Elapsed を追加 #37528

要望のあったログ開始からの Elapsed Time の他に、接続開始からの Elapsed
Time も追加した。ログ取得を一旦終了した後に再度追記でログ取得を開始した
時に基準時間が同じになるので便利かなと。

LogTimestampUTC も時刻形式の一種と考えて、設定を共通化した。
LotTimestampType が設定済みの場合は LogTimestampUTC の設定は無視される。

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

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