fdhwlib  2.0.25
/home/kopmann/git-mirror/fdhwlib/fdhwlib/akutil/simplesocket.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     simplesocket.h  -  description
00003 
00004     begin                : Tue Oct 24 2000
00005     copyright            : (C) 2000 by A Kopmann
00006     email                : kopmann@hpe.fzk.de
00007     status               :
00008     test                 :
00009     history              :
00010  ***************************************************************************/
00011 
00012 
00013 #ifndef SIMPLESOCKET_H
00014 #define SIMPLESOCKET_H
00015 
00016 #include <cstdio>
00017 #include <stdexcept>
00018 #include <stdint.h>
00019 
00020 #include <sys/time.h>
00021 #include <sys/types.h>   // struct timeval
00022 
00023 
00024 #define SIMPLESOCKET_TIMEOUT 2000 // Default timeout [ms]
00025 
00026 
00027 /*
00028 #ifdef __linux__
00029 #include <unistd.h>
00030 #include <sys/types.h>
00031 #include <sys/socket.h>
00032 #include <netinet/in.h>
00033 #include <netdb.h>
00034 #include <arpa/inet.h>
00035 #else
00036 //#include <winsock2.h>
00037 #endif
00038 */
00039 
00056 class SimpleSocket {
00057 public: 
00064   SimpleSocket(const char *server, unsigned short port);
00065 
00073   SimpleSocket(const char *server, unsigned short port, unsigned long timeout);
00074 
00082   SimpleSocket(const char *server, unsigned short port, struct timeval timeout);
00083 
00085   ~SimpleSocket();
00086 
00088   void setTimeout(unsigned long timeout);
00089 
00091   void setTimeout(struct timeval timeout);
00092   
00093   int readMsg(char *msg, int max);
00094   int writeMsg(char *msg, int n=0);
00095 
00096   int readData(unsigned long *data, int max);
00097   int writeData(unsigned long *data, int n);
00098 
00099   int readData(unsigned int *data, int max);
00100   int writeData(unsigned int *data, int n);
00101   
00102   char *getHostName();
00103   char *getPeerName();
00104 
00109   int readPacket(unsigned long *data, int max);
00110 
00115   int writePacket(unsigned long *data, int n);
00116 
00117   
00136   int remoteCall(short cmdid,unsigned long *args, short argc=0,
00137                              unsigned long *data=NULL, short ndata=0,
00138                              unsigned long *ackn=NULL, short len=0);
00139 
00140   int remoteCall(short cmdid,uint32_t *args=NULL, short argc=0,
00141                              uint32_t *data=NULL, short ndata=0,
00142                              uint32_t *ackn=NULL, short len=0);
00143         
00144 
00149   int waitForReading();
00150 
00155   int waitForWriting();
00156                                                                                                                               
00161   int wait(int timeout);
00162 
00164   int getFD();
00165 
00166   
00167 private:
00169   void connectServer(const char *server, unsigned short port);
00170 
00171   int sock;
00172 
00174   bool nonblock;
00175 
00177   struct timeval timeout;  
00178 };
00179 
00180 #endif