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 4158 - (show annotations) (download) (as text)
Fri Nov 19 04:06:40 2010 UTC (13 years, 3 months ago) by doda
Original Path: trunk/teraterm/teraterm/commlib.c
File MIME type: text/x-csrc
File size: 29403 byte(s)
NO_INET6 でビルドが通るようにした。

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

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