-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyRideNetComm.h
288 lines (243 loc) · 9 KB
/
PyRideNetComm.h
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// PyRideNetComm.h
// PyRIDE
//
// Created by Xun Wang on 10/05/10.
// Copyright 2010 GalaxyNetwork. All rights reserved.
//
#ifndef PyRideNetComm_h_DEFINED
#define PyRideNetComm_h_DEFINED
#include "PyRideCommon.h"
#ifdef __cplusplus
#ifdef WIN32
#include <winbase.h>
#include <process.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#endif
#ifndef PYRIDE_REMOTE_CLIENT
#include "DeviceController.h"
#endif
namespace pyride {
class PyRideNetComm;
typedef struct {
PyRideNetComm * mainObj;
void * timerObj;
} timerExecuteData;
class RobotDataHandler
{
public:
#ifdef PYRIDE_REMOTE_CLIENT
RobotDataHandler() { clientID_ = 0; }
virtual ~RobotDataHandler() {}
virtual void setImageFormat( const char cID, ImageFormat format ) = 0;
#else
RobotDataHandler() { clientID_ = 0; telemetryClients_ = 0; }
virtual const RobotInfo & defaultRobotInfo() = 0;
#endif
char clientID() { return clientID_; }
protected:
char clientID_;
virtual void onTimer( const long tID ) {}
virtual void onTimerLapsed( const long tID ) {}
#ifdef PYRIDE_REMOTE_CLIENT
virtual void onRobotCreated( const char cID, const int ipAddr, const RobotInfo * rinfo,
const VideoSettings * vsettings, const AudioSettings * asettings,
const unsigned char * optLabel,
const int optLabelLength ) = 0;
virtual void onRobotDestroyed( const char cID ) = 0;
virtual void onRobotTelemetryData( const char cID, const RobotPose * pose, const FieldObject * objects = NULL,
const int nofObjs = 0 ) = 0;
virtual void onTelemetryStreamStart( const char cID ) = 0;
virtual void onTelemetryStreamStop( const char cID ) = 0;
virtual void onImageStreamStart( const char cID ) = 0;
virtual void onImageStreamStop( const char cID ) = 0;
virtual void onImageFormatChange( const char cID, ImageFormat format ) = 0;
virtual void onVideoSwitchChange( const char cID, const VideoSettings * vsettings ) = 0;
virtual void onExtendedCommandResponse( const char cID, const PyRideExtendedCommand command,
const unsigned char * optionalData = NULL,
const int optionalDataLength = 0 ) = 0;
virtual void onOperationalData( const char cID, const int status,
const unsigned char * optionalData = NULL,
const int optionalDataLength = 0 ) = 0;
#else
int telemetryClients_;
virtual void onTelemetryStreamControl( bool isStart ) {};
virtual bool executeRemoteCommand( const unsigned char * commandData, const int dataLength, int & retVal ) = 0;
virtual void cancelCurrentOperation() = 0;
virtual bool onUserLogOn( const unsigned char * authCode, SOCKET_T fd, struct sockaddr_in & addr ) { return false; }
virtual void onUserLogOff( SOCKET_T fd ) {}
virtual int onExclusiveCtrlRequest( SOCKET_T fd ) { return 0; }
virtual void onExclusiveCtrlRelease( SOCKET_T fd ) {}
#endif
private:
#ifdef PYRIDE_REMOTE_CLIENT
#else
void setTelemetryClients( int nofClients ) { telemetryClients_ = nofClients; }
#endif
friend class PyRideNetComm;
};
class PyRideNetComm
{
public:
PyRideNetComm( RobotDataHandler * pDataHandler );
~PyRideNetComm();
#ifdef PYRIDE_REMOTE_CLIENT
void init();
#else
void init( const VideoDeviceList & videoObjs,
const AudioDeviceList & audioObjs );
#endif
void fini();
void startProcessing();
void continuousProcessing();
void processTimer( void * data );
void stopProcessing();
#ifdef PYRIDE_REMOTE_CLIENT
void discoverRobots();
bool logonToRobot( const char * host, const unsigned char * authCode );
void disconnectRobots();
void startTelemetryStream( const char cID = 0 );
void stopTelemetryStream();
void startCameraImageStream( const char cID );
void stopCameraImageStream( const char cID );
void cancelCurrentOperation( const char cID );
void issueExtendedCommand( const char cID, const PyRideExtendedCommand command,
const unsigned char * optionalData , const int optionalDataLength );
void setImageFormat( const char cID, ImageFormat format );
void switchCamera( const char cID, const char vID );
void issueHeartBeat( const char cID );
#else
void declareRobot( const RobotInfo * robotInfo );
void disconnectConsoles();
void dispatchTelemetryData( const unsigned char * data, const int size );
void dispatchStatusData( const unsigned char * data, const int size );
void takeCameraSnapshot( const VideoDeviceDataHandler * dataHandler, bool allCameras = false );
void blockRemoteExclusiveControl( bool isYes );
#endif
void disconnectClientWithFD( SOCKET_T fd );
bool getIDFromIP( int & addr );
bool getMyIPAddress( int & addr );
bool findClientAddress( const char cID, struct sockaddr_in & cAddr );
long addTimer( float initialTime, long repeats = 0, float interval = 1.0 );
void delTimer( long tID );
bool isTimerRunning( long tID );
bool isTimerExecuting( long tID );
long totalTimers() { return timerCount_; }
void delAllTimers();
private:
struct SocketDataBufferInfo {
unsigned char * bufferedData;
int expectedDataLength;
int bufferedDataLength;
};
typedef struct sClientItem {
SOCKET_T fd;
char cID;
struct sockaddr_in addr;
struct SocketDataBufferInfo dataInfo;
bool pushData;
#ifndef PYRIDE_REMOTE_CLIENT
bool pushImage;
AudioDevice * activeAudioObj;
VideoDevice * activeVideoObj;
time_t nextHeartBeat;
int missingHeartBeats;
#endif
struct sClientItem * pNext;
} ClientItem;
typedef struct sTimerObj {
long tID;
long remainCount; // -1 for infinite
int interval; // unit in 10th of a second
long nextTrigTime; // unit in 10th of a second
bool isExecuting;
#ifdef WIN32
HANDLE timerThread;
#else
pthread_t timerThread;
#endif
struct sTimerObj * pNext;
} TimerObj;
#ifdef WIN32
CRITICAL_SECTION t_criticalSection_;
CRITICAL_SECTION timer_criticalSection_;
#else
pthread_mutex_t t_mutex_;
pthread_mutex_t timer_mutex_;
pthread_mutexattr_t t_mta;
#endif
RobotDataHandler * pDataHandler_;
#ifndef PYRIDE_REMOTE_CLIENT
#ifdef WIN32
HANDLE runThread_;
#else
pthread_t runThread_;
#endif
VideoDeviceList * activeVideoObjs_;
AudioDeviceList * activeAudioObjs_;
ClientItem * exclusiveCtrlClient_;
#endif
struct sockaddr_in sAddr_;
struct sockaddr_in bcAddr_;
SOCKET_T udpSocket_;
SOCKET_T tcpSocket_;
unsigned char * dgramBuffer_;
unsigned char * clientDataBuffer_;
unsigned char * dispatchDataBuffer_;
ClientItem * clientList_;
TimerObj * timerList_;
TimerObj * lastTimer_;
long timerCount_;
long nextTimerID_;
int maxFD_;
fd_set masterFDSet_;
bool netCommEnabled_;
bool keepRunning_;
PyRideNetComm();
bool initUDPListener();
bool initTCPListener();
void processIncomingData( fd_set * readyFDSet );
void checkTimers();
void processUDPInput( const unsigned char * recBuffer, int recBytes, struct sockaddr_in & cAddr );
void processDataInput( ClientItem * client, const unsigned char * receivedMesg,
const int receivedBytes );
#ifdef PYRIDE_REMOTE_CLIENT
void processTelemetryData( ClientItem * client, const unsigned char * commandData,
int dataLen );
void processOperationalData( ClientItem * client, const unsigned char * commandData,
int dataLen );
void processRobotResponse( ClientItem * client, int subcommand,
const unsigned char * commandData, int dataLen );
#else
void declareRobot( const RobotInfo * robotInfo, ClientItem * client );
void processConsoleCommand( ClientItem * client, int subcommand,
const unsigned char * commandData, int dataLen );
int calcTelemetryClients();
#endif
void clientDataSend( const int command, const int subcommand = 0,
const unsigned char * optionalData = NULL,
const int optionalDataLen = 0,
ClientItem * client = NULL, bool useUDP = false, bool forceAll = false );
bool messageValidation( const unsigned char * receivedMesg, const int receivedBytes, char & cID,
char & command, char & subcommand );
void disconnectClient( ClientItem * client, bool sendNotification = false );
void cleanupClient( ClientItem * client );
ClientItem * createTCPTalker( struct sockaddr_in & cAddr );
ClientItem * findClientFromClientList( struct sockaddr_in & cAddr );
ClientItem * findClientFromClientList( const char cID );
ClientItem * findClientFromClientList( SOCKET_T fd );
ClientItem * addFdToClientList( const SOCKET_T & fd, struct sockaddr_in & cAddr );
void initIPAddresses();
bool isNonExclusiveCommand( PyRideExtendedCommand cmd );
};
} // namespace pyride
#endif // __cplusplus
#endif // PyRideNetComm_h_DEFINED