fdhwlib
2.0.25
|
00001 /*************************************************************************** 00002 baseserver.h - description 00003 00004 begin : Fri Sep 24 2004 00005 copyright : (C) 2004 by A Kopmann 00006 email : kopmann@ipe.fzk.de 00007 status : 00008 test : 00009 history : 00010 ***************************************************************************/ 00011 00012 00013 #ifndef BASESERVER_H 00014 #define BASESERVER_H 00015 00016 #include <cstdio> 00017 #include <cstring> 00018 #include <ctype.h> 00019 #include <stdexcept> 00020 #include <sys/select.h> 00021 #include <sys/time.h> 00022 00023 00024 #ifdef __GNUC__ 00025 # include <signal.h> 00026 # include <unistd.h> 00027 # include <sys/types.h> 00028 # include <sys/socket.h> 00029 # include <netinet/in.h> 00030 # include <netdb.h> 00031 # include <arpa/inet.h> 00032 # define SOCKET int 00033 #endif // __GNUC__ 00034 00035 #ifdef __WIN32__ // Windows 00036 #ifndef FD_SETSIZE 00037 //#define FD_SETSIZE 512 00038 // For WinXP a much larger number of sockets is required!! 00039 // 4096 is just a guess - the first socket is about 1960... 00040 // 2004-01-29, ak 00041 #define FD_SETSIZE 4096 00042 #endif 00043 #include <winsock.h> 00044 //#include <winsock2.h> // ??? 00045 #endif // of windows defines 00046 // of windows defines 00047 00048 00096 class BaseServer { 00097 public: 00098 BaseServer(unsigned short port); 00099 //BaseServer(unsigned short port, SimpleServer *s); 00100 00101 virtual ~BaseServer(); 00102 00103 void setDebugLevel(int debug); 00105 void setConnectionLogging(bool logging=true) 00106 { connLogging = logging; } 00107 00108 int readMsg(int client, char *msg, int n); 00109 00110 int writeMsg(int client, char *msg, int n=0); 00111 00112 00113 int readData(int client, unsigned long *data, int n); 00114 00115 int writeData(int client, unsigned long *data, int n); 00116 00117 00118 int readData(int client, unsigned int *data, int n); 00119 00120 int writeData(int client, unsigned int *data, int n); 00121 00126 int readPacket(int client, unsigned long *data, int max); 00127 00132 int writePacket(int client, unsigned long *data, int n); 00133 00134 00135 void monitor(); 00136 00137 // test features 00138 void display(); 00139 virtual void show(); 00140 00142 void init(FILE *fout = stdout); 00143 00151 void init_server(int fd1=-1, int fd2=-1); 00152 00153 void kill_server(); 00154 00159 void displayActiveSockets(); 00160 00164 void displayActiveSockets(FILE *fout); 00165 00168 void setTRef(struct timeval *time); 00169 00171 void enableTimeout(struct timeval *timeout=0); 00172 00173 void disableTimeout(); 00174 00175 00176 protected: 00178 int debug; 00179 00181 void setPort(unsigned short port); 00182 00184 virtual int handle_timeout(); 00185 00187 void getNextTimeout(int *iSample, struct timeval *timeout); 00188 00190 void displayMsg(unsigned char *msg, int len); 00191 00200 virtual int read_from_client(int filedes); 00201 00202 00204 virtual int read_from_keyboard(); 00205 00206 virtual void displayVersion(); 00207 00209 FILE *fout; 00210 00213 bool shutdown; 00214 00216 fd_set active_fd_set; 00217 00220 fd_set change_byteorder_fd_set; 00221 00223 inline void endian_swap(unsigned short& x) { 00224 x = (x>>8) | (x<<8); 00225 } 00226 00228 inline void endian_swap(uint32_t& x) { 00229 x = (x>>24) | 00230 ((x<<8) & 0x00FF0000) | 00231 ((x>>8) & 0x0000FF00) | 00232 (x<<24); 00233 } 00234 00236 inline void endian_swap(unsigned long& x) { 00237 x = (x>>24) | 00238 ((x<<8) & 0x00FF0000) | 00239 ((x>>8) & 0x0000FF00) | 00240 (x<<24); 00241 } 00242 00244 inline void endian_swap(unsigned long long& x) { 00245 x = (x>>56) | 00246 ((x<<40) & 0x00FF000000000000ULL) | 00247 ((x<<24) & 0x0000FF0000000000ULL) | 00248 ((x<<8) & 0x000000FF00000000ULL) | 00249 ((x>>8) & 0x00000000FF000000ULL) | 00250 ((x>>24) & 0x0000000000FF0000ULL) | 00251 ((x>>40) & 0x000000000000FF00ULL) | 00252 (x<<56); 00253 } 00254 00255 00261 inline void endian_prepare_string(unsigned long& x){ 00262 x = (x>>24) | 00263 ((x<<8) & 0x00FF0000) | 00264 ((x>>8) & 0x0000FF00) | 00265 (x<<24); 00266 } 00267 00276 inline void endian_prepare_short(unsigned long& x){ 00277 x = (x>>16) | 00278 (x<<16); 00279 } 00280 00281 00282 #if 0 00283 00284 inline void endian_swap(unsigned int& x){ 00285 //printf("Swap: %d --> ", x); 00286 x = (x>>24) | 00287 ((x<<8) & 0x00FF0000) | 00288 ((x>>8) & 0x0000FF00) | 00289 (x<<24); 00290 //printf("%d \n", x); 00291 } 00292 #endif 00293 00294 inline void endian_swap(double& x){ 00295 endian_swap((unsigned long long &) x); 00296 } 00297 00298 00299 00300 //private: 00301 unsigned short port; 00302 00303 SOCKET sock; 00304 00306 struct timeval tRef; 00307 00308 struct timezone tZone; 00309 00311 struct timeval tSample; 00312 00314 int index; 00315 00317 int nSamples; 00318 00320 int lastIndex; 00321 00323 //struct timeval timeout; 00324 00328 struct timeval ttimer; 00329 00330 bool useTimeout; 00331 00332 bool connLogging; 00333 }; 00334 00335 #endif