00001 /* 00002 * Copyright 1997, Brown University, Providence, RI. 00003 * 00004 * All Rights Reserved 00005 * 00006 * Permission to use, copy, modify, and distribute this software and its 00007 * documentation for any purpose other than its incorporation into a 00008 * commercial product is hereby granted without fee, provided that the 00009 * above copyright notice appear in all copies and that both that 00010 * copyright notice and this permission notice appear in supporting 00011 * documentation, and that the name of Brown University not be used in 00012 * advertising or publicity pertaining to distribution of the software 00013 * without specific, written prior permission. 00014 * 00015 * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 00016 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY 00017 * PARTICULAR PURPOSE. IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR 00018 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00019 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 00020 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 00021 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00022 */ 00023 #ifndef TTY_WIN_HAS_BEEN_INCLUDED 00024 #define TTY_WIN_HAS_BEEN_INCLUDED 00025 00026 #include "tty/tty.H" 00027 #include <Winsock2.h> 00028 00029 class WIN_MANAGER : public FD_MANAGER { 00030 public: 00031 class tty_to_id { 00032 public : 00033 FD_EVENT *_fd; 00034 tty_to_id() : _fd(0) { } 00035 tty_to_id(FD_EVENT *fd) : _fd(fd) { } 00036 int ready() { 00037 COMSTAT stat; 00038 DWORD eflags; 00039 if (ClearCommError((HANDLE)(_fd->fd()), &eflags, &stat)) { 00040 return (stat.cbInQue > 0); 00041 } else if (_fd->fd() == fileno(stdin)) { 00042 return 0; 00043 // return num_bytes_to_read(fileno(stdin)) > 0; 00044 } else { 00045 fd_set fd; 00046 struct timeval tm; 00047 00048 FD_ZERO(&fd); 00049 FD_SET(_fd->fd(), &fd); 00050 tm.tv_usec = 0; 00051 tm.tv_sec = 0; 00052 if (select(_fd->fd()+1, &fd, NULL, NULL, &tm) != SOCKET_ERROR) 00053 return FD_ISSET(_fd->fd(), &fd); 00054 } 00055 return 0; 00056 } 00057 int operator == (const tty_to_id &i) { return _fd == i._fd; } 00058 }; 00059 00060 protected : 00061 ARRAY<tty_to_id> _ids; 00062 00063 public : 00064 WIN_MANAGER() { } 00065 00066 virtual void loop(int infinite=1){ do { 00067 for (int i=0; i<_ids.num(); i++) { 00068 if (_ids[i].ready()) 00069 ((FD_EVENT *)_ids[i]._fd)->sample(); 00070 } 00071 // dfk: adding this.. don't think timeouts are ever getting called on windows?? 00072 for (int j=0; j<_timeouts.num(); j++) { 00073 _timeouts[j]->timeout(); 00074 } 00075 } while (infinite); 00076 } 00077 00078 virtual void add(FD_EVENT *fd) { _ids += tty_to_id(fd); } 00079 virtual void rem(FD_EVENT *fd) { if (_ids.contains(tty_to_id(fd))) 00080 _ids -= tty_to_id(fd); } 00081 00082 virtual void add(FD_TIMEOUT *fd) { FD_MANAGER::add(fd);} 00083 virtual void rem(FD_TIMEOUT *fd) { FD_MANAGER::rem(fd);} 00084 00085 }; 00086 00087 #endif