1 #include "FileDescriptor.h"
4 namespace remote
{ namespace mcs
{
6 void FileDescriptor::serviceLoop()
8 unsigned int i
, instanceCount
;
10 filedescriptorsbyfd_t::iterator fileDescriptorIterator
;
11 // register the timeout handler
12 signal( SIGALRM
, timeoutHandler
);
13 ignoreBrokenPipeSignals();
17 instanceCount
= instances
.size();
18 pollfd fdmap
[instanceCount
];
19 FileDescriptor::buildPollMap(fdmap
);
20 if ( poll(fdmap
,instanceCount
,-1) < 0)
22 __THROW__("Error in poll!");
27 // look for activity on any sockets
28 for ( i
= 0; i
< instanceCount
; i
++ )
30 if ( fdmap
[i
].revents
)
33 fileDescriptorIterator
= instances
.find(f
);
34 if (fileDescriptorIterator
!= instances
.end())
36 fileDescriptorIterator
->second
->handleEvent(fdmap
[i
].revents
);
41 catch (remote::protocols::MMSException e
)
44 log("Exception: %s\n",e
.what());
45 fileDescriptorIterator
->second
->clearTimeout();
46 fileDescriptorIterator
->second
->destroy(false);
52 FileDescriptor::FileDescriptor(int p_fd
)
56 log("Opened fd %i\n",fd
);
60 void FileDescriptor::buildPollMap(pollfd
* map
)
63 filedescriptorsbyfd_t::iterator i
;
64 for (i
= instances
.begin(); i
!= instances
.end(); i
++)
66 map
[j
].fd
= i
->second
->fd
;
67 map
[j
].events
= POLLMSG
|POLLRDBAND
|POLLRDNORM
|POLLIN
|POLLPRI
|POLLERR
|POLLHUP
|POLLNVAL
;
74 FileDescriptor::~FileDescriptor()
82 log("close(%i) - %i isn’t a valid open file descriptor.\n",fd
,fd
);
85 log("close(%i) - The close() call was interrupted by a signal.\n",fd
);
88 log("close(%i) - An I/O error occurred.\n",fd
);
91 log("close(%i) - Unknown error %i occured\n",fd
,err
);
96 log("Closed fd %i\n",fd
);
101 void FileDescriptor::destroy(bool silent
)
107 void FileDescriptor::setTimeout(int secs
)
111 timeout
.it_interval
.tv_sec
= 0;
112 timeout
.it_interval
.tv_usec
= 0;
113 timeout
.it_value
.tv_sec
= secs
;
114 timeout
.it_value
.tv_usec
= 0;
115 if (setitimer(ITIMER_REAL
, &timeout
, NULL
))
117 __THROW__ ("Timer set error\n");
122 void FileDescriptor::clearTimeout()
126 timeout
.it_interval
.tv_sec
= 0;
127 timeout
.it_interval
.tv_usec
= 0;
128 timeout
.it_value
.tv_sec
= 0;
129 timeout
.it_value
.tv_usec
= 0;
130 if (setitimer(ITIMER_REAL
, &timeout
, NULL
))
132 __THROW__ ("Timer clear error\n");
138 RETSIGTYPE
FileDescriptor::timeoutHandler(int sig
)
140 log( "Timeout occured!\n");
141 close(currentFd
); // this should generate an exception
144 void FileDescriptor::ignoreBrokenPipeSignals()
146 struct sigaction act
;
147 act
.sa_handler
= SIG_IGN
;
150 sigaction(SIGPIPE
,&act
,NULL
);
153 filedescriptorsbyfd_t
FileDescriptor::instances
;
154 int FileDescriptor::currentFd
= -1;