-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Socket.cc
187 lines (163 loc) · 5.3 KB
/
Socket.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* AMRISim is based on MobileSim (Copyright 2005 ActivMedia Robotics, 2006-2010
* MobileRobots Inc, 2011-2015 Adept Technology, 2016-2017 Omron Adept Technologies)
* and Stage version 2 (Copyright Richard Vaughan, Brian Gerkey, Andrew Howard, and
* others), published under the terms of the GNU General Public License version 2.
*
* Copyright 2018 Reed Hedges and others
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "AMRISim.hh"
#include "Socket.hh"
#include <sys/stat.h>
#include <unistd.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/select.h>
#include <sys/types.h>
#endif
using namespace AMRISim;
Sockets::SocketsList Sockets::sockets;
void Sockets::addSocketCallback(ArSocket *s, Callback* callback, const std::string& name)
{
//socketsByFD[s->getFD()] = info;
//sockets[s] = SocketInfo(s, callback, name);
sockets.insert(SocketsList::value_type(s, SocketInfo(s, callback, name)));
}
void Sockets::removeSocketCallback(ArSocket *s)
{
//std::map<int, SocketInfo>::const_iterator fi = socketsByFD.find(s->getFD());
//if(fi != socketsByFD.end())
// socketsByFD.erase(fi);
std::map<ArSocket*, SocketInfo>::iterator si = sockets.find(s);
if(si != sockets.end())
{
sockets.erase(si);
}
}
int Sockets::processInput(unsigned int maxTime)
{
if(sockets.size() == 0)
return 1;
struct timeval tv;
//printf("maxtime=%u\n", maxTime);
//assert(maxTime < 99999);
//long usec = (long)(((double)maxTime)*1000.0);
//printf("maxtime usec=%ld\n", usec);
if(maxTime > 1000)
tv.tv_sec = (long)floor((double)maxTime/1000.0);
else
tv.tv_sec = 0;
tv.tv_usec = (long)floor((double)(maxTime-tv.tv_sec)*1000.0);
fd_set rfds;
int maxfd = 0;
FD_ZERO(&rfds);
//print_debug("Sockets::process: Select on sockets:\n");
for(Sockets::SocketsList::const_iterator i = sockets.begin(); i != sockets.end(); ++i)
{
int fd = (*i).second.fd;
#ifndef WIN32
struct stat sb;
int st = fstat(fd, &sb);
//print_debug("\t%d (%s) (fstat=%d)\n", fd, (*i).second.name.c_str(), st);
if(st == -1)
{
print_debug("Skipping invalid socket %d in Sockets::processInput", fd);
}
else
#endif
{
FD_SET(fd, &rfds);
maxfd = max(fd, maxfd);
}
}
if(maxfd == 0)
return 1; // no valid sockets in sockets list
//printf(" (maxFD=%d, tv_sec=%ld, tv_usec=%ld)\n", maxfd, tv.tv_sec, tv.tv_usec);
//printf("select... maxFD=%d, maxTime=%d, sockets.size=%d\n", maxfd, maxTime, sockets.size() ); fflush(stdout);
ArTime t;
int n = select(maxfd+1, &rfds, NULL, NULL, &tv); //NULL);// &tv);
//print_debug("...select returned %d after %ld msec\n\n", n, t.mSecSince()); fflush(stdout);
if(t.mSecSince() < (long)maxTime)
maxTime -= (unsigned int) t.mSecSince();
else
maxTime = 0;
if(n == -1)
{
if(errno == EBADF)
{
// XXX TEMP -- debugging --
print_debug("bad file descriptor error from select()...");
for(Sockets::SocketsList::const_iterator i = sockets.begin(); i != sockets.end(); ++i)
{
int fd = (*i).second.fd;
struct stat sb;
int s = fstat(fd, &sb);
print_debug("...%d is invalid (%d)", fd, s);
}
// XXX TEMP -- end debugging --
}
else if(errno == EINVAL)
puts("Error: bad nfds or timeout argument to select()");
else if(errno == EINTR)
puts("Error: Signal during select()");
else if(errno == ENOMEM)
puts("Error: out of memory during select() call");
}
if(n <= 0)
return n;
// Build a separate list of callbacks to invoke for ready sockets
// Note these callbacks may modify the main 'sockets' list.
std::list<Callback*> readycbs;
for(std::map<ArSocket*, SocketInfo>::const_iterator i = sockets.begin(); n > 0 && i != sockets.end(); ++i)
{
if(FD_ISSET((*i).second.fd, &rfds))
{
readycbs.push_back((*i).second.callback);
--n;
}
}
for(std::list<Callback*>::const_iterator i = readycbs.begin(); i != readycbs.end(); ++i)
{
Callback *c = (*i);
//print_debug("invoking socket callback 0x%x", c);
try
{
c->invoke(maxTime / (unsigned int) readycbs.size());
}
catch(AMRISim::DeletionRequest &r)
{
// a callback into some instance (class unknown here) wants that instance to be deleted after processing it
//print_debug("sockets: got deletion request, handling...");
r.doDelete();
}
}
return 1;
}
ArSocket* Sockets::newSocket(Callback *callback, const std::string& name)
{
ArSocket *s = new ArSocket;
addSocketCallback(s, callback, name);
return s;
}
void Sockets::deleteSocket(ArSocket *socket)
{
removeSocketCallback(socket);
delete socket;
}